python script: zoom to each county in a state shp and export a pdf map

2995
3
Jump to solution
07-31-2013 07:31 AM
baileyhanson
New Contributor
I would like to create a python script that will zoom into each county in a state shapefile and export a pdf map. I have found two DataFrame methods, panToExtent and zoomToSelectedFeatures. I don't want to use panToExtent because I want it to zoom to the shape of the county. My code uses the zoomToSelectedFeatures but i'm not exactly sure how to select features
. This is what my code looks like so far. Thanks for the help!

import arcpy  mxd = "C:/ArcPyTests/Untitled.mxd" mapdoc = arcpy.mapping.MapDocument(mxd) counties = "C:/ArcPyTests/countyPopulation.shp" rows = arcpy.SearchCursor(counties) output = "C:/ArcPyTests/" df = arcpy.mapping.ListDataFrames(mapdoc)   for row in rows:     mapdoc.extent = row.Shape.extent     df.zoomToSelectedFeatures(mapdoc.extent)     outputname = row.County     arcpy.mapping.ExportToPDF (mapdoc, outputname + ".PDF")     print "Output " + outputname + " complete"      del mapdoc
0 Kudos
1 Solution

Accepted Solutions
DanielHall_Ballester
New Contributor III
Hi Bailey,
Have you tried using Data Driven pages to do this?
You need to set up an mxd with the data driven pages settings defined, but you can then use python to run the process.
I've attached some code. This does most of what you need.  You just need to change the output filetype to pdf (I'm using tiff here) and delete any of the user-defined parameters I have added.
I hope this helps.
Thanks
Dan

import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):   mxd.dataDrivenPages.currentPageID = pageNum     # Get the row from the attribute table of the index layer    row = mxd.dataDrivenPages.pageRow    # Get the value from the field called tilename, e.g. ab1234. (This should be a string field)   name = row.getValue("TILE_NAME")    # Set Output location   outputlocation = arcpy.GetParameterAsText(0)      # Concatenate into whole path name   outputName = outputlocation + "\\" + name + "_ArcMap" + ".tif"    #Set the data frame so the output can be cropped to the data frame extent   df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]    #Set the resolution   dpi= arcpy.GetParameterAsText(1)    #Specify the compression type   Compression = arcpy.GetParameterAsText(2)       # Export to tiff   arcpy.mapping.ExportToTIFF(mxd, outputName, df, df_export_width=5153, df_export_height=5107, tiff_compression=Compression, color_mode="24-BIT_TRUE_COLOR", resolution=dpi)  # Clean up del mxd

View solution in original post

0 Kudos
3 Replies
DanielHall_Ballester
New Contributor III
Hi Bailey,
Have you tried using Data Driven pages to do this?
You need to set up an mxd with the data driven pages settings defined, but you can then use python to run the process.
I've attached some code. This does most of what you need.  You just need to change the output filetype to pdf (I'm using tiff here) and delete any of the user-defined parameters I have added.
I hope this helps.
Thanks
Dan

import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):   mxd.dataDrivenPages.currentPageID = pageNum     # Get the row from the attribute table of the index layer    row = mxd.dataDrivenPages.pageRow    # Get the value from the field called tilename, e.g. ab1234. (This should be a string field)   name = row.getValue("TILE_NAME")    # Set Output location   outputlocation = arcpy.GetParameterAsText(0)      # Concatenate into whole path name   outputName = outputlocation + "\\" + name + "_ArcMap" + ".tif"    #Set the data frame so the output can be cropped to the data frame extent   df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]    #Set the resolution   dpi= arcpy.GetParameterAsText(1)    #Specify the compression type   Compression = arcpy.GetParameterAsText(2)       # Export to tiff   arcpy.mapping.ExportToTIFF(mxd, outputName, df, df_export_width=5153, df_export_height=5107, tiff_compression=Compression, color_mode="24-BIT_TRUE_COLOR", resolution=dpi)  # Clean up del mxd
0 Kudos
baileyhanson
New Contributor
Dan,

This is very helpful. I took your advice and modified the code you provided and I am getting this error message.

Traceback (most recent call last):
  File "C:/ArcPyTests/createPDF_datadrivenpages.py", line 5, in <module>
    for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
AttributeError: 'str' object has no attribute 'dataDrivenPages'


Update: I resolved the problem by switching the code from:
import arcpy
mxd = "C:/ArcPyTests/Untitled.mxd"
mapdoc = arcpy.mapping.MapDocument(mxd)
print "import complete"

for pageNum in range(1, mapdoc.dataDrivenPages.pageCount + 1):
  mapdoc.dataDrivenPages.currentPageID = pageNum 


to:
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:/ArcPyTests/Untitled.mxd")

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum 
0 Kudos
DanielHall_Ballester
New Contributor III
Hi Bailey,
I'm glad this helped, and thanks for sharing the code fix, hopefully it will also help others.
Thanks
Dan
0 Kudos