Skip to content
Permalink
Browse files

Merge pull request #5968 from medspx/ProcessingGrassFixExt

[Processing] GRASS fix all ext scripts
  • Loading branch information
alexbruy committed Jan 9, 2018
2 parents a934060 + 7120937 commit 566704a72081ccd243adf544f472603353817769
Showing with 1,191 additions and 2,363 deletions.
  1. +102 −27 python/plugins/processing/algs/grass7/Grass7Algorithm.py
  2. +3 −1 python/plugins/processing/algs/grass7/Grass7AlgorithmProvider.py
  3. +6 −5 python/plugins/processing/algs/grass7/Grass7Utils.py
  4. +27 −206 python/plugins/processing/algs/grass7/TODO.md
  5. +1 −1 python/plugins/processing/algs/grass7/description/i.atcorr.txt
  6. +1 −1 python/plugins/processing/algs/grass7/description/i.cca.txt
  7. +1 −1 python/plugins/processing/algs/grass7/description/i.cluster.txt
  8. +1 −1 python/plugins/processing/algs/grass7/description/i.in.spotvgt.txt
  9. +1 −1 python/plugins/processing/algs/grass7/description/i.landsat.toar.txt
  10. +1 −1 python/plugins/processing/algs/grass7/description/i.maxlik.txt
  11. +0 −13 python/plugins/processing/algs/grass7/description/i.rectify.txt
  12. +1 −1 python/plugins/processing/algs/grass7/description/i.smap.txt
  13. +1 −1 python/plugins/processing/algs/grass7/description/m.cogo.txt
  14. +1 −0 python/plugins/processing/algs/grass7/description/r.blend.rgb.txt
  15. +1 −1 python/plugins/processing/algs/grass7/description/r.category.txt
  16. +3 −3 python/plugins/processing/algs/grass7/description/r.colors.txt
  17. +1 −1 python/plugins/processing/algs/grass7/description/r.in.lidar.info.txt
  18. +1 −1 python/plugins/processing/algs/grass7/description/r.in.lidar.txt
  19. +3 −3 python/plugins/processing/algs/grass7/description/r.li.cwed.ascii.txt
  20. +2 −2 python/plugins/processing/algs/grass7/description/r.li.cwed.txt
  21. +2 −2 python/plugins/processing/algs/grass7/description/r.li.dominance.ascii.txt
  22. +1 −1 python/plugins/processing/algs/grass7/description/r.li.dominance.txt
  23. +2 −2 python/plugins/processing/algs/grass7/description/r.li.edgedensity.ascii.txt
  24. +1 −1 python/plugins/processing/algs/grass7/description/r.li.edgedensity.txt
  25. +2 −2 python/plugins/processing/algs/grass7/description/r.li.mpa.ascii.txt
  26. +1 −1 python/plugins/processing/algs/grass7/description/r.li.mpa.txt
  27. +2 −2 python/plugins/processing/algs/grass7/description/r.li.mps.ascii.txt
  28. +1 −1 python/plugins/processing/algs/grass7/description/r.li.mps.txt
  29. +2 −2 python/plugins/processing/algs/grass7/description/r.li.padcv.ascii.txt
  30. +1 −1 python/plugins/processing/algs/grass7/description/r.li.padcv.txt
  31. +2 −2 python/plugins/processing/algs/grass7/description/r.li.padrange.ascii.txt
  32. +1 −1 python/plugins/processing/algs/grass7/description/r.li.padrange.txt
  33. +2 −2 python/plugins/processing/algs/grass7/description/r.li.padsd.ascii.txt
  34. +1 −1 python/plugins/processing/algs/grass7/description/r.li.padsd.txt
  35. +2 −2 python/plugins/processing/algs/grass7/description/r.li.patchdensity.ascii.txt
  36. +1 −1 python/plugins/processing/algs/grass7/description/r.li.patchdensity.txt
  37. +2 −2 python/plugins/processing/algs/grass7/description/r.li.patchnum.ascii.txt
  38. +1 −1 python/plugins/processing/algs/grass7/description/r.li.patchnum.txt
  39. +2 −2 python/plugins/processing/algs/grass7/description/r.li.pielou.ascii.txt
  40. +1 −1 python/plugins/processing/algs/grass7/description/r.li.pielou.txt
  41. +2 −2 python/plugins/processing/algs/grass7/description/r.li.renyi.ascii.txt
  42. +1 −1 python/plugins/processing/algs/grass7/description/r.li.renyi.txt
  43. +2 −2 python/plugins/processing/algs/grass7/description/r.li.richness.ascii.txt
  44. +1 −1 python/plugins/processing/algs/grass7/description/r.li.richness.txt
  45. +2 −2 python/plugins/processing/algs/grass7/description/r.li.shannon.ascii.txt
  46. +1 −1 python/plugins/processing/algs/grass7/description/r.li.shannon.txt
  47. +2 −2 python/plugins/processing/algs/grass7/description/r.li.shape.ascii.txt
  48. +1 −1 python/plugins/processing/algs/grass7/description/r.li.shape.txt
  49. +2 −2 python/plugins/processing/algs/grass7/description/r.li.simpson.ascii.txt
  50. +1 −1 python/plugins/processing/algs/grass7/description/r.li.simpson.txt
  51. +1 −1 python/plugins/processing/algs/grass7/description/r.mapcalc.txt
  52. +1 −1 python/plugins/processing/algs/grass7/description/r.mfilter.txt
  53. +1 −1 python/plugins/processing/algs/grass7/description/r.profile.txt
  54. +1 −1 python/plugins/processing/algs/grass7/description/r.reclass.txt
  55. +1 −1 python/plugins/processing/algs/grass7/description/r.recode.txt
  56. +3 −3 python/plugins/processing/algs/grass7/description/r.series.interp.txt
  57. +0 −14 python/plugins/processing/algs/grass7/description/r.support.txt
  58. +3 −3 python/plugins/processing/algs/grass7/description/r.topmodel.txt
  59. +4 −4 python/plugins/processing/algs/grass7/description/v.distance.txt
  60. +4 −4 python/plugins/processing/algs/grass7/description/v.edit.txt
  61. +1 −1 python/plugins/processing/algs/grass7/description/v.extract.txt
  62. +1 −1 python/plugins/processing/algs/grass7/description/v.in.ascii.txt
  63. +1 −1 python/plugins/processing/algs/grass7/description/v.in.dxf.txt
  64. +1 −1 python/plugins/processing/algs/grass7/description/v.in.e00.txt
  65. +1 −1 python/plugins/processing/algs/grass7/description/v.in.geonames.txt
  66. +1 −1 python/plugins/processing/algs/grass7/description/v.in.lidar.txt
  67. +1 −1 python/plugins/processing/algs/grass7/description/v.in.lines.txt
  68. +1 −1 python/plugins/processing/algs/grass7/description/v.in.mapgen.txt
  69. +0 −15 python/plugins/processing/algs/grass7/description/v.lrs.create.txt
  70. +0 −7 python/plugins/processing/algs/grass7/description/v.lrs.segment.txt
  71. +0 −8 python/plugins/processing/algs/grass7/description/v.lrs.where.txt
  72. +0 −3 python/plugins/processing/algs/grass7/description/v.net.alloc.txt
  73. +2 −2 python/plugins/processing/algs/grass7/description/v.net.allpairs.txt
  74. +0 −7 python/plugins/processing/algs/grass7/description/v.net.arcs.txt
  75. +1 −0 python/plugins/processing/algs/grass7/description/v.net.bridge.txt
  76. +1 −0 python/plugins/processing/algs/grass7/description/v.net.centrality.txt
  77. +1 −0 python/plugins/processing/algs/grass7/description/v.net.components.txt
  78. +0 −9 python/plugins/processing/algs/grass7/description/v.net.connect.txt
  79. +3 −4 python/plugins/processing/algs/grass7/description/v.net.distance.txt
  80. +0 −3 python/plugins/processing/algs/grass7/description/v.net.iso.txt
  81. +0 −7 python/plugins/processing/algs/grass7/description/v.net.nodes.txt
  82. +1 −1 python/plugins/processing/algs/grass7/description/v.net.nreport.txt
  83. +2 −5 python/plugins/processing/algs/grass7/description/v.net.path.txt
  84. +1 −1 python/plugins/processing/algs/grass7/description/v.net.report.txt
  85. +0 −3 python/plugins/processing/algs/grass7/description/v.net.salesman.txt
  86. +2 −2 python/plugins/processing/algs/grass7/description/v.net.spanningtree.txt
  87. +0 −9 python/plugins/processing/algs/grass7/description/v.net.turntable.txt
  88. +12 −0 python/plugins/processing/algs/grass7/description/v.net.txt
  89. +1 −1 python/plugins/processing/algs/grass7/description/v.net.visibility.txt
  90. +1 −1 python/plugins/processing/algs/grass7/description/v.reclass.txt
  91. +1 −1 python/plugins/processing/algs/grass7/description/v.rectify.txt
  92. +1 −1 python/plugins/processing/algs/grass7/description/v.segment.txt
  93. +124 −129 python/plugins/processing/algs/grass7/ext/i.py
  94. +8 −7 python/plugins/processing/algs/grass7/ext/i_albedo.py
  95. +8 −3 python/plugins/processing/algs/grass7/ext/i_cca.py
  96. +13 −21 python/plugins/processing/algs/grass7/ext/i_cluster.py
  97. +4 −13 python/plugins/processing/algs/grass7/ext/i_colors_enhance.py
  98. +5 −3 python/plugins/processing/algs/grass7/ext/i_evapo_mh.py
  99. +12 −17 python/plugins/processing/algs/grass7/ext/i_gensig.py
  100. +12 −17 python/plugins/processing/algs/grass7/ext/i_gensigset.py
  101. +2 −2 python/plugins/processing/algs/grass7/ext/i_group.py
  102. +3 −3 python/plugins/processing/algs/grass7/ext/i_in_spotvgt.py
  103. +6 −14 python/plugins/processing/algs/grass7/ext/i_landsat_acca.py
  104. +2 −6 python/plugins/processing/algs/grass7/ext/i_landsat_toar.py
  105. +12 −4 python/plugins/processing/algs/grass7/ext/i_maxlik.py
  106. +3 −13 python/plugins/processing/algs/grass7/ext/i_oif.py
  107. +19 −25 python/plugins/processing/algs/grass7/ext/i_pansharpen.py
  108. +0 −87 python/plugins/processing/algs/grass7/ext/i_rectify.py
  109. +3 −2 python/plugins/processing/algs/grass7/ext/i_segment.py
  110. +11 −3 python/plugins/processing/algs/grass7/ext/i_smap.py
  111. +3 −2 python/plugins/processing/algs/grass7/ext/r_blend_combine.py
  112. +19 −55 python/plugins/processing/algs/grass7/ext/r_blend_rgb.py
  113. +33 −69 python/plugins/processing/algs/grass7/ext/r_category.py
  114. +40 −65 python/plugins/processing/algs/grass7/ext/r_colors.py
  115. +18 −43 python/plugins/processing/algs/grass7/ext/r_colors_stddev.py
  116. +6 −0 python/plugins/processing/algs/grass7/ext/r_drain.py
  117. +16 −3 python/plugins/processing/algs/grass7/ext/r_horizon.py
  118. +32 −39 python/plugins/processing/algs/grass7/ext/r_li.py
  119. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_cwed.py
  120. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_cwed_ascii.py
  121. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_dominance.py
  122. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_dominance_ascii.py
  123. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_edgedensity.py
  124. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_edgedensity_ascii.py
  125. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_mpa.py
  126. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_mpa_ascii.py
  127. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_mps.py
  128. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_mps_ascii.py
  129. +0 −133 python/plugins/processing/algs/grass7/ext/r_li_orig.py
  130. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_padcv.py
  131. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_padcv_ascii.py
  132. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_padrange.py
  133. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_padrange_ascii.py
  134. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_padsd.py
  135. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_padsd_ascii.py
  136. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_patchdensity.py
  137. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_patchdensity_ascii.py
  138. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_patchnum.py
  139. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_patchnum_ascii.py
  140. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_pielou.py
  141. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_pielou_ascii.py
  142. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_renyi.py
  143. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_renyi_ascii.py
  144. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_richness.py
  145. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_richness_ascii.py
  146. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_shannon.py
  147. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_shannon_ascii.py
  148. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_shape.py
  149. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_shape_ascii.py
  150. +4 −4 python/plugins/processing/algs/grass7/ext/r_li_simpson.py
  151. +6 −6 python/plugins/processing/algs/grass7/ext/r_li_simpson_ascii.py
  152. +21 −39 python/plugins/processing/algs/grass7/ext/r_mapcalc.py
  153. +0 −39 python/plugins/processing/algs/grass7/ext/r_mask.py
  154. +13 −5 python/plugins/processing/algs/grass7/ext/r_mask_rast.py
  155. +12 −4 python/plugins/processing/algs/grass7/ext/r_mask_vect.py
  156. +2 −2 python/plugins/processing/algs/grass7/ext/r_null.py
  157. +8 −14 python/plugins/processing/algs/grass7/ext/r_reclass.py
  158. +4 −4 python/plugins/processing/algs/grass7/ext/r_resamp_filter.py
  159. +14 −15 python/plugins/processing/algs/grass7/ext/r_series_interp.py
  160. +8 −49 python/plugins/processing/algs/grass7/ext/r_shade.py
  161. +7 −1 python/plugins/processing/algs/grass7/ext/r_statistics.py
  162. +18 −23 python/plugins/processing/algs/grass7/ext/r_stats_quantile_rast.py
  163. +0 −64 python/plugins/processing/algs/grass7/ext/r_support.py
  164. +0 −38 python/plugins/processing/algs/grass7/ext/r_texture.py
  165. +0 −39 python/plugins/processing/algs/grass7/ext/r_topmodel.py
  166. +0 −38 python/plugins/processing/algs/grass7/ext/r_topmodel_topidxstats.py
  167. +2 −30 python/plugins/processing/algs/grass7/ext/r_what_color.py
  168. +11 −21 python/plugins/processing/algs/grass7/ext/v_distance.py
  169. +18 −30 python/plugins/processing/algs/grass7/ext/v_edit.py
  170. +0 −39 python/plugins/processing/algs/grass7/ext/v_in_lidar.py
  171. +0 −44 python/plugins/processing/algs/grass7/ext/v_lrs_create.py
  172. +0 −72 python/plugins/processing/algs/grass7/ext/v_lrs_segment.py
  173. +0 −51 python/plugins/processing/algs/grass7/ext/v_lrs_where.py
  174. +61 −31 python/plugins/processing/algs/grass7/ext/v_net.py
  175. +2 −3 python/plugins/processing/algs/grass7/ext/v_net_alloc.py
  176. +6 −1 python/plugins/processing/algs/grass7/ext/v_net_allpairs.py
  177. +0 −33 python/plugins/processing/algs/grass7/ext/v_net_arcs.py
  178. +13 −2 python/plugins/processing/algs/grass7/ext/v_net_bridge.py
  179. +12 −8 python/plugins/processing/algs/grass7/ext/{v_net_articulation.py → v_net_centrality.py}
  180. +12 −16 python/plugins/processing/algs/grass7/ext/v_net_components.py
  181. +0 −33 python/plugins/processing/algs/grass7/ext/v_net_connect.py
  182. +1 −1 python/plugins/processing/algs/grass7/ext/v_net_connectivity.py
  183. +47 −44 python/plugins/processing/algs/grass7/ext/v_net_distance.py
  184. +3 −3 python/plugins/processing/algs/grass7/ext/v_net_flow.py
  185. +6 −1 python/plugins/processing/algs/grass7/ext/v_net_iso.py
  186. +6 −1 python/plugins/processing/algs/grass7/ext/v_net_path.py
  187. +7 −18 python/plugins/processing/algs/grass7/ext/v_net_salesman.py
  188. +11 −7 python/plugins/processing/algs/grass7/ext/{v_net_nodes.py → v_net_spanningtree.py}
  189. +6 −1 python/plugins/processing/algs/grass7/ext/v_net_steiner.py
  190. +2 −2 python/plugins/processing/algs/grass7/ext/v_net_visibility.py
  191. +0 −35 python/plugins/processing/algs/grass7/ext/v_pack.py
  192. +1 −1 python/plugins/processing/algs/grass7/ext/v_rast_stats.py
  193. +11 −25 python/plugins/processing/algs/grass7/ext/v_rectify.py
  194. +2 −1 python/plugins/processing/algs/grass7/ext/v_sample.py
  195. +1 −1 python/plugins/processing/algs/grass7/ext/v_to_3d.py
  196. +1 −1 python/plugins/processing/algs/grass7/ext/v_vect_stats.py
  197. +6 −6 python/plugins/processing/algs/grass7/ext/{v_what_rast_points.py → v_what_rast.py}
  198. +0 −39 python/plugins/processing/algs/grass7/ext/v_what_rast_centroids.py
  199. +2 −3 python/plugins/processing/algs/grass7/ext/v_what_vect.py
