crash when using SelectLayerByLocation

820
4
Jump to solution
09-27-2021 01:34 PM
GavinPaw
New Contributor

I am attempting to use arcpy.SelectLayerByLocation_management() to select polyline objects on a given layer based on their relationship to a point, but any time I run the code outside of Arcgis (in PyCharm) it results in a crash where an ESRI error report box pops up with nothing printed to console that I can see.

import arcpy

projRoot = arcpy.mp.ArcGISProject("./dat/project/project.aprx")
mainMap = projRoot.listMaps("Map")[0]

roadLayer = mainMap.listLayers("story_county_roads")[0]

arcpy.SelectLayerByLocation_management(roadLayer, "CONTAINS", arcpy.PointGeometry(arcpy.Point(461715.4856701, 4656029.4532028)))

 the above is the entirety of a minimized file I created to test the error and seems to run fine when pasted into an instance of Arcgis Pro using "CURRENT" instead of a path for the project, however using the path still results in a crash even when running in Arcgis. what would be the proper way to use select layer by location externally if the above is broken?

0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

Without an instantiated project, I don't know if you can use layers in this way. Running Select Layer By Location from a standalone script seems to imply you have a project session going.  Using a layer as input to Copy Features is different from modifying selection status the layer. If you run Create Make Feature Layer to instantiate  a layer in memory, will that layer work with select Layer by Location?

View solution in original post

0 Kudos
4 Replies
nzjs
by
New Contributor III

It sounds like your path might be incorrect if it crashes inside ArcGIS with a specific path. I normally do something like this to use a relative path. 

path = os.path.join(os.getcwd(), 'dat', 'project', 'project.aprx')
projRoot = arcpy.mp.ArcGISProject(path)

Try wrapping the whole thing in a try/except statement (print the exception) to see what the issue might be.

0 Kudos
GavinPaw
New Contributor

changing the first couple lines to use path.join instead of just directly typing it in and wrapping the entire thing in a try except block seemed to have no effect on the result... including oddly enough the except part of the try except not firing. it seems like whatever the crash is kills the program without allowing error handling.

it also occurs to me that I have been able to use the same declaration for the roadlayer in other functions like CopyFeatures without error, so the path itself should be fine



 

0 Kudos
curtvprice
MVP Esteemed Contributor

Without an instantiated project, I don't know if you can use layers in this way. Running Select Layer By Location from a standalone script seems to imply you have a project session going.  Using a layer as input to Copy Features is different from modifying selection status the layer. If you run Create Make Feature Layer to instantiate  a layer in memory, will that layer work with select Layer by Location?

0 Kudos
GavinPaw
New Contributor

the function ended up being named MakeFeatureLayer but it worked, passing in roadLayer from above returned something that is able to be used

0 Kudos