I am new to writing scripts, and I am trying to make a map series using arcpy.mapping and Data Driven Pages. I would like to put some text on the map that updates for each map in the series depending on a new definition query for my dataset of sites. So, I set up my map series based on the district numbers the sites are in (districts 1 - 32). So, the pageNum should also equal the district, and I thought I could pass the pageNum into a SQL query and get a new count of sites for each district and then insert that as dynamic text using text.replace.
I was able to get my code to work, so I am replacing the erroneous code with the functioning one.
import arcpy, string, os
mxdPath = "CURRENT"
outPath = r"D:\Geoprocessing\CDMAPS\PDFs\MapPage\\"
SitesConDist = r"D:\Geoprocessing\CDMAPS\MXDs_and_Geodatabase\CDMaps.mdb\Sites\SitesCD"
outputMXD = "D:\Geoprocessing\CDMAPS\MXDs_and_Geodatabase\SeriesMXDs\\"
try:
mxd = arcpy.mapping.MapDocument(mxdPath)
ddp = mxd.dataDrivenPages
df = arcpy.mapping.ListDataFrames(mxd, "Cong*")[0]
for pageNum in range(1, mxd.dataDrivenPages.pageCount +1):
mxd.dataDrivenPages.currentPageID = pageNum
distNo = str(mxd.dataDrivenPages.currentPageID)
arcpy.MakeFeatureLayer_management("SitesConDist","siteLyr","[congress_dist_no] = "+ distNo,"#","")
defCorp = "[COnly] = 'X'"
defCoop = "[COOP] = 'X' AND [COnly] IS NULL"
defNSIP = "[NSIP_Full_or_Partial] = 'X'"
defDisc = "[agency_use_cd] NOT IN ( 'A' , 'L' , 'M' )"
arcpy.SelectLayerByAttribute_management("siteLyr","NEW_SELECTION",defCorp )
corpscount = str(arcpy.GetCount_management("siteLyr"))
arcpy.AddMessage(corpscount + " corps gages in district " + str(pageNum))
print corpscount + " corps gages in district " + str(pageNum)
arcpy.SelectLayerByAttribute_management("siteLyr", "NEW_SELECTION",defCoop)
coopcount = str(arcpy.GetCount_management("siteLyr"))
arcpy.AddMessage(coopcount + " coop gages in district " + str(pageNum))
print coopcount + " coop gages in district " + str(pageNum)
arcpy.SelectLayerByAttribute_management("siteLyr", "NEW_SELECTION",defNSIP)
nsipcount = str(arcpy.GetCount_management("siteLyr"))
arcpy.AddMessage(nsipcount + " nsip gages in district " + str(pageNum))
print nsipcount + " nsip gages in district " + str(pageNum)
arcpy.SelectLayerByAttribute_management("siteLyr", "NEW_SELECTION",defDisc)
disccount = str(arcpy.GetCount_management("siteLyr"))
arcpy.AddMessage(disccount + " discontinued gages in district " + str(pageNum))
print disccount + " discontinued gages in district " + str(pageNum)
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
if elm.name == "CorpCount":
elm.text = "(" + corpscount + ")"
print "Corp line in legend has been replaced"
if elm.name == "CoopCount":
elm.text = "(" + coopcount + ")"
print "Coop line in legend has been replaced"
if elm.name == "NSIPCount":
elm.text = "(" + nsipcount + ")"
print "NSIP line in legend has been replaced"
if elm.name == "DiscCount":
elm.text = "(" + disccount + ")"
print "Discontinued line in legend has been replaced"
arcpy.RefreshActiveView()
Pdf = outPath + "distNo" + str(pageNum) + ".pdf"
arcpy.mapping.ExportToPDF(mxd, Pdf)
mxd.saveACopy(outputMXD)
#os.startfile(Pdf) -> this line can be used to preview each map as it is created
arcpy.AddMessage("Created PDF {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount)))
print "Created PDF {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
del mxd, df
except Exception, e:
import traceback
map(arcpy.AddError, traceback.format_exc().split("\n"))
arcpy.AddError(str(e))