Select to view content in your preferred language

Is this script possible?

732
6
11-24-2011 01:44 AM
eliaperi
Emerging Contributor
So I'm planning to write a script but doubt on it's feasibility. The script would be applied to one shapefile consisting of city districts with various characteristics, for example crime rates and percentage of people above the age of 60. The goal of this script is to select one of these characteristics and create a thematic map out of it, so it would show the city districts with various colours representing the amount or the percentage of the selected feature. It would also have to add a scalebar, a legend and a north-arrow. Any of you know if this is possible? Any good starting points or tips I could use?
Tags (2)
0 Kudos
6 Replies
markdenil
Frequent Contributor
It is not possible in arcpy.mapping. The Python mapping module is very very simple: you can only move about and make (some) adjustements to elements already in existance, and apply symbolization already stored in a layer file.

To create layout elements, and do the other stuff you want, you will need to use ArcObjects. For that you need the (free) VBA capability for Arc10 (knowing that VBA is slated to be lost forever in 10.1) OR use VB.Net or C#.
0 Kudos
AnthonyTimpson2
Regular Contributor
It is not possible in arcpy.mapping. The Python mapping module is very very simple: you can only move about and make (some) adjustements to elements already in existance, and apply symbolization already stored in a layer file.

To create layout elements, and do the other stuff you want, you will need to use ArcObjects. For that you need the (free) VBA capability for Arc10 (knowing that VBA is slated to be lost forever in 10.1) OR use VB.Net or C#.



Python works with Arcobjects as well.
http://www.pierssen.com/arcgis/upload/misc/python_arcobjects.pdf
0 Kudos
eliaperi
Emerging Contributor
I think the following script should get me well under way. The only exception is that I don't need to zoom in to a selected parcel, right? So the bold part of the script I can leave out of the script I think? Will the rest of the script be the same? I tested it but kept running into the following error:

"Traceback (most recent call last):
File "<interactive input>", line 2, in <module>
IndexError: list index out of range
"

arcpy.mapping opens up many possibilities for the types of map books you can create. For example, you can create a thematic atlas with multiple pages specifying a different theme on each page. The following example zooms to a selected parcel, toggles on different layer visibility and exports the layout for multiple themes in order to create a parcel report with a soil map, a flood map and a zoning map: 



import arcpy, os 

#Specify output path and final output PDF 
outPath = r"C:\MyProject\output\\" 
finalPdf = arcpy.mapping.PDFDocumentCreate(outPath + "ParcelReport.pdf") 

#Specify the map document and the data frame 
mxd = arcpy.mapping.MapDocument(r"C:\MyProject\MyParcelMap.mxd") 
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] 

#Select a parcel using the LocAddress attribute and zoom to selected
parcelLayer = arcpy.mapping.ListLayers(mxd, "Parcels", df)[0]
arcpy.SelectLayerByAttribute_management(parcelLayer, "NEW_SELECTION", "\"LocAddress\" = '519 Main St'")
df.zoomToSelectedFeatures()


#Turn on visibility for each theme and export the page 
lyrList = ["Soils", "Floodplains", "Zones"] 
for lyrName in lyrList: 
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0] 
lyr.visible = True 

#Export each theme to a temporary PDF and append to the final PDF 
tmpPdf = outPath + lyrName + "_temp.pdf" 
if os.path.exists(tmpPdf): 
os.remove(tmpPdf) 
arcpy.mapping.ExportToPDF(mxd, tmpPdf) 
finalPdf.appendPages(tmpPdf) 
  
#Turn off layer visibility and clean up for next pass through the loop 
lyr.visible = False 
del lyr, tmpPdf 
del mxd, df, finalPdf 

from:   http://blogs.esri.com/dev/blogs/arcgisdesktop/archive/2010/12/14/combining-data-driven-pages-with-py...

0 Kudos
eliaperi
Emerging Contributor
I've gotten this far and the result are two .pdf file. One is called perc_old_temp.pdf and is suprisingly showing the map for perc_nwa and the other .pdf file is called ParcelReport.pdf and is showing the map of perc_nwa as well.

