<?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: Using Defintion Query in a Python Script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635168#M49469</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Wayne,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the info, I will give it a try.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In the meantime, instead of using the definiton query I just added another select statement after the initial select, "REMOVED_FROM_SELECTED" the ROW's.&amp;nbsp; Should have thought of that first.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Chad&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 09 Oct 2013 20:02:05 GMT</pubDate>
    <dc:creator>ChadFoster</dc:creator>
    <dc:date>2013-10-09T20:02:05Z</dc:date>
    <item>
      <title>Using Defintion Query in a Python Script</title>
      <link>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635164#M49465</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a script that is using a parcel feature class from SDE.&amp;nbsp; This parcel feature has Right-of-Way (ROW) information.&amp;nbsp; I would like to use a definition query so that when I do a select by location, the ROW features are not in the selection.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;for layer in arcpy.mapping.ListLayers(mxd, "*"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if layer.name == "DOUGLAS_GIS.GISDATA.Parcel_Boundary":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer.definitionQuery = "'TAG' IS NOT \'ROW %\'"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Could someone please look at this syntax and let me know if there is something wrong?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is the definition query that I use in an MXD.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;TAG NOT LIKE 'ROW %'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Chad&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 17:20:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635164#M49465</guid>
      <dc:creator>ChadFoster</dc:creator>
      <dc:date>2013-10-09T17:20:57Z</dc:date>
    </item>
    <item>
      <title>Re: Using Defintion Query in a Python Script</title>
      <link>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635165#M49466</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Chad,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Looking at your query, the syntax seems incorrect. Have you checked out this article &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00s50000002t000000"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#//00s50000002t000000&lt;/A&gt;&lt;SPAN&gt; or this one &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001r000000"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001r000000&lt;/A&gt;&lt;SPAN&gt;?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I haven't applied queries in this way, but just looking at the syntax, you are wrapping both TAG and ROW % in single quotes, so the query that is being sent to the layer looks like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;'TAG' IS NOT 'ROW %'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Based on this (from the second link):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"The field delimiters used in a SQL expression differ depending on the format of the queried data. For instance, file geodatabases and shapefiles use double quotes (" "), personal geodatabases use square brackets ([ ]), and ArcSDE geodatabases don't use field delimiters. The AddFieldDelimiters function can take away some of the guess work in ensuring that the field delimiters used with your SQL expression are the correct ones. The following example expands on the above example to add the proper field delimiters for the SQL expression."&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In which case the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;string&lt;/SPAN&gt;&lt;SPAN&gt; TAG will never be equal to the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;string&lt;/SPAN&gt;&lt;SPAN&gt; ROW %&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You want the query python generates to look exactly like what you are using in the MXD, so I think you might want &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;layer.definitionQuery = "TAG IS NOT \'ROW %\'"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Without the data though, can't test this. Hope this helps.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-Doug&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 17:42:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635165#M49466</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2013-10-09T17:42:24Z</dc:date>
    </item>
    <item>
      <title>Re: Using Defintion Query in a Python Script</title>
      <link>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635166#M49467</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you only need the distance, this may be fairly straight forward. Having looked into this before (trying to move points to the closest edge segment of a polygon) it is however a bit more complicated than at face value.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But this article here &lt;/SPAN&gt;&lt;A href="http://math.ucsd.edu/~wgarner/math4c/derivations/distance/distptline.htm"&gt;http://math.ucsd.edu/~wgarner/math4c/derivations/distance/distptline.htm&lt;/A&gt;&lt;SPAN&gt; is a great place to start. The basic equation is given towards the bottom:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Shortest distance from y=m*x + b to (x1, y1):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;|y1 - m*x1 - b| / root(m^2 + 1)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Good luck!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 18:31:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635166#M49467</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2013-10-09T18:31:37Z</dc:date>
    </item>
    <item>
      <title>Re: Using Defintion Query in a Python Script</title>
      <link>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635167#M49468</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Good info, and I'd just like to add escaping quotes can be excruciating, so maybe do it this way:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
layer.definitionQuery = "{0} NOT LIKE '{1} %'".format(arcpy.AddFieldDelimiters(layer, 'TAG'), 'ROW')
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To further explain, the query expression is contained in:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
"{0} NOT LIKE '{1} %'"
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;{0} and {1} are variables for substitution using the '.format' function, so...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;{0} is what is evaluated by arcpy.AddFieldDelimiters(layer, 'TAG')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{1} is the string 'ROW'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...which yields a query string (for an SDE source):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"TAG" NOT LIKE 'ROW %'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You have to be careful with the multiple character wildcard, and if that query yields unexpected results, then you could simply 'lop off' that nonsense with a minor adjustment and include the 'not equal to' operator:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
layer.definitionQuery = "{0} &amp;lt;&amp;gt; '{1}'".format(arcpy.AddFieldDelimiters(layer, 'TAG'), 'ROW')
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...which of course the query string would be (for an SDE source):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"TAG" &amp;lt;&amp;gt; 'ROW'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that clears things up some...this is a fairly powerful technique when it's right - always a pain to trip on just the syntax of it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT - A final comment about SQL syntax, note that you have a space in your value, 'ROW %' --- for me this does not filter any values out if I have no characters after the 'W'; however, using this value, 'ROW%' performs as expected.&amp;nbsp; So a rule of thumb to follow (and it does look like you have your bases covered) is to make certain your SQL is sound i.e. returning what you intended (and only what you intended).&amp;nbsp; Otherwise code your field values a little 'tighter', but that's really a separate topic of data integrity.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 03:01:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635167#M49468</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2021-12-12T03:01:09Z</dc:date>
    </item>
    <item>
      <title>Re: Using Defintion Query in a Python Script</title>
      <link>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635168#M49469</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Wayne,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the info, I will give it a try.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In the meantime, instead of using the definiton query I just added another select statement after the initial select, "REMOVED_FROM_SELECTED" the ROW's.&amp;nbsp; Should have thought of that first.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Chad&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 20:02:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635168#M49469</guid>
      <dc:creator>ChadFoster</dc:creator>
      <dc:date>2013-10-09T20:02:05Z</dc:date>
    </item>
    <item>
      <title>Re: Using Defintion Query in a Python Script</title>
      <link>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635169#M49470</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That'll be fine, although really not as efficient...you get a point regardless for ingenuity.&amp;nbsp; Still, if you run across this type thing again, a def query could be your ticket and is worth the practice.&amp;nbsp; Also, if you're just interested in working with in-memory layers rather than messing with your ArcMap TOC layer objects, you could use Make Feature Layer with a query (or copy to in_memory workspace), etc...and you're back to the need for query syntax again.&amp;nbsp; Nice to know that there are various legitimate options!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Glad you got it!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 20:09:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-defintion-query-in-a-python-script/m-p/635169#M49470</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-10-09T20:09:31Z</dc:date>
    </item>
  </channel>
</rss>

