Selectbylocation and CopyFeatures via Arcpy

241
6
Jump to solution
06-21-2021 01:04 PM
Labels (1)
ZoeChen
New Contributor III

Hello everyone, I am having trouble when trying to automate the process of select-by-location and copy-features in python. My codes looks like below: The logic seems to be right but once it runs, it runs forever which definitely exceeds the time I ran in Arcpro. Any ideas would be appreciated, thank you!

import arcpy
from arcpy import env
env.workspace = "D:/Backup/shapefile/NE_validation"
outworkspace = "D:/Backup/shapefile/NE_validation"
NEEEfishnet = "D:/Backup/shapefile/NE_validation/NE_validation.gdb/NEEE_NewEngland_fishnet"

shplist = arcpy.ListFeatureClasses("*Point*")
for shp in shplist:
        # select by location
        arcpy.management.SelectLayerByLocation(NEEEfishnet, "INTERSECT", shp, None, "NEW_SELECTION", "NOT_INVERT")
        # export selected features (use copy features tool)
        outfeature = outworkspace + "/" + NEEEfishnet[52:75] + shp[37:40] + ".shp"
        arcpy.management.CopyFeatures(NEEEfishnet, outfeature, '', None, None, None)
        # clear selection
        arcpy.SelectLayerByAttribute_management(NEEEfishnet, "CLEAR_SELECTION")

 

Update: I change the code to adopt two more functions: make feature layer and save layer to file. The code runs but it does not produce shapefile that is openable. The error message says below. I am going to stop updating, and maybe someone someday will find a solution to this problem...

ZoeChen_0-1624471881198.png

 

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Selections don't apply to data sets, they apply to layers or views.  Although you can now pass a data set directly to a Select Layer tool (personally, I disagreed with this change when Esri made it a couple years back), the selection isn't being made directly on the data set.  Instead, when a data set is passed directly to a Select Layer tool, a layer or view is created behind the scenes and the selection is placed on it.  With your code, you are not retrieving the auto-generated layer.

I still personally think it is best practice to create a layer first and then pass it to Select Layer tools.

View solution in original post

6 Replies
DanPatterson
MVP Honored Contributor

it runs forever which definitely exceeds the time I ran in Arcpro

does it produce output though?


... sort of retired...
0 Kudos
ZoeChen
New Contributor III

Thanks, Dan, it does not produce any output. 

0 Kudos
DavidPike
MVP Frequent Contributor

definitely add some print statements to try and track what's going on, I would also separate the directories that your outputs are going into as it's asking for issues.

0 Kudos
ZoeChen
New Contributor III

Thanks, David. 👍

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Selections don't apply to data sets, they apply to layers or views.  Although you can now pass a data set directly to a Select Layer tool (personally, I disagreed with this change when Esri made it a couple years back), the selection isn't being made directly on the data set.  Instead, when a data set is passed directly to a Select Layer tool, a layer or view is created behind the scenes and the selection is placed on it.  With your code, you are not retrieving the auto-generated layer.

I still personally think it is best practice to create a layer first and then pass it to Select Layer tools.

View solution in original post

ZoeChen
New Contributor III

Wow, thanks for this answer Joshua! It really cleared my puzzles 👏

0 Kudos