AnsweredAssumed Answered

Layer definition query and symbology not updating using arcpy

Question asked by AJZumwalt9 on Aug 6, 2018
Latest reply on Aug 8, 2018 by AJZumwalt9

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 == "Overhead":
                lyr.definitionQuery = "esUplineFeeder = '" + feeder + "'"
            if == "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"
        print "Feature Layer Created"
        addLayer = arcpy.mapping.Layer('FeederMapIndexLAYER')
        print "Feature Layer Added to Map"
        arcpy.mapping.AddLayer(df, addLayer)

        # 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 == "FeederMapIndexLAYER":
                lyr.definitionQuery = "InMap = 1"
                print "Index Definition Query Worked"

        #Export the Map to a PDF
        arcpy.ApplySymbologyFromLayer_management('FeederMapIndexLAYER', 'C:/Users/azumwalt.UECOOP/Desktop/FeederMapIndex.lyr')
        outputPath = r"Z:\FeederMaps\PDFs\Map" + feeder + "Cover.pdf"
        arcpy.mapping.ExportToPDF(mxd_c, outputPath)
        print"Map Book Printed" + feeder

print "complete"