1

I am trying to select the largest polygon that contains each feature in a feature class but none of the features in another feature class. I have written the code that seems logical to me but (of course) it doesn't work.

I am using a search cursor to iterate through my first feature class and for each feature I want to run my point in polygon query. it seems that my cursor is not a valid input. Is there a special way to express it, or is it not possible to use the search cursor as an input to this. If not is there an other was of searching on each point one by one?

Polygons = r"\\...\\tests.gdb\\out_contour_g"
peakPoints = r"\\...\\tests.gdb\\inv_peak"
troughPoints = r"\\...\\tests.gdb\\peaktest" 
OutputFC = r"\\...\\tests.gdb\\OUTPUT1"

arcpy.RepairGeometry_management(Polygons)
arcpy.AddField_management(Polygons,"area_calc","DOUBLE")
arcpy.CalculateField_management(Polygons, "area_calc","!Shape!.area","PYTHON_9.3")



points = arcpy.da.SearchCursor(peakPoints, "SHAPE@")
for point in points:
    arcpy.SelectLayerByLocation_management(Polygons, "CONTAINS", point)
    arcpy.SelectLayerByLocation_management('out_contour_g',"CONTAINS",'peaktest',"","REMOVE_FROM_SELECTION")
with arcpy.da.SearchCursor(Polygons, "SHAPE@") as rows:
    for row in rows:
        list = []
        list.append(row[0].area)
        largestArea = max(list)
        arcpy.SelectLayerByAttribute_management(Polygons, "SUBSET_SELECTION", "area_calc =" + str(largestArea)) ####the where clause is probably wrong
        arcpy.Append_management(Polygons, OutputFC, "NO_TEST")
1
  • 1
    I've never tried to see whether SelectLayerByLocation_management will work on an arcpy.Point object as a parameter as you have done. I bet no since SelectLayerByLocation doesn't even work on standard feature classes - you have to convert to layers. Try using the arcpy.Polygon.contains() method instead to discern whether each point object falls in/out of each polygon object. Or covert the arcpy.Point to a layer and you can keep using Sel. By Loc. Also, change code to: for point[0] in points:.
    – John
    Commented Sep 15, 2015 at 16:46

2 Answers 2

1

Here's a cursor free option that I think would work.

arcpy.SelectLayerByLocation_management(Polygons, "CONTAINS", peakPoints, "NEW_SELECTION")
arcpy.SelectLayerByLocation_management(Polygons, "CONTAINS", troughPoints, "REMOVE_FROM_SELECTION")
field = "area_calc"
arr = arcpy.da.TableToNumpyArray(Polygons, field)
maxArea = arr["area_calc"].max()
arcpy.SelectLayerByAttribute_management(Polygons, ' "area_calc" = ' + str(maxArea))
arcpy.FeatureClassToFeatureClass_conversion(Polygons, OutputGDB, OutputFC)

You can do more Select by locations with "REMOVE_FROM_SELECTION" if you have more feature classes that you need to exclude.

0

The list is initialized for each row in rows thereby keeping the last element to be max, not the maximum of the list. Move the list initialization out of the loop and retest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.