So, two problems: one, the script didn't deliver a .pdf file showing both the layers (perc_old and perc_nwa). And two, it seems it is confusing the two layers since the temp.pdf file it created showed the wrong layer.

Any clue on solving this? I don't get what I'm doing wrong, it shouldn't be too hard to create a thematic map booklet from a .mxd file with two layers right???

>>> import arcpy, os
>>> outPath=r"F:\projectx\\"
>>> finalPdf=arcpy.mapping.PDFDocumentCreate(outPath + "ParcelReport.pdf")
>>> mxd=arcpy.mapping.MapDocument(r"F:\projectx\testscript.mxd")
>>> df=arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
>>> lyrList=["perc_nwa", "perc_old"]
>>> for lyrName in lyrList:
...  lyr=arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
...  lyr.visible=True
... 
>>> tmpPdf=outPath + lyrName + "_temp.pdf"
>>> if os.path.exists(tmpPdf):
...  os.remove(tmpPdf)
... 
>>> arcpy.mapping.ExportToPDF(mxd, tmpPdf)
>>> finalPdf.appendPages(tmpPdf)
>>> lyr.visible=False
>>> del lyr, tmpPdf
>>> del mxd, df, finalPdf
0 Kudos
eliaperi
Emerging Contributor
Found another sample script but can't get it to work either. Maybe the problem lies in the way my data is structured? I'm testing it on a .mxd file with two layers. My goal is to create a script that creates a thematic map booklet. So in this case it would create two pages, one showing the perc_old layer and one showing the perc_nwa layer.

I have at least identified one problem. The data driven pages functionality won't let me switch between layers, it will only let me switch between the several citydistricts the shapefile is made of. Anyone know a solution for this or a solution to the overall problem of writing a script that makes a thematic map booklet?



Uploaded with [URL=http://imageshack.us]ImageShack.us[/URL]

#This script will take an mxd and export a pdf mapbook for
#each thematic layer in the map. The subtitle of the map
#will be dynamically changed with each mapbook.




#Import the arcpy package
import arcpy                                                       

#Assign the Western States Mapping Document to variable mxd
mxd = arcpy.mapping.MapDocument(r"C:\ArcPy_Training\Map_Documents\Western_States.mxd")

#Create a data driven pages object using "mxd" and assign it to variable "ddp"
ddp = mxd.dataDrivenPages                                          

#Create a list of all text elements in the map layout
textlist = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT")

#Cycle through the text elements   
for txt in textlist:  

    #When you find one whose text says "MAP SUBTITLE"...                                              
    if txt.text == r"MAP SUBTITLE":    
                 
        #....Assign it to variable "subtitle"         
        subtitle = txt                                             

#Create a list of the layers in the map document
layerlist = arcpy.mapping.ListLayers(mxd)                         

#Cycle through each layer
for lyr in layerlist:                                              

    #If the layer is NOT visible...
    if lyr.visible == False:                                       

        #...Turn the layer on
        lyr.visible = True                                         

        #Change the Subtitle text element to match the layer's name
        subtitle.text = lyr.name                                   

        #Create a string showing the path of the pdf that will be written out
        pdfpath = "C:\\arcpy_training\\pdfs\\" + lyr.name + r" mapbook.pdf"

        #Print that we are exporting a mapbook
        print "Exporting " + pdfpath + "....."                     

        #Export the mapbook to pdf
        mxd.dataDrivenPages.exportToPDF(pdfpath,"ALL")             

        #Turn the layer off again
        lyr.visible = False                                        

#Delete variables to clean up memory usage
del ddp, mxd                                                       

print "Done!"

0 Kudos
RaphaelR
Deactivated User
have you tried this without data driven pages enabled?
i´m using practically the same script (the one from the esri blog) and it works fine here (as long as there are no layers with the same name within different grouplayers...if there are, it gets complicated).
0 Kudos