1、import arcpy, os#该文件夹下的 mxd 将批量执行mxdFolderPath = r“C:tempGJHY-16“ #加r避免被转义, 如t#旧的数据源 # 数据库连接orcl.sdeoldDataSources = r“C:UsersAdministratorAppDataRoamingESRIDesktop10.0ArcCatalogGJHY2.sde“# oldDataSources = r“C:UsersAdministratorAppDataRoamingESRIDesktop10.0ArcCatalogConnection to 127.0.0.1.sde“# ol
2、dDataSources = r“C:UsersAdministratorAppDataRoamingESRIDesktop10.0ArcCatalogConnection to 10.1.4.22.sde“#新的数据源newDataSources = r“C:UsersAdministratorAppDataRoamingESRIDesktop10.0ArcCatalogGJHY.sde“#是否要求新数据源有效validateNewDataSources = FalsesaveFolderPath = os.path.join(mxdFolderPath,“save“)if not os.p
3、ath.exists(saveFolderPath):os.mkdir(saveFolderPath)for filename in os.listdir(mxdFolderPath):fullpath = os.path.join(mxdFolderPath, filename)if os.path.isfile(fullpath):basename, extension = os.path.splitext(filename)if extension.lower() = “.mxd“:mxd = arcpy.mapping.MapDocument(fullpath)print fullpa
4、th#debugprint “=更改前:=“brknList = arcpy.mapping.ListBrokenDataSources(mxd)print “BrokenDataSources 个数:%d“ % len(brknList)print “【数据源损坏的图层 】:“if len(brknList)0:for lyr in brknList:print “- “ + lyr.nameprint “t“ + lyr.dataSource# print “【所有图层】:“# for lyr in arcpy.mapping.ListLayers(mxd):# print “- “ +
5、lyr.name# if lyr.supports(“DATASOURCE“):# print “t“ + lyr.dataSource# else:# print “t *该图层无 dataSource 属性。“# 修改数据源mxd.findAndReplaceWorkspacePaths(oldDataSources,newDataSources,validateNewDataSources)print “=更改后:=“ #debugbrknList = arcpy.mapping.ListBrokenDataSources(mxd)print “BrokenDataSources 个数:
6、%d“ % len(brknList)print “【数据源损坏的图层 】:“if len(brknList)0:for lyr in brknList:print “- “ + lyr.nameprint “t“ + lyr.dataSource # print “【所有图层】:“# for lyr in arcpy.mapping.ListLayers(mxd):# print “- “ + lyr.name# if lyr.supports(“DATASOURCE“):# print “t“ + lyr.dataSource# else:# print “t *该图层无 dataSour
7、ce 属性。“# 保存 -注意:存为副本后,由副本再次存为副本会报错# mxd.save() # newFilePath = filename.replace(“.mxd“,“_new.mxd“)newFilePath = os.path.join(saveFolderPath,filename)print “保存至: %s“ %newFilePathif os.path.exists(newFilePath):os.remove(newFilePath)# mxd.saveACopy(newFilePath)mxd.saveACopy(newFilePath,“10.0“)del mxdprint “OK!“