Select to view content in your preferred language

export map layout with loop definition query

5728
22
Jump to solution
09-15-2021 02:08 AM
MahmoudHegazy
Regular Contributor

i would like to complete my script to zoom to layer per definition and export pdf but the script give me bugs when it export pdf and not zooming can any help me

import arcpy, os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []

layerFile = arcpy.mapping.Layer(layerName)
layerFile.definitionQuery = ""

print df.name


with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
for row in cursor:
for item in row:
whereList.append(item)

for wl in whereList:
layerFile.definitionQuery = fieldName + "= '" + wl + "'"
df = layerFile.getExtent()
df.extent = outFile
outFile = imgLocation + "\\" + wl + ".pdf"

arcpy.mapping.ExportToPDF(mxd, outFile)

del mxd

Tags (4)
0 Kudos
22 Replies
MahmoudHegazy
Regular Contributor

how can i set camera object extent instead of get extent

import arcpy, os

arcpy.env.overwriteOutput = True
aprx = arcpy.mp.ArcGISProject("CURRENT")
df = aprx.listMaps()[0]

layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []

layerFile = df.listLayers(layerName)[0]
layerFile.definitionQuery = ""

print ((df.name))


with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
    for row in cursor:
        for item in row:
            whereList.append(item)

for wl in whereList:

    
       
    layerFile.definitionQuery = fieldName + "= '" + wl + "'"
    #new_extent = layerFile.getExtent()
    new_extent = df.camera.setExtent(df.getLayerExtent(layerFile, False, True))
    df.extent = new_extent
    df.scale *= 1.5
    
    for elm in arcpy.mp.ListLayoutElements(mxd, "TEXT_ELEMENT", "title"):      
    
        if elm.name == "title":
           elm.text = wl

    
    name = ''
    for char in wl:
        if char.isalpha():
            name+=char

    outFile = imgLocation + "\\" + name  + ".pdf"   
    arcpy.mp.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")
del aprx

 

0 Kudos
MahmoudHegazy
Regular Contributor

it worked, thanks so much

i made a post as you said to make anyone help me to convert the script to arcgis pro becouse of dynamic table 

in the linke blow:

https://community.esri.com/t5/python-questions/convert-script-python-work-with-quot-export-layout/m-...

0 Kudos
MahmoudHegazy
Regular Contributor

error in arcgis pro

 invalid syntax line 31

import arcpy, os

arcpy.env.overwriteOutput = True
aprx = arcpy.mp.ArcGISProject("CURRENT")
df = aprx.listMaps()[0]

layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []

layerFile = df.listLayers(layerName)[0]
layerFile.definitionQuery = ""

print ((df.name))


with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
    for row in cursor:
        for item in row:
            whereList.append(item)

for wl in whereList:      
    layerFile.definitionQuery = fieldName + "= '" + wl + "'"
    #new_extent = layerFile.getExtent()
    lay = aprx.listLayouts("Layout")
    mf = lay.listElements("Layers Map Frame","New Data Frame Map Frame")
    new_extent = mf.camera.setExtent(mf.getLayerExtent(layerFile, False, True)
    #df.extent = new_extent
    #df.scale *= 1.5
    for lyt in aprx.listLayouts("Layout"):
        for elm in lyt.listElements("TEXT_ELEMENT"):
            if elm.name == "Text":
                elm.text = wl          

 

 

0 Kudos