|
POST
|
I don't mind posting the whole thing actually. Not quite as many comments as I would like in it, but if you have questions about how any of it works let me know. The following script loops through features in a single shapefile or gdb with the particular fields I need, and uses those field values with several premade map templates to create a series of 3 maps for each feature in the shapefile or FC. Each template already has all layers needed saved to it, as well as text elements created and named, but the values of these elements will change according to the field values. All filepaths have been replaced with a generic filepath/ for my sake. Important to note, I had to make a specific folder structure for how I wanted all the maps and pdfs saved prior to running this script, there are other ways to make sure they are created as a script such as this runs, but making it prior was easier. #Importing Modules
import arcpy, time
#Checking start time
print time.time()
#Permanent Variables
shp = r"filepath\Export_Output.shp"
symbologyLayer = r"filepath\Test.lyr"
symbologyLayer2 = r"filepath\Test2.lyr"
outlayer = shp + "_lyr"
Month = "February"
Year = "2015"
OutputLoc = r"filepath" + "\\"
DPI = 200
#fields from feature class that will be used for text elements/file naming
fields = [ "Acreage", "City", "County" , "Name" , "State", "ED_Region", "ID"]
#Looping through each record in shapefile or feature class
with arcpy.da.SearchCursor(shp, fields) as cursor:
for row in cursor:
if row[4] == "MS":
pass
else:
SiteName = row[3]
State = row[4]
City = row[1]
County = row[2]
Acres = row[0]
Region = row[5]
Hectares = str(int(int(Acres) * .4047))
Acres = str(int(Acres))
ID = row[6]
#Optional output additions for additional subfolders.
Output = OutputLoc + Region + "\\" + County + "\\"
FileName = County + "_" + SiteName + "_" + str(ID)
Title = "Aerial Imagery & FEMA 100 Year Floodplain"
Fullpath = Output + FileName
#Assembling Map - Selecting MXD, Dataframe, and New Layer for Aerial Map w/floodplain
mxd = arcpy.mapping.MapDocument(r"filepath\AerialLetterTemplate.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
newlayer = arcpy.mapping.Layer(shp)
print "adding layer"
arcpy.mapping.AddLayer(df, newlayer, "TOP")
Layer = arcpy.mapping.ListLayers(mxd, newlayer.name)[0]
#Selecting by Attribute, Zooming to Feature, and Setting Scale
arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION", ' "Name" = ' + "'" + SiteName + "'" )
#Symbology from default lyr file
arcpy.ApplySymbologyFromLayer_management (Layer, symbologyLayer)
df.zoomToSelectedFeatures()
print "Zooming to scale"
#I set all my scales by multiples of 1200, engineers love this, I take the initial zoom scale, round it to nearest integer, then zoom it out an additional 1200
df.scale = ((round(df.scale / 1200.0) * 1200) + 1200)
if df.scale == 8400:
df.scale = 9600
arcpy.SelectLayerByAttribute_management(Layer, "CLEAR_SELECTION")
#Editting Text Elements, names for text elements are hardcoded into the map
print "Setting Text Elements"
Layer2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT" , "Site Name and Size") [0]
Layer2.text = SiteName + "\n" + " \n+/-" + Acres + " Acres \n +/-" + Hectares + " Hectares"
Layer3 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT" , "Title") [0]
Layer3.text = Title + "\n" + SiteName + "\n" + City + ", " + State + " (" + County + " Co.)"
Layer4 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT" , "Prepared by Date") [0]
Layer4.text = "Prepared " + Month + " " + Year + " by:"
arcpy.RefreshActiveView()
#Saving and Exporting Map
print "Saving Map"
print Fullpath + "_Aerial.mxd"
#mxd.saveACopy(OutputLoc + SiteName +" - Aerial.mxd")
mxd.saveACopy(Fullpath + "_Aerial.mxd")
print "Saving PDF"
#arcpy.mapping.ExportToPDF(mxd, OutputLoc + SiteName + " _ Aerial.pdf", "Page_Layout", 640, 480, DPI)
arcpy.mapping.ExportToPDF(mxd, Fullpath + "_Aerial.pdf", "Page_Layout", 640, 480, DPI)
print "Save Complete!"
del Layer; del Layer2; del Layer3; del Layer4; del mxd
print time.time()
print "Next Map"
#Assembling Map - Selecting MXD, Dataframe, and New Layer
mxd = arcpy.mapping.MapDocument(r"filepath\TopoLetterTemplate.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
arcpy.mapping.AddLayer(df, newlayer, "TOP")
Layer = arcpy.mapping.ListLayers(mxd, newlayer.name)[0]
arcpy.ApplySymbologyFromLayer_management (Layer, symbologyLayer)
print Layer.name
Title = "USGS Topo Map & NWI Wetlands"
arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION", ' "Name" = ' + "'" + SiteName + "'" )
#Applying Symbology from default lyr file
arcpy.ApplySymbologyFromLayer_management (Layer, symbologyLayer)
df.zoomToSelectedFeatures()
df.scale = ((round(df.scale / 1200.0) * 1200) + 1200)
if df.scale == 8400:
df.scale = 9600
arcpy.SelectLayerByAttribute_management(Layer, "CLEAR_SELECTION")
#Setting Layer and Text Elements
WetlandLayer = arcpy.mapping.ListLayers(mxd, "*" + State, df)[0]
WetlandLayer.visible = True
Layer2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT" , "Site Name and Size") [0]
Layer2.text = SiteName + "\n" + " \n+/-" + Acres + " Acres \n+/-" + Hectares + " Hectares"
Layer3 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT" , "Title") [0]
Layer3.text = Title + "\n" + SiteName + "\n" + City + ", " + State + " (" + County + " Co.)"
Layer4 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT" , "Prepared by Date") [0]
Layer4.text = "Prepared " + Month + " " + Year + " by:"
arcpy.RefreshActiveView()
#mxd.saveACopy(OutputLoc + SiteName +" - Aerial.mxd")
mxd.saveACopy(Fullpath + "_Topo.mxd")
print "Saving PDF"
#arcpy.mapping.ExportToPDF(mxd, OutputLoc + SiteName + " - Aerial.pdf", "Page_Layout", 640, 480, DPI)
arcpy.mapping.ExportToPDF(mxd, Fullpath + "_Topo.pdf", "Page_Layout", 640, 480, DPI)
del Layer; del Layer2; del Layer3; del Layer4; del WetlandLayer; del mxd
mxd = arcpy.mapping.MapDocument(r"filepath\LocationLetterTemplate.mxd")
Title = "Site Location Map"
#This map has multiple dataframes, which I need to loop through
for df in arcpy.mapping.ListDataFrames(mxd):
if df.name == "Area Map":
arcpy.mapping.AddLayer(df, newlayer, "TOP")
Layer = arcpy.mapping.ListLayers(mxd, newlayer.name)[0]
OutLayer = "OutLayer_lyr1"
print Layer.name
arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION", ' "Name" = ' + "'" + SiteName + "'" )
print "Making Feature Layer"
arcpy.MakeFeatureLayer_management(Layer, OutLayer)
OutLayer2 = arcpy.mapping.Layer(OutLayer)
arcpy.mapping.AddLayer(df, OutLayer2, "TOP")
print "Removing Old Layer"
Layer3 = arcpy.mapping.ListLayers(mxd, OutLayer2)[0]
#Applying Symbology from default lyr file
arcpy.ApplySymbologyFromLayer_management (Layer3, symbologyLayer)
df.zoomToSelectedFeatures()
df.scale = 126720
arcpy.SelectLayerByAttribute_management(Layer, "CLEAR_SELECTION")
arcpy.mapping.RemoveLayer(df, Layer)
del Layer;
elif df.name == "Inset":
arcpy.mapping.AddLayer(df, newlayer, "TOP")
Layer = arcpy.mapping.ListLayers(mxd, newlayer.name, df)[0]
OutLayerA = "OutLayer_lyr2"
print Layer.name
arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION", ' "Name" = ' + "'" + SiteName + "'" )
#Applying Symbology from default lyr file
arcpy.MakeFeatureLayer_management(Layer, OutLayerA)
OutLayerC = arcpy.mapping.Layer(OutLayerA)
arcpy.mapping.AddLayer(df, OutLayerC, "TOP")
OutLayerB = arcpy.mapping.ListLayers(mxd, OutLayerC.name)[0]
arcpy.ApplySymbologyFromLayer_management (OutLayerB, symbologyLayer2)
df.zoomToSelectedFeatures()
df.scale = 2300000
arcpy.SelectLayerByAttribute_management(Layer, "CLEAR_SELECTION")
arcpy.mapping.RemoveLayer(df, Layer)
del OutLayer2; del OutLayerC; del OutLayerB; del Layer3
else:
continue
Layer3 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT" , "Title") [0]
Layer3.text = Title + "\n" + SiteName + "\n" + City + ", " + State + " (" + County + " Co.)"
Layer4 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT" , "Prepared by Date") [0]
Layer4.text = "Prepared " + Month + " " + Year + " by:"
arcpy.RefreshActiveView()
#mxd.saveACopy(OutputLoc + SiteName +" - Aerial.mxd")
mxd.saveACopy(Fullpath + "_Location.mxd")
print "Saving PDF"
#arcpy.mapping.ExportToPDF(mxd, OutputLoc + SiteName + " - Aerial.pdf", "Page_Layout", 640, 480, DPI)
arcpy.mapping.ExportToPDF(mxd, Fullpath + "_Location.pdf", "Page_Layout", 640, 480, DPI)
del Layer; del Layer3; del Layer4; del mxd;
arcpy.Delete_management(OutLayer)
arcpy.Delete_management(OutLayerA)
print time.time()
del shp
... View more
08-24-2015
06:36 AM
|
0
|
0
|
2083
|
|
POST
|
Hi Gary, Could you be a little more clear with what you were wanting to do? Are you wanting to iterate through a list of feature and export out for each one of them at different scales? I could gladly help out with something like that, in fact looking over the original script now I'm finding a few ways to make it work easier for people and more efficient. The more information you can give the better.
... View more
08-24-2015
05:48 AM
|
0
|
3
|
3519
|
|
POST
|
Continuing to update here. Found a good resource for direct LiDAR laz file downloads, based on the data shown on the USIEI. NOAA has a data viewer for project on which they host the LiDAR data and it can be downloaded by a custom bounding box. Digital Coast Data Access Viewer Additionally I think its worth mentioning that processed DEM's from NOAA and other federal agency products are available from the USDA NRCS Geospatial Portal. https://gdg.sc.egov.usda.gov/ I believe its worth mentioning that you have the option on the sidebar to select a custom AOI for data, that way you aren't downloading an entire counties worth of data if you don't have to. Saves alot of time on the download, and of course the data is free if you download through the FTP. Personally I find these two sources much better for high resolution terrain data than EarthExplorer or the NationalMap, both hosted in conjunction with the USGS.
... View more
08-19-2015
03:17 PM
|
1
|
0
|
1572
|
|
POST
|
you are still using the new cursor, you will need to use the old cursor with the syntax you used before to work with the while loop I wrote. cursor1 = arcpy.UpdateCursor(env+hospitals+'.shp') #I swill set up the update cursor row = cursor1.next() #I will use a while loop in order to copy all the information from the #city,state,address,and zipcode fields into the single FullAddR field. while row: #I am setting the variables for the fields I want to take information from #in order to merge it into the new field I just created. part1 = row.getValue(address) part2 = row.getValue(city) part3 = row.getValue(state) part4 = row.getValue(zipcode) # Concatenating field values together ( str(part4) is in case zipcode is a numeric field instead of string) row.setValue(field1, part1 + " " + part2 + " " + part3 + " " + str(part4)) cursor.updateRow(row)
... View more
08-19-2015
09:00 AM
|
1
|
0
|
3527
|
|
POST
|
ah you are now mixing and matching with the old and new cursors, I wrote that last bit for using the old style cursor(arcpy.UpdateCursor). I'm not sure how to set it up with a while loop with the new cursors, the old style cursors had a good example with the while that I could use, the new style one does not.
... View more
08-19-2015
08:14 AM
|
0
|
2
|
3527
|
|
POST
|
Personally, I try to never use while loops unless I absolutely have to. While its good to know how they function(since it looks like you are learning right now), for loops are much safer and can automatically loop through all my features, without telling it to go to the next row manually. Its good you are expanding your horizons for different ways of doing things(there are about 10 ways to do anything in ArcGIS I was always told), an update cursor is definitely faster than using a geoprocessing tool, if you are doing a large amount of data processing. For the old style cursor, you set it up correctly, I believe the issue is you need to get each field value individually and then concatentate them into one string before setting it for the new value. while row:
part1 = row.getValue(address)
part2 = row.getValue(city)
part3 = row.getValue(state)
part4 = row.getValue(zipcode)
# Concatenating field values together ( str(part4) is in case zipcode is a numeric field instead of string)
row.setValue(field1, part1 + " " + part2 + " " + part3 + " " + str(part4))
cursor.updateRow(row)
row = cursor1.next() May not be the most pythonic way to do it, but it should function. What workbook are you working out of?
... View more
08-19-2015
07:32 AM
|
0
|
5
|
3527
|
|
POST
|
I'm curious why are wanting to do this with a cursor and not with a geoprocessing tool such as Calculate Field. You could concatenate together multiple string fields fairly easily with it. Calculate Field—Help | ArcGIS for Desktop While a cursor is a good way to do this as well, I would suggest you use the far easier to use arcpy.da.UpdateCursor. The parameters are slightly different, but they are significantly faster and would be easier to use. UpdateCursor—Help | ArcGIS for Desktop Example script for using the arcpy.da.SearchCursor: #import arcpy module
import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True
# I am setting the work path
env = r'S:\\376\\Summer15-2\\ahutche1\\lab07_data\\SectionB\\'
#I am setting the variable for the hospitals shapefile
hospitals = 'Hospitals'
#I will add a new "FULLAddR" field to the table in the hospitals shapefile
arcpy.AddField_management(env+hospitals+'.shp','FullAddR', "FLOAT",20)
#I am setting the variable for the new field.
field1 = "FullAddR"
#I am setting the variables for the fields I want to take information from
#in order to merge it into the new field I just created.
address = "ADDRESS"
city = "CITY"
state = "STATE"
zipcode = "ZIPCODE"
# Starting cursor using with statement to automatically close after completing
with arcpy.da.UpdateCursor(env+hospitals+'.shp' , [field1 , address, city, state , zipcode]) as cursor:
for row in cursor:
#Making row[0] aka Full address field equal to address(row[1]) plus city(row[2]) plus state(row[3]) plus zipcode(row[4])
row[0] = row[1] + " " + row[2] + " " + row[3] + " " + row[4]
#updating current row values(only row[0] has changed and will be updated
cursor.updateRow(row) Script is untested, but between it and the help, you should get it.
... View more
08-19-2015
07:06 AM
|
1
|
7
|
3527
|
|
POST
|
Sometimes you get an answer, but its not the answer you want. My thought would be if you could make a new field(s) and populated it with the the values that would be returned for each of your original fields from the abbreviation dictionary using python. Then you could use those fields without the abbreviation dictionary and apply the formatting tags. What things are you having abbreviated(I'm guessing address related things)?
... View more
08-17-2015
01:31 PM
|
0
|
1
|
3388
|
|
POST
|
This question has been answered here for future reference of anyone finding this topic. Label Expression, Abbreviation Dictionary Error
... View more
08-17-2015
01:25 PM
|
0
|
0
|
771
|
|
POST
|
It says pretty clearly in the 10.3 help(which I will assume is valid for 10.2.2) that: "Labels containing text formatting tags will not be abbreviated by the Maplex Label Engine when using an abbreviation dictionary." Text formatting tags—ArcGIS Pro | ArcGIS for Desktop http://pro.arcgis.com/en/pro-app/help/mapping/text/text-formatting-tags.htm
... View more
08-17-2015
12:54 PM
|
2
|
3
|
3388
|
|
POST
|
Dan I know, its just a pain to go find 10.3 links sometimes. Hence why I have it posted with the bare url showing, that way a user can just copy and paste the text link. In other news, we all need to stop hijacking discussions with somewhat off-topic conversations, I know we aren't quite as strict as stackexchange, but I feel posts in these threads should be relevant to the question, so either we should take the conversation to a relevant thread(I know there have been a few relating to hyperlinks), create a new thread, or take care of it in a PM(if people are following each other).
... View more
08-17-2015
09:13 AM
|
0
|
0
|
3280
|
|
POST
|
That is my problem as well, I can easily google search the 10.1/10.2 links, but 10.3 is more trouble. I probably should just bookmark the 10.3 help home, that way I can use the internal search.
... View more
08-17-2015
09:06 AM
|
0
|
0
|
3280
|
|
POST
|
You would probably need to use the Delete Tool. http://resources.arcgis.com/EN/HELP/MAIN/10.2/index.html#//001700000052000000
... View more
08-14-2015
03:21 PM
|
1
|
7
|
3280
|
|
POST
|
By template I mean I have a map document or set of map documents that have all the layers, text elements, layout elements etc already loaded into them that will be used for each type of map I need. Thus I can use the same map templates over and over again and all will be assured to have the same features, same text elements, same layouts. I have a python script that iterates over features in a feature class, zooms to each features, symbolizes them, turns on and off various layers saved in each map, and controls my text and layout elements based on values in the attribute table. I then save a copy of the created map in folders and export them to pdf. Actually I'm currently working on a script to move my existing maps to a new template, some of the layout elements have changed and I need to adjust my existing maps. Its been fun so far and almost done that one. I will say that have a deliberate naming scheme for layers, layout elements, and map names have made the work substantially easier.
... View more
08-14-2015
11:37 AM
|
2
|
0
|
2083
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 02-22-2017 08:58 AM | |
| 1 | 10-05-2015 05:43 AM | |
| 1 | 05-08-2015 07:03 AM | |
| 1 | 10-20-2015 02:20 PM | |
| 1 | 10-05-2015 05:46 AM |
| Online Status |
Offline
|
| Date Last Visited |
11-11-2020
02:23 AM
|