Layer definition query and symbology not updating using arcpy - what am I missing?
import arcpy, os arcpy.env.overwriteOutput = True grab = arcpy.GetParameterAsText(0) prep_list = grab.split(";") mxd_c = arcpy.mapping.MapDocument(r"T:\GIS\Projects\GIS\Online\MapDocuments\FeederMapCover.mxd") lyrs_c = arcpy.mapping.ListLayers(mxd_c) feederList = ["HB2", "HB3"] print "got feeder list" arcpy.AddMessage("...starting map book...") for feeder in feederList: arcpy.env.overwriteOutput = True for lyr in arcpy.mapping.ListLayers(mxd_c): if lyr.name == "Overhead": lyr.definitionQuery = "esUplineFeeder = '" + feeder + "'" if lyr.name == "Primary Underground Conductor": lyr.definitionQuery = "esUplineFeeder = '" + feeder + "'" print "Definition Queries Set" + feeder # Find Overview Data Frame and Zoom to the Map Index df = arcpy.mapping.ListDataFrames(mxd_c, "Layers")[0] zoomLayer = arcpy.mapping.ListLayers(mxd_c, "Overhead", df)[0] df.extent = zoomLayer.getSelectedExtent(False) df.scale = df.scale * 1.5 print "Zoomed Successfully" # Make and Add the FeederMapIndexLAYER feature layer to the map and refresh the view arcpy.env.workspace = r"T:\GIS\Projects\GIS\Online\SpatialData\FeederMaps.gdb" FeederMap = "T:\GIS\Projects\GIS\Online\SpatialData\FeederMaps.gdb\FeederMapIndex" arcpy.MakeFeatureLayer_management(FeederMap, "FeederMapIndexLAYER") print "Feature Layer Created" addLayer = arcpy.mapping.Layer('FeederMapIndexLAYER') print "Feature Layer Added to Map" arcpy.mapping.AddLayer(df, addLayer) arcpy.RefreshTOC() arcpy.RefreshActiveView() # Field Calculator InMap = 0 arcpy.CalculateField_management("FeederMapIndexLAYER", "InMap", expression= 0 ) print "Index Field Calculator Worked" # Select FeederMapIndexLayer polygons that intersect Overhead Lines and Calculate FeederMapIndex = 1 Selection = arcpy.SelectLayerByLocation_management('FeederMapIndexLAYER', 'INTERSECT', 'Overhead') print "Index Selection Completed" arcpy.CalculateField_management("FeederMapIndexLAYER", "InMap", expression= 1 ) print "Index Field Calculator Worked" #Set FeederMapIndex Layer Definition Query and Refresh Map for lyr in arcpy.mapping.ListLayers(mxd_c): if lyr.name == "FeederMapIndexLAYER": lyr.definitionQuery = "InMap = 1" arcpy.RefreshTOC() arcpy.RefreshActiveView() print "Index Definition Query Worked" #Export the Map to a PDF arcpy.ApplySymbologyFromLayer_management('FeederMapIndexLAYER', 'C:/Users/azumwalt.UECOOP/Desktop/FeederMapIndex.lyr') arcpy.RefreshTOC() arcpy.RefreshActiveView() outputPath = r"Z:\FeederMaps\PDFs\Map" + feeder + "Cover.pdf" arcpy.mapping.ExportToPDF(mxd_c, outputPath) print"Map Book Printed" + feeder print "complete"
Is ArcMap open and the "current" document specified?
If Python is used to modify the table of contents within the current map document (e.g., change a layer name), the map will not automatically update with the changes. RefreshTOC is only needed if you want to see the table of contents updated.
See the code snippet in that topic... I don't see this line
mxd = arcpy.mapping.MapDocument("CURRENT")
Would be best to include the complete code since your variable "mxd_c" is probably defined somewhere before, but we can't see how it has been defined.
The current map document is not open. I just added the rest of the code so you can see all of it.
therein lies the problem... from RefreshActiveView
RefreshActiveView is only needed if you want to see the active view of the current map document updated.
arcpy.mapping export, save, and printing functions will generate the expected updated results without use of RefreshActiveView.
So you need to save or export or print the mxd before seeing anything reflected in the document.
You do have an Export to pdf line, is that the issue?
You might try running in "current" mode to see it you get what you are looking for
Since you don't save the mxd at the end, you can investigate in the mxd to see if things were set properly or not. Since you also have selections going on, it would be good to or save the mxd (perhaps using save a copy with different names to be able to see the intermediate result in the mxd and validate if things go right) or include a number of print statements printing a property of a layer after setting it to be sure it works.
Thanks for all your help guys! I am getting the notice "Cannot find reference "saveACopy" in mapping.py." I'm using pycharm and am not sure how to correct this. My Project Interpreter is referencing 10.6:
If this needs to be in a new thread let me know. I just want to be able to save my mxd's to troubleshoot
MapDocument—Help | ArcGIS Desktop
saveACopy (file_name, {version}) | Provides an option to save a map document (.mxd) to a new file, and optionally, to a previous version. |
It is there... Does pycharm import arcpy properly? Hope you don't have multiple versions of python installed
Hello again,
So I am saving mxd's at regular intervals now but when it comes to intersecting the FeederMapIndexLayer with "Overhead" it's not recognizing the definition query - it just intersects it with ALL of the overhead lines. When I open the mxd the definition query is present.... any thoughts?
import arcpy arcpy.env.overwriteOutput = True grab = arcpy.GetParameterAsText(0) prep_list = grab.split(";") mxd_c = arcpy.mapping.MapDocument(r"T:\GIS\Projects\GIS\Online\MapDocuments\FeederMapCover.mxd") lyrs_c = arcpy.mapping.ListLayers(mxd_c) feederList = ["HB2", "HB3"] print "got feeder list" arcpy.AddMessage("...starting map book...") for feeder in feederList: arcpy.env.overwriteOutput = True for lyr in arcpy.mapping.ListLayers(mxd_c): if lyr.name == "Overhead": lyr.definitionQuery = "esUplineFeeder = '" + feeder + "'" if lyr.name == "Underground": lyr.definitionQuery = "esUplineFeeder = '" + feeder + "'" print "Definition Queries Set" + feeder # Make and Add the FeederMapIndexLAYER feature layer to the map arcpy.env.workspace = r"T:\GIS\Projects\GIS\Online\SpatialData\FeederMaps.gdb" FeederMap = "T:\GIS\Projects\GIS\Online\SpatialData\FeederMaps.gdb\FeederMapIndex" arcpy.MakeFeatureLayer_management(FeederMap, "FeederMapIndexLAYER") print "Feature Layer Created" addLayer = arcpy.mapping.Layer('FeederMapIndexLAYER') print "Feature Layer Added to Map" # Field Calculator InMap = 0 arcpy.CalculateField_management("FeederMapIndexLAYER", "InMap", expression= 0 ) print "Index Field Calculator Worked" # Select FeederMapIndexLayer polygons that intersect Overhead Lines and Calculate FeederMapIndex = 1 Selection = arcpy.SelectLayerByLocation_management('FeederMapIndexLAYER', 'INTERSECT', 'Overhead') print "Index Selection Completed" arcpy.CalculateField_management("FeederMapIndexLAYER", "InMap", expression= 1 ) print "Index Field Calculator Worked" mxd_c.saveACopy("Z:\FeederMaps\MXDs\TEST1_" + feeder + ".mxd")
I see that you add a layer ( the same layer) for every feeder and you use the same name for them. This might make it harder for the code to reference the one you want. Why would you want to add the same layer twice? Isn't it possible to use the same layer for both feeders?
What do you see in the saved MXD? The query definition is properly set and the correct records have the value 1?