Select to view content in your preferred language

How can I calculate the centroid of all point features contained in each polygon feature (search cursor)?

1860
4
Jump to solution
11-28-2016 04:56 PM
KristenM
Deactivated User

I have a polygon feature class and a point feature class. For each polygon, I would like to:

(1) Select the points contained in that polygon and 

(2) Calculate the centroid of those selected points

Ideally, I would end up with a new point feature class called "centroids", for example, that would have a centroid for each polygon (they are named with "Name").

I'm a beginner in ArcPy, especially with search cursors. So far, I've tried writing something to iterate over each polygon (farm), select the points(turbines) within the farm, calculate the centroid of those points, and create a layer named "turbine_center"+the name of the farm. I'm running into an error in the SelectLayerByLocation tool:

working="S:/Wind/"
turbines= working + "Turbines_eastern.shp"
farms_poly= working + "Farms_polygons.shp"

arcpy.MakeFeatureLayer_management(farms_poly, "farms_poly_lyr") 
arcpy.MakeFeatureLayer_management(turbines, "turbines_lyr") 

fc="farms_poly_lyr"
rows=arcpy.SearchCursor(fc)
for farm in rows:
    farmName=farm.getValue("Name")
    print farmName
    arcpy.SelectLayerByLocation_management("turbines_lyr", "intersect", farm)
    arcpy.MeanCenter_stats("turbines_lyr", "turbine_center_"+farmName)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Any help would be greatly appreciated!

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

It's been so long ago that I last used one of the old-style cursors so I can't recall how to deal with geometry using them, but if you're just getting into arcpy, you should bypass those and jump straight to the modern version in the data access module: SearchCursor—Help | ArcGIS for Desktop 

fc = 'farms_poly_lyr'
with arcpy.da.SearchCursor(fc,['SHAPE@','Name']) as cursor:
    for row in cursor:
        arcpy.SelectLayerByLocation_management("turbines_lyr","INTERSECT",row[0])
        arcpy.MeanCenter_stats("turbines_lyr", "turbine_center_"+row[1])

^ untested. I'm not sure if you can run select by location against a geometry object. You may have to convert it to a layer first: Make Feature Layer—Help | ArcGIS for Desktop 

View solution in original post

4 Replies
DarrenWiens2
MVP Honored Contributor

It's been so long ago that I last used one of the old-style cursors so I can't recall how to deal with geometry using them, but if you're just getting into arcpy, you should bypass those and jump straight to the modern version in the data access module: SearchCursor—Help | ArcGIS for Desktop 

fc = 'farms_poly_lyr'
with arcpy.da.SearchCursor(fc,['SHAPE@','Name']) as cursor:
    for row in cursor:
        arcpy.SelectLayerByLocation_management("turbines_lyr","INTERSECT",row[0])
        arcpy.MeanCenter_stats("turbines_lyr", "turbine_center_"+row[1])

^ untested. I'm not sure if you can run select by location against a geometry object. You may have to convert it to a layer first: Make Feature Layer—Help | ArcGIS for Desktop 

KristenM
Deactivated User

That worked beautifully--Thanks!

 

Does the indexing you do on row (row[0], row[1]) refer to the field names you asked for when creating the searchcursor? As in, does row[0] refer to the geometry of the given row and row[1] refer to the name?

0 Kudos
DarrenWiens2
MVP Honored Contributor

Yes that's exactly right, and glad it worked! 

RebeccaStrauch__GISP
MVP Emeritus

I think you can just use the MeanCenter_stats without using a cursor....if you have a field that you are using to select, use that as a "case field"

Syntax

MeanCenter_stats (Input_Feature_Class, Output_Feature_Class, {Weight_Field}, {Case_Field}, 
{Dimension_Field})

If the field doesn't exist yet, you can use other tools like Intersect, Identity, or Union ...or a spatial join to get the field.

Then you shouldn't even need the cursor or a python script.  

Of course, this assumes the mean center is the location you want.