@@ -56,6 +56,7 @@
QgsProcessingParameterVectorDestination,
QgsProcessingParameterRasterDestination,
QgsProcessingParameterFileDestination,
QgsProcessingParameterFile,
QgsProcessingParameterFolderDestination,
QgsProcessingOutputFolder,
QgsProcessingOutputVectorLayer,
@@ -409,8 +410,12 @@ def processInputs(self, parameters, context):
paramName = param.name()
if not paramName in parameters:
continue
if isinstance(parameters[paramName], str) and len(parameters[paramName]) == 0:
# Handle Null parameter
if parameters[paramName] is None:
continue
elif isinstance(parameters[paramName], str) and len(parameters[paramName]) == 0:
continue

# Raster inputs needs to be imported into temp GRASS DB
if isinstance(param, QgsProcessingParameterRasterLayer):
if paramName not in self.exportedLayers:
@@ -419,11 +424,13 @@ def processInputs(self, parameters, context):
# Vector inputs needs to be imported into temp GRASS DB
elif isinstance(param, QgsProcessingParameterVectorLayer):
if paramName not in self.exportedLayers:
self.loadVectorLayerFromParameter(
paramName, parameters, context)
# TODO: find the best replacement for ParameterTable
#if isinstance(param, ParameterTable):
# pass
# Attribute tables are also vector inputs
if QgsProcessing.TypeFile in param.dataTypes():
self.loadAttributeTableFromParameter(
paramName, parameters, context)
else:
self.loadVectorLayerFromParameter(
paramName, parameters, context, None)
# For multiple inputs, process each layer
elif isinstance(param, QgsProcessingParameterMultipleLayers):
layers = self.parameterAsLayerList(parameters, paramName, context)
@@ -434,7 +441,7 @@ def processInputs(self, parameters, context):
self.loadRasterLayer(layerName, layer)
# Add a vector layer
elif layer.type() == QgsMapLayer.VectorLayer:
self.loadVectorLayer(layerName, layer)
self.loadVectorLayer(layerName, layer, None)

