Select to view content in your preferred language

Data Driven Page Export ArcPRO to Image

998
3
07-10-2018 09:32 AM
elpone
by
Occasional Contributor

The key issue to me seems to be:

arcpy.AddMessage(theRow.getValue(theNameField))

Is not working in arcpro in respect to a row object from a layout mapseries, this worked  just fine in ArcGIS (10.3)

Details

I am trying to automate the export of a data driven page into .png in arcpro.  The issue I am running into working out how to automate the naming of the output .png's.

The naming pattern I want to use is

theOutput = outDir + '\\' + theBase + '_'+str(theNameValue) + ".png"

Where

outDir is an output directory which is passed into the job as a parameter.

theBase is the base file name of the arcpro project and is defined thus

theBase = os.path.splitext(os.path.basename(aprx.filePath))[0]

The issue is arising in setting theNameValue. 

I want to use the value of mapsSeries.pageNameField for the current page the script is iterating through. 

A sample script is attached and requires the following parameters.

outDir = arcpy.GetParameterAsText(0) # Create or use an output directory variable
outRes = arcpy.GetParameterAsText(1) # Resolution of PNG in dpi.
theLayout = arcpy.GetParameterAsText(2) #The layout we want to push out DDPs on.

Now on my test data I happen to have set the map series name field to be a field named: Name (the use case is bridges, but that's not really relevant...) but it is successfully used in line 23

arcpy.AddMessage(theRow.Name)

Now according to the help on map series line 20 on the snippet below, theRow should be returning a row object.   

Checking what theRow is via line24 returns:  [0, 'Aughnacrew Bridge Culvert', 'BR01', ' ']

on the test data I was using this is expected.  According to the help on row objects this should have a method

getValue (field_name)

that Gets the field value....

However when I try to use it like in line 26:

arcpy.AddMessage(theRow.getValue(theNameField))

It will cause the code to fail like the image below and as far as I can tell this should work.

Can anyone help me understand why this is not working?  

I have managed to get this script to  sort of work by calling

theRow.__getattribute__(theNameField)

But this does not strike me as good form.

I am using ArcPRO 2.2.0

for lyt in aprx.listLayouts(theLayout):
    if lyt.name == theLayout:
        #we check we are hitting on our desired 
        if  lyt.mapSeries is not None:
            ms = lyt.mapSeries
            idxLyer = ms.indexLayer
            idxLyerName = ms.indexLayer.name
            if ms.enabled:
                arcpy.AddMessage("\n\tMap Doc is data driven page.\n\tAll good to proceed")
                
                #get the name field.
                theNameField = ms.pageNameField.name
                test = ms.pageNameField
                
                #get the number of pages to use for push out
                for pageNum in range(1,ms.pageCount + 1):
                    #make the page the current page number..
                    ms.currentPageNumber = pageNum
                    arcpy.AddMessage(pageNum)
                    theRow = ms.pageRow
                    fields = arcpy.ListFields(idxLyerName)
                    arcpy.AddMessage(theNameField)
                    arcpy.AddMessage(theRow.Name)
                    arcpy.AddMessage(theRow)    #seeing what row contains
                    arcpy.AddMessage(theRow[1]) #access row by index
                    #arcpy.AddMessage(theRow.getValue(theNameField))    #This causing fail
                    arcpy.AddMessage(dir(theRow))   #find out what methods and attributes theRow has.
                    arcpy.AddMessage(theRow.__getattribute__(theNameField)) #This works?
                    arcpy.AddMessage("where is this going wrong?")
                   
                    
                    theOutput = outDir + '\\' + theBase + '_'+str(theRow.__getattribute__(theNameField)) + ".png"
                    arcpy.AddMessage(theOutput)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

geoprocessing fail due to line:arcpy.AddMessage(theRow.getValue(theNameField))

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus
msg = theRow.getValue(theNameField)
arcpy.AddMessage("{}".format(msg))

python format strings automatically convert whatever to a string representation.  You can control whether you want str or repr to be used as in the example below

a = np.arange(10)

"{}".format(a)
'[0 1 2 3 4 5 6 7 8 9]'

"{!s:}".format(a)      # str is used by default
'[0 1 2 3 4 5 6 7 8 9]'

"{!r:}".format(a)      # repr can be used if the object has one
'array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])'
0 Kudos
elpone
by
Occasional Contributor

Hi Dan Patterson

This is not really the key issue, getting a string returned is the desired outcome.  The issue to me seems to be a

arcpy.AddMessage(theRow.getValue(theNameField))

Is not working in arcpro. 

The same line in a script that worked just fine in ArcGIS (10.3) is below.  All I was attempting was to upgrade a script for use in ArcPRO.  The bit wrecking my head is according to the documentation it "should work". 

As far as I can tell the row object returned from the layout mapseries for the current page we are iterating through our pages does not have access to getValue (field_name) method but according to the ArcPY documentation for Row that method is alive and well...

#Get details of map document that called this script to run...
mxd = arcpy.mapping.MapDocument("CURRENT")

# get the name field
theNameField = mxd.dataDrivenPages.pageNameField.name

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    
    #get the value of theNameField  for the current row of the index layer
    theRow = mxd.dataDrivenPages.pageRow
    theNameValue = theRow.getValue(theNameField )‍‍‍‍‍‍‍‍‍‍‍‍

However thanks for the reply, this will be a handy tip in debugging when one is not sure what you are really getting back.

Regards.

0 Kudos
LouisDECOMBE
Frequent Contributor

Hi, 

I try also to make png file for my mapseries. I saw your script and its seems great. I dont know if you find a solution.

If yes,would it possible please to share it again.

Sorry for my poor english.

Regards 

Louis

0 Kudos