本文整理汇总了Python中SagaUtils类的典型用法代码示例。如果您正苦于以下问题:Python SagaUtils类的具体用法?Python SagaUtils怎么用?Python SagaUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SagaUtils类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: processAlgorithm
def processAlgorithm(self, progress):
# TODO: check correct num of bands
input = self.getParameterValue(SplitRGBBands.INPUT)
temp = getTempFilename(None).replace('.', '')
basename = os.path.basename(temp)
validChars = \
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
safeBasename = ''.join(c for c in basename if c in validChars)
temp = os.path.join(os.path.dirname(temp), safeBasename)
r = self.getOutputValue(SplitRGBBands.R)
g = self.getOutputValue(SplitRGBBands.G)
b = self.getOutputValue(SplitRGBBands.B)
commands = []
version = SagaUtils.getSagaInstalledVersion(True)
trailing = "000" if version != "2.1.4" else ""
lib = "" if isWindows() else "lib"
commands.append('%sio_gdal 0 -GRIDS "%s" -FILES "%s"' % (lib, temp, input)
+ '"')
commands.append('%sio_gdal 1 -GRIDS "%s_%s1.sgrd" -FORMAT 1 -TYPE 0 -FILE "%s"' %(lib, temp, trailing, r)
)
commands.append('%sio_gdal 1 -GRIDS "%s_%s2.sgrd" -FORMAT 1 -TYPE 0 -FILE "%s"' %(lib, temp, trailing, g)
)
commands.append('%sio_gdal 1 -GRIDS "%s_%s3.sgrd" -FORMAT 1 -TYPE 0 -FILE "%s"' %(lib, temp, trailing, b)
)
SagaUtils.createSagaBatchJobFileFromSagaCommands(commands)
SagaUtils.executeSaga(progress)
示例2: _loadAlgorithms
def _loadAlgorithms(self):
self.algs = []
version = SagaUtils.getSagaInstalledVersion(True)
if version is None:
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
self.tr("Problem with SAGA installation: SAGA was not found or is not correctly installed"),
)
return
if version not in self.supportedVersions:
lastVersion = sorted(self.supportedVersions.keys())[-1]
if version > lastVersion:
version = lastVersion
else:
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
self.tr("Problem with SAGA installation: installed SAGA version (%s) is not supported" % version),
)
return
folder = SagaUtils.sagaDescriptionPath()
folder = os.path.join(folder, self.supportedVersions[version][0])
for descriptionFile in os.listdir(folder):
if descriptionFile.endswith("txt"):
f = os.path.join(folder, descriptionFile)
self._loadAlgorithm(f, version)
self.algs.append(SplitRGBBands())
示例3: unload
def unload(self):
AlgorithmProvider.unload(self)
if (isWindows() or isMac()) and SagaUtils.findSagaFolder() is None:
ProcessingConfig.removeSetting(SagaUtils.SAGA_FOLDER)
ProcessingConfig.removeSetting(SagaUtils.SAGA_LOG_CONSOLE)
ProcessingConfig.removeSetting(SagaUtils.SAGA_LOG_COMMANDS)
示例4: _loadAlgorithms
def _loadAlgorithms(self):
self.algs = []
version = SagaUtils.getSagaInstalledVersion(True)
if version is None:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('Problem with SAGA installation: SAGA was not found or is not correctly installed'))
return
if version not in self.supportedVersions:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('Problem with SAGA installation: installed SAGA version (%s) is not supported' % version))
return
folder = SagaUtils.sagaDescriptionPath()
folder = os.path.join(folder, self.supportedVersions[SagaUtils.getSagaInstalledVersion()][0])
for descriptionFile in os.listdir(folder):
if descriptionFile.endswith('txt'):
f = os.path.join(folder, descriptionFile)
self._loadAlgorithm(f)
self.algs.append(SplitRGBBands())
示例5: _loadAlgorithm
def _loadAlgorithm(self, descriptionFile):
try:
alg = self.supportedVersions[SagaUtils.getSagaInstalledVersion()][1](descriptionFile)
if alg.name.strip() != '':
self.algs.append(alg)
else:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('Could not open SAGA algorithm: %s' % descriptionFile))
except Exception, e:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('Could not open SAGA algorithm: %s\n%s' % (descriptionFile, str(e))))
示例6: processAlgorithm
def processAlgorithm(self, progress):
# TODO: check correct num of bands
input = self.getParameterValue(SplitRGBBands.INPUT)
temp = getTempFilename(None).replace('.', '')
basename = os.path.basename(temp)
validChars = \
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
safeBasename = ''.join(c for c in basename if c in validChars)
temp = os.path.join(os.path.dirname(temp), safeBasename)
r = self.getOutputValue(SplitRGBBands.R)
g = self.getOutputValue(SplitRGBBands.G)
b = self.getOutputValue(SplitRGBBands.B)
commands = []
if isWindows():
commands.append('io_gdal 0 -GRIDS "' + temp + '" -FILES "' + input
+ '"')
commands.append('io_gdal 1 -GRIDS "' + temp
+ '_0001.sgrd" -FORMAT 1 -TYPE 0 -FILE "' + r + '"'
)
commands.append('io_gdal 1 -GRIDS "' + temp
+ '_0002.sgrd" -FORMAT 1 -TYPE 0 -FILE "' + g + '"'
)
commands.append('io_gdal 1 -GRIDS "' + temp
+ '_0003.sgrd" -FORMAT 1 -TYPE 0 -FILE "' + b + '"'
)
else:
commands.append('libio_gdal 0 -GRIDS "' + temp + '" -FILES "'
+ input + '"')
commands.append('libio_gdal 1 -GRIDS "' + temp
+ '_0001.sgrd" -FORMAT 1 -TYPE 0 -FILE "' + r + '"'
)
commands.append('libio_gdal 1 -GRIDS "' + temp
+ '_0002.sgrd" -FORMAT 1 -TYPE 0 -FILE "' + g + '"'
)
commands.append('libio_gdal 1 -GRIDS "' + temp
+ '_0003.sgrd" -FORMAT 1 -TYPE 0 -FILE "' + b + '"'
)
SagaUtils.createSagaBatchJobFileFromSagaCommands(commands)
SagaUtils.executeSaga(progress)
示例7: initializeSettings
def initializeSettings(self):
if (isWindows() or isMac()) and SagaUtils.findSagaFolder() is None:
ProcessingConfig.addSetting(Setting("SAGA",
SagaUtils.SAGA_FOLDER, self.tr('SAGA folder'),
'',
valuetype=Setting.FOLDER))
ProcessingConfig.addSetting(Setting("SAGA",
SagaUtils.SAGA_IMPORT_EXPORT_OPTIMIZATION,
self.tr('Enable SAGA Import/Export optimizations'), False))
ProcessingConfig.addSetting(Setting("SAGA",
SagaUtils.SAGA_LOG_COMMANDS,
self.tr('Log execution commands'), True))
ProcessingConfig.addSetting(Setting("SAGA",
SagaUtils.SAGA_LOG_CONSOLE,
self.tr('Log console output'), True))
ProcessingConfig.settingIcons["SAGA"] = self.getIcon()
ProcessingConfig.addSetting(Setting("SAGA", "ACTIVATE_SAGA",
self.tr('Activate'), self.activate))
示例8: processAlgorithm
#.........这里部分代码省略.........
elif not layerfile.endswith('sgrd'):
exportCommand = self.exportRasterLayer(layerfile)
if exportCommand is not None:
commands.append(exportCommand)
param.value = ";".join(layers)
elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY:
for layerfile in layers:
layer = dataobjects.getObjectFromUri(layerfile, False)
if layer:
filename = dataobjects.exportVectorLayer(layer)
self.exportedLayers[layerfile] = filename
elif not layerfile.endswith('shp'):
raise GeoAlgorithmExecutionException(
self.tr('Unsupported file format'))
# 2: Set parameters and outputs
command = self.undecoratedGroup + ' "' + self.cmdname + '"'
if self.hardcodedStrings:
for s in self.hardcodedStrings:
command += ' ' + s
for param in self.parameters:
if param.value is None:
continue
if isinstance(param, (ParameterRaster, ParameterVector,
ParameterTable)):
value = param.value
if value in self.exportedLayers.keys():
command += ' -' + param.name + ' "' \
+ self.exportedLayers[value] + '"'
else:
command += ' -' + param.name + ' "' + value + '"'
elif isinstance(param, ParameterMultipleInput):
s = param.value
for layer in self.exportedLayers.keys():
s = s.replace(layer, self.exportedLayers[layer])
command += ' -' + param.name + ' "' + s + '"'
elif isinstance(param, ParameterBoolean):
if param.value:
command += ' -' + param.name
elif isinstance(param, ParameterFixedTable):
tempTableFile = getTempFilename('txt')
f = open(tempTableFile, 'w')
f.write('\t'.join([col for col in param.cols]) + '\n')
values = param.value.split(',')
for i in range(0, len(values), 3):
s = values[i] + '\t' + values[i + 1] + '\t' + values[i
+ 2] + '\n'
f.write(s)
f.close()
command += ' -' + param.name + ' "' + tempTableFile + '"'
elif isinstance(param, ParameterExtent):
# 'We have to substract/add half cell size, since SAGA is
# center based, not corner based
halfcell = self.getOutputCellsize() / 2
offset = [halfcell, -halfcell, halfcell, -halfcell]
values = param.value.split(',')
for i in range(4):
command += ' -' + self.extentParamNames[i] + ' ' \
+ unicode(float(values[i]) + offset[i])
elif isinstance(param, (ParameterNumber, ParameterSelection)):
command += ' -' + param.name + ' ' + unicode(param.value)
else:
command += ' -' + param.name + ' "' + unicode(param.value) + '"'
for out in self.outputs:
command += ' -' + out.name + ' "' + out.getCompatibleFileName(self) + '"'
commands.append(command)
# special treatment for RGB algorithm
#TODO: improve this and put this code somewhere else
for out in self.outputs:
if isinstance(out, OutputRaster):
filename = out.getCompatibleFileName(self)
filename2 = filename + '.sgrd'
if self.cmdname == 'RGB Composite':
commands.append('io_grid_image 0 -IS_RGB -GRID:"' + filename2
+ '" -FILE:"' + filename
+ '"')
# 3: Run SAGA
commands = self.editCommands(commands)
SagaUtils.createSagaBatchJobFileFromSagaCommands(commands)
loglines = []
loglines.append(self.tr('SAGA execution commands'))
for line in commands:
progress.setCommand(line)
loglines.append(line)
if ProcessingConfig.getSetting(SagaUtils.SAGA_LOG_COMMANDS):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
SagaUtils.executeSaga(progress)
if self.crs is not None:
for out in self.outputs:
if isinstance(out, (OutputVector, OutputRaster)):
prjFile = os.path.splitext(out.getCompatibleFileName(self))[0] + ".prj"
with open(prjFile, "w") as f:
f.write(self.crs.toWkt())
示例9: processAlgorithm
#.........这里部分代码省略.........
for param in self.parameters:
if param.value is None:
continue
if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable)):
value = param.value
if value in self.exportedLayers.keys():
command += " -" + param.name + ' "' + self.exportedLayers[value] + '"'
else:
command += " -" + param.name + ' "' + value + '"'
elif isinstance(param, ParameterMultipleInput):
s = param.value
for layer in self.exportedLayers.keys():
s = s.replace(layer, self.exportedLayers[layer])
command += " -" + param.name + ' "' + s + '"'
elif isinstance(param, ParameterBoolean):
if param.value:
command += " -" + param.name
elif isinstance(param, ParameterFixedTable):
tempTableFile = getTempFilename("txt")
f = open(tempTableFile, "w")
f.write("\t".join([col for col in param.cols]) + "\n")
values = param.value.split(",")
for i in range(0, len(values), 3):
s = values[i] + "\t" + values[i + 1] + "\t" + values[i + 2] + "\n"
f.write(s)
f.close()
command += " -" + param.name + ' "' + tempTableFile + '"'
elif isinstance(param, ParameterExtent):
# 'We have to substract/add half cell size, since SAGA is
# center based, not corner based
halfcell = self.getOutputCellsize() / 2
offset = [halfcell, -halfcell, halfcell, -halfcell]
values = param.value.split(",")
for i in range(4):
command += " -" + self.extentParamNames[i] + " " + str(float(values[i]) + offset[i])
elif isinstance(param, (ParameterNumber, ParameterSelection)):
command += " -" + param.name + " " + str(param.value)
else:
command += " -" + param.name + ' "' + str(param.value) + '"'
for out in self.outputs:
if isinstance(out, OutputRaster):
filename = out.getCompatibleFileName(self)
filename += ".sgrd"
command += " -" + out.name + ' "' + filename + '"'
if isinstance(out, OutputVector):
filename = out.getCompatibleFileName(self)
command += " -" + out.name + ' "' + filename + '"'
if isinstance(out, OutputTable):
filename = out.getCompatibleFileName(self)
command += " -" + out.name + ' "' + filename + '"'
commands.append(command)
# 3: Export resulting raster layers
# optim = ProcessingConfig.getSetting(SagaUtils.SAGA_IMPORT_EXPORT_OPTIMIZATION)
for out in self.outputs:
if isinstance(out, OutputRaster):
filename = out.getCompatibleFileName(self)
filename2 = filename + ".sgrd"
formatIndex = 4 if isWindows() else 1
sessionExportedLayers[filename] = filename2
# Do not export is the output is not a final output
# of the model
# dontExport = True
# if self.model is not None and optim:
# for subalg in self.model.algOutputs:
# if out.name in subalg:
# if subalg[out.name] is not None:
# dontExport = False
# break
# if dontExport:
# continue
if self.cmdname == "RGB Composite":
commands.append('io_grid_image 0 -IS_RGB -GRID:"' + filename2 + '" -FILE:"' + filename + '"')
else:
commands.append(
'io_gdal 1 -GRIDS "'
+ filename2
+ '" -FORMAT '
+ str(formatIndex)
+ ' -TYPE 0 -FILE "'
+ filename
+ '"'
)
# 4: Run SAGA
commands = self.editCommands(commands)
SagaUtils.createSagaBatchJobFileFromSagaCommands(commands)
loglines = []
loglines.append(self.tr("SAGA execution commands"))
for line in commands:
progress.setCommand(line)
loglines.append(line)
if ProcessingConfig.getSetting(SagaUtils.SAGA_LOG_COMMANDS):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
SagaUtils.executeSaga(progress)
示例10: getDescription
def getDescription(self):
version = SagaUtils.getSagaInstalledVersion()
return 'SAGA (%s)' % version if version is not None else 'SAGA'