<?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 How Can I Set a Definition Query from a Selection in Arcpy? in ArcMap Questions</title>
    <link>https://community.esri.com/t5/arcmap-questions/how-can-i-set-a-definition-query-from-a-selection/m-p/1246218#M4218</link>
    <description>&lt;P&gt;Hello, I am trying to get a script to set a definition query from a selection. The selection is set by using an intersect from a buffer layer. I know the first part works, because if I just run this, it will create a selection in the mxd. I have also tested the second 'for...' with an mxd open and it works. However, I need to run this with the mxd closed, as it is being newly created. All I get is "OBJECTID IN ()" in the county layer's query.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;        for lyr in arcpy.mapping.ListLayers (mxd,'HALF_MILE_BUFFER'):
            county_lyr = arcpy.mapping.ListLayers(mxd, 'County')[0]
            arcpy.SelectLayerByLocation_management(county_lyr,"INTERSECT",lyr)
            
        for lyr in arcpy.mapping.ListLayers (mxd,'County'):   
            fids = arcpy.Describe(lyr).FIDSet
            idfield = 'OBJECTID'
            wc = idfield + ' IN (' + fids.replace(';',',') + ')'
            county_lyr.definitionQuery = wc
            arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 06 Jan 2023 22:38:23 GMT</pubDate>
    <dc:creator>rescobar</dc:creator>
    <dc:date>2023-01-06T22:38:23Z</dc:date>
    <item>
      <title>How Can I Set a Definition Query from a Selection in Arcpy?</title>
      <link>https://community.esri.com/t5/arcmap-questions/how-can-i-set-a-definition-query-from-a-selection/m-p/1246218#M4218</link>
      <description>&lt;P&gt;Hello, I am trying to get a script to set a definition query from a selection. The selection is set by using an intersect from a buffer layer. I know the first part works, because if I just run this, it will create a selection in the mxd. I have also tested the second 'for...' with an mxd open and it works. However, I need to run this with the mxd closed, as it is being newly created. All I get is "OBJECTID IN ()" in the county layer's query.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;        for lyr in arcpy.mapping.ListLayers (mxd,'HALF_MILE_BUFFER'):
            county_lyr = arcpy.mapping.ListLayers(mxd, 'County')[0]
            arcpy.SelectLayerByLocation_management(county_lyr,"INTERSECT",lyr)
            
        for lyr in arcpy.mapping.ListLayers (mxd,'County'):   
            fids = arcpy.Describe(lyr).FIDSet
            idfield = 'OBJECTID'
            wc = idfield + ' IN (' + fids.replace(';',',') + ')'
            county_lyr.definitionQuery = wc
            arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jan 2023 22:38:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/how-can-i-set-a-definition-query-from-a-selection/m-p/1246218#M4218</guid>
      <dc:creator>rescobar</dc:creator>
      <dc:date>2023-01-06T22:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: How Can I Set a Definition Query from a Selection in Arcpy?</title>
      <link>https://community.esri.com/t5/arcmap-questions/how-can-i-set-a-definition-query-from-a-selection/m-p/1246406#M4222</link>
      <description>&lt;P&gt;Can't test in ArcMap, but I saw the same behavior in ArcGIS Pro. I could solve it by creating a new layer in memory:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;buffer_lyr = arcpy.mapping.ListLayers (mxd,'HALF_MILE_BUFFER')[0]

# get the layer in the map
county_lyr_in_map = arcpy.mapping.ListLayers(mxd, 'County')[0]
# create a new layer using the map layer's data source
county_lyr = arcpy.management.MakeFeatureLayer(county_lyr_in_map.dataSource, "SomeName")
# select in that new layer
arcpy.management.SelectLayerByLocation(county_lyr, "INTERSECT", buffer_lyr)
# set the definition query in the map layer
fids = arcpy.Describe(county_lyr).FIDSet
county_lyr_in_map.definitionQuery = 'OBJECTID IN (' + fids.replace(';', ',') + ')'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 13:01:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/how-can-i-set-a-definition-query-from-a-selection/m-p/1246406#M4222</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2023-01-09T13:01:34Z</dc:date>
    </item>
    <item>
      <title>Re: How Can I Set a Definition Query from a Selection in Arcpy?</title>
      <link>https://community.esri.com/t5/arcmap-questions/how-can-i-set-a-definition-query-from-a-selection/m-p/1246514#M4223</link>
      <description>&lt;P&gt;Thank you so much Johannes, this works! Hope you have a good week &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 16:46:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/how-can-i-set-a-definition-query-from-a-selection/m-p/1246514#M4223</guid>
      <dc:creator>rescobar</dc:creator>
      <dc:date>2023-01-09T16:46:40Z</dc:date>
    </item>
  </channel>
</rss>

