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
Solved! Go to Solution.
# 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")
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")
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). ---------------------------------------------------------------------------
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:
Hi,
Assets IS a feature class in a File GDB. Is there a way to run the selection on the FC?
Thanks
# 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")
THANK YOU