<?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: Replace Definition Query in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354935#M27917</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for the reply. The code works fine in that it runs without errors, but for whatever reason it's keeping those quotes in the resulting definition query. Using the code I provided above, my expected result is: &lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;SHAPE.STLength() and I'm getting: '&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;SHAPE.STLength()' which is a syntax error in the definition query.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;I've tried swapping out double quotes/single quotes and the code will bring over either one that is used in the script. Removing the quotes altogether generates the following error: &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;File "C:\temp\DefQueryScript\ReplaceStatement.py", line 25, in &amp;lt;moldule&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'shape_length'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SHAPE.STLength(),&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;NameError: name 'SHAPE' is not defined.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;which is to be expected. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;If I go into the MXD and create the same definition query manually, no quotes are used. &lt;/SPAN&gt;Is it possible there is a setting somewhere that allows DQs to run without the quotes or would put them back in?&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 16 Dec 2015 15:20:29 GMT</pubDate>
    <dc:creator>SallieVaughn</dc:creator>
    <dc:date>2015-12-16T15:20:29Z</dc:date>
    <item>
      <title>Replace Definition Query</title>
      <link>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354933#M27915</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've got a series of MXDs with definition queries with file gdb-specific syntax. I'd like to batch modify those DQs to work with enterprise gdb syntax. Not every layer has a DQ, and not all DQs are the same. The problem I'm running into is with the expressions portion... the function is passing 'SHAPE.STLength()' with the quotes to the definition query, which doesn't match the syntax (need it without the quotes). Changing them to double quotes returns similar results, and removing the quotes all together causes the function to fail.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;from arcpy import env&lt;/P&gt;&lt;P&gt;from os import path&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;env.workspace = ws = r"C:\temp\DefQueryScript\test1"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;expressions = {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'shape_length'&amp;nbsp; : 'SHAPE.STLength()',&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'shape_area'&amp;nbsp;&amp;nbsp;&amp;nbsp; : 'SHAPE.STArea()',&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'SHAPE_Length'&amp;nbsp; : 'SHAPE.STLength()',&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'SHAPE_Area'&amp;nbsp;&amp;nbsp;&amp;nbsp; : 'SHAPE.STArea()',&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'SHAPE_length' : 'SHAPE.STLength()',&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'SHAPE_area'&amp;nbsp;&amp;nbsp; : 'SHAPE.STArea()',&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def letsGetThisPartyStarted():&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ''' For each Map Document in the workspace, check each layer for a&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; definition query, and if found, update it according to the contents of&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; the expression dictionary.'''&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Let's get this party started!\n"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxds = getMXDs()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i, mxd in enumerate(mxds):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for layer in getLayers(mxd):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; evaluateExpression(layer)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; saveMXD(mxd, i)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def getMXDs():&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ''' Create a list of each Map Document in the workspace(s).'''&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxds = [arcpy.mapping.MapDocument(m) for m in arcpy.ListFiles("*.mxd")]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return mxds&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def getLayers(mxd):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ''' Collects all feature layer in a Map Document.'''&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "{0} layers:\n".format(mxd.filePath)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; layers = [l for l in arcpy.mapping.ListLayers(mxd) if l.isFeatureLayer]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return layers&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def evaluateExpression(lyr):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ''' If the layer has a Definition Query, update that query using the&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dictionary of expressions.'''&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.definitionQuery:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for exp in expressions.items():&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; key, value = exp[0], exp[1]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if key in lyr.definitionQuery:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; updateDefinitionQuery(lyr, key, value)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {0}: No Definition Query found\n".format(lyr.name)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def updateDefinitionQuery(lyr, old_exp, new_exp):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ''' Replaces the old expression with the new one. Fill in the old/new&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; associations in the "expressions" dictionary above.'''&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {0}:".format(lyr.name)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Old expression: {0}".format(lyr.definitionQuery)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = lyr.definitionQuery.replace(old_exp, new_exp)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; New expression: {0}\n".format(lyr.definitionQuery)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def saveMXD(mxd, i):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ''' Saves a copy of the Map Document which has had it's layers Definition&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Queries updated.'''&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(path.join(ws, "COPY_" + mxd.filePath + "_{0}.mxd".format(i)))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del mxd&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if __name__ == "__main__":&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; letsGetThisPartyStarted()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "'fin.'"&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Dec 2015 13:31:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354933#M27915</guid>
      <dc:creator>SallieVaughn</dc:creator>
      <dc:date>2015-12-16T13:31:21Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Definition Query</title>
      <link>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354934#M27916</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I just tried your code on an MXD I threw together and everything worked fine, the definition query was updated properly and not extraneous quotes were included.&amp;nbsp; At some point is the code generating an error on your end?&amp;nbsp; If so, sharing the specific error message/output might be helpful to others.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Dec 2015 15:06:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354934#M27916</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-12-16T15:06:06Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Definition Query</title>
      <link>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354935#M27917</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for the reply. The code works fine in that it runs without errors, but for whatever reason it's keeping those quotes in the resulting definition query. Using the code I provided above, my expected result is: &lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;SHAPE.STLength() and I'm getting: '&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;SHAPE.STLength()' which is a syntax error in the definition query.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;I've tried swapping out double quotes/single quotes and the code will bring over either one that is used in the script. Removing the quotes altogether generates the following error: &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;File "C:\temp\DefQueryScript\ReplaceStatement.py", line 25, in &amp;lt;moldule&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'shape_length'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SHAPE.STLength(),&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;NameError: name 'SHAPE' is not defined.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;which is to be expected. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;If I go into the MXD and create the same definition query manually, no quotes are used. &lt;/SPAN&gt;Is it possible there is a setting somewhere that allows DQs to run without the quotes or would put them back in?&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Dec 2015 15:20:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354935#M27917</guid>
      <dc:creator>SallieVaughn</dc:creator>
      <dc:date>2015-12-16T15:20:29Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Definition Query</title>
      <link>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354936#M27918</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Are you processing shape files, by chance?&amp;nbsp; Since your code worked fine on a feature class I had in a file geodatabase, it got me thinking about different storage formats having different field delimiters.&amp;nbsp; I know shape files use quotes, and I think file geodatabases used to or still do in some circumstances.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Anyhow, I wonder if the quotes are already there and you are substituting your expression &lt;SPAN style="text-decoration: underline;"&gt;within&lt;/SPAN&gt; the existing quotes, so it looks like you are passing quotes when you are not actually doing so.&amp;nbsp; Since I don't have one of your MXDs to look at, that is the best guess I can come up with.&amp;nbsp; Otherwise, I am stumped.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Dec 2015 16:20:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354936#M27918</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-12-16T16:20:10Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Definition Query</title>
      <link>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354937#M27919</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;No, I'm in an SDE environment with SQL Server 2008 R2 on the back end. I wondered about the storage format too.&lt;/P&gt;&lt;P&gt;I'll change up the quotes and see if I can get anything to stick -- the obvious choice is to remove them completely, but that throws the syntax error. &lt;SPAN style="line-height: 1.5;"&gt;I'm wondering if the .replace function just isn't meant to function this way.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Dec 2015 16:32:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354937#M27919</guid>
      <dc:creator>SallieVaughn</dc:creator>
      <dc:date>2015-12-16T16:32:19Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Definition Query</title>
      <link>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354938#M27920</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/updatingandfixingdatasources.htm" title="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/updatingandfixingdatasources.htm"&gt;Updating and fixing data sources with arcpy.mapping—Help | ArcGIS for Desktop&lt;/A&gt; link has a bunch of stuff towards the bottom going through the hoops you need to use to get definitionQuery to work with.&amp;nbsp; it is about 2/3rds of the way down.&amp;nbsp; might apply&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Dec 2015 23:06:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-definition-query/m-p/354938#M27920</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2015-12-25T23:06:48Z</dc:date>
    </item>
  </channel>
</rss>

