-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplaceMXDLayerDataSource.py
More file actions
58 lines (51 loc) · 2.32 KB
/
replaceMXDLayerDataSource.py
File metadata and controls
58 lines (51 loc) · 2.32 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import arcpy
import os
templateMXD = r"C:\Workfolders\MACBIO\Fiji\MAPS_of_FIJI\PA_workshop_July2016.mxd"
inFolder = r'C:\Workfolders\MACBIO\Fiji\MAPS_of_FIJI'
inMXDs = []
for f in os.listdir(inFolder):
if f.endswith(".mxd") and f.startswith("PA_workshop_July2016"):
inMXDs.append(os.path.join(inFolder, f))
for fixMXD in inMXDs:
outMXD = fixMXD.split(os.extsep)[0] + "_fixed.mxd"
layinfo = {}
MXD = arcpy.mapping.MapDocument(templateMXD)
for lyr in arcpy.mapping.ListLayers(MXD):
if lyr.supports("DATASOURCE"):
layinfo[lyr.name] = lyr.dataSource
del MXD
print str(len(layinfo)), "layers found in template MXD"
MXD = arcpy.mapping.MapDocument(fixMXD)
for lyr in arcpy.mapping.ListLayers(MXD):
if lyr.supports("DATASOURCE"):
print "processing", lyr.name
if lyr.isFeatureLayer:
oldlyrds = lyr.dataSource
oldWkspcPath = os.path.dirname(oldlyrds)
lyrnm = lyr.name
if lyrnm in layinfo:
newlyrds = layinfo[lyrnm]
if not oldlyrds == newlyrds:
newWkspcPath = os.path.dirname(newlyrds)
newDsName = os.path.basename(newlyrds).split(os.extsep)[0]
try:
lyr.replaceDataSource(newWkspcPath, "SHAPEFILE_WORKSPACE", newDsName, "validate")
except:
print arcpy.GetMessages()
print lyrnm, "didn't work"
if lyr.isRasterLayer:
oldlyrds = lyr.dataSource
oldWkspcPath = os.path.dirname(oldlyrds)
lyrnm = lyr.name
if lyrnm in layinfo:
newlyrds = layinfo[lyrnm]
if not oldlyrds == newlyrds:
newWkspcPath = os.path.dirname(newlyrds)
newDsName = os.path.basename(newlyrds).split(os.extsep)[0]
try:
lyr.replaceDataSource(newWkspcPath, "RASTER_WORKSPACE", newDsName)
except:
print arcpy.GetMessages()
print lyrnm, "didn't work"
MXD.saveACopy(outMXD)
del MXD