parameters.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. """
  2. ***************************************************************************
  3. Parameters.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. import sys
  21. from qgis.core import (QgsRasterLayer,
  22. QgsVectorLayer,
  23. QgsMapLayer,
  24. QgsCoordinateReferenceSystem,
  25. QgsExpression,
  26. QgsProject,
  27. QgsRectangle,
  28. QgsWkbTypes,
  29. QgsVectorFileWriter,
  30. QgsProcessing,
  31. QgsProcessingUtils,
  32. QgsProcessingParameters,
  33. QgsProcessingParameterDefinition,
  34. QgsProcessingParameterRasterLayer,
  35. QgsProcessingParameterVectorLayer,
  36. QgsProcessingParameterBand,
  37. QgsProcessingParameterBoolean,
  38. QgsProcessingParameterCrs,
  39. QgsProcessingParameterRange,
  40. QgsProcessingParameterPoint,
  41. QgsProcessingParameterGeometry,
  42. QgsProcessingParameterEnum,
  43. QgsProcessingParameterExtent,
  44. QgsProcessingParameterExpression,
  45. QgsProcessingParameterMatrix,
  46. QgsProcessingParameterFile,
  47. QgsProcessingParameterField,
  48. QgsProcessingParameterVectorDestination,
  49. QgsProcessingParameterFileDestination,
  50. QgsProcessingParameterFolderDestination,
  51. QgsProcessingParameterRasterDestination,
  52. QgsProcessingParameterPointCloudDestination,
  53. QgsProcessingParameterString,
  54. QgsProcessingParameterMapLayer,
  55. QgsProcessingParameterMultipleLayers,
  56. QgsProcessingParameterFeatureSource,
  57. QgsProcessingParameterNumber,
  58. QgsProcessingParameterColor,
  59. QgsProcessingParameterPointCloudLayer,
  60. QgsProcessingParameterAnnotationLayer)
  61. from qgis.PyQt.QtCore import QCoreApplication
  62. PARAMETER_NUMBER = 'number'
  63. PARAMETER_DISTANCE = 'distance'
  64. PARAMETER_SCALE = 'scale'
  65. PARAMETER_RASTER = 'raster'
  66. PARAMETER_TABLE = 'vector'
  67. PARAMETER_VECTOR = 'source'
  68. PARAMETER_STRING = 'string'
  69. PARAMETER_EXPRESSION = 'expression'
  70. PARAMETER_BOOLEAN = 'boolean'
  71. PARAMETER_TABLE_FIELD = 'field'
  72. PARAMETER_EXTENT = 'extent'
  73. PARAMETER_FILE = 'file'
  74. PARAMETER_POINT = 'point'
  75. PARAMETER_GEOMETRY = 'geometry'
  76. PARAMETER_CRS = 'crs'
  77. PARAMETER_MULTIPLE = 'multilayer'
  78. PARAMETER_BAND = 'band'
  79. PARAMETER_LAYOUTITEM = 'layoutitem'
  80. PARAMETER_MAP_LAYER = 'layer'
  81. PARAMETER_RANGE = 'range'
  82. PARAMETER_ENUM = 'enum'
  83. PARAMETER_MATRIX = 'matrix'
  84. PARAMETER_VECTOR_DESTINATION = 'vectorDestination'
  85. PARAMETER_FILE_DESTINATION = 'fileDestination'
  86. PARAMETER_FOLDER_DESTINATION = 'folderDestination'
  87. PARAMETER_RASTER_DESTINATION = 'rasterDestination'
  88. PARAMETER_POINTCLOUD_DESTINATION = 'pointCloudDestination'
  89. def getParameterFromString(s, context=''):
  90. # Try the parameter definitions used in description files
  91. if '|' in s and (s.startswith("QgsProcessingParameter") or s.startswith("*QgsProcessingParameter") or s.startswith('Parameter') or s.startswith('*Parameter')):
  92. isAdvanced = False
  93. if s.startswith("*"):
  94. s = s[1:]
  95. isAdvanced = True
  96. tokens = s.split("|")
  97. params = [t if str(t) != str(None) else None for t in tokens[1:]]
  98. if True:
  99. clazz = getattr(sys.modules[__name__], tokens[0])
  100. # convert to correct type
  101. if clazz == QgsProcessingParameterRasterLayer:
  102. if len(params) > 3:
  103. params[3] = True if params[3].lower() == 'true' else False
  104. elif clazz == QgsProcessingParameterPointCloudLayer:
  105. if len(params) > 3:
  106. params[3] = True if params[3].lower() == 'true' else False
  107. elif clazz == QgsProcessingParameterAnnotationLayer:
  108. if len(params) > 3:
  109. params[3] = True if params[3].lower() == 'true' else False
  110. elif clazz == QgsProcessingParameterBand:
  111. if len(params) > 4:
  112. params[4] = True if params[4].lower() == 'true' else False
  113. if len(params) > 5:
  114. params[5] = True if params[5].lower() == 'true' else False
  115. elif clazz == QgsProcessingParameterVectorLayer:
  116. if len(params) > 2:
  117. try:
  118. params[2] = [int(p) for p in params[2].split(';')]
  119. except ValueError:
  120. params[2] = [getattr(QgsProcessing, p.split(".")[1]) for p in params[2].split(';')]
  121. if len(params) > 4:
  122. params[4] = True if params[4].lower() == 'true' else False
  123. elif clazz == QgsProcessingParameterMapLayer:
  124. if len(params) > 3:
  125. params[3] = True if params[3].lower() == 'true' else False
  126. try:
  127. params[4] = [int(p) for p in params[4].split(';')]
  128. except ValueError:
  129. params[4] = [getattr(QgsProcessing, p.split(".")[1]) for p in params[4].split(';')]
  130. elif clazz == QgsProcessingParameterBoolean:
  131. if len(params) > 2:
  132. params[2] = True if params[2].lower() == 'true' else False
  133. if len(params) > 3:
  134. params[3] = True if params[3].lower() == 'true' else False
  135. elif clazz == QgsProcessingParameterPoint:
  136. if len(params) > 3:
  137. params[3] = True if params[3].lower() == 'true' else False
  138. elif clazz == QgsProcessingParameterGeometry:
  139. if len(params) > 3:
  140. params[3] = True if params[3].lower() == 'true' else False
  141. if len(params) > 4:
  142. try:
  143. params[4] = [int(p) for p in params[4].split(';')]
  144. except ValueError:
  145. params[4] = [getattr(QgsWkbTypes, p.split(".")[1]) for p in params[4].split(';')]
  146. if len(params) > 5:
  147. params[5] = True if params[5].lower() == 'true' else False
  148. elif clazz == QgsProcessingParameterCrs:
  149. if len(params) > 3:
  150. params[3] = True if params[3].lower() == 'true' else False
  151. elif clazz == QgsProcessingParameterRange:
  152. if len(params) > 2:
  153. try:
  154. params[2] = int(params[2])
  155. except ValueError:
  156. params[2] = getattr(QgsProcessingParameterNumber, params[2].split(".")[1])
  157. if len(params) > 4:
  158. params[4] = True if params[4].lower() == 'true' else False
  159. elif clazz == QgsProcessingParameterExtent:
  160. if len(params) > 3:
  161. params[3] = True if params[3].lower() == 'true' else False
  162. elif clazz == QgsProcessingParameterExpression:
  163. if len(params) > 3:
  164. params[3] = True if params[3].lower() == 'true' else False
  165. elif clazz == QgsProcessingParameterEnum:
  166. if len(params) > 2:
  167. params[2] = params[2].split(';')
  168. if len(params) > 3:
  169. params[3] = True if params[3].lower() == 'true' else False
  170. if len(params) > 4:
  171. # For multiple values; default value is a list of int
  172. if params[3] is True:
  173. params[4] = [int(v) for v in params[4].split(',')] if params[4] is not None else None
  174. else:
  175. params[4] = int(params[4]) if params[4] is not None else None
  176. if len(params) > 5:
  177. params[5] = True if params[5].lower() == 'true' else False
  178. elif clazz == QgsProcessingParameterFeatureSource:
  179. if len(params) > 2:
  180. try:
  181. params[2] = [int(p) for p in params[2].split(';')]
  182. except ValueError:
  183. params[2] = [getattr(QgsProcessing, p.split(".")[1]) for p in params[2].split(';')]
  184. if len(params) > 4:
  185. params[4] = True if params[4].lower() == 'true' else False
  186. elif clazz == QgsProcessingParameterMultipleLayers:
  187. if len(params) > 2:
  188. try:
  189. params[2] = int(params[2])
  190. except ValueError:
  191. params[2] = getattr(QgsProcessing, params[2].split(".")[1])
  192. if len(params) > 4:
  193. params[4] = True if params[4].lower() == 'true' else False
  194. elif clazz == QgsProcessingParameterMatrix:
  195. if len(params) > 2:
  196. params[2] = int(params[2])
  197. if len(params) > 3:
  198. params[3] = True if params[3].lower() == 'true' else False
  199. if len(params) > 4:
  200. params[4] = params[4].split(';')
  201. if len(params) > 6:
  202. params[6] = True if params[6].lower() == 'true' else False
  203. elif clazz == QgsProcessingParameterField:
  204. if len(params) > 4:
  205. try:
  206. params[4] = int(params[4])
  207. except ValueError:
  208. params[4] = getattr(QgsProcessingParameterField, params[4].split(".")[1])
  209. if len(params) > 5:
  210. params[5] = True if params[5].lower() == 'true' else False
  211. if len(params) > 6:
  212. params[6] = True if params[6].lower() == 'true' else False
  213. if len(params) > 7:
  214. params[7] = True if params[7].lower() == 'true' else False
  215. elif clazz == QgsProcessingParameterFile:
  216. if len(params) > 2:
  217. try:
  218. params[2] = int(params[2])
  219. except ValueError:
  220. params[2] = getattr(QgsProcessingParameterFile, params[2].split(".")[1])
  221. if len(params) > 5:
  222. params[5] = True if params[5].lower() == 'true' else False
  223. elif clazz == QgsProcessingParameterNumber:
  224. if len(params) > 2:
  225. try:
  226. params[2] = int(params[2])
  227. except ValueError:
  228. params[2] = getattr(QgsProcessingParameterNumber, params[2].split(".")[1])
  229. if len(params) > 3:
  230. params[3] = float(params[3].strip()) if params[3] is not None else None
  231. if len(params) > 4:
  232. params[4] = True if params[4].lower() == 'true' else False
  233. if len(params) > 5:
  234. params[5] = float(params[5].strip()) if params[5] is not None else -sys.float_info.max + 1
  235. if len(params) > 6:
  236. params[6] = float(params[6].strip()) if params[6] is not None else sys.float_info.max - 1
  237. elif clazz == QgsProcessingParameterString:
  238. if len(params) > 3:
  239. params[3] = True if params[3].lower() == 'true' else False
  240. if len(params) > 4:
  241. params[4] = True if params[4].lower() == 'true' else False
  242. elif clazz == QgsProcessingParameterColor:
  243. if len(params) > 3:
  244. params[3] = True if params[3].lower() == 'true' else False
  245. if len(params) > 4:
  246. params[4] = True if params[4].lower() == 'true' else False
  247. elif clazz == QgsProcessingParameterFileDestination:
  248. if len(params) > 4:
  249. params[4] = True if params[4].lower() == 'true' else False
  250. if len(params) > 5:
  251. params[5] = True if params[5].lower() == 'true' else False
  252. elif clazz == QgsProcessingParameterFolderDestination:
  253. if len(params) > 3:
  254. params[3] = True if params[3].lower() == 'true' else False
  255. if len(params) > 4:
  256. params[4] = True if params[4].lower() == 'true' else False
  257. elif clazz == QgsProcessingParameterRasterDestination:
  258. if len(params) > 3:
  259. params[3] = True if params[3].lower() == 'true' else False
  260. if len(params) > 4:
  261. params[4] = True if params[4].lower() == 'true' else False
  262. elif clazz == QgsProcessingParameterPointCloudDestination:
  263. print(params)
  264. if len(params) > 3:
  265. params[3] = True if params[3].lower() == 'true' else False
  266. if len(params) > 4:
  267. params[4] = True if params[4].lower() == 'true' else False
  268. elif clazz == QgsProcessingParameterVectorDestination:
  269. if len(params) > 2:
  270. try:
  271. params[2] = int(params[2])
  272. except ValueError:
  273. params[2] = getattr(QgsProcessing, params[2].split(".")[1])
  274. if len(params) > 4:
  275. params[4] = True if params[4].lower() == 'true' else False
  276. if len(params) > 5:
  277. params[5] = True if params[5].lower() == 'true' else False
  278. param = clazz(*params)
  279. if isAdvanced:
  280. param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  281. param.setDescription(QCoreApplication.translate(context, param.description()))
  282. return param
  283. else:
  284. return None
  285. else: # try script syntax
  286. # try native method
  287. param = QgsProcessingParameters.parameterFromScriptCode(s)
  288. if param:
  289. return param