exportToPDF Mapframe empty

477
4
06-24-2020 03:32 AM
BrunoGranger
New Contributor

Hi everyone,

I am using #Arcgis pro 2.5 and I have an issue 😞

I am using the code above with a aprx with a map, a layout and some bookmarks.to export to pdf a layout zooming to differents boukmarks...

And it work unless I uncomment this line :

cursor = arcpy.da.SearchCursor(layerSet,"*")

Then, the mapframe is empty 😞

But I want to iterate through Row objects and extract field values in my global code !

Any idea ?

import arcpy, os, time
from arcpy import env

# define the project
aprx = arcpy.mp.ArcGISProject("CURRENT")

# Define the first map
m = aprx.listMaps()[0]

# first layer in m
layerSet = m.listLayers()[0]
#cursor = arcpy.da.SearchCursor(layerSet,"*")

# define the first layout
layoutwork = aprx.listLayouts()[0]

# define first mapframe in the layout
mf = layoutwork.listElements("MAPFRAME_ELEMENT")[0]

# list of the bookmarks
bkmks = mf.map.listBookmarks()

#loop of the boukmarks
for bkmk in bkmks:
    # zoom to the bookmark
    mf.zoomToBookmark(bkmk)
    # export in pdf
    layoutwork.exportToPDF(os.path.join(r"C:\temp", f"{bkmk.name}.pdf"))
0 Kudos
4 Replies
DavidPike
MVP Frequent Contributor

Hi, I'd probably investigate what the listlayers() is generating by throwing in a print statement.

The cursor should accept a layer object as input, but maybe try layer.dataSource instead (look up the syntax as my capital letters are usually mixed up...).

A final thought is that you may have to specify the dataframe object being passed into listlayers()

0 Kudos
BrunoGranger
New Contributor

Hi David,

Thank you for your answer 😉

print(layerSet)

Return the longName of the first layer ie : sel\Region

print(layerSet.name)

Return the 'short' name of the first layer : Region

cursor = arcpy.da.SearchCursor(layerSet.dataSource,"*")

Have the same result : the mapframe is empty 😞

How do you do :

David Pike a écrit:

 specify the dataframe object being passed into listlayers()

Thank you

0 Kudos
DavidPike
MVP Frequent Contributor

oh I thought print(layerSet) would return an Object.

What are you intending to do with the cursor and the code in general? I cant see a use of the cursor after it's instantiated. Would give me a better insight when reading the code.

0 Kudos
BrunoGranger
New Contributor

Hi David,

This is my code :

import arcpy, os

from arcpy import env

# define the projet
aprx = arcpy.mp.ArcGISProject("CURRENT")

# Défine the map
m = aprx.listMaps("Departmental")[0]

# define the layout
layoutwork = aprx.listLayouts("MEP")[0]
# define the mapframe
mf = layoutwork.listElements("MAPFRAME_ELEMENT")[0]

# layers list in m(ap)
layerSets = m.listLayers()
# Region layout
layerRegion =  m.listLayers("Region")[0]

# Number of row in Region
result = arcpy.GetCount_management("Sel\Region")
nbregion = int(result.getOutput(0))
print("nb regions : "+str(nbregion))

# Loop Region layer
with arcpy.da.SearchCursor(layerRegion, 'admin1Name') as cursor:
    # for every région
    for row in cursor:
        LaRegion = row[0]
        print("--- Création des cartes pour : "+LaRegion)
        # find the bookmark of the region
        bkmks = mf.map.listBookmarks()
        for bkmk in bkmks:
            if bkmk.name == LaRegion:
                mf.zoomToBookmark(bkmk)
        # loop for every layer in the map
        for layerSet1 in layerSets:
            # If The layer is in a group and it's not region
            if layerSet1.name != layerSet1.longName and layerSet1.name != "Region":
                print(layerSet1.name)
                # If the layer is gris
                if layerSet1.name == "gris":
                    print("yoman------gris")
                # If the layer is not gris
                else:
                    layerSet1.visible = True
                    ensDef = '"admin1Name" =\'' + LaRegion + '\''
                    layerSet1.definitionQuery = ensDef
                    NomFichier = "D:\\temp\\"+LaRegion+"_"+layerSet1.name+".pdf"
                    layoutwork.exportToPDF(NomFichier)
                    layerSet1.visible = False

Thank you for your help 😉

And the result :

0 Kudos