Skip to content

Commit

Permalink
Remove broken algorithms and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Médéric RIBREUX committed Dec 30, 2017
1 parent 486bcd6 commit 5a3a0da
Show file tree
Hide file tree
Showing 55 changed files with 110 additions and 383 deletions.
61 changes: 53 additions & 8 deletions python/plugins/processing/algs/grass7/Grass7Algorithm.py
Expand Up @@ -415,7 +415,7 @@ def processInputs(self, parameters, context):
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:
Expand All @@ -424,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)
# For multiple inputs, process each layer
elif isinstance(param, QgsProcessingParameterMultipleLayers):
layers = self.parameterAsLayerList(parameters, paramName, context)
Expand Down Expand Up @@ -667,7 +669,7 @@ def loadRasterLayer(self, name, layer, external=True, band=1, destName=None):
'band={} '.format(band) if band else '',
destName)
self.commands.append(command)

def exportRasterLayerFromParameter(self, name, parameters, context, colorTable=True):
"""
Creates a dedicated command to export a raster from
Expand Down Expand Up @@ -772,7 +774,7 @@ def loadVectorLayer(self, name, layer, external=False):
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',
Expand Down Expand Up @@ -826,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.
Expand Down
13 changes: 0 additions & 13 deletions python/plugins/processing/algs/grass7/description/i.rectify.txt

This file was deleted.

15 changes: 0 additions & 15 deletions python/plugins/processing/algs/grass7/description/v.lrs.create.txt

This file was deleted.

This file was deleted.

This file was deleted.

Expand Up @@ -3,14 +3,11 @@ Allocates subnets for nearest centers (direction from center)
Vector (v.*)
QgsProcessingParameterVectorLayer|input|Input vector line layer (arcs)|1|None|False
QgsProcessingParameterVectorLayer|points|Centers point layer (nodes)|0|None|False
QgsProcessingParameterVectorLayer|turn_layer|Layer with turntable (with -t option)|0|None|True
QgsProcessingParameterVectorLayer|turn_cat_layer|Layer with unique categories used in turntable (with -t option)|0|None|True
QgsProcessingParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|QgsProcessingParameterNumber.Double|50.0|False|0.0|None
*QgsProcessingParameterString|center_cats|Category values|1-100000|False|False
*QgsProcessingParameterEnum|arc_type|Arc type|line;boundary|True|0,1|False
*QgsProcessingParameterField|arc_column|Arc forward/both direction(s) cost column (number)|None|input|0|False|True
*QgsProcessingParameterField|arc_backward_column|Arc backward direction cost column (number)|None|input|0|False|True
*QgsProcessingParameterField|node_column|Node cost column (number)|None|points|0|False|True
*QgsProcessingParameterBoolean|-t|Use turntable|False|True
*QgsProcessingParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
QgsProcessingParameterVectorDestination|output|Network Alloction
Expand Up @@ -3,15 +3,12 @@ Splits network by cost isolines.
Vector (v.*)
QgsProcessingParameterVectorLayer|input|Input vector line layer (arcs)|1|None|False
QgsProcessingParameterVectorLayer|points|Centers point layer (nodes)|0|None|False
QgsProcessingParameterVectorLayer|turn_layer|Layer with turntable (with -t option)|0|None|True
QgsProcessingParameterVectorLayer|turn_cat_layer|Layer with unique categories used in turntable (with -t option)|0|None|True
QgsProcessingParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|QgsProcessingParameterNumber.Double|50.0|False|0.0|None
*QgsProcessingParameterEnum|arc_type|Arc type|line;boundary|True|0,1|False
*QgsProcessingParameterString|center_cats|Category values|1-100000|False|False
QgsProcessingParameterString|costs|Costs for isolines|1000,2000,3000|False|False
*QgsProcessingParameterField|arc_column|Arc forward/both direction(s) cost column (number)|None|input|0|False|True
*QgsProcessingParameterField|arc_backward_column|Arc backward direction cost column (number)|None|input|0|False|True
*QgsProcessingParameterField|node_column|Node cost column (number)|None|points|0|False|True
*QgsProcessingParameterBoolean|-t|Use turntable|False|True
*QgsProcessingParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
QgsProcessingParameterVectorDestination|output|Network_Iso
Expand Up @@ -3,16 +3,13 @@ Finds shortest path on vector network
Vector (v.*)
QgsProcessingParameterVectorLayer|input|Input vector line layer (arcs)|1|None|False
QgsProcessingParameterVectorLayer|points|Centers point layer (nodes)|0|None|False
QgsProcessingParameterVectorLayer|turn_layer|Layer with turntable (with -t option)|0|None|True
QgsProcessingParameterVectorLayer|turn_cat_layer|Layer with unique categories used in turntable (with -t option)|0|None|True
QgsProcessingParameterFile|file|Name of file containing start and end points|False|txt|None|False
QgsProcessingParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|QgsProcessingParameterNumber.Double|50.0|False|0.0|None
*QgsProcessingParameterEnum|arc_type|Arc type|line;boundary|True|0,1|False
*QgsProcessingParameterField|arc_column|Arc forward/both direction(s) cost column (number)|None|input|0|False|True
*QgsProcessingParameterField|arc_backward_column|Arc backward direction cost column (number)|None|input|0|False|True
*QgsProcessingParameterField|node_column|Node cost column (number)|None|points|0|False|True
*QgsProcessingParameterNumber|dmax|Maximum distance to the network|QgsProcessingParameterNumber.Double|1000.0|True|0.0|None
*QgsProcessingParameterBoolean|-t|Use turntable|False|True
*QgsProcessingParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
*QgsProcessingParameterBoolean|-s|Write output as original input segments, not each path as one line|False|True
QgsProcessingParameterVectorDestination|output|Network_Path
Expand Up @@ -3,14 +3,11 @@ Creates a cycle connecting given nodes (Traveling salesman problem)
Vector (v.*)
QgsProcessingParameterVectorLayer|input|Input vector line layer (arcs)|1|None|False
QgsProcessingParameterVectorLayer|points|Centers point layer (nodes)|0|None|False
QgsProcessingParameterVectorLayer|turn_layer|Layer with turntable (with -t option)|0|None|True
QgsProcessingParameterVectorLayer|turn_cat_layer|Layer with unique categories used in turntable (with -t option)|0|None|True
QgsProcessingParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|QgsProcessingParameterNumber.Double|50.0|False|0.0|None
*QgsProcessingParameterEnum|arc_type|Arc type|line;boundary|True|0,1|False
*QgsProcessingParameterString|center_cats|Category values|1-100000|False|False
*QgsProcessingParameterField|arc_column|Arc forward/both direction(s) cost column (number)|None|input|0|False|True
*QgsProcessingParameterField|arc_backward_column|Arc backward direction cost column (number)|None|input|0|False|True
*QgsProcessingParameterBoolean|-t|Use turntable|False|True
*QgsProcessingParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
QgsProcessingParameterVectorDestination|output|Network_Salesman
QgsProcessingParameterFileDestination|sequence|Output file holding node sequence|Html files (*.html)|report.html|True
Expand Down

This file was deleted.

4 changes: 1 addition & 3 deletions python/plugins/processing/algs/grass7/description/v.net.txt
Expand Up @@ -3,10 +3,8 @@ Performs network maintenance
Vector (v.*)
QgsProcessingParameterVectorLayer|input|Input vector line layer (arcs)|1|None|True
QgsProcessingParameterVectorLayer|points|Input vector point layer (nodes)|0|None|True
QgsProcessingParameterVectorLayer|turn_layer|Layer with turntable|0|None|True
QgsProcessingParameterVectorLayer|turn_cat_layer|Layer with unique categories used in turntable|0|None|True
QgsProcessingParameterFile|file|Name of input arcs file|False|txt|None|True
QgsProcessingParameterEnum|operation|Operation to be performed|nodes;connect;arcs;turntable|False|0|False
QgsProcessingParameterEnum|operation|Operation to be performed|nodes;connect;arcs|False|0|False
QgsProcessingParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|QgsProcessingParameterNumber.Double|50.0|False|0.0|None
QgsProcessingParameterEnum|arc_type|Arc type|line;boundary|True|0,1|True
*QgsProcessingParameterBoolean|-s|Snap points to network|False
Expand Down
7 changes: 3 additions & 4 deletions python/plugins/processing/algs/grass7/ext/i.py
Expand Up @@ -51,14 +51,14 @@ def orderedInput(alg, parameters, context, src, tgt, numSeq=None):
# Handle specific range
if numSeq is None:
numSeq = list(range(1, len(rasters) + 1))

for idx, raster in enumerate(rasters):
rasterName = '{}{}'.format(rootFilename, numSeq[idx])
alg.loadRasterLayer(rasterName, raster, False, None, rasterName)

# Don't forget to remove the old input parameter
alg.removeParameter(src)


def regroupRasters(alg, parameters, context, src, group, subgroup=None, extFile=None):
"""
Expand Down Expand Up @@ -114,10 +114,9 @@ def regroupRasters(alg, parameters, context, src, group, subgroup=None, extFile=
# 'subgroup', new_parameters[subgroup.name()],
# extFile[ext], shortFileName)
# copyFile(alg, extFileName, destPath)


alg.removeParameter(src)

return groupName, subgroupName


Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/i_cca.py
Expand Up @@ -40,6 +40,6 @@ def processCommand(alg, parameters, context):
signatureFile = alg.parameterAsString(parameters, 'signature', context)
shortSigFile = importSigFile(alg, group, subgroup, signatureFile)
parameters['signature'] = shortSigFile

# Handle other parameters
alg.processCommand(parameters, context)
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/grass7/ext/i_cluster.py
Expand Up @@ -28,6 +28,7 @@
import os
from .i import regroupRasters, verifyRasterNum, exportSigFile


def checkParameterValuesBeforeExecuting(alg, parameters, context):
return verifyRasterNum(alg, parameters, context, 'input', 2)

Expand All @@ -41,10 +42,9 @@ def processCommand(alg, parameters, context):
# Regroup rasters
group, subgroup = regroupRasters(alg, parameters, context, 'input', 'group', 'subgroup')
alg.processCommand(parameters, context)

# Re-add signature files
parameters['signaturefile'] = signatureFile

# Export signature file
exportSigFile(alg, group, subgroup, signatureFile)

4 changes: 2 additions & 2 deletions python/plugins/processing/algs/grass7/ext/i_evapo_mh.py
Expand Up @@ -28,9 +28,9 @@

def checkParameterValuesBeforeExecuting(alg, parameters, context):
if (alg.parameterAsBool(parameters, '-h', context)
and alg.parameterAsLayer(parameters, 'precipitation', context)):
and alg.parameterAsLayer(parameters, 'precipitation', context)):
return alg.tr('You can\'t use original Hargreaves flag and precipitation parameter together!')
if (not alg.parameterAsBool(parameters, '-h', context)
and not alg.parameterAsLayer(parameters, 'precipitation', context)):
and not alg.parameterAsLayer(parameters, 'precipitation', context)):
return alg.tr('If you don\'t use original Hargreaves flag, you must set the precipitation raster parameter!')
return None
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/grass7/ext/i_gensig.py
Expand Up @@ -28,6 +28,7 @@
import os
from .i import regroupRasters, exportSigFile


