<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to get select by location to honor definition query that is set in tool? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-get-select-by-location-to-honor-definition/m-p/1623535#M74374</link>
    <description>&lt;P&gt;Try using feature layer with definition query.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;temp_points = "memory/temp_points"
    arcpy.management.MakeFeatureLayer(
        points_lyr,
        temp_points,
        where_clause=query
    )

    arcpy.management.SelectLayerByLocation(
        in_layer=line_lyr,
        overlap_type="INTERSECT",
        select_features=temp_points,
        search_distance=None,
        selection_type="NEW_SELECTION"
    )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 13 Jun 2025 18:24:19 GMT</pubDate>
    <dc:creator>TonyAlmeida</dc:creator>
    <dc:date>2025-06-13T18:24:19Z</dc:date>
    <item>
      <title>How to get select by location to honor definition query that is set in tool?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-select-by-location-to-honor-definition/m-p/1623491#M74372</link>
      <description>&lt;P&gt;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&amp;nbsp; the lines that intersect the unqueried point layer.&lt;BR /&gt;&lt;BR /&gt;Here is my code:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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"
    )
    &lt;/LI-CODE&gt;&lt;P&gt;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.&lt;BR /&gt;&lt;BR /&gt;I could do a select by attribute, but visually I only want those queried points in my map.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jun 2025 16:32:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-select-by-location-to-honor-definition/m-p/1623491#M74372</guid>
      <dc:creator>ZacharyKasson</dc:creator>
      <dc:date>2025-06-13T16:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to get select by location to honor definition query that is set in tool?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-select-by-location-to-honor-definition/m-p/1623535#M74374</link>
      <description>&lt;P&gt;Try using feature layer with definition query.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;temp_points = "memory/temp_points"
    arcpy.management.MakeFeatureLayer(
        points_lyr,
        temp_points,
        where_clause=query
    )

    arcpy.management.SelectLayerByLocation(
        in_layer=line_lyr,
        overlap_type="INTERSECT",
        select_features=temp_points,
        search_distance=None,
        selection_type="NEW_SELECTION"
    )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jun 2025 18:24:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-select-by-location-to-honor-definition/m-p/1623535#M74374</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2025-06-13T18:24:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to get select by location to honor definition query that is set in tool?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-select-by-location-to-honor-definition/m-p/1623549#M74375</link>
      <description>&lt;P&gt;Ah, that's a good idea. Not exactly the functionality I am hoping for but it should suffice if def query is not possible. Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jun 2025 19:16:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-select-by-location-to-honor-definition/m-p/1623549#M74375</guid>
      <dc:creator>ZacharyKasson</dc:creator>
      <dc:date>2025-06-13T19:16:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to get select by location to honor definition query that is set in tool?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-select-by-location-to-honor-definition/m-p/1623564#M74376</link>
      <description>&lt;P&gt;I think you just need to save the aprx:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;...
    setDefinitionQuery(Point_Name,query)# this is sucessful - layer is queried after tool is finished running
    arcpy.RefreshLayer(Points_Layer) # Refresh Layer
  + aprx.save()
...&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also started down my cursor rabbit hole and came up with a version that only uses geometry and SearchCursors lol:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcpy.da import SearchCursor
from arcpy import PointGeometry
from functools import reduce
from arcpy import (
    AddError,
)
from arcpy._mp import (
    Layer, 
    Map, 
    ArcGISProject,
)
from arcpy.management import SelectLayerByAttribute

def script_tool(map_name: str, point_layer_name: str, line_layer_name: str, sql_query: str) -&amp;gt; None:
    
    # Grab layers and such
    aprx = ArcGISProject('CURRENT')
    map_objs: list[Map] = aprx.listMaps(map_name)
    if not map_objs:
        AddError(f"{map_name} does not exist!")
        return
    map_obj = map_objs[0]
    
    point_layer_objs: list[Layer] = map_obj.listLayers(point_layer_name)
    if not point_layer_objs:
        AddError(f"{point_layer_name} does not exist in {map_name}!")
        return
    point_layer_obj = point_layer_objs[0]
    
    line_layer_objs: list[Layer] = map_obj.listLayers(line_layer_name)
    if not line_layer_objs:
        AddError(f"{line_layer_name} does not exist in {map_name}!")
        return
    line_layer_obj = line_layer_objs[0]
    
    # Get point geos of points that match the query
    point_geos: list[PointGeometry] = [
        rec[0]
        for rec in SearchCursor(point_layer_obj.dataSource, ['SHAPE@'], where_clause=sql_query)
    ]
    
    # Merge points into a multipoint
    multipoint = reduce(lambda a, b: a+b, point_geos)
    
    # Grab all line ids that intersect the multipoint
    # Need to cast to string for query interpolation
    line_ids: list[str] = [
        str(oid)
        for oid, line in SearchCursor(line_layer_obj, ['OID@'], spatial_filter=multipoint)
    ]
    
    SelectLayerByAttribute(
        in_layer_or_view=line_layer_obj,
        selection_type='NEW_SELECTION',
        where_clause=f"OBJECTID IN ({','.join(line_ids)})"  
    )&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 13 Jun 2025 20:26:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-select-by-location-to-honor-definition/m-p/1623564#M74376</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-06-13T20:26:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to get select by location to honor definition query that is set in tool?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-select-by-location-to-honor-definition/m-p/1627721#M74443</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/690908"&gt;@ZacharyKasson&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;The code above worked just fine for me after I updated the code below. Applied def query and selected by location worked as it should only selecting lines that intersected the points available from the def query.&lt;/P&gt;&lt;P&gt;With ArcGIS Pro 3.4&lt;/P&gt;&lt;LI-CODE lang="python"&gt;setDefinitionQuery(Points_Layer,query) ## setDefinitionQuery(Point_Name,query)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When you create the tool how are you defining the parameters? like below...&lt;/P&gt;&lt;LI-CODE lang="python"&gt;map_name = arcpy.GetParameterAsText(0)

point_name = arcpy.GetParameterAsText(1)

line_name = arcpy.GetParameterAsText(2)

query = arcpy.GetParameterAsText(3)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Clubdebambos_0-1751023748779.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/135275iC954AC9A0C1AE21F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Clubdebambos_0-1751023748779.png" alt="Clubdebambos_0-1751023748779.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;All the best,&lt;/P&gt;&lt;P&gt;Glen&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jun 2025 11:31:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-select-by-location-to-honor-definition/m-p/1627721#M74443</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2025-06-27T11:31:01Z</dc:date>
    </item>
  </channel>
</rss>

