Skip to main content
edited tags
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Can you add Adding dynamic text from a count of features in a layer with ArcGIS 10 ArcPyusing arcpy.mapping?

I am new to writing scripts, and I am trying to make a map series using ArcPyarcpy.mapping and data driven map pagesData 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.

Here is my code - can anyone tell me where I'm going wrong with this? I would be so grateful!

 

EDIT II was able to get my code to work, so I am replacing the erroneous code with the functioning one, in case it is useful for someone else... or if someone can show me how to make it more elegant and efficient;-)

Can you add dynamic text from a count of features in a layer with ArcGIS 10 ArcPy.mapping?

I am new to writing scripts, and I am trying to make a map series using ArcPy.mapping and data driven map 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.

Here is my code - can anyone tell me where I'm going wrong with this? I would be so grateful!

EDIT I was able to get my code to work, so I am replacing the erroneous code with the functioning one, in case it is useful for someone else... or if someone can show me how to make it more elegant and efficient;-)

Adding dynamic text from count of features in layer using arcpy.mapping

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.

edited tags
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
changed out the erroneous code for the functioning one
Source Link

EDIT I was able to get my code to work, so I am replacing the erroneous code with the functioning one, in case it is useful for someone else... or if someone can show me how to make it more elegant and efficient;-)

import arcpy, string, os

