-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMapExportTools.py
More file actions
344 lines (309 loc) · 13.5 KB
/
MapExportTools.py
File metadata and controls
344 lines (309 loc) · 13.5 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import arcpy
class ExportBookmarks(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.category = "Map Export"
self.label = "Export Bookmarks"
self.description = "Exports all selected bookmarks in an MXD to PNG image."
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
# parameter0
param0 = arcpy.Parameter(
displayName="Use Currently Opened File",
name="useCurrentFile",
datatype="GPBoolean",
parameterType="Required",
direction="Input")
param0.value = True
param1 = arcpy.Parameter(
displayName="Map File",
name="mxdFile",
datatype="DEFile",
parameterType="Optional",
direction="Input")
param1.enabled = False
param1.filter.list = ["mxd"]
param2 = arcpy.Parameter(
displayName="Output Location",
name="outLocation",
datatype="DEFolder",
parameterType="Required",
direction="Input")
param3 = arcpy.Parameter(
displayName="Bookmarks List",
name="exportList",
datatype="GPString",
parameterType="Required",
direction="Input",
multiValue=True)
param4 = arcpy.Parameter(
displayName="Export Layout",
name="exportLayout",
datatype="GPBoolean",
parameterType="Required",
direction="Input")
param4.value = False
param5 = arcpy.Parameter(
displayName="Output Width",
name="outputW",
datatype="GPDouble",
parameterType="Required",
direction="Input")
param5.enabled = False
param6 = arcpy.Parameter(
displayName="Output Height",
name="outputH",
datatype="GPDouble",
parameterType="Required",
direction="Input")
param6.enabled = False
param7 = arcpy.Parameter(
displayName="Output Format",
name="outFormat",
datatype="GPString",
parameterType="Required",
direction="Input")
param7.filter.list = ["PDF", "PNG", "JPEG"]
params = [param0, param1, param2, param3, param4, param5, param6, param7]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
if parameters[0].value is True:
parameters[1].enabled = False
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
bkmkList = arcpy.mapping.ListBookmarks(mxd, "", df)
parameters[3].filter.list = [bkmk.name for bkmk in bkmkList]
if parameters[0].value is False:
parameters[1].enabled = True
if parameters[1].altered:
mxd = arcpy.mapping.MapDocument(str(parameters[1].value))
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
bkmkList = arcpy.mapping.ListBookmarks(mxd, "", df)
parameters[3].filter.list = [bkmk.name for bkmk in bkmkList]
if parameters[4].value is False:
parameters[5].enabled = True
parameters[5].value = 6.95
parameters[6].enabled = True
parameters[6].value = 8.16
if parameters[4].value is True:
parameters[5].enabled = False
parameters[6].enabled = False
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
if parameters[1].altered:
if len(parameters[3].filter.list) == 0:
parameters[1].setErrorMessage("This map document has no bookmarks!")
if parameters[0].altered & parameters[0].value is True:
if len(parameters[3].filter.list) == 0:
parameters[0].setErrorMessage("This map document has no bookmarks!")
return
def execute(self, parameters, messages):
"""The source code of the tool."""
# get necessary input parameters
if parameters[0].value is True:
mxdFile = "CURRENT"
else:
mxdFile = parameters[1].valueAsText
outLocation = parameters[2].valueAsText
exportList = parameters[3].valueAsText
exportLayout = parameters[4].value
outputW = parameters[5].value
outputH = parameters[6].value
outFormat = parameters[7].valueAsText
mxd = arcpy.mapping.MapDocument(mxdFile)
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
bkmkList = arcpy.mapping.ListBookmarks(mxd, "", df)
for bkmk in bkmkList:
df.extent = bkmk.extent
if bkmk.name in exportList:
if outFormat == "PDF":
outFile = outLocation + "\\" + bkmk.name + ".pdf"
arcpy.AddMessage("Exporting " + outFile)
arcpy.GetMessage(0)
if exportLayout is False:
arcpy.mapping.ExportToPDF(mxd, outFile, df, df_export_width=outputW * 300, df_export_height=outputH * 300, resolution=300)
else:
arcpy.mapping.ExportToPDF(mxd, outFile, resolution=300)
elif outFormat == "PNG":
outFile = outLocation + "\\" + bkmk.name + ".png"
arcpy.AddMessage("Exporting " + outFile)
arcpy.GetMessage(0)
if exportLayout is False:
arcpy.mapping.ExportToPNG(mxd, outFile, df, df_export_width=outputW * 300, df_export_height=outputH * 300, resolution=300)
else:
arcpy.mapping.ExportToPNG(mxd, outFile, resolution=300)
elif outFormat == "JPEG":
outFile = outLocation + "\\" + bkmk.name + ".jpeg"
arcpy.AddMessage("Exporting " + outFile)
arcpy.GetMessage(0)
if exportLayout is False:
arcpy.mapping.ExportToJPEG(mxd, outFile, df, df_export_width=outputW * 300, df_export_height=outputH * 300, resolution=300)
else:
arcpy.mapping.ExportToJPEG(mxd, outFile, resolution=300)
return
class ExportDDP (object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.category = "Map Export"
self.label = "Export Data Driven Pages"
self.description = "Exports all selected data driven pages in an MXD to JPEG, PDF or PNG image."
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
# parameter0
param0 = arcpy.Parameter(
displayName="Map File",
name="mapFile",
datatype="DEFile",
parameterType="Required",
direction="Input")
param0.filter.list = ["mxd"]
# parameter1
param1 = arcpy.Parameter(
displayName="Output Location",
name="outDir",
datatype="DEFolder",
parameterType="Required",
direction="Input")
# parameter2
param2 = arcpy.Parameter(
displayName="Data Driven Pages List",
name="exportList",
datatype="GPString",
parameterType="Required",
direction="Input",
multiValue=True)
# parameter3
param3 = arcpy.Parameter(
displayName="Output Format",
name="outFormat",
datatype="GPString",
parameterType="Required",
direction="Input")
param3.filter.list = ["JPEG", "PDF", "PNG"]
params = [param0, param1, param2, param3]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
if parameters[0].altered:
mxd = arcpy.mapping.MapDocument(str(parameters[0].value))
totalPages = mxd.dataDrivenPages.pageCount
exportList = []
for page in range(1, totalPages + 1):
mxd.dataDrivenPages.currentPageID = page
pageField = mxd.dataDrivenPages.pageNameField.name
pageName = mxd.dataDrivenPages.pageRow.getValue(pageField)
exportList.append(str(page) + "-" + pageName)
parameters[2].filter.list = exportList
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
mapFile = parameters[0].valueAsText
outDir = parameters[1].valueAsText
exportList = parameters[2].valueAsText
outFormat = parameters[3].value
# open mxd for reading
mxd = arcpy.mapping.MapDocument(mapFile)
totalPages = mxd.dataDrivenPages.pageCount
# loop through list of pages
for page in range(1, totalPages + 1):
mxd.dataDrivenPages.currentPageID = page
pageField = mxd.dataDrivenPages.pageNameField.name
pageName = str(page) + "-" + mxd.dataDrivenPages.pageRow.getValue(pageField)
outPage = outDir + "\\" + pageName
if pageName in exportList:
arcpy.AddMessage("Exporting " + outPage)
if outFormat == "PNG":
arcpy.mapping.ExportToPNG(mxd, outPage + ".png", resolution=300)
if outFormat == "PDF":
arcpy.mapping.ExportToPDF(mxd, outPage + ".pdf", resolution=300)
if outFormat == "JPEG":
arcpy.mapping.ExportToJPEG(mxd, outPage + ".jpg", resolution=300)
class DumpMXDs (object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.category = "Map Export"
self.label = "Dump MXDs"
self.description = "Exports all MXD documents in the Source Folder to the Output File Format and saves them in the Destination Folder."
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
# parameter0
param0 = arcpy.Parameter(
displayName="Source Folder",
name="source",
datatype="DEFolder",
parameterType="Required",
direction="Input")
# parameter 1
param1 = arcpy.Parameter(
displayName="Destination Folder",
name="destination",
datatype="DEFolder",
parameterType="Required",
direction="Input")
# parameter 2
param2 = arcpy.Parameter(
displayName="Output File Format",
name="outFormat",
datatype="GPString",
parameterType="Required",
direction="Input")
param2.filter.list = ["PDF", "PNG", "JPEG"]
params = [param0, param1, param2]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
import os
source = parameters[0].valueAsText
destination = parameters[1].valueAsText
outFormat = parameters[2].valueAsText
for root, dirs, files, in os.walk(source):
for fname in files:
# loop for mxd files
if fname[-3:] in ["mxd", "MXD"]:
filePath = source + "\\" + fname
mxd = arcpy.mapping.MapDocument(filePath)
# make files and export
if outFormat == "PDF":
savePath = os.path.join(destination + "\\" + fname[:-4] + ".pdf")
print savePath
arcpy.mapping.ExportToPDF(mxd, savePath, resolution=300)
elif outFormat == "PNG":
savePath = os.path.join(destination + "\\" + fname[:-4] + ".png")
print savePath
arcpy.mapping.ExportToPNG(mxd, savePath, resolution=300)
elif outFormat == "JPEG":
savePath = os.path.join(destination + "\\" + fname[:-4] + ".jpeg")
print savePath
arcpy.mapping.ExportToJPEG(mxd, savePath, resolution=300)