1

I have an ArcMap 10.3 MXD in which which I am trying to update the definition query of an enterprise geodatabase layer using Python:

import arcpy, os, re
from arcpy import env

inmxd = r'C:\Temp\Tenure_Shire Notification.mxd'
tenID = 'E40/01300'
env.workspace = os.path.dirname(inmxd)
mxd = arcpy.mapping.MapDocument(inmxd)
df = arcpy.mapping.ListDataFrames(mxd, "Main Map")[0]
tenLMAT = arcpy.mapping.ListLayers(mxd, "Tenement Application - Live Mining Act Tenure")[0]
oldExp = tenLMAT.definitionQuery
oldTenID = re.split(r"\'", oldExp)[1]
newExp = oldExp.replace(oldTenID,tenID)
tenLMAT.definitionQuery = newExp

My issue is that using this code, my definition query has not changed when I check the layer properties. What am I doing wrong?

closed as off-topic by user2856, Fran Raga, Vince, Evil Genius, Dan C Jun 25 at 13:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – Vince, Evil Genius, Dan C
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Have also tried arcpy.RefreshActiveView() after setting the definition query – user145096 Jun 24 at 1:23
  • 2
    Please edit your question to state whether you are running this code from within arcmap or from a standalone script, whether you are saving the mxd in your script then opening it in a new arcmap session. – user2856 Jun 24 at 1:30
  • 1
    On the line oldTenID = re.split(r"\'", oldExp)[1] you're escaping a raw string, it should be oldTenID = re.split("\'", oldExp)[1] (without the r) or oldTenID = re.split(r"'", oldExp)[1] (without the escape char). As it stands you're looking for \' to split and that isn't likely in your definition query. – Michael Stimson Jun 24 at 1:40
  • 1
    Can you reveal what the query is and what you're expecting to get in newExp please. If you're running this from command line or on a mapdocument other than the one that is currently open have you called mxd.save(); when running in ArcMap have you changed the mxd = arcpy.mapping.MapDocument(inmxd) to mxd = arcpy.mapping.MapDocument('CURRENT')? – Michael Stimson Jun 24 at 2:10
  • 1
    Have got it to work with the mxd.save() from an empty map document. Figured out that I had not changed the hard coded variable! Thanks FelixIP and Michael for your help! – user145096 Jun 24 at 2:33
0

It seems the answer is very simple. Run the script outside the active map document and use mxd.save() after setting the definition query:

import arcpy, os, re
from arcpy import env

inmxd = r'C:\Temp\Tenure_Shire Notification.mxd'
tenID = 'E40/01300'
env.workspace = os.path.dirname(inmxd)
mxd = arcpy.mapping.MapDocument(inmxd)
df = arcpy.mapping.ListDataFrames(mxd, "Main Map")[0]
tenLMAT = arcpy.mapping.ListLayers(mxd, "Tenement Application - Live Mining Act Tenure")[0]
oldExp = tenLMAT.definitionQuery
oldTenID = re.split("\'", oldExp)[1]
newExp = oldExp.replace(oldTenID,tenID)
tenLMAT.definitionQuery = newExp

mxd.save()  

Thanks for everyone's suggestions.

Not the answer you're looking for? Browse other questions tagged or ask your own question.