# Script Name: Change_datasource # Description: This script allows you to select a specific layer # in your table of content and change the data source. # Created By: Pierre # Date: 29/04/2014 import arcpy import os # Read the parameter values: # 1: Input workspace # 2: Input layer # 3: Set new server connection # 4: Set new source dataset # mxdLocation = arcpy.GetParameterAsText(0) layer = arcpy.GetParameterAsText(1) serverConnection = arcpy.GetParameterAsText(2) newSource = arcpy.GetParameterAsText(3) newMXDname = arcpy.GetParameterAsText(4) def gdbType(connPath): connType = os.path.splitext(connPath)[1] if connType == '.sde': arcpy.AddMessage('\nsde source.') wsType = 'SDE_WORKSPACE' elif connType == '.gdb': arcpy.AddMessage('\nfile gdb source:') wsType = 'FILEGDB_WORKSPACE' else: arcpy.AddMessage('\nunhandled source type') wsType = '' return wsType arcpy.env.workspace = serverConnection if arcpy.Exists(newSource): wsType = gdbType(serverConnection) arcpy.AddMessage('the workspace for the new source is {0}\n'.format(serverConnection)) MXDdir = os.path.dirname(mxdLocation) newMXDname = os.path.join(MXDdir, newMXDname + '.mxd') mxd = arcpy.mapping.MapDocument(mxdLocation) # single-layer datasource replacement, 1st one listed from TOC. #lyr = arcpy.mapping.ListLayers(mxd, layer)[0] # multiple-layer datasource replacement... for lyr in arcpy.mapping.ListLayers(mxd, layer): if lyr.supports("DATASOURCE") and wsType != '': arcpy.AddMessage('DATASOURCE property is supported for {0}.\n'.format(lyr.longName)) lyr.replaceDataSource(serverConnection, wsType, newSource) else: arcpy.AddMessage('DATASOURCE property is either not supported or handling by this script is not provided.\n') if not os.path.exists(newMXDname): arcpy.AddMessage('Please check the layer properties for ALL layers named \'{0}\', checking the source change in your new map doc:'.format(layer)) arcpy.AddMessage('{0}.\n'.format(newMXDname)) mxd.saveACopy(newMXDname) else: arcpy.AddWarning('mxd name is not unique and will not be saved...\n') arcpy.AddMessage('The End.')
mxdLocation = arcpy.GetParameterAsText(0) layer = arcpy.GetParameterAsText(1) serverConnection = arcpy.GetParameterAsText(2) newSource = arcpy.GetParameterAsText(3) newMXDname = arcpy.GetParameterAsText(4) def gdbType(connPath): connType = os.path.splitext(connPath)[1] if connType == '.sde': arcpy.AddMessage('\nsde source.') wsType = 'SDE_WORKSPACE' elif connType == '.gdb': arcpy.AddMessage('\nfile gdb source:') wsType = 'FILEGDB_WORKSPACE' else: arcpy.AddMessage('\nunhandled source type') wsType = '' return wsType arcpy.env.workspace = serverConnection if arcpy.Exists(newSource): wsType = gdbType(serverConnection) arcpy.AddMessage('the workspace for the new source is {0}\n'.format(serverConnection)) MXDdir = os.path.dirname(mxdLocation) newMXDname = os.path.join(MXDdir, newMXDname + '.mxd') mxd = arcpy.mapping.MapDocument(mxdLocation) lyr = arcpy.mapping.ListLayers(mxd, layer)[0] # single-layer datasource replacement, 1st one listed from TOC. if lyr: if lyr.supports("DATASOURCE") and wsType != '': arcpy.AddMessage('DATASOURCE property is supported for this layer.\n') lyr.replaceDataSource(serverConnection, wsType, newSource) if not arcpy.Exists(newMXDname): arcpy.AddMessage('Please check the {0} layer properties for the source change in your new map doc:'.format(lyr.name)) arcpy.AddMessage('{0}.\n'.format(newMXDname)) mxd.saveACopy(newMXDname) else: arcpy.AddWarning('mxd name is not unique and will not be saved...\n') else: arcpy.AddMessage('DATASOURCE property is either not supported or handling by this script is not provided.\n') arcpy.AddMessage('The End.')
if targetLyr is not None: print "your selected layer is {0}.format(targetLyr.longName)
print "your selected layer is" [0].format(targetLyr.longName)
import arcpy mxd = arcpy.mapping.MapDocument('CURRENT') targetLyrName = "enter name of layer in TOC" targetLyr = arcpy.mapping.ListLayers(mxd, targetLyrName)[0] if targetLyr is not None: print "your selected layer is {0}.format(targetLyr.longName)
serverConnection = r"enter an SDE connection file pathname" newSource = "enter the full name of the feature class to use for replacement" arcpy.env.workspace = serverConnection if arcpy.Exists(newSource): print 'good to go' if targetLyr.supports("DATASOURCE"): print "DATASOURCE property is supported for this layer." targetLyr.replaceDataSource(serverConnection, "SDE_WORKSPACE", newSource) else: print "DATASOURCE property is not supported for this layer."
if lyr.supports("DATASOURCE"): lyr.replaceDataSource(serverConnection, "SDE_WORKSPACE", newSource) arcpy.AddMessage('replace executed...')
import arcpy class ToolValidator(object): """Class for validating a tool's parameter values and controlling the behavior of the tool's dialog.""" def __init__(self): """Setup arcpy and the list of tool parameters.""" self.params = arcpy.GetParameterInfo() def initializeParameters(self): """Refine the properties of a tool's parameters. This method is called when the tool is opened.""" return def updateParameters(self): """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed.""" stringFilterTargetLyrs = self.params[1].filter stringFilterSourceLyrs = self.params[3].filter theMap = self.params[0].value theGDB = self.params[2].value if theMap: mxd = arcpy.mapping.MapDocument(str(theMap)) lyrList = [str(lyr) for lyr in arcpy.mapping.ListLayers(mxd)] stringFilterTargetLyrs.list = lyrList if theGDB: arcpy.env.workspace = str(theGDB) fcList = arcpy.ListFeatureClasses() stringFilterSourceLyrs.list = fcList return def updateMessages(self): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.