-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateDataSource.py
More file actions
34 lines (30 loc) · 1.21 KB
/
updateDataSource.py
File metadata and controls
34 lines (30 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import arcpy
from arcpy import mapping
def logMessage(msg):
print(msg)
arcpy.AddMessage(msg)
targetFile = r"C:\data\JOE_20160915.mxd" #This is for test
# targetFile = arcpy.GetParameterAsText(0) #For user input in script tool
mxd = mapping.MapDocument(targetFile)
layers = mapping.ListLayers(mxd)
brokenLayers = mapping.ListBrokenDataSources(mxd)
paths = []
for layer in layers:
if layer in brokenLayers:
logMessage("Layer " + str(layer) + " is broken")
continue
else:
if layer.supports("DATASOURCE") and layer.supports("workspacePath") and layer.supports("SERVICEPROPERTIES"):
#"workspacepath" excludes web service, "serviceproperties" excludes layer from file goedatabase
if arcpy.Exists(layer.workspacePath):
#this check excludes broken layers
desc = arcpy.Describe(layer.workspacePath)
if desc.connectionProperties.server.lower() == "gistst":
if layer.workspacePath not in paths:
paths.append(layer.workspacePath)
logMessage("Updating datasource...")
for path in paths:
mxd.findAndReplaceWorkspacePaths(path, r"Database Connections\RPUD_PRODDB.sde")
logMessage("Replaced data source from " + str(path) + " to Database Connections\RPUD_PRODDB.sde")
mxd.save()
logMessage("Datasource updated")