Data Driven Pages and the arcpy DA Module

719
3
02-06-2018 02:56 PM
OlyPowers
New Contributor III

I've written a script that is going to run Data Driven Pages nightly after checking to see if edits have been made to a feature class. In my code below the first function does not work, and I cannot for the life of me figure out why. It stops functioning at line 22 while I'm trying to set the current page for the DDP. After some serious experimentation, the second function does exactly what it's supposed to do. At this point the problem is solved, however I would like to know why the first function doesn't work. So far the only thing I've been able to come up with is that DDP does not play nice with search cursors.

import arcpy
from arcpy import env
env.overwriteOutput = True
import arcpy.da
from pytz import timezone
from datetime import datetime, timedelta


workspacePath = r"\\pathway\files\GIS\Projects\SDE_CONNECTION_FILES\GIS_USER_DC_GISDS_GIS.sde"
projectFC = workspacePath + "\GIS.DBO.ENG_PW_CONSTRUCTION_PROJECTS"
nowUTC = datetime.now(timezone('UTC'))
now = nowUTC.replace(tzinfo=None)
yesterdaysDate = now - timedelta(days=1)
mxd = arcpy.mapping.MapDocument(r"Q:\GIS\Projects\Engineering\Project_ID_Maps\ENG_Projects.mxd")
ddp = mxd.dataDrivenPages
indexLayer = ddp.indexLayer

def lastEditedOriginalCode():
    with arcpy.da.SearchCursor(projectFC, ['ID_KEY', 'last_edited_date'])as searchCursor:
        for row in searchCursor:
            pageID = ddp.getPageIDFromName(row[0])
            ddp.currentPageID = pageID
            if row[1] > yesterdaysDate:
                arcpy.mapping.ExportToJPEG(mxd, r"\\INTRANET\GISmap\\" + str(row[0]) + ".jpg", resolution=300)
                print "Exported New Map"
            else:
                print "No New Map Needed"


def lastEditedWorking():
    with arcpy.da.SearchCursor(projectFC, ['ID_KEY', 'last_edited_date'])as searchCursor:
        for row in searchCursor:
            if row[1] > yesterdaysDate:
                arcpy.SelectLayerByAttribute_management(indexLayer, "NEW_SELECTION", ' "ID_KEY" = \'{0}\''.format(row[0]))
                for each in ddp.selectedPages:
                    ddp.currentPageID = each
                    arcpy.mapping.ExportToJPEG(mxd, r"\\INTRANET\GISmap\\" + str(row[0]) + ".jpg", resolution=300)
                print "Exported New Map"
            else:
                print "No New Map Needed"

lastEditedOriginalCode()
lastEditedWorking()

Here's the Error I get when I run the OriginalCode

  File "Q:/GIS/Projects/Engineering/Project_ID_Maps/Eng_Proj_Script/EngProjMap.py", line 45, in <module>
    lastEditedOriginalCode()
  File "Q:/GIS/Projects/Engineering/Project_ID_Maps/Eng_Proj_Script/EngProjMap.py", line 24, in lastEditedOriginalCode
    pageID = ddp.getPageIDFromName(row[0])
  File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\utils.py", line 182, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\_mapping.py", line 340, in getPageIDFromName
    return convertArcObjectToPythonObject(self._arc_object.getPageIDFromName(*gp_fixargs((page_name,), True)))
ValueError: 478

Value Error is the actual value of the page that's supposed to be the current page.

I just want to know why my first attempt didn't work. Any feedback would be welcome.

0 Kudos
3 Replies
DanPatterson_Retired
MVP Esteemed Contributor

if that is the page number why not try its number since this suggests a key,which could be text (string).  I would throw a print statement in to see what it is actually returning and whether it exists

getPageIDFromName (page_name)

Returns a Data Driven Pages index value based on the name of the page

There is more information here http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/datadrivenpages-class.htm

where they use the pagenumber from the number of pages (see the code sample)

mxd.dataDrivenPages.currentPageID = pageNum

0 Kudos
OlyPowers
New Contributor III

Well, I'm getting the same error when I try to print the value of pageID. 

Which is interesting.

def lastEditedOriginalCode():
    with arcpy.da.SearchCursor(projectFC, ['ID_KEY', 'last_edited_date'])as searchCursor:
        for row in searchCursor:
            pageID = ddp.getPageIDFromName(row[0])
            print pageID
            # ddp.currentPageID = pageID
            # if row[1] > yesterdaysDate:
            #     arcpy.mapping.ExportToJPEG(mxd, r"Q:\GIS\Projects\Engineering\Project_ID_Maps\ExportTests\\" + str(row[0]) + ".jpg", resolution=300)
            #     print "Exported New Map"
            # else:
            #     print "No New Map Needed"

File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\_mapping.py", line 340, in getPageIDFromName
return convertArcObjectToPythonObject(self._arc_object.getPageIDFromName(*gp_fixargs((page_name,), True)))
ValueError: 478

Let me dig into this some more

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Hmmm. 10.4, you might want to check my Issues Addressed links in the Py Links to see if it is version specific problem

0 Kudos