AnsweredAssumed Answered

Data Driven Pages - Page Name as output in Python

Question asked by caroytct on Jan 7, 2011
Latest reply on Dec 5, 2014 by caroytct
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

Outcomes