Select to view content in your preferred language

arcpy batch to export PDF - problem of refresh mapframe in layout

997
6
03-10-2021 03:03 AM
Labels (2)
NatashaManzuiga
Occasional Contributor

Dear Everybody, I'm trying to make a batch to export PDF from ArcGIS Pro.

Before I export PDF I need to set definition query for each layers based on values in a table.

Then, there is the export but what I'm getting is always a blank PDF, it looks like it's not refreshing after I set the new extent.

After the script, of course, the layout show me the last extent I set....and it's correct!

Thanks in advance,

 

project  = arcpy.mp.ArcGISProject("CURRENT")
layout   = project.listLayouts("Layout")[0]
mapframe = layout.listElements("MAPFRAME_ELEMENT", "Map Frame")[0]
mapLayers = project.listMaps("Layers")[0]
fields=['field1', 'field2', 'field3', 'field4', 'field5']
search_cursor=arcpy.da.SearchCursor("theTable",fields)
 
for row in search_cursor:
    print(u'{0}, {1}, {2}'.format(row[0], row[1], row[2]))
    queryStr="MAPID="+str(row[0])
    for lyr in mapLayers.listLayers():
        if lyr.supports("DEFINITIONQUERY"):
            lyr.definitionQuery = queryStr
    layer=mapLayers.listLayers("l")[0]
    print(u'{0}, {1}, {2}, {3}'.format(row[0], row[1], row[2], queryStr))       
    layer_extent = mapframe.getLayerExtent(layer, False, False)
    mapframe.camera.setExtent(layer_extent)
    layout.exportToPDF(os.path.join(r"D:\test", f"{row[1]}.pdf"))

 

 

 

 

 

Tags (3)
6 Replies
ElizabetJaneRoot
New Contributor II

I encountered the same issue. I would like to escalate the priority of this question.

George_Thompson
Esri Notable Contributor

If you need immediate assistance, I would recommend contacting Esri Technical Support to work with an analyst on this.

--- George T.
ElizabetJaneRoot
New Contributor II

Thanks @George_Thompson 
Fortunately, I solved the issue.

@NatashaManzuiga ,
for your case, you need to probably add the layer to the map like below like map.addLayer(layerObject or path)

Please note that you should not do that within function scope but only global scope.

JamesTurner2
New Contributor III

I'm having this same issue. How did you resolve this? 

I found that my code works as expected if executed as a python notebook in pro, but if I try to run in the python window, I get the mysterious empty layout.

The layer I would like to display on the layout is already in the map, so I don't think ElizabethJaneRoot's comment applied.

@George_Thompson 

AlderMaps
Occasional Contributor

I've been having this same issue with multiple projects, but have found a workaround based on this thread!

There was mention of adding the layer, but then also a comment that it would not apply if the layer was already in the map. Well I thought, that is kind of like the refresh that I want...

So even though all the layers area already in the map I'm trying to export, I (programmatically) removed them all and then re-added from a .lyrx file right before I exported the map. Currently working for me and this solves a BIG headache I've battled for a few months now!

0 Kudos
JamesTurner2
New Contributor III

Interesting follow up. I don't recall if I tried removing and re-adding the layer. I settled on using a python notebook instead, which while slowed, made things easier overall. I didn't have much experience with notebooks before, but they are a great tool for troubleshooting and I had a lot of that to do!

My organization is dragging on updating to Pro 3.x so maybe I will revisit moving to a script instead of a notebook when that happens. I think there are some other new functions I'll be able to incorporate when we upgrade as well.

0 Kudos