Select to view content in your preferred language

How to run geoprocessing off a script output in the same script?

1033
4
10-22-2024 04:25 AM
Labels (1)
Davec43
Regular Contributor

I get errors every time I try running multiple geoprocessing tools off outputs created from the same script.

For instance:

arcpy.management.XYTableToPoint(in_table, out_feature_class, x_field, y_field, {z_field}, {coordinate_system})

and then

arcpy.management.SelectLayerByLocation(in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type}, {invert_spatial_relationship})

 

The XY Table to point will work but then I'll get an error on the Select layer by location. I know I'm missing something simple.

 

0 Kudos
4 Replies
JCGuarneri
Frequent Contributor

Are you trying to select the newly generated XY features? Or use them to select something else? You would put the output feature class from step one as in_layer or select_features, respectively. Either way, make sure you can get the select by location working by itself first, then try stringing them together. That way you know you're only working on one problem at a time.

Here's some sample code:

 

 

#set variables
inTable = r"C:\Path\to\table.csv"
tableToPoint = r"in_memory\tablePoints" #if you don't need to keep it. Otherwise, save to file system
featuresToSelect = "C:\Sample.gdb\myFeatures"

#create points
arcpy.management.XYTableToPoint(inTable, tableToPoint, "x","y") #ideally you would know the coordinate system, defaults to WGS84

#select features touching xy points new selection
arcpy.management.SelectLayerByLocation(featuresToSelect, 'INTERSECT',tableToPoint)

#select features within 15 map units of xy points, new selection
arcpy.management.SelectLayerByLocation(featuresToSelect, 'INTERSECT',tableToPoint,15, 'NEW_SELECTION')

#select features more than 15 map units of xy points, new selection - note the "INVERT"
arcpy.management.SelectLayerByLocation(featuresToSelect, 'INTERSECT',tableToPoint,15, 'NEW_SELECTION', 'INVERT')

#select only features within 30 map units of xy points from an existing selection
arcpy.management.SelectLayerByLocation(featuresToSelect, 'INTERSECT',tableToPoint,30, 'SUBSET_SELECTION'))

#clean up
arcpy.management.Delete(tableToPoint)

 

Just a note on the in_memory workspace. It's basically a file geodatabase that lives in the RAM. Great for smaller outputs that you don't need to keep, but use with caution. If you put a large dataset in there, it could use up too much memory and gum up the works until you close Pro.

 

Davec43
Regular Contributor

I get an error and I think the issue is the (x,y table to point) doesn't gets pushed to the Contents so then it's not there for it to do the select layer by location.

I dont' know how to force the (x, y table to point) to get pushed before the other process starts.

0 Kudos
JCGuarneri
Frequent Contributor

Could you share the exact error your getting? Also, the exact code you're using. Just copy and paste both here for us to see.

0 Kudos
Luke_Pinner
MVP Regular Contributor

You haven't defined "in_layer". Try Make Feature Layer (Data Management).

 

arcpy.management.XYTableToPoint(in_table, out_feature_class, x_field, y_field, {z_field}, {coordinate_system})

in_layer = arcpy.management.MakeFeatureLayer(out_feature_class, "Layer Name")[0]

arcpy.management.SelectLayerByLocation(in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type}, {invert_spatial_relationship})

 

 Of course fill in {parameters} in curly braces in the above example.