self.postInputs()

@@ -518,10 +525,19 @@ def processCommand(self, parameters, context, delOutputs=False):
elif isinstance(param, QgsProcessingParameterBoolean):
if self.parameterAsBool(parameters, paramName, context):
command += ' {}'.format(paramName)
# For Extents, remove if the value is null
elif isinstance(param, QgsProcessingParameterExtent):
if self.parameterAsExtent(parameters, paramName, context):
value = self.parameterAsString(parameters, paramName, context)
# For enumeration, we need to grab the string value
elif isinstance(param, QgsProcessingParameterEnum):
idx = self.parameterAsEnum(parameters, paramName, context)
value = '"{}"'.format(param.options()[idx])
# Handle multiple values
if param.allowMultiple():
indexes = self.parameterAsEnums(parameters, paramName, context)
else:
indexes = [self.parameterAsEnum(parameters, paramName, context)]
if indexes:
value = '"{}"'.format(','.join([param.options()[i] for i in indexes]))
# For strings, we just translate as string
elif isinstance(param, QgsProcessingParameterString):
data = self.parameterAsString(parameters, paramName, context)
@@ -532,9 +548,14 @@ def processCommand(self, parameters, context, delOutputs=False):
)
# For fields, we just translate as string
elif isinstance(param, QgsProcessingParameterField):
value = '{}'.format(
self.parameterAsString(parameters, paramName, context)
value = ','.join(
self.parameterAsFields(parameters, paramName, context)
)
elif isinstance(param, QgsProcessingParameterFile):
if self.parameterAsString(parameters, paramName, context):
value = '"{}"'.format(
self.parameterAsString(parameters, paramName, context)
)
# For numbers and points, we translate as a string
elif isinstance(param, (QgsProcessingParameterNumber,
QgsProcessingParameterPoint)):
@@ -550,6 +571,9 @@ def processCommand(self, parameters, context, delOutputs=False):
# Handle outputs
if not delOutputs:
for out in self.destinationParameterDefinitions():
# We exclude hidden parameters
if out.flags() & QgsProcessingParameterDefinition.FlagHidden:
continue
outName = out.name()
# For File destination
if isinstance(out, QgsProcessingParameterFileDestination):
@@ -624,24 +648,26 @@ def loadRasterLayerFromParameter(self, name, parameters, context, external=True,
layer = self.parameterAsRasterLayer(parameters, name, context)
self.loadRasterLayer(name, layer, external, band)

def loadRasterLayer(self, name, layer, external=True, band=1):
def loadRasterLayer(self, name, layer, external=True, band=1, destName=None):
"""
Creates a dedicated command to load a raster into
the temporary GRASS DB.
:param name: name of the parameter.
:param layer: QgsMapLayer for the raster layer.
:param external: True if using r.external.
:param band: imports only specified band. None for all bands.
:param destName: force the destination name of the raster.
"""
self.inputLayers.append(layer)
self.setSessionProjectionFromLayer(layer)
destFilename = 'a' + os.path.basename(getTempFilename())
self.exportedLayers[name] = destFilename
if not destName:
destName = 'rast_{}'.format(os.path.basename(getTempFilename()))
self.exportedLayers[name] = destName
command = '{0} input="{1}" {2}output="{3}" --overwrite -o'.format(
'r.external' if external else 'r.in.gdal',
os.path.normpath(layer.source()),
'band={} '.format(band) if band else '',
destFilename)
destName)
self.commands.append(command)

def exportRasterLayerFromParameter(self, name, parameters, context, colorTable=True):
@@ -683,33 +709,36 @@ def exportRasterLayer(self, grassName, fileName,
# Adjust region to layer before exporting
cmd.append('g.region raster={}'.format(grassName))
cmd.append(
'r.out.gdal -c -m{0} input="{1}" output="{2}" format="{3}" {4}{5} --overwrite'.format(
' -t' if colorTable else '',
'r.out.gdal -t -m{0} input="{1}" output="{2}" format="{3}" {4}{5} --overwrite'.format(
'' if colorTable else ' -c',
grassName, fileName,
outFormat,
' createopt="{}"'.format(createOpt) if createOpt else '',
' metaopt="{}"'.format(metaOpt) if metaOpt else ''
)
)

def exportRasterLayersIntoDirectory(self, name, parameters, context, colorTable=True):
def exportRasterLayersIntoDirectory(self, name, parameters, context, colorTable=True, wholeDB=False):
"""
Creates a dedicated loop command to export rasters from
temporary GRASS DB into a directory via gdal.
:param name: name of the output directory parameter.
:param parameters: Algorithm parameters dict.
:param context: Algorithm context.
:param colorTable: preserve color Table.
:param wholeDB: export every raster layer from the GRASSDB
"""
# Grab directory name and temporary basename
outDir = os.path.normpath(
self.parameterAsString(parameters, name, context))
basename = name + self.uniqueSuffix
basename = ''
if not wholeDB:
basename = name + self.uniqueSuffix

# Add a loop export from the basename
for cmd in [self.commands, self.outputCommands]:
# Adjust region to layer before exporting
# TODO: Does-it works under MS-Windows or MacOSX?
# TODO Windows support
# TODO Format/options support
cmd.append("for r in $(g.list type=rast pattern='{}*'); do".format(basename))
cmd.append(" r.out.gdal -m{0} input=${{r}} output={1}/${{r}}.tif {2}".format(
' -t' if colorTable else '', outDir,
@@ -718,7 +747,7 @@ def exportRasterLayersIntoDirectory(self, name, parameters, context, colorTable=
)
cmd.append("done")

def loadVectorLayerFromParameter(self, name, parameters, context, external=None):
def loadVectorLayerFromParameter(self, name, parameters, context, external=False):
"""
Creates a dedicated command to load a vector into
the temporary GRASS DB.
@@ -730,7 +759,7 @@ def loadVectorLayerFromParameter(self, name, parameters, context, external=None)
layer = self.parameterAsVectorLayer(parameters, name, context)
self.loadVectorLayer(name, layer, external)

def loadVectorLayer(self, name, layer, external=None):
def loadVectorLayer(self, name, layer, external=False):
"""
Creates a dedicated command to load a vector into
temporary GRASS DB.
@@ -745,7 +774,7 @@ def loadVectorLayer(self, name, layer, external=None):
Grass7Utils.GRASS_USE_VEXTERNAL)
self.inputLayers.append(layer)
self.setSessionProjectionFromLayer(layer)
destFilename = 'a' + os.path.basename(getTempFilename())
destFilename = 'vector_{}'.format(os.path.basename(getTempFilename()))
self.exportedLayers[name] = destFilename
command = '{0}{1}{2} input="{3}" output="{4}" --overwrite -o'.format(
'v.external' if external else 'v.in.ogr',
@@ -781,13 +810,16 @@ def exportVectorLayer(self, grassName, fileName, dataType='auto', layer=None, no
"""
Creates a dedicated command to export a vector from
temporary GRASS DB into a file via ogr.
:param grassName: name of the parameter
:param fileName: file path of raster layer
:param grassName: name of the parameter.
:param fileName: file path of raster layer.
:param dataType: GRASS data type for exporting data.
:param layer: In GRASS a vector can have multiple layers.
:param nocats: Also export features without category if True.
"""
for cmd in [self.commands, self.outputCommands]:
cmd.append(
'v.out.ogr{0} type={1} {2} input="{3}" output="{4}" {5}'.format(
'' if nocats else ' -c',
' -c' if nocats else '',
dataType,
'layer={}'.format(layer) if layer else '',
grassName,
@@ -796,6 +828,49 @@ def exportVectorLayer(self, grassName, fileName, dataType='auto', layer=None, no
)
)

def loadAttributeTableFromParameter(self, name, parameters, context):
"""
Creates a dedicated command to load an attribute table
into the temporary GRASS DB.
:param name: name of the parameter
:param parameters: Parameters of the algorithm.
:param context: Processing context
"""
table = self.parameterAsVectorLayer(parameters, name, context)
self.loadAttributeTable(name, table)

def loadAttributeTable(self, name, layer, destName=None):
"""
Creates a dedicated command to load an attribute table
into the temporary GRASS DB.
:param name: name of the input parameter.
:param layer: a layer object to import from.
:param destName: force the name for the table into GRASS DB.
"""
self.inputLayers.append(layer)
if not destName:
destName = 'table_{}'.format(os.path.basename(getTempFilename()))
self.exportedLayers[name] = destName
command = 'db.in.ogr --overwrite input="{0}" output="{1}"'.format(
os.path.normpath(layer.source()), destName)
self.commands.append(command)

def exportAttributeTable(self, grassName, fileName, outFormat='CSV', layer=1):
"""
Creates a dedicated command to export an attribute
table from the temporary GRASS DB into a file via ogr.
:param grassName: name of the parameter.
:param fileName: file path of raster layer.
:param outFormat: file format for export.
:param layer: In GRASS a vector can have multiple layers.
"""
for cmd in [self.commands, self.outputCommands]:
cmd.append(
'db.out.ogr input="{0}" output="{1}" layer={2} format={3} --overwrite'.format(
grassName, fileName, layer, outFormat
)
)

def setSessionProjectionFromProject(self):
"""
Set the projection from the project.
@@ -70,11 +70,13 @@ def load(self):
self.tr('Location of GRASS docs'),
Grass7Utils.grassHelpPath()))
# Add a setting for using v.external instead of v.in.ogr
# But set it to False by default because some algorithms
# can't be used with external data (need a solid v.in.ogr).
ProcessingConfig.addSetting(Setting(
self.name(),
Grass7Utils.GRASS_USE_VEXTERNAL,
self.tr('For vector layers, use v.external (faster) instead of v.in.ogr'),
True))
False))
ProcessingConfig.readSettings()
self.refreshAlgorithms()
return True
@@ -540,9 +540,10 @@ def getRasterFormatFromFilename(filename):
"""
ext = os.path.splitext(filename)[1].lower()
ext = ext.lstrip('.')
supported = GdalUtils.getSupportedRasters()
for name in list(supported.keys()):
exts = supported[name]
if ext in exts:
return name
if ext:
supported = GdalUtils.getSupportedRasters()
for name in list(supported.keys()):
exts = supported[name]
if ext in exts:
return name
return 'GTiff'

0 comments on commit 566704a

Please sign in to comment.
You can’t perform that action at this time.