2014-07-14 14:19:09 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
Parameters.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2015-06-22 20:04:08 +02:00
|
|
|
|
2014-07-14 14:19:09 +02:00
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
import sys
|
2016-09-07 14:30:20 +02:00
|
|
|
|
2017-11-27 16:03:14 +02:00
|
|
|
from qgis.core import (QgsRasterLayer,
|
|
|
|
QgsVectorLayer,
|
|
|
|
QgsMapLayer,
|
|
|
|
QgsCoordinateReferenceSystem,
|
2017-05-17 07:50:01 +10:00
|
|
|
QgsExpression,
|
2017-04-03 15:41:32 +10:00
|
|
|
QgsProject,
|
2017-05-17 07:41:15 +10:00
|
|
|
QgsRectangle,
|
2017-05-16 16:36:00 +10:00
|
|
|
QgsVectorFileWriter,
|
2017-10-22 10:25:09 +02:00
|
|
|
QgsProcessing,
|
2017-11-27 16:03:14 +02:00
|
|
|
QgsProcessingUtils,
|
2017-06-26 15:48:21 +10:00
|
|
|
QgsProcessingParameters,
|
2017-09-03 17:42:13 +02:00
|
|
|
QgsProcessingParameterDefinition,
|
|
|
|
QgsProcessingParameterRasterLayer,
|
|
|
|
QgsProcessingParameterVectorLayer,
|
|
|
|
QgsProcessingParameterBoolean,
|
2017-09-05 18:14:26 +02:00
|
|
|
QgsProcessingParameterCrs,
|
2017-09-04 16:31:31 +02:00
|
|
|
QgsProcessingParameterRange,
|
2017-09-05 18:14:26 +02:00
|
|
|
QgsProcessingParameterPoint,
|
2017-09-03 17:42:13 +02:00
|
|
|
QgsProcessingParameterEnum,
|
2017-09-05 18:14:26 +02:00
|
|
|
QgsProcessingParameterExtent,
|
2017-09-03 17:42:13 +02:00
|
|
|
QgsProcessingParameterMatrix,
|
|
|
|
QgsProcessingParameterFile,
|
|
|
|
QgsProcessingParameterField,
|
2017-10-22 10:25:09 +02:00
|
|
|
QgsProcessingParameterVectorDestination,
|
2017-09-07 18:47:37 +02:00
|
|
|
QgsProcessingParameterFileDestination,
|
2017-09-14 16:22:43 +02:00
|
|
|
QgsProcessingParameterFolderDestination,
|
2017-11-09 20:15:30 +02:00
|
|
|
QgsProcessingParameterRasterDestination,
|
2017-10-22 10:25:09 +02:00
|
|
|
QgsProcessingParameterString,
|
2017-09-03 17:42:13 +02:00
|
|
|
QgsProcessingParameterMultipleLayers,
|
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterNumber)
|
2017-09-14 16:22:43 +02:00
|
|
|
|
2016-09-19 11:52:31 +03:00
|
|
|
|
2016-09-05 12:59:21 +02:00
|
|
|
def getParameterFromString(s):
|
2016-09-07 14:30:20 +02:00
|
|
|
# Try the parameter definitions used in description files
|
2017-09-03 17:42:13 +02:00
|
|
|
if '|' in s and (s.startswith("QgsProcessingParameter") or s.startswith("*QgsProcessingParameter") or s.startswith('Parameter') or s.startswith('*Parameter')):
|
2016-09-08 10:30:30 +02:00
|
|
|
isAdvanced = False
|
|
|
|
if s.startswith("*"):
|
|
|
|
s = s[1:]
|
|
|
|
isAdvanced = True
|
2016-09-05 12:59:21 +02:00
|
|
|
tokens = s.split("|")
|
2016-09-27 19:51:06 +02:00
|
|
|
params = [t if str(t) != str(None) else None for t in tokens[1:]]
|
2017-09-03 17:42:13 +02:00
|
|
|
|
|
|
|
if True:
|
2016-09-12 06:17:23 +02:00
|
|
|
clazz = getattr(sys.modules[__name__], tokens[0])
|
2017-09-03 17:42:13 +02:00
|
|
|
# convert to correct type
|
|
|
|
if clazz == QgsProcessingParameterRasterLayer:
|
|
|
|
if len(params) > 3:
|
|
|
|
params[3] = True if params[3].lower() == 'true' else False
|
|
|
|
elif clazz == QgsProcessingParameterVectorLayer:
|
|
|
|
if len(params) > 2:
|
|
|
|
params[2] = [int(p) for p in params[2].split(';')]
|
|
|
|
if len(params) > 4:
|
|
|
|
params[4] = True if params[4].lower() == 'true' else False
|
|
|
|
elif clazz == QgsProcessingParameterBoolean:
|
|
|
|
if len(params) > 2:
|
|
|
|
params[2] = True if params[2].lower() == 'true' else False
|
|
|
|
if len(params) > 3:
|
|
|
|
params[3] = True if params[3].lower() == 'true' else False
|
2017-09-05 18:14:26 +02:00
|
|
|
elif clazz == QgsProcessingParameterPoint:
|
|
|
|
if len(params) > 3:
|
|
|
|
params[3] = True if params[3].lower() == 'true' else False
|
|
|
|
elif clazz == QgsProcessingParameterCrs:
|
|
|
|
if len(params) > 3:
|
|
|
|
params[3] = True if params[3].lower() == 'true' else False
|
2017-09-04 16:31:31 +02:00
|
|
|
elif clazz == QgsProcessingParameterRange:
|
|
|
|
if len(params) > 2:
|
2018-02-08 17:22:20 +02:00
|
|
|
try:
|
|
|
|
params[2] = int(params[2])
|
|
|
|
except:
|
|
|
|
params[2] = getattr(QgsProcessingParameterNumber, params[2].split(".")[1])
|
2017-09-04 16:31:31 +02:00
|
|
|
if len(params) > 4:
|
|
|
|
params[4] = True if params[4].lower() == 'true' else False
|
2017-09-05 18:14:26 +02:00
|
|
|
elif clazz == QgsProcessingParameterExtent:
|
|
|
|
if len(params) > 3:
|
|
|
|
params[3] = True if params[3].lower() == 'true' else False
|
2017-09-03 17:42:13 +02:00
|
|
|
elif clazz == QgsProcessingParameterEnum:
|
|
|
|
if len(params) > 2:
|
|
|
|
params[2] = params[2].split(';')
|
|
|
|
if len(params) > 3:
|
|
|
|
params[3] = True if params[3].lower() == 'true' else False
|
|
|
|
if len(params) > 4:
|
2017-11-04 18:34:25 +01:00
|
|
|
# For multiple values; default value is a list of int
|
|
|
|
if params[3] == True:
|
2017-12-03 19:30:33 +01:00
|
|
|
params[4] = [int(v) for v in params[4].split(',')] if params[4] is not None else None
|
2017-11-04 18:34:25 +01:00
|
|
|
else:
|
2017-12-03 19:30:33 +01:00
|
|
|
params[4] = int(params[4]) if params[4] is not None else None
|
2017-09-03 17:42:13 +02:00
|
|
|
if len(params) > 5:
|
|
|
|
params[5] = True if params[5].lower() == 'true' else False
|
|
|
|
elif clazz == QgsProcessingParameterFeatureSource:
|
|
|
|
if len(params) > 2:
|
2018-02-11 14:05:09 +02:00
|
|
|
try:
|
|
|
|
params[2] = [int(p) for p in params[2].split(';')]
|
|
|
|
except:
|
|
|
|
params[2] = [getattr(QgsProcessing, p.split(".")[1]) for p in params[2].split(';')]
|
2017-09-03 17:42:13 +02:00
|
|
|
if len(params) > 4:
|
|
|
|
params[4] = True if params[4].lower() == 'true' else False
|
|
|
|
elif clazz == QgsProcessingParameterMultipleLayers:
|
|
|
|
if len(params) > 2:
|
2018-02-08 17:22:20 +02:00
|
|
|
try:
|
|
|
|
params[2] = int(params[2])
|
|
|
|
except:
|
|
|
|
params[2] = getattr(QgsProcessing, params[2].split(".")[1])
|
2017-09-03 17:42:13 +02:00
|
|
|
if len(params) > 4:
|
|
|
|
params[4] = True if params[4].lower() == 'true' else False
|
|
|
|
elif clazz == QgsProcessingParameterMatrix:
|
|
|
|
if len(params) > 2:
|
|
|
|
params[2] = int(params[2])
|
|
|
|
if len(params) > 3:
|
|
|
|
params[3] = True if params[3].lower() == 'true' else False
|
|
|
|
if len(params) > 4:
|
|
|
|
params[4] = params[4].split(';')
|
2018-02-11 14:05:09 +02:00
|
|
|
if len(params) > 6:
|
|
|
|
params[6] = True if params[6].lower() == 'true' else False
|
2017-09-03 17:42:13 +02:00
|
|
|
elif clazz == QgsProcessingParameterField:
|
|
|
|
if len(params) > 4:
|
2018-02-11 14:05:09 +02:00
|
|
|
try:
|
|
|
|
params[4] = int(params[4])
|
|
|
|
except:
|
|
|
|
params[4] = getattr(QgsProcessingParameterField, params[4].split(".")[1])
|
2017-09-03 17:42:13 +02:00
|
|
|
if len(params) > 5:
|
|
|
|
params[5] = True if params[5].lower() == 'true' else False
|
|
|
|
if len(params) > 6:
|
|
|
|
params[6] = True if params[6].lower() == 'true' else False
|
|
|
|
elif clazz == QgsProcessingParameterFile:
|
|
|
|
if len(params) > 2:
|
2018-02-08 17:22:20 +02:00
|
|
|
try:
|
|
|
|
params[2] = int(params[2])
|
|
|
|
except:
|
|
|
|
params[2] = getattr(QgsProcessingParameterFile, params[2].split(".")[1])
|
2017-09-03 17:42:13 +02:00
|
|
|
if len(params) > 5:
|
|
|
|
params[5] = True if params[5].lower() == 'true' else False
|
|
|
|
elif clazz == QgsProcessingParameterNumber:
|
|
|
|
if len(params) > 2:
|
2018-02-08 17:22:20 +02:00
|
|
|
try:
|
|
|
|
params[2] = int(params[2])
|
|
|
|
except:
|
|
|
|
params[2] = getattr(QgsProcessingParameterNumber, params[2].split(".")[1])
|
2017-09-03 17:42:13 +02:00
|
|
|
if len(params) > 3:
|
2017-12-01 18:18:39 +10:00
|
|
|
params[3] = float(params[3].strip()) if params[3] is not None else None
|
2017-09-03 17:42:13 +02:00
|
|
|
if len(params) > 4:
|
|
|
|
params[4] = True if params[4].lower() == 'true' else False
|
|
|
|
if len(params) > 5:
|
|
|
|
params[5] = float(params[5].strip()) if params[5] is not None else -sys.float_info.max + 1
|
|
|
|
if len(params) > 6:
|
|
|
|
params[6] = float(params[6].strip()) if params[6] is not None else sys.float_info.max - 1
|
|
|
|
elif clazz == QgsProcessingParameterString:
|
|
|
|
if len(params) > 3:
|
|
|
|
params[3] = True if params[3].lower() == 'true' else False
|
|
|
|
if len(params) > 4:
|
|
|
|
params[4] = True if params[4].lower() == 'true' else False
|
2017-09-07 18:47:37 +02:00
|
|
|
elif clazz == QgsProcessingParameterFileDestination:
|
|
|
|
if len(params) > 4:
|
|
|
|
params[4] = True if params[4].lower() == 'true' else False
|
2017-10-25 21:29:28 +02:00
|
|
|
elif clazz == QgsProcessingParameterFolderDestination:
|
|
|
|
if len(params) > 3:
|
|
|
|
params[3] = True if params[3].lower() == 'true' else False
|
2017-11-04 18:34:25 +01:00
|
|
|
elif clazz == QgsProcessingParameterRasterDestination:
|
|
|
|
if len(params) > 3:
|
|
|
|
params[3] = True if params[3].lower() == 'true' else False
|
2017-10-22 10:25:09 +02:00
|
|
|
elif clazz == QgsProcessingParameterVectorDestination:
|
|
|
|
if len(params) > 2:
|
2018-02-08 17:22:20 +02:00
|
|
|
try:
|
|
|
|
params[2] = int(params[2])
|
|
|
|
except:
|
|
|
|
params[2] = getattr(QgsProcessing, params[2].split(".")[1])
|
2017-10-22 10:25:09 +02:00
|
|
|
if len(params) > 4:
|
|
|
|
params[4] = True if params[4].lower() == 'true' else False
|
2017-09-07 18:47:37 +02:00
|
|
|
|
2016-09-12 06:17:23 +02:00
|
|
|
param = clazz(*params)
|
2017-05-16 16:36:00 +10:00
|
|
|
if isAdvanced:
|
|
|
|
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
2017-09-07 18:47:37 +02:00
|
|
|
|
2016-09-15 12:28:03 +02:00
|
|
|
return param
|
2017-09-03 17:42:13 +02:00
|
|
|
else:
|
2016-09-12 06:17:23 +02:00
|
|
|
return None
|
2016-09-07 14:30:20 +02:00
|
|
|
else: # try script syntax
|
2017-06-26 15:48:21 +10:00
|
|
|
|
|
|
|
# try native method
|
|
|
|
param = QgsProcessingParameters.parameterFromScriptCode(s)
|
|
|
|
if param:
|
|
|
|
return param
|