clm42

Data Driven Pages Index Code

Discussion created by clm42 on Jan 20, 2011
Latest reply on Dec 20, 2011 by mjosefso
I dont like the code for the index that is up on the resource center. So I wrote my own. This uses a page layout in arcmap with 4 columns that are 1.5 inchs wide. The font is Courier New at 9 pt so that allows for 21 characters per line and 69 lines per 10 inch column on an 8.5x11 page. I merged my roads with Unsplit Line before running this. I am thinking about generalizing this so that it can go on the resource center with little changing needed to adapt it to someone elses project. Unlike the other one out there, this one uses only standard library and arcpy functions and methods. This exports a slew of PDF documents which I dont use arcpy to merge. I tried it a few times and was unhappy with the result. It kept putting pages out of order.

import arcpy, textwrap, os, re
from operator import itemgetter
mxd=arcpy.mapping.MapDocument('CURRENT')
path = os.path.dirname(mxd.filePath)
arcpy.AddMessage('Creating Roads Feature Layer...')
arcpy.MakeFeatureLayer_management(path+ r"/Roads_UnsplitLine.shp",'Roads_UnsplitLine', '"ST_NAME"<>\'\'')
arcpy.AddMessage('Creating Grid FeatureLayer...')
arcpy.MakeFeatureLayer_management(path+ r'/Grid.shp','Grid')
 
arcpy.AddMessage('Making Road List...')
alphaList,numList=[],[]
for feature in arcpy.UpdateCursor('Roads_UnsplitLine'):
    arcpy.SelectLayerByLocation_management('Grid','INTERSECT',feature.SHAPE)
    tileList,roadName=[],[]
    for tile in arcpy.SearchCursor('Grid'):
        tileList.append('%s %s-%s'%(tile.Page,tile.COL,tile.ROW))
    if feature.DIR in ['E','W','S','N','NW']: DIR=feature.DIR
    else: DIR=' '
    if feature.ST_NAME[0].isdigit():
        match=re.search(r'(\d*)(\D*)', feature.ST_NAME)
        numList.append([DIR,int(match.group(1)),match.group(2),feature.ST_TYPE,', '.join(tileList)])
    else:
        alphaList.append([DIR,feature.ST_NAME,feature.ST_TYPE,', '.join(tileList)])
 
arcpy.AddMessage('Merging Strings...')
numList_merged=['%s %s%s %s: %s'%(road[0],road[1],road[2],road[3],road[4]) for road in sorted(numList,key=itemgetter(1,3,0))]
alphaList_merged=['%s %s %s: %s'%(road[0],road[1],road[2],road[3]) for road in sorted(alphaList,key=itemgetter(1,2,0))]
roadList=[road for road in alphaList_merged]
for road in numList_merged:
    roadList.append(road)
 
arcpy.AddMessage('Setting Up Word Wrapping...')
roadList_wrapped=[]
wrapper=textwrap.TextWrapper(width=21, subsequent_indent='  ', expand_tabs=False)
for line in roadList:
    line=str(line)
    for part in wrapper.wrap(line.lstrip()):
        roadList_wrapped.append(part)
 
roadList_split=['\n'.join(roadList_wrapped[pos:pos+69]) for pos in xrange(0, len(roadList_wrapped), 69)]
 
arcpy.AddMessage('Exporting PDF...')
One=arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT', 'One')[0]
Two=arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT', 'Two')[0]
Three=arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT', 'Three')[0]
Four=arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT', 'Four')[0]
 
count=1
for textbox in [roadList_split[pos:pos+4] for pos in xrange(0, len(roadList_split), 4)]:
    if len(textbox)==4:
        oneText,twoText,threeText,fourText=textbox[0],textbox[1],textbox[2],textbox[3]
    elif len(textbox)==3:
        oneText,twoText,threeText,fourText=textbox[0],textbox[1],textbox[2],' '
    elif len(textbox)==2:
        oneText,twoText,threeText,fourText=textbox[0],textbox[1],' ',' '
    elif len(textbox)==1:
        oneText,twoText,threeText,fourText=textbox[0],' ',' ',' '
    One.text,One.elementPositionX,One.elementPositionY=oneText,.6875,10.5
    Two.text,Two.elementPositionX,Two.elementPositionY=twoText,2.5625,10.5
    Three.text,Three.elementPositionX,Three.elementPositionY=threeText,4.4375,10.5
    Four.text,Four.elementPositionX,Four.elementPositionY=fourText,6.3125,10.5
    arcpy.mapping.ExportToPDF(mxd, path+ r'/IndexPage%s.pdf'%count)
    count+=1
 
One.text,One.elementPositionX,One.elementPositionY='BOX ONE',.6875,10.5
Two.text,Two.elementPositionX,Two.elementPositionY='BOX TWO',2.5625,10.5
Three.text,Three.elementPositionX,Three.elementPositionY='BOX THREE',4.4375,10.5
Four.text,Four.elementPositionX,Four.elementPositionY='BOX FOUR',6.3125,10.5
 

I tried to use list comprehensions as much as possible to cut down on the number of lines. If anyone has any questions about this script I would be happy to answer them.

Outcomes