Select to view content in your preferred language

Data Driven Pages python help!

4166
6
04-05-2013 10:27 AM
RyanMiller
Deactivated User
I am attempting to export a set of maps using python and data driven pages. I have been successful in getting the code to work but I am struggling with the file naming of the exported JPEG's. I need to the file names to coincide with a field in the attribute table. Basically I am making a set of parcel maps, and need the file name to be ONLY the parcel number (PARCEL_NO). Here is the code I am using. I am a novice when it comes to this stuff so please be patient. Thanks in advance!

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    pageID = mxd.dataDrivenPages.getPageIDFromName("PARCEL_NO")
    arcpy.mapping.ExportToJPEG(mxd, r"P:\Township Maps\Bloomingdale\JPEG" + pageName + ".jpeg", resolution=100)
del mxd
0 Kudos
6 Replies
JoelCalhoun
Deactivated User
Without seeing how your DDP is actually set up I can't be sure but you could try something like this:


import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    fieldValue = mxd.dataDrivenPages.pageRow.PARCEL_NO
    fieldValue = str(fieldValue)
    arcpy.mapping.ExportToJPEG(mxd, r"P:\Township Maps\Bloomingdale\JPEG" + fieldValue + ".jpeg", resolution=100)
del mxd
0 Kudos
BruceRea
Deactivated User

Thanks for the discussion on this!  And thank you Joel Calhoun, pageRow fixed my situation perfectly. 

0 Kudos
RyanMiller
Deactivated User
Ok sorry.  What do you need from me?
0 Kudos
CPoynter
Frequent Contributor
Not sure, but you have a variable pageID, but then don't use it in code.

I have substituted into code to make it look like:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    pageID = mxd.dataDrivenPages.getPageIDFromName("PARCEL_NO")
    arcpy.mapping.ExportToJPEG(mxd, r"P:\Township Maps\Bloomingdale\JPEG" + "\" + pageID + ".jpeg", resolution=100)
del mxd


I also added an additional "\" to folder jpeg images under JPEG folder. You were trying to append new file to JPEG folder name.

Regards,

Craig
0 Kudos
JoelCalhoun
Deactivated User
Craig,


I don't think it's a good practice to use the pageID to label your exported images since the pageID is an internal index value and my not represent what you think it does.

Here is the note from the help:


getPageIDFromName (page_name)
Parameter Explanation Data Type
page_name A value in the index layer that corresponds to the Name field that was used to set up Data Driven Pages
String

Many of the Data Driven Pages properties and methods use an internal index value rather than the literal names of the pages used to create the index layer. The index values are automatically generated based on the Name and Sort fields. It may not be obvious which index value represents a specific page. The getPageIDFromName method provides a mechanism for this translation.

pageID = mxd.dataDrivenPages.getPageIDFromName("HarborView")
mxd.dataDrivenPages.currentPageID = pageID



In my example I pulled the parcel number straight from the table for the current page which I think is what he wanted to use as the image name but I didn't notice that JPEG was a separate folder name so that was a good catch.
0 Kudos
CPoynter
Frequent Contributor
Only noticed PageID as a variable as I quickly scanned over code.

Good tip on correct referencing of it.

Very difficult to troubleshoot other peoples problems with limited information and feedback.

Would be good to know if a solution was found.
0 Kudos