def plot_with_arcpy(init_time, replace_workspace_dir,origin_workspace_dir, images_dir, mxd_path, resolution=100): mxd = mp.MapDocument(mxd_path) # 修改预报时效 txts = mp.ListLayoutElements(mxd, "TEXT_ELEMENT") init_time_datetime = datetime.datetime.strptime(init_time, '%y%m%d%H') for valid_h, i in zip(range(0, 132, 12), range(23, 12, -1)): beg = (init_time_datetime + datetime.timedelta(hours=valid_h)).strftime('%d%H') end = (init_time_datetime + datetime.timedelta(hours=valid_h + 12)).strftime('%d%H') txts[i].text = beg + '-' + end # 替换数据源 mxd.replaceWorkspaces(origin_workspace_dir, "RASTER_WORKSPACE", replace_workspace_dir, "RASTER_WORKSPACE", False) # 处理缺失数据 broken_lyrs = mp.ListBrokenDataSources(mxd) for broken_lyr in broken_lyrs: # 让缺失数据图层及与其同在一个dataframe里其他的图层不显示 broken_df = mp.ListDataFrames(mxd, broken_lyr.name) if len(broken_dfs) > 0: lyrs_in_broken_df = mp.ListLayers(mxd, "", broken_df[0]) for lyr in lyrs_in_broken_df: lyr.visible = False #mxd.save() 不要保存 mp.ExportToJPEG(mxd, os.path.join(images_dir, init_time), resolution=resolution)
def checkfileValidation(self,mxdLists): print "++++++++INFO:开始检查文档的有效性++++++++" file_to_be_published=[] for file in mxdLists: mxd=mapping.MapDocument(file) brknlist=mapping.ListBrokenDataSources(mxd) if not len(brknlist)==0: print "++++++++ERROR:地图文档,"+os.path.split(file)[1]+"损坏,无法发布服务++++++++" else: file_to_be_published.append(file) print "++++++++INFO:地图文档有效性检查完毕++++++" return file_to_be_published
def mxd_broken_links(folder): """ Given a folder path, search folder directory for mxd files with broken links Parameters ---------- folder: string, folder pathway Returns ------- result: A sequence of feature class information to store in csv file. For each broken layer found within a MXD file, the following information is stored: map name: string broken layer: string broken layer data path: string """ for root, dirs, files in os.walk(folderPath): for fileName in files: # Crawl folder for files with .mxd extention if fileName.lower().endswith(".mxd"): # Return MXD pathways fullPath = os.path.join(root, fileName) # Return MXD properties mxd = mapping.MapDocument(fullPath) print "\nChecking MXD: " + fileName # Loop broken layers in MXDs, returning a list brokenlinks = mapping.ListBrokenDataSources(mxd) if brokenlinks == None: fileName = "No broken links were found" linkname = "No broken links were found" DataSource = "No broken links were found" seq = (fileName, linkname, DataSource) yield seq else: for brokenlink in brokenlinks: print "Broken Data Link: " + brokenlink.name linkname = brokenlink.name DataSource = brokenlink.dataSource seq = (fileName, linkname, DataSource) yield seq del mxd else: #fileName = "No MXDs were found in folder" linkname = "File is not a MXD" DataSource = os.path.join(root, fileName) seq = (fileName, linkname, DataSource) yield seq print "\nFile(s) is not a MXD: " + fileName
def checkfileValidation(self,mxdLists): arcpy.AddMessage( "++++++++INFO: Validate MXD++++++++") file_to_be_published=[] for file in mxdLists: mxd=mapping.MapDocument(file) brknlist=mapping.ListBrokenDataSources(mxd) if not len(brknlist)==0: arcpy.AddMessage( "++++++++ERROR:Map Document,"+os.path.split(file)[1]+"is broken++++++++") else: file_to_be_published.append(file) arcpy.AddMessage( "++++++++INFO: Done validating mxd++++++") return file_to_be_published
def crawlmxds(folder): for root, dirs, files in os.walk(folderPath): for f in files: # Crawl folder for files with .mxd extention if f.lower().endswith(".mxd"): # Return MXD names and pathways mxdName = os.path.splitext(f)[0] mxdPath = os.path.join(root, f) print '\nChecking MXD titled: ' + mxdName # Return MXD properties mxd = mapping.MapDocument(mxdPath) # Return and loop broken layer list in MXD for lyr in mapping.ListBrokenDataSources(mxd): lyrName = lyr.name brokensource = lyr.dataSource if lyr.supports("DATASOURCE"): if old_gdb in brokensource: print ' \nReplacing workspace of broken layer: ' + lyrName lyr.findAndReplaceWorkspacePath(old_gdb, new_gdb) mxd.save() del mxd
#------------------------------------------------------------------------------- # Name: module1 # Purpose: # # Author: Eric # # Created: 06/04/2018 # Copyright: (c) Eric 2018 # Licence: <your licence> #------------------------------------------------------------------------------- # import module import arcpy.mapping as mapping # reference map document file mxd = mapping.MapDocument(r"c:\ArcpyBook\Ch3\Crime_BrokenDataLinks.mxd") # get a list of broken data sources listBrokenDS = mapping.ListBrokenDataSources(mxd) # iterate over the list and print out the layer names for i in listBrokenDS: print i.name
def crawlmxds(folder): """ Given a directory search for mxd files. Every file with extension ".mxd" will be opened in order to list layers with broken data links. If user input matches any features listed, it then replaces the workspace of broken layers. Parameters ---------- folder: string, directory to map documents Returns ------- result: relinked feature data sources. """ for root, dirs, files in os.walk(folderPath): for f in files: # Crawl folder for files with .mxd extention if f.lower().endswith(".mxd"): # Return MXD names and pathways mxdName = os.path.splitext(f)[0] mxdPath = os.path.join(root, f) print '\nChecking MXD titled: ' + mxdName # Return MXD properties mxd = mapping.MapDocument(mxdPath) # Return tables in MXDs tables = mapping.ListTableViews(mxd) # Return and loop broken layer list in MXD try: brokenlayers = mapping.ListBrokenDataSources(mxd) for brokenlayer in brokenlayers: layername = brokenlayer.name print ' Broken Layer: ' + layername if original_feature in brokenlayers: matchedname = original_feature.name print ' Replacing broken layer workspace: ' + matchedname # replace old data source with new data source original_feature.replaceDataSource( renamed_feature_workspace, "FILEGDB_WORKSPACE", renamed_feature_basename) else: print ' No broken links were fixed because 1) there were no broken layers or 2) no broken layers matched original feature' # for lyr in mapping.ListBrokenDataSources(mxd): # lyrName = lyr.name # print ' Broken Layer in ' + mxdName + '.mxd: ' + lyrName # if lyr.supports("DATASOURCE"): # # See if the original feature is one of the broken data layers # if original_feature in lyr.dataSource: # print ' Replacing broken layer workspace: ' + lyrName # # replace old data source with new data source # lyr.replaceDataSource(renamed_feature_workspace, # "FILEGDB_WORKSPACE", renamed_feature_basename) # Ignore tables except: #AttributeError for table in tables: if table.isBroken == True: pass mxd.save() del mxd
import arcpy.mapping as mapping, os path = r'C:\EsriTraining' f = open('BrokenDataList.txt', 'w') for root, dirs, files in os.walk(path): for filename in files: basename, extension = os.path.splitext(filename) if extension == ".mxd": fullPath = os.path.join(path, filename) mxd = mapping.MapDocument( r'C:\EsriTraining\PythEveryone\PythonInArcGIS\Crime_DataLinksFixed.mxd' ) f.write("MXD: " + filename + "\n") brknList = mapping.ListBrokenDataSources(mxd) for brknItem in brknList: f.write("\t" + brknItem.name + "\n") f.close() print "script complete!"