Created
August 1, 2018 19:18
-
-
Save pmacMaps/e22e07122724ad9e33a7a7a54680f393 to your computer and use it in GitHub Desktop.
ArcPy Python script to list out broken data sources in an ArcMap document
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
#------------------------------------------------------------------------------- | |
# Name: Lesson 24 - Working With Multiple Maps In A Directory, | |
# Geospatial Programming | |
# | |
# Author: Patrick McKinney, Harrisburg Area Community College | |
# | |
# Created: 6/13/17 | |
# | |
# Description: sample script for looping through a folder (directory) | |
# and listing broken data sources in each map document (.mxd) | |
# file within the folder | |
# | |
# Copyright: Harrisburg Area Community College, 2017# | |
#------------------------------------------------------------------------------- | |
import arcpy, os, sys | |
try: | |
# placeholder for text for text file | |
logMsg = '' | |
# text file to write report information to | |
logFile = r'C:\[path]\[to]\[folder]\GIS Python Course\Lessons\Lesson Scripts\Unit 6\Broken Data Sources Report.txt' | |
# directory to search within for broken data sources in map documents (.mxd) | |
directory = r'C:\[path]\[to]\[folder]\GIS Python Course\Lessons\Lesson Scripts\Unit 6\Maps' | |
# add message | |
logMsg += 'Finding broken data sources for map documents (.mxd) in {}\n'.format(directory) | |
# loop through files in directory | |
# os module - https://docs.python.org/2/library/os.html | |
# https://docs.python.org/2/library/os.html#os.listdir | |
# os.listdir returns a list containing the names of the entries in the directory | |
for fileName in os.listdir(directory): | |
# fullPath is set to the path and file name for each file in directory | |
# i.e., C:\[path]\[to]\[folder]\GIS Python Course\Lessons\Lesson Scripts\Unit 6\Maps\Transportation Map.mxd | |
fullPath = os.path.join(directory, fileName) | |
# https://docs.python.org/2/library/os.path.html#os.path.isfile | |
# Returns True if path is an existing regular file | |
if os.path.isfile(fullPath): | |
# https://docs.python.org/2/library/os.path.html#os.path.splitext | |
# Split the pathname path into a pair (root, ext) such that root + ext == path | |
# get the extension for the file | |
basename, extension = os.path.splitext(fullPath) | |
# if the file is a map document (.mxd), execute code to find broken data sources | |
if extension == ".mxd": | |
# add message | |
logMsg += '\nListing of broken data sources for {}:\n'.format(fileName) | |
# variable for map document | |
mxd = arcpy.mapping.MapDocument(fullPath) | |
# get a list of broken data sources in mapd document | |
brknList = arcpy.mapping.ListBrokenDataSources(mxd) | |
# test if there are any broken data sources | |
if len(brknList) == 0: # condition for no broken data sources | |
logMsg += '\n\tThere are no broken data sources in this map\n' | |
else: # condition for broken data sources | |
# loop through list of broken data layers and write the name of the layer | |
for brknItem in brknList: | |
logMsg += '\n\t{}\n'.format(brknItem.name) | |
# end for broken data sources list | |
# add spacer | |
logMsg += '\n' | |
logMsg += '*' * 50 | |
logMsg += '\n' | |
# end if for mxd file type | |
# end if for files within directory | |
# end for files/folders within directory | |
# handle error outside of Python system with Environment Error | |
except EnvironmentError as e: | |
# Store information about the error | |
tbE = sys.exc_info()[2] | |
# write the line number the error occured | |
logMsg += 'Failed at Line %i \n' % tbE.tb_lineno | |
# write the error message | |
logMsg += '\nError: {}\n'.format(str(e)) | |
# Exception error | |
except Exception as e: | |
# Store information about the error | |
tbE = sys.exc_info()[2] | |
# write the line number the error occured | |
logMsg += 'Failed at Line %i \n' % tbE.tb_lineno | |
# write the error message | |
logMsg += '\nError: {}\n'.format(e.message) | |
# clean up operations | |
finally: | |
# del mxd to release locks | |
if mxd: | |
del mxd | |
# write information to text file | |
try: | |
with open(logFile,'w') as f: | |
f.write(str(logMsg)) | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment