AnsweredAssumed Answered

Data Driven Pages and the arcpy DA Module

Question asked by Olesya on Feb 6, 2018
Latest reply on Feb 7, 2018 by Dan_Patterson

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.

Outcomes