3

I am trying to run a script which for each row in an attribute able of catchment-shapefile select points from postal shapefile that fall inside the area of the selected catchment row feature.

Here is my script. I am having an issue where it does not select features correctly I end up with an exact copy of the original postal shapefile. I cannot see why this is happening.

import os, arcpy
arcpy.env.overwriteOutput = True
postal = r"U:\fwd_sept16\postal2_fz2.shp"
catchment = r"H:\Documents\robgis\fwd Data\properties_catchment.shp"
infield = "catch"
outname = r"U:\fwd_sept16"

arcpy.MakeFeatureLayer_management(postal,"temp")
with arcpy.da.SearchCursor(catchment, "catch") as cursor:
    for row in cursor:
        #SELECT PROPERTIES BY CATCHMENT
        arcpy.SelectLayerByLocation_management("temp","INTERSECT",catchment,"","NEW_SELECTION") 
        #OUTFIELD NAMEING
        outFeature = outname + "\\" + row[0]
        #COPY SELECTED FEATURES
        arcpy.CopyFeatures_management("temp", outFeature) 
        print row[0]
3

catchment is your entire feature class, not the row being processed by the cursor. To use the current row's geometry in the Select By Location tool, your code should look like this, making use of the SHAPE@ token:

...
arcpy.MakeFeatureLayer_management(postal,"temp")
with arcpy.da.SearchCursor(catchment, ["catch", "SHAPE@"]) as cursor:
    for row in cursor:
        #SELECT PROPERTIES BY CATCHMENT
        arcpy.SelectLayerByLocation_management("temp","INTERSECT",row[1],"","NEW_SELECTION") 
...
2

perhaps this problem would be really easy, if you make a spatial join (postal point to catchment) than select point with same ID and export to shapefile. No code need you can create a script with modelbuilde adding itenerators (row selection) than "copy feature "

0

I think the problem is the select statement: If no features get selected all of them will get copied. Try adding an if statement:

#SELECT PROPERTIES BY CATCHMENT  
arcpy.SelectLayerByLocation_management("temp","INTERSECT",catchment,"","NEW_SELECTION") 
selectioncount = arcpy.GetCount_management("temp")
if selectioncount.inputCount >0:
    #OUTFIELD NAMEING
    outFeature = outname + "\\" + row[0]
    #COPY SELECTED FEATURES
    arcpy.CopyFeatures_management("temp", outFeature)

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.