forked from Esri/OptimizeRasters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptimizeRasters.pyt
910 lines (825 loc) · 35.4 KB
/
OptimizeRasters.pyt
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
#------------------------------------------------------------------------------
# Copyright 2017 Esri
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#------------------------------------------------------------------------------
# Name: OptimizeRasters.pyt
# Description: UI for OptimizeRasters
# Version: 20171002
# Requirements: ArcMap / gdal_translate / gdaladdo
# Required Arguments:optTemplates, inType, inprofiles, inBucket, inPath, outType
# outprofiles, outBucket, outPath
# Optional Arguments:intempFolder, outtempFolder, cloneMRFFolder, cacheMRFFolder
# Usage: To load within ArcMap
# Author: Esri Imagery Workflows team
#------------------------------------------------------------------------------
import arcpy
from arcpy import env
import sys
import os
import subprocess
import time
if (sys.version_info[0] < 3):
import ConfigParser
else:
import configparser as ConfigParser
from xml.dom import minidom
from datetime import datetime
templateinUse = None
AzureRoot = '.OptimizeRasters/Microsoft'
GoogleRoot = '.OptimizeRasters/Google'
AwsRoot = '.aws'
def returnDate():
sDate = str(datetime.date(datetime.today())).replace('-', '')
sTime = str(datetime.time(datetime.today())).split('.')[0].replace(':', '')
return sDate + sTime
def setXMLXPathValue(doc, xPath, key, value):
if (not doc or
not xPath or
not key or
not value):
return False
nodes = doc.getElementsByTagName(key)
for node in nodes:
parents = []
c = node
while(c.parentNode):
parents.insert(0, c.nodeName)
c = c.parentNode
p = '/'.join(parents)
if (p == xPath):
if (not node.hasChildNodes()):
node.appendChild(doc.createTextNode(value))
return True
node.firstChild.data = str(value)
return True
return False
def returntemplatefiles():
selfscriptpath = os.path.dirname(__file__)
templateloc = os.path.join(selfscriptpath, 'templates')
templatefilelist = os.listdir(templateloc)
global allactualxmlFiles
allactualxmlFiles = []
allxmlFiles = []
for ft in templatefilelist:
if ft.endswith('.xml'):
allactualxmlFiles.append(ft)
ft = ft.replace('.xml', '')
allxmlFiles.append(ft)
userTempLoc = os.path.join(selfscriptpath, 'UserTemplates')
if os.path.exists(userTempLoc) == True:
userTempLoclist = os.listdir(userTempLoc)
for ft in userTempLoclist:
if ft.endswith('.xml'):
allactualxmlFiles.append(ft)
ft = ft.replace('.xml', '')
allxmlFiles.append(ft)
return allxmlFiles
def returnjobFiles():
selfscriptpath = os.path.dirname(__file__)
jobfileList = os.listdir(selfscriptpath)
allactualjobfiles = []
alljobFiles = []
for ft in jobfileList:
if ft.endswith('.orjob'):
allactualjobfiles.append(ft)
ft = ft.replace('.orjob', '')
alljobFiles.append(ft)
return alljobFiles
def setPaths(xFname, values):
overExisting = True
rootPath = 'OptimizeRasters/Defaults/'
xfName2 = os.path.normpath(xFname)
doc = minidom.parse(xfName2)
for keyValueList in values:
aKey = keyValueList[0]
aVal = keyValueList[1]
pathtoreplace = rootPath + aKey
setXMLXPathValue(doc, pathtoreplace, aKey, aVal)
if 'UserTemplates' in xFname:
if overExisting == True:
fnToWrite = xfName2
else:
asuffix = returnDate()
fnToWrite = xfName2.replace('.xml', '_' + asuffix + '.xml')
else:
selfscriptpath = os.path.dirname(__file__)
userLoc = os.path.join(selfscriptpath, 'UserTemplates')
if os.path.exists(userLoc) == False:
os.mkdir(userLoc)
baseName = os.path.basename(xFname)
asuffix = returnDate()
baseName = baseName.replace('.xml', '_' + asuffix + '.xml')
fnToWrite = os.path.join(userLoc, baseName)
c = open(fnToWrite, "w")
c.write(doc.toprettyxml(encoding='UTF-8'))
c.close()
return fnToWrite
def returnPaths(xFname):
keyList = ['Mode', 'RasterFormatFilter', 'ExcludeFilter', 'IncludeSubdirectories', 'Compression', 'Quality', 'LERCPrecision', 'BuildPyramids', 'PyramidFactor', 'PyramidSampling', 'PyramidCompression', 'NoDataValue', 'BlockSize', 'Scale', 'KeepExtension', 'Threads', 'Op','GDAL_Translate_UserParameters']
xfName2 = os.path.normpath(xFname)
if (not os.path.exists(xfName2)):
return None
doc = minidom.parse(xfName2)
valueList = []
for key in keyList:
nodes = doc.getElementsByTagName(key)
for node in nodes:
if node.firstChild is not None:
aVal = node.firstChild.data
else:
aVal = ''
valueList.append([key, aVal])
return ([keyList, valueList])
def attchValues(toolcontrol, allValues):
keylist = allValues[0]
valList = allValues[1]
toolcontrol.value = valList
return
def returnTempFolder():
templiist = []
templiist.append('TMP')
templiist.append('TEMP')
for tt in templiist:
tempVal = os.getenv(tt)
if tempVal is not None:
return tempVal
break
def config_Init(parentfolder, filename):
if (not parentfolder or
not filename):
return None
global config
global awsfile
config = ConfigParser.RawConfigParser()
homedrive = os.getenv('HOMEDRIVE')
homepath = os.getenv('HOMEPATH')
homefolder = os.path.join(homedrive, homepath)
awsfolder = os.path.join(homefolder, parentfolder)
if (filename == '*.json'): # google cs filter
for r, d, f in os.walk(awsfolder):
for service in f:
config.add_section(os.path.join(r, service).replace('\\', '/'))
break
return config
awsfile = os.path.join(awsfolder, filename)
if os.path.exists(awsfile) == True:
print (awsfile)
try:
config.read(awsfile)
except:
pass
return config
else:
if os.path.exists(os.path.dirname(awsfile)) == False:
os.makedirs(os.path.dirname(awsfile))
mode = 'w+'
tmpFile = open(awsfile, mode)
with open(awsfile, mode) as tmpFIle:
tmpFIle.close
return config
def config_writeSections(configfileName, peAction, section, option1, value1, option2, value2, option3, value3):
peAction_ = peAction.lower()
if peAction_ == 'overwrite existing':
appConfig = config
appConfig.remove_section(section)
mode = 'w'
elif peAction_ == 'delete existing':
config.remove_section(section)
mode = 'w'
with open(configfileName, mode) as configfile:
config.write(configfile)
return True
else:
appConfig = ConfigParser.RawConfigParser()
mode = 'a'
# let's validate the credentials before writing out.
if (peAction_.startswith('overwrite') or # update existing or add new but ignore for del.
mode == 'a'):
try:
import OptimizeRasters
except Exception as e:
arcpy.AddError(str(e))
return False
storageType = OptimizeRasters.CCLOUD_AMAZON
if (option1 and
option1.lower().startswith('azure')):
storageType = OptimizeRasters.CCLOUD_AZURE
profileEditorUI = OptimizeRasters.ProfileEditorUI(section, storageType, value1, value2)
ret = profileEditorUI.validateCredentials()
if (not ret):
[arcpy.AddError(i) for i in profileEditorUI.errors]
return False
# ends
appConfig.add_section(section)
isIAMRole = section.lower().startswith('iamrole:')
if (not isIAMRole): # if not IAM role, write out the credential key pair
appConfig.set(section, option1, value1)
appConfig.set(section, option2, value2)
if (value3):
appConfig.set(section, option3, value3.strip())
with open(configfileName, mode) as configfile:
appConfig.write(configfile)
return True
def getAvailableBuckets(ctlProfileType, ctlProfileName):
try:
import OptimizeRasters
if (ctlProfileType.valueAsText):
inputSourceType = ctlProfileType.valueAsText.lower()
storageType = OptimizeRasters.Store.TypeAmazon
if (inputSourceType.startswith('local')):
return []
elif (inputSourceType.find('azure') != -1):
storageType = OptimizeRasters.Store.TypeAzure
elif (inputSourceType.find('google') != -1):
storageType = OptimizeRasters.Store.TypeGoogle
ORUI = OptimizeRasters.OptimizeRastersUI(ctlProfileName.value, storageType)
if (not ORUI):
raise Exception()
return ORUI.getAvailableBuckets()
except:
pass
return []
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [OptimizeRasters, ProfileEditor, ResumeJobs]
class ResumeJobs(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Resume Jobs"
self.description = ""
self.canRunInBackground = True
self.tool = 'ProfileEditor'
def getParameterInfo(self):
pendingJobs = arcpy.Parameter(
displayName="Pending Jobs",
name="pendingJobs",
datatype="GPString",
parameterType="Required",
direction="Input")
pendingJobs.filter.type = "ValueList"
pendingJobs.filter.list = returnjobFiles()
parameters = [pendingJobs]
return parameters
pass
def updateParameters(self, parameters):
# let's remove the entry if successful executed thru (def execute).
pendingJobs = parameters[0]
pendingJobs.filter.list = returnjobFiles()
parameters[0] = [pendingJobs]
# ends
def updateMessages(self, parameters):
pass
def isLicensed(parameters):
"""Set whether tool is licensed to execute."""
return True
def execute(self, parameters, messages):
CORJOB = '.orjob'
args = {}
aJob = parameters[0].valueAsText
if (not aJob.lower().endswith(CORJOB)):
aJob += CORJOB
template_path = os.path.realpath(__file__)
configFN = '{}/{}'.format(os.path.dirname(template_path), os.path.basename(aJob)).replace('\\', '/')
if (not os.path.exists(configFN)): # detect errors early.
arcpy.AddError('Err. OptimizeRasters job file ({}) is not found!'.format(configFN))
return False
args['input'] = configFN
# let's run (OptimizeRasters)
import OptimizeRasters
app = OptimizeRasters.Application(args)
if (not app.init()):
arcpy.AddError('Err. Unable to initialize (OptimizeRasters module)')
return False
app.postMessagesToArcGIS = True
return app.run()
# ends
class ProfileEditor(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Profile Editor"
self.description = ""
self.canRunInBackground = True
self.tool = 'ProfileEditor'
def getParameterInfo(self):
profileType = arcpy.Parameter(
displayName="Profile Type",
name="profileType",
datatype="GPString",
parameterType="Required",
direction="Output")
profileType.filter.type = "ValueList"
profileType.filter.list = ['Amazon S3', 'Microsoft Azure']
profileType.value = 'Amazon S3'
profileName = arcpy.Parameter(
displayName="Profile Name",
name="profileName",
datatype="GPString",
parameterType="Required",
direction="Input")
#profileName.value = 'or_public_in'
iAmRolePara = arcpy.Parameter(
displayName="IAM Role Profile",
name="iAmRolePara",
datatype="GPBoolean",
parameterType="Optional",
direction="Input")
accessKey = arcpy.Parameter(
displayName="Access/Account Key ID",
name="accessKey",
datatype="GPString",
parameterType="Required",
direction="Input")
secretAccessKey = arcpy.Parameter(
displayName="Secret Access/Account Key",
name="secretAccessKey",
datatype="GPString",
parameterType="Required",
direction="Input")
imRoleURL = arcpy.Parameter(
displayName="Endpoint URL",
name="imRoleURL",
datatype="GPString",
parameterType="Optional",
direction="Input")
action = arcpy.Parameter(
displayName="Editor Option",
name="action",
datatype="GPString",
parameterType="Optional",
direction="Input")
action.filter.type = "ValueList"
action.filter.list = ['Overwrite Existing', 'Delete Existing']
action.value = 'Overwrite Existing'
action.enabled = False
iAmRolePara.value = False
parameters = [profileType, profileName, iAmRolePara, accessKey, secretAccessKey, imRoleURL, action]
return parameters
def updateParameters(self, parameters):
isIAMRole = parameters[2].value
if parameters[0].altered == True:
pType = parameters[0].valueAsText
if pType == 'Amazon S3':
pFolder = AwsRoot
pfileName = 'credentials'
elif pType == 'Microsoft Azure':
pFolder = AzureRoot
pfileName = 'azure_credentials'
config_Init(pFolder, pfileName)
if parameters[1].altered == True:
pName = parameters[1].valueAsText
if (config.has_section('{}{}'.format('iamrole:' if isIAMRole else '', pName))):
parameters[6].enabled = True
else:
parameters[6].enabled = False
if parameters[6].enabled == True:
pass
if parameters[3].value is None:
parameters[3].value = 'None'
if parameters[4].value is None:
parameters[4].value = 'None'
else:
pass
if parameters[3].value == 'None':
parameters[3].value = ''
if parameters[4].value == 'None':
parameters[4].value = ''
parameters[3].enabled = not isIAMRole # access_key
parameters[4].enabled = not isIAMRole # secret_key
if isIAMRole == True:
if parameters[3].valueAsText == '' or parameters[3].value is None:
parameters[3].value = 'None'
if parameters[4].valueAsText == '' or parameters[4].value is None:
parameters[4].value = 'None'
def updateMessages(self, parameters):
if parameters[0].altered == True:
pType = parameters[0].valueAsText
if (pType != 'Amazon S3') and (pType != 'Microsoft Azure'):
parameters[0].setErrorMessage('Invalid Value. Pick from List only.')
return
else:
parameters[0].clearMessage()
if parameters[1].altered == True:
pType = parameters[0].valueAsText
pName = parameters[1].valueAsText
if (parameters[1] == True):
pName = 'iamrole:' + pName
if (config.has_section(pName)):
parameters[1].setWarningMessage('Profile name already exists. Select the appropriate action.')
else:
parameters[1].clearMessage()
def isLicensed(parameters):
"""Set whether tool is licensed to execute."""
return True
def execute(self, parameters, messages):
pType = parameters[0].valueAsText
homedrive = os.getenv('HOMEDRIVE')
homepath = os.getenv('HOMEPATH')
homefolder = os.path.join(homedrive, homepath)
if pType == 'Amazon S3':
pFolder = AwsRoot
pfileName = 'credentials'
option1 = 'aws_access_key_id'
option2 = 'aws_secret_access_key'
option3 = 'aws_endpoint_url'
elif pType == 'Microsoft Azure':
pFolder = AzureRoot
pfileName = 'azure_credentials'
option1 = 'azure_account_name'
option2 = 'azure_account_key'
option3 = 'azure_endpoint_url'
awsfolder = os.path.join(homefolder, pFolder)
#awsfile = os.path.join(awsfolder,pfileName)
pName = parameters[1].valueAsText
if parameters[6].enabled == False:
peAction = ''
else:
peAction = parameters[6].valueAsText
if parameters[2].value == False:
accessKeyID = parameters[3].valueAsText
accessSeceretKey = parameters[4].valueAsText
else:
pName = 'iamrole:' + pName
option1 = None
accessKeyID = ''
option2 = None
accessSeceretKey = ''
endPointURL = parameters[5].valueAsText
config_writeSections(awsfile, peAction, pName, option1, accessKeyID, option2, accessSeceretKey, option3, endPointURL)
class OptimizeRasters(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "OptimizeRasters"
self.description = ""
self.canRunInBackground = True
self.tool = 'ConvertFiles'
def getParameterInfo(self):
storageTypes = ['Local', 'Amazon S3', 'Microsoft Azure', 'Google Cloud'] # 'local' must be the first element.
optTemplates = arcpy.Parameter(
displayName="Configuration Files",
name="optTemplates",
datatype="GPString",
parameterType="Required",
direction="Input")
optTemplates.filter.type = "ValueList"
optTemplates.filter.list = returntemplatefiles()
inType = arcpy.Parameter(
displayName="Input Source",
name="inType",
datatype="GPString",
parameterType="Required",
direction="Input")
inType.filter.type = "ValueList"
inType.filter.list = storageTypes
inType.value = storageTypes[0]
inprofiles = arcpy.Parameter(
displayName="Input Profile",
name="inprofiles",
datatype="GPString",
parameterType="Required",
direction="Input")
inprofiles.filter.type = "ValueList"
inBucket = arcpy.Parameter(
displayName="Input Bucket/Container",
name="inBucket",
datatype="GPString",
parameterType="Required",
direction="Input")
inBucket.filter.type = "ValueList"
inPath = arcpy.Parameter(
displayName="Input Path",
name="inPath",
datatype=['DEFolder', 'GPString'],
parameterType="Required",
direction="Input")
intempFolder = arcpy.Parameter(
displayName="Input Temporary Folder",
name="intempFolder",
datatype="DEFolder",
parameterType="Optional",
direction="Input")
outType = arcpy.Parameter(
displayName=" Output Destination",
name="outType",
datatype="GPString",
parameterType="Required",
direction="Input")
outType.filter.type = "ValueList"
outType.filter.list = storageTypes
outType.value = storageTypes[0]
outprofiles = arcpy.Parameter(
displayName="Output Profile to Use",
name="outprofiles",
datatype="GPString",
parameterType="Required",
direction="Input")
outprofiles.filter.type = "ValueList"
outBucket = arcpy.Parameter(
displayName="Output Bucket/Container",
name="outBucket",
datatype="GPString",
parameterType="Required",
direction="Input")
outPath = arcpy.Parameter(
displayName="Output Path",
name="outPath",
datatype=['DEFolder', 'GPString'],
parameterType="Required",
direction="Input")
outtempFolder = arcpy.Parameter(
displayName="Output Temporary Folder",
name="outtempFolder",
datatype="DEFolder",
parameterType="Optional",
direction="Input")
cloneMRFFolder = arcpy.Parameter(
displayName="Raster Proxy Output Folder",
name="rasterProxyFolder",
datatype="DEFolder",
parameterType="Optional",
direction="Input")
cacheMRFFolder = arcpy.Parameter(
displayName="Cache Folder",
name="cacheMRFFolder",
datatype="DEFolder",
parameterType="Optional",
direction="Input")
editValue = arcpy.Parameter(
displayName="Edit Configuration Values",
name="editValue",
datatype="GPBoolean",
parameterType="Optional",
direction="Input")
editValue.category = 'Advanced'
configVals = arcpy.Parameter(
displayName='Configuration Values:',
name='configVals',
datatype='GPValueTable',
parameterType='Optional',
direction='Input')
configVals.columns = [['GPString', 'Parameter'], ['GPString', 'Value']]
configVals.enabled = False
configVals.category = 'Advanced'
parameters = [optTemplates, inType, inprofiles, inBucket, inPath, intempFolder, outType, outprofiles, outBucket, outPath, outtempFolder, cloneMRFFolder, cacheMRFFolder, editValue, configVals]
return parameters
def updateParameters(self, parameters):
configParams = parameters[0]
configParams.filter.list = returntemplatefiles()
if parameters[13].value == True:
parameters[14].enabled = True
else:
parameters[14].enabled = False
if parameters[0].altered == True:
if parameters[14].altered == False:
optTemplates = parameters[0].valueAsText
global templateinUse
templateinUse = optTemplates
template_path = os.path.realpath(__file__)
_CTEMPLATE_FOLDER = 'Templates'
configFN = os.path.join(os.path.join(os.path.dirname(template_path), _CTEMPLATE_FOLDER), optTemplates + '.xml')
if not os.path.exists(configFN):
_CTEMPLATE_FOLDER = 'UserTemplates'
configFN = os.path.join(os.path.join(os.path.dirname(template_path), _CTEMPLATE_FOLDER), optTemplates + '.xml')
allValues = returnPaths(configFN)
if (allValues):
attchValues(parameters[14], allValues)
else:
optTemplates = parameters[0].valueAsText
if templateinUse != optTemplates:
template_path = os.path.realpath(__file__)
_CTEMPLATE_FOLDER = 'Templates'
configFN = os.path.join(os.path.join(os.path.dirname(template_path), _CTEMPLATE_FOLDER), optTemplates + '.xml')
if not os.path.exists(configFN):
_CTEMPLATE_FOLDER = 'UserTemplates'
configFN = os.path.join(os.path.join(os.path.dirname(template_path), _CTEMPLATE_FOLDER), optTemplates + '.xml')
allValues = returnPaths(configFN)
if (allValues):
attchValues(parameters[14], allValues)
templateinUse = optTemplates
if parameters[1].altered == True:
if parameters[1].valueAsText == 'Local':
parameters[2].filter.list = []
parameters[3].filter.list = []
parameters[2].value = 'Profile'
parameters[2].enabled = False
parameters[3].enabled = False
else:
pFolder = pfileName = None
if parameters[1].valueAsText == 'Amazon S3':
pFolder = AwsRoot
pfileName = 'credentials'
parameters[2].enabled = True
parameters[3].enabled = True
elif parameters[1].valueAsText == 'Microsoft Azure':
pFolder = AzureRoot
pfileName = 'azure_credentials'
parameters[2].enabled = True
parameters[3].enabled = True
elif parameters[1].valueAsText == 'Google Cloud':
pFolder = GoogleRoot
pfileName = '*.json'
parameters[2].enabled = True
parameters[3].enabled = True
if parameters[3].value == 'Local':
parameters[3].value = ''
if parameters[2].value == 'Profile':
parameters[2].value = ''
if (pFolder):
p2Config = config_Init(pFolder, pfileName)
if (p2Config):
p2List = p2Config.sections()
parameters[2].filter.list = p2List
if parameters[2].altered == True:
# fetch the list of bucket names available for the selected input profile
availableBuckets = getAvailableBuckets(parameters[1], parameters[2])
if (availableBuckets):
parameters[3].filter.list = availableBuckets # 3 == bucket names
else:
if (parameters[1].value == 'Local'):
parameters[3].filter.list = [' ']
parameters[3].enabled = False
parameters[3].value = ' '
else:
parameters[3].filter.list = []
if (parameters[2].value is not None and
not parameters[2].value.lower().startswith('iamrole:') and
not parameters[2].value.lower().startswith('aws_publicbucket')):
if (not parameters[2].value.lower().endswith('public-buckets.json')):
parameters[3].value = ''
# ends
if parameters[6].altered == True:
if parameters[6].valueAsText == 'Local':
parameters[7].filter.list = []
parameters[8].filter.list = []
parameters[7].value = 'Profile'
parameters[8].value = 'Local'
parameters[7].enabled = False
parameters[8].enabled = False
else:
pFolder = pfileName = None
parameters[7].enabled = True
parameters[8].enabled = True
if parameters[6].valueAsText == 'Amazon S3':
pFolder = AwsRoot
pfileName = 'credentials'
parameters[7].enabled = True
parameters[8].enabled = True
elif parameters[6].valueAsText == 'Microsoft Azure':
pFolder = AzureRoot
pfileName = 'azure_credentials'
parameters[7].enabled = True
parameters[8].enabled = True
elif parameters[6].valueAsText == 'Google Cloud':
pFolder = GoogleRoot
pfileName = '*.json'
parameters[7].enabled = True
parameters[8].enabled = True
if parameters[8].value == 'Local':
parameters[8].value = ''
if parameters[7].value == 'Profile':
parameters[7].value = ''
if (pFolder):
p6Config = config_Init(pFolder, pfileName)
if (p6Config):
p6List = p6Config.sections()
parameters[7].filter.list = p6List
if parameters[7].altered == True:
# fetch the list of bucket names available for the selected output profile
availableBuckets = getAvailableBuckets(parameters[6], parameters[7])
if (availableBuckets):
parameters[8].filter.list = availableBuckets # 8 == bucket names
else:
if (parameters[6].value == 'Local'):
parameters[8].filter.list = [' ']
parameters[8].value = ' '
parameters[8].enabled = False
else:
parameters[8].filter.list = []
if (parameters[7].value is not None and
not parameters[7].value.lower().startswith('iamrole:')):
if (not parameters[7].value.lower().endswith('public-buckets.json')):
parameters[8].value = ''
# ends
if parameters[14].altered == True:
configValList = parameters[14].value
aVal = configValList[0][1].strip().lower()
op = configValList[len(configValList) - 1][1].strip().lower()
if (aVal == 'clonemrf' or aVal == 'cachingmrf' or aVal == 'rasterproxy'):
parameters[10].enabled = False
parameters[11].enabled = False
parameters[12].enabled = True
else:
parameters[10].enabled = True
parameters[11].enabled = True
parameters[12].enabled = True
if (op == 'copyonly'):
parameters[11].enabled = False
parameters[12].enabled = False
def updateMessages(self, parameters):
storageTypes = ('Local', 'Amazon S3', 'Microsoft Azure', 'Google Cloud') # 'local' must be the first element.
errMessageListOnly = 'Invalid Value. Pick from List only.'
if parameters[1].altered == True:
pType = parameters[1].valueAsText
if (pType not in storageTypes):
parameters[1].setErrorMessage(errMessageListOnly)
else:
parameters[1].clearMessage()
if parameters[6].altered == True:
pType = parameters[6].valueAsText
if (pType not in storageTypes):
parameters[6].setErrorMessage(errMessageListOnly)
else:
parameters[6].clearMessage()
if (pType in storageTypes[1:]): # skip the first element/local.
if parameters[10].altered == False:
if parameters[10].enabled == True:
parameters[10].SetWarningMessage('For cloud storage output, a temporary output location is required.')
else:
if parameters[10].valueAsText != '':
parameters[10].clearMessage()
def isLicensed(parameters):
"""Set whether tool is licensed to execute."""
return True
def execute(self, parameters, messages):
args = {}
optTemplates = parameters[0].valueAsText
template_path = os.path.realpath(__file__)
_CTEMPLATE_FOLDER = 'Templates'
configFN = os.path.join(os.path.join(os.path.dirname(template_path), _CTEMPLATE_FOLDER), optTemplates + '.xml')
if os.path.exists(configFN) == False:
_CTEMPLATE_FOLDER = 'UserTemplates'
configFN = os.path.join(os.path.join(os.path.dirname(template_path), _CTEMPLATE_FOLDER), optTemplates + '.xml')
inType = parameters[1].valueAsText
inprofiles = parameters[2].valueAsText
inBucket = parameters[3].valueAsText
inPath = parameters[4].valueAsText
intempFolder = parameters[5].valueAsText
outType = parameters[6].valueAsText
outprofiles = parameters[7].valueAsText
outBucket = parameters[8].valueAsText
outPath = parameters[9].valueAsText
outtempFolder = parameters[10].valueAsText
cloneMRFFolder = parameters[11].valueAsText
cacheOutputFolder = parameters[12].valueAsText
if parameters[13].enabled == True:
if parameters[13].value == True:
editedValues = parameters[14].value
configFN = setPaths(configFN, editedValues)
args['config'] = configFN
args['output'] = outPath
args['tempinput'] = intempFolder
if (outtempFolder):
args['tempoutput'] = outtempFolder # used only if -cloudupload=true
args['input'] = inPath
if inType == 'Local':
pass
else:
args['clouddownload'] = 'true'
args['inputbucket'] = inBucket # case-sensitive
if (not inprofiles.lower().startswith('aws_publicbucket')):
args['inputprofile'] = inprofiles
if inType == 'Amazon S3':
args['clouddownloadtype'] = 'amazon'
elif inType == 'Microsoft Azure':
args['clouddownloadtype'] = 'azure'
elif inType == 'Google Cloud':
args['clouddownloadtype'] = 'google'
if outType == 'Local':
pass
else:
args['cloudupload'] = 'true'
args['outputprofile'] = outprofiles
clouduploadtype_ = 'amazon'
if (outType == 'Microsoft Azure'):
clouduploadtype_ = 'azure'
elif (outType == 'Google Cloud'):
clouduploadtype_ = 'google'
args['clouduploadtype'] = clouduploadtype_
args['outputbucket'] = outBucket
if cacheOutputFolder is not None:
args['cache'] = cacheOutputFolder
if cloneMRFFolder is not None:
args['rasterproxypath'] = cloneMRFFolder
# let's run (OptimizeRasters)
import OptimizeRasters
app = OptimizeRasters.Application(args)
if (not app.init()):
arcpy.AddError('Err. Unable to initialize (OptimizeRasters module)')
return False
app.postMessagesToArcGIS = True
return app.run()
# ends