我正在尝试用我们的新 sde 路径“替换数据源”。我在 Arc10.2 中使用 v2.7。我们有多个直接连接路径名称和一个将服务器从使用 Oracle 更改为 Sql Server 的服务连接。我的代码在打印语句中一直有效,但随后出现错误消息“无法保存 ACopy,请检查我的权限”。请让我知道我是否走在正确的轨道上,将所有各种连接名称放入一个列表中,然后按照我编写的方式遍历它们。此外,我已经尝试了 mxd.saveACopy 和 del mxd 的所有缩进,但两周后似乎没有任何效果,所以我想我终于要请教一些地理极客的智慧了!
Code:
导入 arcpy, os, glob arcpy.env.workspace = "C:\Users\kmetivier\Documents\BrokenPaths\Folder5"
对于根目录、子文件夹、>os.walk(r"C:\Users\kmetivier\Documents\BrokenPaths\Folder5") 中的文件:
for filename in files: fullpath = os.path.join(root, filename) basename, extension = os.path.splitext(fullpath) if extension.lower() == ".mxd": print "------------------------------" print filename #open the map document mxd = arcpy.mapping.MapDocument(fullpath) #get all the layers for lyr in arcpy.mapping.ListLayers(mxd): #get the source from the layer if lyr.supports("datasource"): pathList = ["Database Connections\PWDB.arvada.org.sde","Database >Connections\GIS - PWDB.sde","Database Connections\PROD - GIS.sde","Database Connections\DC >- PROD - GIS.sde","Database Connections\GIS to SDE1.sde"] print "%s -> %s" % (lyr, pathList[0]) basename, extension = os.path.splitext (pathList[0]) if extension.lower() == ".sde": #NEW SOURCE datapath = r"Database Connections\GEODATA - GIS.sde" #overwrite the old path lyr.replaceDataSource(datapath, "SDE_WORKSPACE", "") print "replaced " + pathList[0] + " with " + datapath print "---------finished " + filename mxd.saveACopy(filename +"_2") del mxd
for lyr in arcpy.mapping.ListLayers(mxd): if lyr.supports("SERVICEPROPERTIES"): pathList1= r"(PWDB.arvada.org, 5151, {sde1}, {Database_authentication}, >{GDS}, {""}, {save_username_password}, {version}, {save_version_info}" servProp = lyr.serviceProperties print "Layer name:" + lyr.name print "-----------------------------------------------------" if lyr.serviceProperties["ServiceType"] != "SDE": print "Service Type: " + servProp.get('ServiceType', 'N/A') print " URL: " + servProp.get('URL', 'N/A') print " Connection: " + servProp.get('Connection', 'N/A') print " Server: " + servProp.get('Server', 'N/A') print " Cache: " + str(servProp.get('Cache', 'N/A')) print " User Name: " + servProp.get('UserName', 'N/A') print " Password: " + servProp.get('Password', 'N/A') print "" if extension.lower() == ".sde": #This is the NEW SOURCE that you want to point to datapath1 = r"Database Connections\GEODATA - GIS.sde" #replace the old path wih the new lyr.replaceDataSource(pathList1, "SDE_WORKSPACE", "") print "replaced " + pathList1 + " with " + datapath1 print "finished " + filename mxd.saveACopy(filename +"_2") else: print "Service Type: " + servProp.get('ServiceType', 'N/A') print " Database: " + servProp.get('Database', 'N/A') print " Server: " + servProp.get('Server', 'N/A') print " Service: " + servProp.get('Instance', 'N/A') print " Version: " + servProp.get('Version', 'N/A') print " User name: " + servProp.get('UserName', 'N/A') print " Authentication: " + servProp.get('AuthenticationMode', >'N/A') print "" del mxd>>