Skip to content

Instantly share code, notes, and snippets.

@danmaps
Last active October 31, 2017 20:52
Show Gist options
  • Save danmaps/aaa0c158ce14e5a86eedf5d35f2dcc13 to your computer and use it in GitHub Desktop.
Save danmaps/aaa0c158ce14e5a86eedf5d35f2dcc13 to your computer and use it in GitHub Desktop.
Takes an input directory and walks through it, finding all MXDs and searching for fedlands layers
#-------------------------------------------------------------------------------
# Title: Remove all fedlands layers
# Purpose: Remove all fedlands from MXDs. This script takes an input
# directory and walks through it, finding all MXDs and searching
# for fedlands layers
#
# Author: Esri Support
# Based on: https://github.com/Esri/developer-support/tree/master/python/arcpy-python/remove-all-basemaps-batch
#
# Created: 10/31/2017
#-------------------------------------------------------------------------------
"""This script is designed to open an MXD, check to see if
USA Federal Lands exists, and if it does, removes it from the map"""
#import modules
import arcpy
import os
def remove_NationalMapFederalLands(path):
for r,d,f in os.walk(path):
for m in f:
if m.endswith(".mxd"):
mxd = arcpy.mapping.MapDocument(os.path.join(r, m))
#get dataframes
dataframes = arcpy.mapping.ListDataFrames(mxd)
for df in dataframes:
#loop through layers
layers = arcpy.mapping.ListLayers(mxd, "*USA Federal Lands*")
for layer in layers:
print("USA Federal Lands Layer found in " + m)
#remove an layers in this list, as they contain the USA Federal Lands wildcard
try:
arcpy.mapping.RemoveLayer(df, layer)
print(str(layer.name) + " removed from " + m)
#save the changes
mxd.save()
print("changes applied to " + m)
del mxd, layers
except:
print(m + " cannot be saved")
print("script complete")
if __name__ == '__main__':
#set workspace
path = "C:\TestData"
remove_NationalMapFederalLands(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment