AnsweredAssumed Answered

Checking for null values in a map series attribute

Question asked by FranklinCountyPaGIS on Feb 17, 2020
Latest reply on Feb 17, 2020 by FranklinCountyPaGIS

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

Outcomes