1

I am trying to select Zones that intersect the dataframe (polygon). If there is only one return then I want to turn it into a sting. If there is multiple I want to turn it into a string with " ," (space then comma) in between each.

My code is as follow:

arcpy.MakeFeatureLayer_management(ZoneSHP, "ZoneLYR")
arcpy.SelectLayerByLocation_management("ZoneLYR", "INTERSECT", "polygon"):
with arcpy.da.SearchCursor(ZoneLYR, "Zone") as cursor:
    int(arcpy.GetCount_management(ZoneLYR).getOutput(0))
    if getOutput == 1:
            Zone = row[]
    else:
        for row in curser:
            zone1 = ", " + row[]
        Zone = zone1[3:]
  • 1
    What happens when you run the code snippet that you provided? I think you should include any error thrown as an edit to your question. – PolyGeo May 11 '15 at 12:14
6

The issue I see is that you are not assigning your count to a variable. Also, row[] isn't correct syntax. And you're missing a for row in cursor:. And you spell cursor wrong in a spot. And you have a colon where you shouldn't in your select by location code. And your logic for combining zone values doesn't quite work.

Try this:

arcpy.MakeFeatureLayer_management(ZoneSHP, "ZoneLYR")
arcpy.SelectLayerByLocation_management("ZoneLYR", "INTERSECT", "polygon")
with arcpy.da.SearchCursor(ZoneLYR, "Zone") as cursor:
    outPut = int(arcpy.GetCount_management(ZoneLYR).getOutput(0))
    if outPut == 1:
        for row in cursor:
            Zone = row[0]
    else:
        Zone = ""
        for row in cursor:
            if Zone:
                Zone = Zone + ", " + row[0]
            else:
                Zone = row[0]

Or maybe even better:

arcpy.MakeFeatureLayer_management(ZoneSHP, "ZoneLYR")
arcpy.SelectLayerByLocation_management("ZoneLYR", "INTERSECT", "polygon")
zones = [r[0] for r in arcpy.da.SearchCursor ("ZoneLYR", "Zone")]

Zone = ", ".join (zones)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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