Data Driven Pages - Page Name as output in Python

10927
7
Jump to solution
01-07-2011 06:09 AM
Charles-AndreRoy
New Contributor III
Hi, I'm trying to write a code to export maps with Data Driven Page in Python (arcpy module). I'm at my beginning...

My objective here is to use "Select Attributes by Location" tool to make a subset of my index layer (I don't want to export all index polygon). So I select some index polygons that intersects some polylines. Then I'd like to export 1 PDF map + 1 PNG map by index polygon.

By default Data Driven Pages export the map by giving the page numbers to the output name. I seen a part of the code here elsewhere but it doesn't work. An error message appear: "<type 'exceptions.AttributeError'>: GetValue". Is it because through the loop, the cursor don't know which cell to get value from?

# Import arcpy module
import arcpy

# Local variables:
TCTquadrants_shp = "C:\\TCTData\\Layers\\PDF Maps\\TCTquadrants.shp"
CDA_trail_PDF_Dissolve_shp = "C:\\TCTData\\Layers\\PDF Maps\\CDA_trail_PDF_Dissolve.shp"
NL_quadrants_Layer = "NL_quadrants_Layer"
NL_trail = "NL_trail"
NL_quadrants_shp = "C:\\TCTData\\Test\\NL_quadrants.shp"
TCT_quadrants_Layer = "TCT_quadrants_Layer"
TCT_pageName = "C:\\TCTData\\Test\\TCT_pageName.shp"

# Process: Make Feature Layer (Quadrant)
arcpy.MakeFeatureLayer_management(TCTquadrants_shp, NL_quadrants_Layer, "\"quadrant\" = 'NL'", "", "FID FID VISIBLE NONE;Shape Shape VISIBLE NONE;Quadrant Quadrant VISIBLE NONE;Number Number VISIBLE NONE;index index VISIBLE NONE;index_more index_more VISIBLE NONE;X X VISIBLE NONE;Y Y VISIBLE NONE;Trail_1 Trail_1 VISIBLE NONE;Trail_2 Trail_2 VISIBLE NONE;Trail_3 Trail_3 VISIBLE NONE;Trail_4 Trail_4 VISIBLE NONE;Trail_5 Trail_5 VISIBLE NONE;Trail_6 Trail_6 VISIBLE NONE;Trail_7 Trail_7 VISIBLE NONE;TrailNames TrailNames VISIBLE NONE;Comment Comment VISIBLE NONE;CreatedBy CreatedBy VISIBLE NONE;UpdatedBy UpdatedBy VISIBLE NONE;State State VISIBLE NONE")

# Process: Make Feature Layer (Trail)
arcpy.MakeFeatureLayer_management(CDA_trail_PDF_Dissolve_shp, NL_trail, "\"ID_PROVINC\" = '01'", "", "FID FID VISIBLE NONE;Shape Shape VISIBLE NONE;ID_TRAILTY ID_TRAILTY VISIBLE NONE;ID_TRAILST ID_TRAILST VISIBLE NONE;Name_Trail Name_Trail VISIBLE NONE;ID_SEGMENT ID_SEGMENT VISIBLE NONE;ID_PROVINC ID_PROVINC VISIBLE NONE;Name Name VISIBLE NONE")

# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(NL_quadrants_Layer, "INTERSECT", NL_trail, "", "NEW_SELECTION")

# Process: Copy Features
arcpy.CopyFeatures_management(NL_quadrants_Layer, NL_quadrants_shp, "", "0", "0", "0")

# Process: Select and Copy Feature for Page Name
arcpy.CopyFeatures_management(NL_quadrants_shp, TCT_pageName, "", "0", "0", "0")

# Export Maps
mxd = arcpy.mapping.MapDocument(r"C:\\TCTData\\Test\\Trail_PDF_Maps_layout_NL.mxd")

# Use cursor to cycle through TCT_pageName feature class
cur = arcpy.SearchCursor(TCT_pageName)
row = cur.next()

# PDF
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum
  pageName = row.GetValue("index")
  arcpy.mapping.ExportToPDF(mxd, r"C:\TCTData\Test\No_" + pageName + ".pdf")
  row = cur.next()

  # PNG
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum
  pageName = row.getvalue("index")
  row = cur.next()
  arcpy.mapping.ExportToPNG(mxd, r"C:\TCTData\Test\No_" + pageName + ".png")
del mxd, cur, row


Thanks for the help
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Charles-AndreRoy
New Contributor III
I will reply to my own thread. I finally found my way to export page name as output instead of page number. For a reason that I ignore the function row.getvalue() doesn't work. I used the other way instead as pageRow.FIELDNAME. I don't need to use a cursor. Here is the code:

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum
  arcpy.AddMessage("Exporting PDF Map " + str(pageNum) + " of " + str(mxd.dataDrivenPages.pageCount))
  pageName = mxd.dataDrivenPages.pageRow.index
  arcpy.mapping.ExportToPDF(mxd, r"C:\TCTData\MAPS\PDF_MAPS\No_" + pageName + ".pdf", resolution=200)


Cheers,

View solution in original post

7 Replies
Charles-AndreRoy
New Contributor III
I will reply to my own thread. I finally found my way to export page name as output instead of page number. For a reason that I ignore the function row.getvalue() doesn't work. I used the other way instead as pageRow.FIELDNAME. I don't need to use a cursor. Here is the code:

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum
  arcpy.AddMessage("Exporting PDF Map " + str(pageNum) + " of " + str(mxd.dataDrivenPages.pageCount))
  pageName = mxd.dataDrivenPages.pageRow.index
  arcpy.mapping.ExportToPDF(mxd, r"C:\TCTData\MAPS\PDF_MAPS\No_" + pageName + ".pdf", resolution=200)


Cheers,
JimmyMack
New Contributor

Hello Charles-Andre Roy ,

I have a question related to yours (which you successfully solved). Whenever I try to set a string variable to mxd.dataDrivenPages.pageRow.index I get the error: "RuntimeError: Row: Field index does not exist" ... I am trying to export data driven pages to adobe illustrator using the page name. Here's what I have:

import arcpy, os, sys, string
from arcpy import env

mxd = arcpy.mapping.MapDocument("CURRENT")

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
      mxd.dataDrivenPages.currentPageID = pageNum
      arcpy.AddMessage("Exporting PDF Map " + str(pageNum) + " of " + str(mxd.dataDrivenPages.pageCount))
      pageName = mxd.dataDrivenPages.pageRow.index
      arcpy.mapping.ExportToAI(mxd, r"C:\Dropbox (WSA)\Tucson\PROJECTS ACTIVE\GSA\2014-58 Richfield Travel Routes\Maps\Site_Location\Site_Location_Map\_" + pageName + ".ai")
del mxd

I'd appreciate any help! Thanks!

Jimmy

0 Kudos
Charles-AndreRoy
New Contributor III

Hi Jimmy,

I wrote this code a long time ago, but as far as I remember it, the name of the field in the feature class layer is called "index". Remember that python is case sensitive, so "Index" is not the same as "index".

JimmyMack
New Contributor

Thank you Charles-Andre Roy‌. I thought I had tried replacing index with my field name already, but somehow I got my wires crossed. All is working now.

0 Kudos
Charles-AndreRoy
New Contributor III

Happy to help!

0 Kudos
KarlHillstrom
New Contributor III
A followup question to this is...can you get the page name without supplying the name field explicitly?  So far I can use a known page name to get pageID, but not the other way around.
0 Kudos
Charles-AndreRoy
New Contributor III

Hi Karl,

I didn't look back to this since 3 years, so maybe the new arcpy allows to get the pageName automatically from the index field selected from the ArcMap dialog. But 3 years ago, the arcpy module was just getting the pageNum for the filename. The name field could be selected as an user input as well.

0 Kudos