mxdPath = "CURRENT"
outPath = r"D:\Working\MapBook.mxd"\Geoprocessing\CDMAPS\PDFs\MapPage\\"
outPathSitesConDist = r"D:\temp\\"\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):
        pagestr
        
        mxd.dataDrivenPages.currentPageID = pageNum
        distNo = str(pageNummxd.dataDrivenPages.currentPageID)
 
    for lyr in  arcpy.mapping.ListLayersMakeFeatureLayer_management(mxd"SitesConDist", """siteLyr","[congress_dist_no] df= "+ distNo,"#","")[0]:
        if 
 lyr.name == "Group1\Corps":     defCorp = "[COnly] = 'X'"
        defCoop = "[COOP] = arcpy.MakeFeatureLayer_management(corps,'X' "corpsLYR",AND "[dist_num][COnly] IS NULL"
        defNSIP = ""[NSIP_Full_or_Partial] += pagestr'X'"
        defDisc = "[agency_use_cd] NOT IN ( 'A' , ""'L' , ""'M' )"

        arcpy.SelectLayerByAttribute_management("siteLyr","NEW_SELECTION",defCorp )
   corpcnt     corpscount = str(arcpy.GetCount_management("corpsLYR""siteLyr"))
        arcpy.AddMessage(corpscount + " corps 
 gages in district " + str(pageNum))
   if lyr.name == "Group1\Coop":
  print corpscount + " corps gages in district " + arcpy.MakeFeatureLayer_managementstr(coop,pageNum)
 "coopLYR", "[dist_num] = " + pagestr, "", "")
        arcpy.SelectLayerByAttribute_management("siteLyr", "NEW_SELECTION",defCoop)
   coopcnt     coopcount = str(arcpy.GetCount_management("coopLYR""siteLyr"))
 
        if lyrarcpy.nameAddMessage(coopcount ==+ "Group1\Program":" coop gages in district " + str(pageNum))
        print coopcount + " arcpy.MakeFeatureLayer_management(nsip,coop "nsipLYR",gages "[dist_num]in =district " + pagestr,str(pageNum)
 "", "")      
        arcpy.SelectLayerByAttribute_management("siteLyr", "NEW_SELECTION",defNSIP)
    nsipcnt    nsipcount = str(arcpy.GetCount_management("nsipLYR""siteLyr"))
 
        if lyrarcpy.nameAddMessage(nsipcount ==+ "Group1\Discontinued":" nsip gages in district " + str(pageNum))
        print nsipcount + " arcpy.MakeFeatureLayer_management(disc,nsip "discLYR",gages "[dist_num]in =district " + pagestr,str(pageNum)
 "", "")      
        arcpy.SelectLayerByAttribute_management("siteLyr", "NEW_SELECTION",defDisc)
   disccnt     disccount = str(arcpy.GetCount_management("discLYR""siteLyr"))
 
        printarcpy.AddMessage(disccount "Creating+ map" {0}discontinued ofgages {1}in district ".format( + str(mxd.dataDrivenPages.currentPageIDpageNum),)
        print disccount + " discontinued gages in district " + str(mxd.dataDrivenPages.pageCount)pageNum)
        
        for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
                if elm.textname == "CorpsCount""CorpCount":
                    elm.text = elm.text.replace"("CorpsCount"," corpcnt+ corpscount + ")"
                    print "Corp line in legend has been replaced"
                if elm.textname == "CoopCount":
                    elm.text = elm.text.replace"("CoopCount"," coopcnt+ coopcount + ")"
                    print "Coop line in legend has been replaced"
                if elm.textname == "NSIPCount":
                    elm.text = elm.text.replace"("NSIPCount"," nsipcnt+ nsipcount + ")"
                    print "NSIP line in legend has been replaced"
                if elm.textname == "DiscCount":
                    elm.text = elm.text.replace"("DiscCount"," disccnt+ disccount + ")"
                    print "Discontinued line in legend has been replaced"
        arcpy.RefreshActiveView()

        Pdf = outPath + lyrName"distNo" + str(pageNum) + ".pdf"
        arcpy.mapping.ExportToPDF(mxd, Pdf)
    os    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))
import arcpy, string, os

mxdPath = r"D:\Working\MapBook.mxd"
outPath = r"D:\temp\\"

try:
    mxd = arcpy.mapping.MapDocument(mxdPath)
    df = arcpy.mapping.ListDataFrames(mxd, "Cong*")[0]
    for pageNum in range(1, mxd.dataDrivenPages.pageCount +1):
        pagestr = str(pageNum)
 
    for lyr in arcpy.mapping.ListLayers(mxd, "", df)[0]:
        if lyr.name == "Group1\Corps":
            arcpy.MakeFeatureLayer_management(corps, "corpsLYR", "[dist_num] = " + pagestr, "", "")
            corpcnt = str(arcpy.GetCount_management("corpsLYR"))
            
         if lyr.name == "Group1\Coop":
            arcpy.MakeFeatureLayer_management(coop, "coopLYR", "[dist_num] = " + pagestr, "", "")
            coopcnt = str(arcpy.GetCount_management("coopLYR"))
 
        if lyr.name == "Group1\Program":
            arcpy.MakeFeatureLayer_management(nsip, "nsipLYR", "[dist_num] = " + pagestr, "", "")
            nsipcnt = str(arcpy.GetCount_management("nsipLYR"))
 
        if lyr.name == "Group1\Discontinued":
            arcpy.MakeFeatureLayer_management(disc, "discLYR", "[dist_num] = " + pagestr, "", "")
            disccnt = str(arcpy.GetCount_management("discLYR"))
 
        print "Creating map {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
        
        for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
            if elm.text == "CorpsCount":
                elm.text = elm.text.replace("CorpsCount", corpcnt)
            if elm.text == "CoopCount":
                elm.text = elm.text.replace("CoopCount", coopcnt)
            if elm.text == "NSIPCount":
                elm.text = elm.text.replace("NSIPCount", nsipcnt)
            if elm.text == "DiscCount":
                elm.text = elm.text.replace("DiscCount", disccnt)

    Pdf = outPath + lyrName + ".pdf"
    arcpy.mapping.ExportToPDF(mxd, Pdf)
    os.startfile(Pdf)

    del mxd

except Exception, e:
    import traceback
    map(arcpy.AddError, traceback.format_exc().split("\n"))
    arcpy.AddError(str(e))

EDIT I was able to get my code to work, so I am replacing the erroneous code with the functioning one, in case it is useful for someone else... or if someone can show me how to make it more elegant and efficient;-)

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))
Source Link
Loading