Created
August 19, 2020 11:54
-
-
Save staffordsmith83/e41a0154b8b39ef059523375db9ba72f to your computer and use it in GitHub Desktop.
Python 2 script using arcpy - Buffer all the layers in the mapdoc!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Title of Script: buffer_all_layers.py | |
# Description: Takes a map document, workspace, and buffer distance. | |
# Creates a buffered layer for each layer in the map document. Names buffered layers as: <original name>_buffer. | |
# If output name already exists, the naming convention | |
# Inserts buffered layer below original layer in TOC. | |
# Map documents with multiple dataframes are allowed, will be processed iteratively. | |
# Author: Stafford Smith | |
# Creation date: 26/10/2019 (SS) | |
# Last update: 30/10/2019 (SS) | |
# === IMPORTS | |
import arcpy | |
import time | |
import sys | |
# === FUNCTIONS | |
def getLayers(mapdoc, df): | |
# lists the layers of dataframe df in mapdoc. Returns lyrlist. | |
lyrList = arcpy.mapping.ListLayers(mapdoc, "", df) | |
# must filter lyrList to remove layers that cannot be buffered, such as WMS | |
for lyr in lyrList: | |
# Find out if the layer represents a feature class | |
try: | |
desc = arcpy.Describe(lyr) | |
print lyr.name + " is a valid layer." | |
except: | |
print lyr.name + "Not a regular feature class, excluded from layer list." | |
lyrList.remove(lyr) | |
return lyrList | |
def bufferLyrs(df, lyrList, buffer_dist): | |
# creates output paths, checks if each layer is bufferable, if so the layer is buffered | |
# and added to the map document dataframe. | |
for lyr in lyrList: | |
# newLyrname = lyr.name + str(buffer_dist) # this line provides an alternate layer naming strategy. | |
# This needs further development, as name length limits are likely to be encountered. | |
newLyrName = lyr.name + "_buffer" | |
outputPath = newLyrName + ".shp" | |
if arcpy.Exists(outputPath): # Must check if output path is already used, or buffer operation will fail: | |
print "Output path <" + outputPath + "> is already used." | |
outputPath = newLyrName + "_" + str(int(time.time())) + ".shp" | |
print "Output path <" + outputPath + "> will be used instead." | |
# the previous step may need to be called recursively, if there is a likelihood of the name | |
# <layer name>_buffer_new.shp being already in use. The layer would then be called | |
# <layer name>_buffer_new.shp | |
try: | |
arcpy.Buffer_analysis(in_features=lyr, | |
out_feature_class=outputPath, | |
buffer_distance_or_field=buffer_dist, line_side="FULL", line_end_type="ROUND", | |
dissolve_option="NONE", dissolve_field="", method="PLANAR") | |
# create layer in TOC and reference it in a variable for possible other actions | |
newLyrName = arcpy.mapping.Layer(outputPath) | |
# Adds layer to mxd | |
# arcpy.mapping.AddLayer(df, newLyrName, "BOTTOM") | |
arcpy.mapping.InsertLayer(df, lyr, newLyrName, "AFTER") | |
except: | |
print lyr.name + " cannot be buffered. Moving on to next layer..." | |
print "Buffering completed" | |
# === GLOBALS | |
# get from the user buffer distance, map doc location, workspace | |
print "This script will ask the user for a map document location, a data workspace, and a buffer distance.\n" \ | |
"It will then perform a buffer operation on each layer in the specified map document, using the specified\n"\ | |
"buffer distance. It will save these layers in the specified workspace, and add them to the map document." | |
try: | |
mxd = raw_input("Please enter the full path to your .mxd file:") | |
mapdoc = arcpy.mapping.MapDocument(mxd) | |
except SyntaxError as error_details: | |
print('SyntaxError, Goodbye!\n', error_details) | |
except IOError as error_details: | |
print('IOError, Goodbye!\n', error_details) | |
except ValueError as error_details: | |
print('ValueError, Goodbye!\n', error_details) | |
except TypeError as error_details: | |
print('TypeError, Goodbye!\n', error_details) | |
try: | |
buffer_dist = int(input("Please enter the buffer distance in m:")) | |
except SyntaxError as error_details: | |
print('SyntaxError, Goodbye!\n', error_details) | |
except IOError as error_details: | |
print('IOError, Goodbye!\n', error_details) | |
except ValueError as error_details: | |
print('ValueError, Goodbye!\n', error_details) | |
except TypeError as error_details: | |
print('TypeError, Goodbye!\n', error_details) | |
try: | |
arcpy.env.workspace = raw_input("Please enter the full path to your workspace folder:") | |
except SyntaxError as error_details: | |
print('SyntaxError, Goodbye!\n', error_details) | |
except IOError as error_details: | |
print('IOError, Goodbye!\n', error_details) | |
except ValueError as error_details: | |
print('ValueError, Goodbye!\n', error_details) | |
except TypeError as error_details: | |
print('TypeError, Goodbye!\n', error_details) | |
# === MAIN SCRIPT | |
def main(): | |
if sys.version_info[0] > 2.7: | |
raise Exception("Python 2.7 is required.") # guards against running the script with Python 3.x | |
global mapdoc # ensures that <mapdoc> is set to be a global variable | |
dfList = arcpy.mapping.ListDataFrames(mapdoc) # gets list of dataframes fro, the specified map document | |
for df in dfList: # this allows for map documents with multiple dataframes. | |
lyrList = getLayers(mapdoc, df) | |
bufferLyrs(df, lyrList, buffer_dist) | |
del lyrList # deletes the lyrList object to clean up memory | |
mapdoc.save() # saves the mapdocument so that added layers will be visible when the map document is opened. | |
del mapdoc # deletes the mapdoc object to clean up memory | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment