Run script against selected features

472
6
Jump to solution
08-15-2022 10:31 AM
GaryBarden
New Contributor III

I feel like I'm missing something simple here. All I want to do is select a subset from a layer based on an attribute and then do a spatial join of just the subset to a polygon layer.

Script works fine, but it's executing the spatial join against the entire layer, not just the selected records.

# Execute Select
arcpy.management.SelectLayerByAttribute(asset, "NEW_SELECTION", '"COORDINATE_VALID_DATE" = CURRENT_DATE' )

#Execute Spatial Join
arcpy.SpatialJoin_analysis(asset, polygonLayer01, "asset_join_poly01", "JOIN_ONE_TO_MANY", "KEEP_COMMON", "", "INTERSECT")

 

Thanks

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor
# you can turn asset into a layer:
asset_layer = arcpy.management.MakeFeatureLayer(asset, "asset_layer")
# and then you can call the selection on the layer.


# But, much easier: MakeFeatureLayer takes an optional SQL where clause. So:
asset_layer = arcpy.management.MakeFeatureLayer(asset, "asset_layer", '"COORDINATE_VALID_DATE" = CURRENT_DATE' )
arcpy.SpatialJoin_analysis(asset_layer, polygonLayer01, "asset_join_poly01", "JOIN_ONE_TO_MANY", "KEEP_COMMON", "", "INTERSECT")

Have a great day!
Johannes

View solution in original post

0 Kudos
6 Replies
AlfredBaldenweck
MVP Regular Contributor

Try making the select function into a variable, then passing that into the spatial join.

# Execute Select
asset_S = arcpy.management.SelectLayerByAttribute(asset, "NEW_SELECTION", '"COORDINATE_VALID_DATE" = CURRENT_DATE' )

#Execute Spatial Join
arcpy.SpatialJoin_analysis(asset_S, polygonLayer01, "asset_join_poly01", "JOIN_ONE_TO_MANY", "KEEP_COMMON", "", "INTERSECT")

 
0 Kudos
GaryBarden
New Contributor III

got this error

 

---------------------------------------------------------------------------
ExecuteError                              Traceback (most recent call last)
In  [9]:
Line 20:    arcpy.SpatialJoin_analysis(asset_sel, polygonLayer01, "asset_join_poly01", "JOIN_ONE_TO_MANY", "KEEP_COMMON", "", "INTERSECT")

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\analysis.py, in SpatialJoin:
Line 793:   raise e

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\analysis.py, in SpatialJoin:
Line 790:   retval = convertArcObjectToPythonObject(gp.SpatialJoin_analysis(*gp_fixargs((target_features, join_features, out_feature_class, join_operation, join_type, field_mapping, match_option, search_radius, distance_field_name), True)))

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py, in <lambda>:
Line 512:   return lambda *args: val(*gp_fixargs(args, True))

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Target Features: Dataset GIS_ASSET_DETAIL_Layer17 does not exist or is not supported
Failed to execute (SpatialJoin).

---------------------------------------------------------------------------

In [ ]:
 
 
 
 
 
0 Kudos
JohannesLindner
MVP Frequent Contributor

Maybe asset is actually a feature class, not a layer? Selection doesn't work on feature classes.

If that's not it, you should probably post the whole script, so that we can see what's going on before the two lines you posted.

 

 

To post code:

JohannesLindner_0-1660635913390.png

JohannesLindner_1-1660635928900.png

 


Have a great day!
Johannes
0 Kudos
GaryBarden
New Contributor III

Hi,

Assets IS a feature class in a File GDB. Is there a way to run the selection on the FC?

Thanks

0 Kudos
JohannesLindner
MVP Frequent Contributor
# you can turn asset into a layer:
asset_layer = arcpy.management.MakeFeatureLayer(asset, "asset_layer")
# and then you can call the selection on the layer.


# But, much easier: MakeFeatureLayer takes an optional SQL where clause. So:
asset_layer = arcpy.management.MakeFeatureLayer(asset, "asset_layer", '"COORDINATE_VALID_DATE" = CURRENT_DATE' )
arcpy.SpatialJoin_analysis(asset_layer, polygonLayer01, "asset_join_poly01", "JOIN_ONE_TO_MANY", "KEEP_COMMON", "", "INTERSECT")

Have a great day!
Johannes
0 Kudos
GaryBarden
New Contributor III

THANK YOU

0 Kudos