def processCommand(alg, parameters, context):
# We need to extract the basename of the signature file
signatureFile = alg.parameterAsString(parameters, 'signaturefile', context)
Expand All @@ -37,10 +38,9 @@ def processCommand(alg, parameters, context):
# Regroup rasters
group, subgroup = regroupRasters(alg, parameters, context, 'input', 'group', 'subgroup')
alg.processCommand(parameters, context)

# Re-add signature files
parameters['signaturefile'] = signatureFile

# Export signature file
exportSigFile(alg, group, subgroup, signatureFile)

4 changes: 2 additions & 2 deletions python/plugins/processing/algs/grass7/ext/i_gensigset.py
Expand Up @@ -28,6 +28,7 @@
import os
from .i import regroupRasters, exportSigFile


def processCommand(alg, parameters, context):
# We need to extract the basename of the signature file
signatureFile = alg.parameterAsString(parameters, 'signaturefile', context)
Expand All @@ -37,10 +38,9 @@ def processCommand(alg, parameters, context):
# Regroup rasters
group, subgroup = regroupRasters(alg, parameters, context, 'input', 'group', 'subgroup')
alg.processCommand(parameters, context)

# Re-add signature files
parameters['signaturefile'] = signatureFile

# Export signature file
exportSigFile(alg, group, subgroup, signatureFile, 'sigset')

3 changes: 1 addition & 2 deletions python/plugins/processing/algs/grass7/ext/i_maxlik.py
Expand Up @@ -36,7 +36,6 @@ def processCommand(alg, parameters, context):
signatureFile = alg.parameterAsString(parameters, 'signaturefile', context)
shortSigFile = importSigFile(alg, group, subgroup, signatureFile)
parameters['signaturefile'] = shortSigFile

# Handle other parameters
alg.processCommand(parameters, context)

1 change: 0 additions & 1 deletion python/plugins/processing/algs/grass7/ext/i_oif.py
Expand Up @@ -30,4 +30,3 @@

def checkParameterValuesBeforeExecuting(alg, parameters, context):
return verifyRasterNum(alg, parameters, context, 'input', 4)

0 comments on commit 5a3a0da

Please sign in to comment.