def update_metadata(document='CURRENT'): """ updates metadata in an arcmap document document - (optional) the path to an arcmap document or the keyword 'CURRENT'. The default is 'CURRENT' """ #set local variables dir = GetInstallInfo("desktop")["InstallDir"] translator = dir + "Metadata/Translator/ESRI_ISO2ISO19139.xml" mxd = MapDocument(document) df = ListDataFrames(mxd) temp_path = "C:/temp" for layer in ListLayers(mxd, "*", df[0]): if not layer.isGroupLayer: description_text = "" path = temp_path + '/' + layer.datasetName + '.xml' print(path) ExportMetadata_conversion(layer.dataSource, translator, path) dom = parse(path) fields = ('abstract', 'purpose', 'credit') for field in fields: tags = dom.getElementsByTagName(field) print(str(len(tags)) + ' | ' + str(tags)) if len(tags): tag_string = tags[0].getElementsByTagName( 'gco:CharacterString')[0].childNodes[0].nodeValue description_text = "{} <br /> <p><strong>{}</strong>: {}</p>".format( description_text, field.capitalize(), tag_string) if field == 'credit': layer.credit = tag_string print(description_text) layer.description = description_text
def findAndReplaceOldPaths(mxdList, sdeList, featureDict): for mxdItem in mxdList: mxd = MapDocument(mxdItem) print "\nFor the mxd at " + str(mxdItem) print "the layers were:\n" # featureDict changes: mxdLayers = ListLayers(mxd) for mxdLayer in mxdLayers: # Might be causing locked data sources to not get changed. # If you can't access the mxdLayer source, it might not # use the supports method correctly. if mxdLayer.supports("dataSource"): layerSource = mxdLayer.dataSource print str(layerSource) if layerSource in featureDict.keys(): mxdLayer.replaceDataSource( featureDict[layerSource][0], featureDict[layerSource][1], featureDict[layerSource][2] ) else: pass else: pass # sdeList changes: for sdeItem in sdeList: mxd.findAndReplaceWorkspacePaths(sdeItem[0], sdeItem[1]) print "\nThe new layers are:\n" mxdLayers = ListLayers(mxd) for mxdLayer in mxdLayers: if mxdLayer.supports("dataSource"): layerSource = mxdLayer.dataSource print str(layerSource) else: pass # Make the changes permanent. print "\nSaving mxd...\n" mxd.save() del mxd
def add_layer_to_display(layer): """ Adds the given |layer| to the list of ArcMap layers so that it is visible. Returns True on success and False on failure. """ try: data_frame = ListDataFrames(MapDocument("CURRENT"), "Layers")[0] AddLayer(data_frame, Layer(layer), "AUTO_ARRANGE") return True except: return False
def findAndReplaceOldPaths(mxdList, sdeList, featureDict): for mxdItem in mxdList: mxd = MapDocument(mxdItem) print "\nFor the mxd at " + str(mxdItem) print "the layers were:\n" # featureDict changes: mxdLayers = ListLayers(mxd) for mxdLayer in mxdLayers: # Might be causing locked data sources to not get changed. # If you can't access the mxdLayer source, it might not # use the supports method correctly. if mxdLayer.supports('dataSource'): layerSource = mxdLayer.dataSource print str(layerSource) if layerSource in featureDict.keys(): mxdLayer.replaceDataSource(featureDict[layerSource][0], featureDict[layerSource][1], featureDict[layerSource][2]) else: pass else: pass # sdeList changes: for sdeItem in sdeList: mxd.findAndReplaceWorkspacePaths(sdeItem[0], sdeItem[1]) print "\nThe new layers are:\n" mxdLayers = ListLayers(mxd) for mxdLayer in mxdLayers: if mxdLayer.supports('dataSource'): layerSource = mxdLayer.dataSource print str(layerSource) else: pass # Make the changes permanent. print "\nSaving mxd...\n" mxd.save() del mxd
def read_mxd(mxd_path): """ """ # open layer file mxd = MapDocument(mxd_path) # basic informations print(mxd.title) print(mxd.description) print(mxd.author) print(mxd.filePath) print(mxd.credits) print(mxd.tags) print(mxd.summary) print(('relative paths ?', mxd.relativePaths)) print(mxd.hyperlinkBase) print(mxd.dateExported) print(mxd.datePrinted) print(mxd.dateSaved) # print(mxd.activeDataFrame) # print(mxd.activeView) print(mxd.dataDrivenPages) # dataframes dfs = ListDataFrames(mxd) # broken datasources lost_ds = ListBrokenDataSources(mxd) for layer in lost_ds: print(layer.name) del mxd # End read MXD return
# Turn off visibility for the slower layers in a map that's not loading. # -- Should make it load faster. ''' Created on 2014-08-14 @author: Dirk updated 2014-09-04 by Dirk ''' from arcpy import RefreshTOC, RefreshActiveView from arcpy.mapping import MapDocument, ListDataFrames, ListLayers, RemoveLayer import os #mapDocName = r'\\gisdata\ArcGIS\GISdata\MXD\2014090401_CCL_TESTing.mxd' mapDocName = r'C:\GIS\MXD\2014090401_CCL_TESTing.mxd' # Try local file copy instead. mxd = MapDocument(mapDocName) df = ListDataFrames(mxd)[0] layers = ListLayers(mxd, "K*", df) # Use K* to include the KDOR/Tax_Units group layer. print layers for layer in layers: if hasattr(layer, 'visible'): layer.visible = False else: pass del layers
# Turn off visibility for the slower layers in a map that's not loading. # -- Should make it load faster. ''' Created on 2014-08-14 @author: Dirk updated 2014-09-04 by Dirk ''' from arcpy import RefreshTOC, RefreshActiveView from arcpy.mapping import MapDocument, ListDataFrames, ListLayers, RemoveLayer import os #mapDocName = r'\\gisdata\ArcGIS\GISdata\MXD\2014090401_CCL_TESTing.mxd' mapDocName = r'C:\GIS\MXD\2014090401_CCL_TESTing.mxd' # Try local file copy instead. mxd = MapDocument(mapDocName) df = ListDataFrames(mxd)[0] layers = ListLayers(mxd, "K*", df) # Use K* to include the KDOR/Tax_Units group layer. print layers for layer in layers: if hasattr(layer, 'visible'): layer.visible = False else: pass del layers layers = ListLayers(mxd, "B*", df) # Use B* to include the background layer.
def __init__(self, mxd_path, dico_mxd, tipo, txt=''): u""" Uses arcpy functions to extract basic informations about Esri map documents and store into dictionaries. mxdpath = path to the MXD file dico_mxd = dictionary for global informations tipo = format text = dictionary of text in the selected language see: http://help.arcgis.com/fr/arcgisdesktop/10.0/help/index.html#/na/00s30000000n000000/ """ # changing working directory to layer folder chdir(path.dirname(mxd_path)) # raising arcpy specific exceptions self.alert = 0 # opening MXD mxd = MapDocument(mxd_path) # basics dico_mxd['title'] = mxd.title dico_mxd['description'] = mxd.description dico_mxd['creator_prod'] = mxd.author dico_mxd['folder'] = mxd.filePath dico_mxd['credits'] = mxd.credits dico_mxd['keywords'] = mxd.tags dico_mxd['subject'] = mxd.summary dico_mxd['relpath'] = mxd.relativePaths dico_mxd['url'] = mxd.hyperlinkBase dico_mxd['date_export'] = mxd.dateExported dico_mxd['date_print'] = mxd.datePrinted dico_mxd['date_actu'] = mxd.dateSaved dico_mxd['active_df'] = mxd.activeDataFrame.name dico_mxd['active_view'] = mxd.activeView # dico_mxd['active_ddp'] = mxd.dataDrivenPages # by default let's start considering there is only one layer dframes = ListDataFrames(mxd) dico_mxd['subdatasets_count'] = len(dframes) li_dframes_names = [] dico_mxd['dframes_names'] = li_dframes_names x = 0 for dframe in dframes: x += 1 # dictionary where will be stored informations dico_dframe = OrderedDict() # parent GDB dico_dframe[u'name'] = dframe.name # getting layer globlal informations self.infos_dataframe(dframe, dico_dframe) # storing layer into the GDB dictionary dico_mxd['{0}_{1}'.format(x, dico_dframe.get('name'))] = dico_dframe # reset del dico_dframe # dico_mxd[u'layers_count'] = total_layers # scale # dico_mxd['maxScale'] = layer_obj.maxScale # dico_mxd['minScale'] = layer_obj.minScale # # secondary # dico_mxd['license'] = layer_obj.credits # dico_mxd['broken'] = layer_obj.isBroken size = path.getsize(mxd_path) dico_mxd[u"total_size"] = self.sizeof(size) # global dates dico_mxd[u'date_crea'] = strftime('%d/%m/%Y', localtime(path.getctime(mxd_path))) # # total fields count # total_fields = 0 # dico_mxd['total_fields'] = total_fields # # total objects count # total_objs = 0 # dico_mxd['total_objs'] = total_objs # # parsing layers return