I am creating a tool to write information from one layer to another via a spatial join. I am setting a definition query on a my points layer (successfully sets definition query) - then running a select by location to intersect my line layer with the queried points layer. The select by location does not honor the definition query and selects the lines that intersect the unqueried point layer.
Here is my code:
def script_tool(map_name, Point_Name,Line_Name,query):
# Pro project env variables
arcpy.env.overwriteOutput = True
aprx = arcpy.mp.ArcGISProject('CURRENT')
MasterMap=aprx.listMaps(map_name)[0]
Points_Layer = MasterMap.listLayers(Point_Name)[0]
# # # SET DEFINITION QUERY # # #
def setDefinitionQuery(layer, sqlFilter):
layer.updateDefinitionQueries(
[
{'name': 'Query 1', 'sql': sqlFilter, 'isActive': True}
]
)
setDefinitionQuery(Point_Name,query)# this is sucessful - layer is queried after tool is finished running
arcpy.RefreshLayer(Points_Layer) # Refresh Layer
# # # Select Anchors by location # # #
arcpy.management.SelectLayerByLocation(
in_layer=Line_Name,
overlap_type="INTERSECT",
select_features= Point_Name, #also used layer object with same result.
search_distance=None,
selection_type="NEW_SELECTION",
invert_spatial_relationship="NOT_INVERT"
)
This selects 10,000 features instead of the desired 18. It works fine in a notebook, but not as a tool. How do I ensure the layer refreshes before doing a select layer by location.
I could do a select by attribute, but visually I only want those queried points in my map.