Checking for null values in a map series attribute

67
2
02-17-2020 08:52 AM
Kara_Shindle
Occasional Contributor III

So I am trying to convert a data driven pages script from Python 2.7 to map series Python 3.

My original script, I had an attribute that sometimes had a  file path stored in it, and whenever I would get to a specific page in the DDP, I would store the possible row value as a variable, check if it existed, & append it to my PDF if it did.

Original Script:

pageID = parMXD1.dataDrivenPages.getPageIDFromName(UPI)
parMXD1.dataDrivenPages.currentPageID = pageID

finalPDF = os.path.join(folder, UPI) + ".pdf"
parMXD1.dataDrivenPages.exportToPDF(finalPDF, "CURRENT")

#The attribute with possible sketch pathway
comSketch = parMXD1.dataDrivenPages.pageRow.COMMERCIAL_SKETCH

#opens first PRC Page
prc = arcpy.mapping.PDFDocumentOpen(finalPDF)
if os.path.exists(str(comSketch)):
prc.appendPages(str(comSketch))
prc.saveAndClose()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Now, in my code for the map series, if the attribute is Null, it is not letting me store that row as a variable & checking to see if it exists.

Current Script:

mxdFile = arcpy.mp.ArcGISProject(mxdPath)
cleanName = name.replace("/", "_")
finalPDF = os.path.join(outFolder, cleanName + ".pdf")

l = mxdFile.listLayouts("Page_1")[0]
if not l.mapSeries is None:
ms = l.mapSeries
if ms.enabled:
ms.currentPageNumber = ms.getPageNumberFromName(name)
file = os.path.join(outFolder, cleanName)
ms.exportToPDF(file, "CURRENT", resolution=300)

#Checks for existence of Sketch PDF & appends it to new PDF
commSketch = ms.pageRow.COMMERCIAL_SKETCH
if os.path.exists(commSketch):
pdfDoc = arcpy.mp.PDFDocumentOpen(finalPDF)
pdfDoc.appendPages(file2)
pdfDoc.saveAndClose()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I have struggled with trying to access via row objects, search cursors, and for loops, and I am not having any luck trying to store that attribute and check for if it is Null or not.  

Update:

This is script I am testing to make a function so that I can call it in multiprocessing for creating about 140,000 multi-page PDFs.

import os, sys
import arcpy
arcpy.env.overwriteOutput = True

def worker(mxdPath, outFolder, name):

try:

mxdFile = arcpy.mp.ArcGISProject(mxdPath)
cleanName = name.replace("/", "_")
finalPDF = os.path.join(outFolder, cleanName + ".pdf")

l = mxdFile.listLayouts("Page_1")[0]
if not l.mapSeries is None:
ms = l.mapSeries
if ms.enabled:
ms.currentPageNumber = ms.getPageNumberFromName(name)
file = os.path.join(outFolder, cleanName)
ms.exportToPDF(file, "CURRENT", resolution=300)

pdfDoc = arcpy.mp.PDFDocumentOpen(finalPDF)
l2 = mxdFile.listLayouts("Page_2")[0]
if not l2.mapSeries is None:
ms2 = l2.mapSeries
if ms2.enabled:
ms2.currentPageNumber = ms.getPageNumberFromName(name)
pageTwo = cleanName + "t2"
file2 = os.path.join(outFolder, pageTwo + ".pdf")
ms2.exportToPDF(file2, "CURRENT", resolution=300)
pdfDoc.appendPages(file2)
os.remove(file2)
# ms.refresh()

resSketch = ms.pageRow.SKETCH
if os.path.exists(resSketch):
l3 = mxdFile.listLayouts("Page_3")[0]
if not l3.mapSeries is None:
ms3 = l3.mapSeries
if ms3.enabled:
ms3.currentPageNumber = ms.getPageNumberFromName(name)
pageThree = cleanName + "t3"
file3 = os.path.join(outFolder, pageThree + ".pdf")
ms3.exportToPDF(file3, "CURRENT", resolution=300)
pdfDoc.appendPages(file3)
os.remove(file3)
del file3, resSketch
else:
pass

#Checks for existence of Sketch PDF & appends it to new PDF
commSketch = ms.pageRow.COMMERCIAL_SKETCH
if os.path.exists(commSketch):
pdfDoc.appendPages(str(commSketch))
else:
pass

pdfDoc.saveAndClose()


except arcpy.ExecuteError:
# Geoprocessor threw an error
arcpy.AddError(arcpy.GetMessages(2))
print("Execute Error:", arcpy.ExecuteError)

except Exception as e:
tb = sys.exc_info()[2]
print("Failed at Line %i \n" % tb.tb_lineno)
print("Error: {} \n".format(e))

finally:
del mxdFile, pdfDoc
return False
Reply
0 Kudos
2 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Not sure which thing you are trying to collect, but you have no "else" statements anywhere.  That is where you would collect the False returns for the if statements on lines 6, 8 and 15.  If any of those conditions are not met, then the script proceeds on as if nothing happens.  If you want to store a False condition, then you need a container to put the result/condition in (eg a list), then you can check those values after

Reply
0 Kudos
Kara_Shindle
Occasional Contributor III

I had stripped most of my else statements out to try and parse down the code, so I apologize if it made it confusing.  

I am trying to capture line 13 as a variable to check if it is false or not.  It won't let me set that variable, as it states that it doesn't exist for my records where the attribute is Null.  I need to figure out how to test whether that attribute is Null or not.

I think what I need is something like:

#store page Row variable Commercial Sketch as commSketch

#if Commercial Sketch exists:

   #append file path to existing PDF

#if Commercial Sketch doesn't exist:

   #do nothing

Whenever my code hits line 13, I get the following error:

Error: 'pageRow' object has no attribute 'COMMERCIAL_SKETCH' 

So I haven't figured out how to even get it to the point where it allows me to test if it is False or not.

This is actually part of a function where I am implementing multiprocessing for several thousand PDFs that need created.  

I updated my code with the actual function I'm working on

Reply
0 Kudos