Merge pull request #2628 from medspx/work_vnet_processing

[Feature][Processing] GRASS v.net modules
This commit is contained in:
Alexander Bruy 2016-01-13 14:49:53 +02:00
commit 9551ca7ca8
41 changed files with 1106 additions and 82 deletions

View File

@ -2,5 +2,7 @@ FILE(GLOB PY_FILES *.py)
FILE(GLOB OTHER_FILES grass7.txt)
FILE(GLOB DESCR_FILES description/*.txt)
ADD_SUBDIRECTORY(ext)
PLUGIN_INSTALL(processing algs/grass7 ${PY_FILES} ${OTHER_FILES})
PLUGIN_INSTALL(processing algs/grass7/description ${DESCR_FILES})

View File

@ -69,6 +69,14 @@ class Grass7Algorithm(GeoAlgorithm):
self.descriptionFile = descriptionfile
self.defineCharacteristicsFromFile()
self.numExportedLayers = 0
self.uniqueSufix = unicode(uuid.uuid4()).replace('-', '')
# Use the ext mechanism
name = self.commandLineName().replace('.', '_')[len('grass7:'):]
try:
self.module = importlib.import_module('processing.algs.grass7.ext.' + name)
except ImportError:
self.module = None
def getCopy(self):
newone = Grass7Algorithm(self.descriptionFile)
@ -189,7 +197,7 @@ class Grass7Algorithm(GeoAlgorithm):
else:
layer = dataobjects.getObjectFromUri(param.value)
cellsize = max(cellsize, (layer.extent().xMaximum()
- layer.extent().xMinimum())
- layer.extent().xMinimum())
/ layer.width())
elif isinstance(param, ParameterMultipleInput):
@ -215,9 +223,10 @@ class Grass7Algorithm(GeoAlgorithm):
self.tr('GRASS GIS 7 folder is not configured. Please '
'configure it before running GRASS GIS 7 algorithms.'))
commands = []
# Create brand new commands lists
self.commands = []
self.outputCommands = []
self.exportedLayers = {}
outputCommands = []
# If GRASS session has been created outside of this algorithm then
# get the list of layers loaded in GRASS otherwise start a new
@ -228,8 +237,57 @@ class Grass7Algorithm(GeoAlgorithm):
else:
Grass7Utils.startGrass7Session()
# 1: Export layer to grass mapset
# Handle ext functions for inputs/command/outputs
if self.module:
if hasattr(self.module, 'processInputs'):
func = getattr(self.module, 'processInputs')
func(self)
else:
self.processInputs()
if hasattr(self.module, 'processCommand'):
func = getattr(self.module, 'processCommand')
func(self)
else:
self.processCommand()
if hasattr(self.module, 'processOutputs'):
func = getattr(self.module, 'processOutputs')
func(self)
else:
self.processOutputs()
else:
self.processInputs()
self.processCommand()
self.processOutputs()
# Run GRASS
loglines = []
loglines.append(self.tr('GRASS GIS 7 execution self.commands'))
for line in self.commands:
progress.setCommand(line)
loglines.append(line)
if ProcessingConfig.getSetting(Grass7Utils.GRASS_LOG_COMMANDS):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
Grass7Utils.executeGrass7(self.commands, progress, self.outputCommands)
for out in self.outputs:
if isinstance(out, OutputHTML):
with open(self.getOutputFromName("rawoutput").value) as f:
rawOutput = "".join(f.readlines())
with open(out.value, "w") as f:
f.write("<pre>%s</pre>" % rawOutput)
# If the session has been created outside of this algorithm, add
# the new GRASS GIS 7 layers to it otherwise finish the session
if existingSession:
Grass7Utils.addSessionLayers(self.exportedLayers)
else:
Grass7Utils.endGrass7Session()
def processInputs(self):
"""Prepare the GRASS import commands"""
for param in self.parameters:
if isinstance(param, ParameterRaster):
if param.value is None:
@ -241,8 +299,8 @@ class Grass7Algorithm(GeoAlgorithm):
if value in self.exportedLayers.keys():
continue
else:
self.setSessionProjectionFromLayer(value, commands)
commands.append(self.exportRasterLayer(value))
self.setSessionProjectionFromLayer(value, self.commands)
self.commands.append(self.exportRasterLayer(value))
if isinstance(param, ParameterVector):
if param.value is None:
continue
@ -250,8 +308,8 @@ class Grass7Algorithm(GeoAlgorithm):
if value in self.exportedLayers.keys():
continue
else:
self.setSessionProjectionFromLayer(value, commands)
commands.append(self.exportVectorLayer(value))
self.setSessionProjectionFromLayer(value, self.commands)
self.commands.append(self.exportVectorLayer(value))
if isinstance(param, ParameterTable):
pass
if isinstance(param, ParameterMultipleInput):
@ -265,17 +323,17 @@ class Grass7Algorithm(GeoAlgorithm):
if layer in self.exportedLayers.keys():
continue
else:
self.setSessionProjectionFromLayer(layer, commands)
commands.append(self.exportRasterLayer(layer))
self.setSessionProjectionFromLayer(layer, self.commands)
self.commands.append(self.exportRasterLayer(layer))
elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY:
for layer in layers:
if layer in self.exportedLayers.keys():
continue
else:
self.setSessionProjectionFromLayer(layer, commands)
commands.append(self.exportVectorLayer(layer))
self.setSessionProjectionFromLayer(layer, self.commands)
self.commands.append(self.exportVectorLayer(layer))
self.setSessionProjectionFromProject(commands)
self.setSessionProjectionFromProject(self.commands)
region = \
unicode(self.getParameterValue(self.GRASS_REGION_EXTENT_PARAMETER))
@ -295,13 +353,14 @@ class Grass7Algorithm(GeoAlgorithm):
self.getParameterValue(self.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
commands.append(command)
# 2: Set parameters and outputs
self.commands.append(command)
def processCommand(self):
"""Prepare the GRASS algorithm command"""
command = self.grass7Name
command += ' ' + ' '.join(self.hardcodedStrings)
# Add algorithm command
for param in self.parameters:
if param.value is None or param.value == '':
continue
@ -331,14 +390,13 @@ class Grass7Algorithm(GeoAlgorithm):
else:
command += ' ' + param.name + '="' + unicode(param.value) + '"'
uniqueSufix = unicode(uuid.uuid4()).replace('-', '')
for out in self.outputs:
if isinstance(out, OutputFile):
command += ' > ' + out.value
elif not isinstance(out, OutputHTML):
# We add an output name to make sure it is unique if the session
# uses this algorithm several times.
uniqueOutputName = out.name + uniqueSufix
uniqueOutputName = out.name + self.uniqueSufix
command += ' ' + out.name + '=' + uniqueOutputName
# Add output file to exported layers, to indicate that
@ -346,55 +404,55 @@ class Grass7Algorithm(GeoAlgorithm):
self.exportedLayers[out.value] = uniqueOutputName
command += ' --overwrite'
commands.append(command)
# 3: Export resulting layers to a format that qgis can read
self.commands.append(command)
def processOutputs(self):
"""Prepare the GRASS v.out.ogr commands"""
for out in self.outputs:
if isinstance(out, OutputRaster):
filename = out.value
# Raster layer output: adjust region to layer before
# exporting
commands.append('g.region raster=' + out.name + uniqueSufix)
outputCommands.append('g.region raster=' + out.name
+ uniqueSufix)
self.commands.append('g.region raster=' + out.name + self.uniqueSufix)
self.outputCommands.append('g.region raster=' + out.name
+ self.uniqueSufix)
if self.grass7Name == 'r.statistics':
# r.statistics saves its results in a non-qgis compatible
# way. Post-process them with r.mapcalc.
calcExpression = 'correctedoutput' + uniqueSufix
calcExpression += '=@' + out.name + uniqueSufix
calcExpression = 'correctedoutput' + self.uniqueSufix
calcExpression += '=@' + out.name + self.uniqueSufix
command = 'r.mapcalc expression="' + calcExpression + '"'
commands.append(command)
outputCommands.append(command)
self.commands.append(command)
self.outputCommands.append(command)
command = 'r.out.gdal -c createopt="TFW=YES,COMPRESS=LZW"'
command += ' input='
command += 'correctedoutput' + uniqueSufix
command += 'correctedoutput' + self.uniqueSufix
command += ' output="' + filename + '"'
elif self.grass7Name == 'r.composite':
command = 'r.out.gdal -c createopt="TFW=YES,COMPRESS=LZW"'
command += ' input='
command += 'correctedoutput' + uniqueSufix
command += 'correctedoutput' + self.uniqueSufix
command += ' output="' + filename + '"'
else:
command = 'r.out.gdal -c createopt="TFW=YES,COMPRESS=LZW"'
command += ' input='
if self.grass7Name == 'r.horizon':
command += out.name + uniqueSufix + '_0'
command += out.name + self.uniqueSufix + '_0'
elif self.grass7Name == 'r.composite':
commands.append(command)
outputCommands.append(command)
self.commands.append(command)
self.outputCommands.append(command)
elif self.grass7Name == 'r.statistics':
commands.append(command)
outputCommands.append(command)
self.commands.append(command)
self.outputCommands.append(command)
else:
command += out.name + uniqueSufix
command += out.name + self.uniqueSufix
command += ' output="' + filename + '"'
commands.append(command)
outputCommands.append(command)
self.commands.append(command)
self.outputCommands.append(command)
if isinstance(out, OutputVector):
filename = out.value
@ -402,49 +460,23 @@ class Grass7Algorithm(GeoAlgorithm):
outtype = ('auto' if typeidx
is None else self.OUTPUT_TYPES[typeidx])
if self.grass7Name == 'r.flow':
command = 'v.out.ogr type=line layer=0 -s -e input=' + out.name + uniqueSufix
command = 'v.out.ogr type=line layer=0 -s -e input=' + out.name + self.uniqueSufix
elif self.grass7Name == 'v.voronoi':
if '-l' in command:
command = 'v.out.ogr type=line layer=0 -s -e input=' + out.name + uniqueSufix
if '-l' in self.commands[-1]:
command = 'v.out.ogr type=line layer=0 -s -e input=' + out.name + self.uniqueSufix
else:
command = 'v.out.ogr -s -e input=' + out.name + uniqueSufix
command = 'v.out.ogr -s -e input=' + out.name + self.uniqueSufix
command += ' type=' + outtype
elif self.grass7Name == 'v.sample':
command = 'v.out.ogr type=point -s -e input=' + out.name + uniqueSufix
command = 'v.out.ogr type=point -s -e input=' + out.name + self.uniqueSufix
else:
command = 'v.out.ogr -s -e input=' + out.name + uniqueSufix
command = 'v.out.ogr -s -e input=' + out.name + self.uniqueSufix
command += ' type=' + outtype
command += ' output="' + os.path.dirname(out.value) + '"'
command += ' format=ESRI_Shapefile'
command += ' output_layer=' + os.path.basename(out.value)[:-4]
commands.append(command)
outputCommands.append(command)
# 4: Run GRASS
loglines = []
loglines.append(self.tr('GRASS GIS 7 execution commands'))
for line in commands:
progress.setCommand(line)
loglines.append(line)
if ProcessingConfig.getSetting(Grass7Utils.GRASS_LOG_COMMANDS):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
Grass7Utils.executeGrass7(commands, progress, outputCommands)
for out in self.outputs:
if isinstance(out, OutputHTML):
with open(self.getOutputFromName("rawoutput").value) as f:
rawOutput = "".join(f.readlines())
with open(out.value, "w") as f:
f.write("<pre>%s</pre>" % rawOutput)
# If the session has been created outside of this algorithm, add
# the new GRASS GIS 7 layers to it otherwise finish the session
if existingSession:
Grass7Utils.addSessionLayers(self.exportedLayers)
else:
Grass7Utils.endGrass7Session()
self.commands.append(command)
self.outputCommands.append(command)
def exportVectorLayer(self, orgFilename):
@ -485,7 +517,7 @@ class Grass7Algorithm(GeoAlgorithm):
command = 'g.proj'
command += ' -c'
command += ' proj4="' + proj4 + '"'
commands.append(command)
self.commands.append(command)
Grass7Utils.projectionSet = True
def setSessionProjectionFromLayer(self, layer, commands):
@ -496,7 +528,7 @@ class Grass7Algorithm(GeoAlgorithm):
command = 'g.proj'
command += ' -c'
command += ' proj4="' + proj4 + '"'
commands.append(command)
self.commands.append(command)
Grass7Utils.projectionSet = True
def exportRasterLayer(self, layer):
@ -523,11 +555,8 @@ class Grass7Algorithm(GeoAlgorithm):
return msg
def checkParameterValuesBeforeExecuting(self):
name = self.commandLineName().replace('.', '_')[len('grass7:'):]
try:
module = importlib.import_module('processing.algs.grass7.ext.' + name)
except ImportError:
return
if hasattr(module, 'checkParameterValuesBeforeExecuting'):
func = getattr(module, 'checkParameterValuesBeforeExecuting')
return func(self)
if self.module:
if hasattr(self.module, 'checkParameterValuesBeforeExecuting'):
func = getattr(self.module, 'checkParameterValuesBeforeExecuting')
return func(self)
return

View File

@ -0,0 +1,13 @@
v.net.alloc
Allocates subnets for nearest centers (direction from center)
Vector (v.*)
ParameterVector|input|Input vector line layer (arcs)|1|False
ParameterVector|points|Centers point layer (nodes)|0|False
ParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|0.0|None|50.0|False
*ParameterString|center_cats|Category values|1-100000|False|False
*ParameterSelection|arc_type|Arc type|line;boundary;line,boundary|2
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (number)|input|0|True
*ParameterTableField|arc_backward_column|Arc backward direction cost column (number)|input|0|True
*ParameterTableField|node_column|Node cost column (number)|points|0|True
*ParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
OutputVector|output|Network_Alloc

View File

@ -0,0 +1,13 @@
v.net.allpairs
Computes the shortest path between all pairs of nodes in the network
Vector (v.*)
ParameterVector|input|Input vector line layer (arcs)|1|False
ParameterVector|points|Centers point layer (nodes)|0|False
ParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|0.0|None|50.0|False
*ParameterString|cats|Category values|1-10000|False|False
*ParameterString|where|WHERE condition of SQL statement without 'where' keyword'||True|False
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (number)|input|0|True
*ParameterTableField|arc_backward_column|Arc backward direction cost column (number)|input|0|True
*ParameterTableField|node_column|Node cost column (number)|points|0|True
*ParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
OutputVector|output|Network_Allpairs

View File

@ -0,0 +1,7 @@
v.net
v.net.arcs - Creates arcs from a file of points
Vector (v.*)
ParameterVector|points|Input vector point layer (nodes)|0|False
ParameterFile|file|Name of input arcs file|False|False
Hardcoded|operation=arcs
OutputVector|output|Arcs

View File

@ -0,0 +1,8 @@
v.net.bridge
v.net.articulation - Computes articulation points in the network
Vector (v.*)
ParameterVector|input|Input vector line layer (network)|1|False
Hardcoded|method=articulation
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (name)|input|0|True
*ParameterTableField|arc_backward_column|Arc backward direction cost column (name)|input|0|True
OutputVector|output|Articulation

View File

@ -0,0 +1,8 @@
v.net.bridge
Computes bridges in the network.
Vector (v.*)
ParameterVector|input|Input vector line layer (network)|1|False
Hardcoded|method=bridge
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (name)|input|0|True
*ParameterTableField|arc_backward_column|Arc backward direction cost column (name)|input|0|True
OutputVector|output|Bridge

View File

@ -0,0 +1,17 @@
v.net.centrality
Computes degree, centrality, betweeness, closeness and eigenvector centrality measures in the network.
Vector (v.*)
ParameterVector|input|Input vector line layer (network)|1|False
ParameterString|degree|Name of degree centrality column|degree|False|True
ParameterString|closeness|Name of closeness centrality column|closeness|False|True
ParameterString|betweenness|Name of betweenness centrality column||False|True
ParameterString|eigenvector|Name of eigenvector centrality column||False|True
*ParameterNumber|iterations|Maximum number of iterations to compute eigenvector centrality|1|None|1000|True
*ParameterNumber|error|Cumulative error tolerance for eigenvector centrality|0.001|None|0.1|True
*ParameterString|cats|Category values||False|True
*ParameterString|where|WHERE conditions of SQL statement without 'where' keyword||True|True
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (number)|input|0|True
*ParameterTableField|arc_backward_column|Arc backward direction cost column (number)|input|0|True
*ParameterBoolean|-a|Add points on nodes|True|True
*ParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
OutputVector|output|Network_Centrality

View File

@ -0,0 +1,10 @@
v.net.components
Computes strongly and weakly connected components in the network.
Vector (v.*)
ParameterVector|input|Input vector line layer (network)|1|False
ParameterSelection|method|Type of components|weak;strong|0|False
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (number)|input|0|True
*ParameterTableField|arc_backward_column|Arc backward direction cost column (number)|input|0|True
*ParameterBoolean|-a|Add points on nodes|True|True
OutputVector|output|Network_Components_Line
OutputVector|output_point|Network_Components_Point

View File

@ -0,0 +1,9 @@
v.net
v.net.connect - Connects points to nearest arcs in a network
Vector (v.*)
ParameterVector|input|Input vector line layer (arcs)|1|False
ParameterVector|points|Input vector point layer (nodes)|0|False
Hardcoded|operation=connect
ParameterNumber|threshold|Threshold for connection distance|None|None|50.0
*ParameterBoolean|-s|Snap points to network|False
OutputVector|output|Network

View File

@ -0,0 +1,14 @@
v.net.connectivity
Computes vertex connectivity between two sets of nodes in the network.
Vector (v.*)
ParameterVector|input|Input vector line layer (network)|1|False
ParameterVector|points|Input vector point layer (first set of nodes)|0|False
ParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|0.0|None|50.0|False
ParameterString|set1_cats|Set1 Category values||False|True
ParameterString|set1_where|Set1 WHERE conditions of SQL statement without 'where' keyword||True|True
ParameterString|set2_cats|Set2 Category values||False|True
ParameterString|set2_where|Set2 WHERE conditions of SQL statement without 'where' keyword||True|True
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (number)|input|0|True
*ParameterTableField|arc_backward_column|Arc backward direction cost column (number)|input|0|True
*ParameterTableField|node_column|Node cost column (number)|points|0|True
OutputVector|output|Network_Connectivity

View File

@ -0,0 +1,18 @@
v.net.distance
Computes shortest distance via the network between the given sets of features.
Vector (v.*)
ParameterVector|input|Input vector line layer (network)|1|False
ParameterVector|from_points|Input vector point layer (from)|0|False
ParameterVector|to_points|Input vector point layer (to)|0|False
ParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|0.0|None|50.0|False
*ParameterSelection|arc_type|Arc type|line;boundary;line,boundary|2
*ParameterString|from_cats|From Category values||False|True
*ParameterString|from_where|From WHERE conditions of SQL statement without 'where' keyword||True|True
*ParameterSelection|to_type|To feature type|point;line;boundary|0
*ParameterString|to_cats|To Category values||False|True
*ParameterString|to_where|To WHERE conditions of SQL statement without 'where' keyword||True|True
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (number)|input|0|True
*ParameterTableField|arc_backward_column|Arc backward direction cost column (number)|input|0|True
*ParameterTableField|node_column|Node cost column (number)|from_points|0|True
*ParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
OutputVector|output|Network_Distance

View File

@ -0,0 +1,15 @@
v.net.flow
Computes the maximum flow between two sets of nodes in the network.
Vector (v.*)
ParameterVector|input|Input vector line layer (network)|1|False
ParameterVector|points|Input vector point layer (flow nodes)|0|False
ParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|0.0|None|50.0|False
ParameterString|source_cats|Source Category values||False|True
ParameterString|source_where|Source WHERE conditions of SQL statement without 'where' keyword||True|True
ParameterString|sink_cats|Sink Category values||False|True
ParameterString|sink_where|Sink WHERE conditions of SQL statement without 'where' keyword||True|True
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (number)|input|0|True
*ParameterTableField|arc_backward_column|Arc backward direction cost column (number)|input|0|True
*ParameterTableField|node_column|Node cost column (number)|points|0|True
OutputVector|output|Network_Flow
OutputVector|cut|Network_Cut

View File

@ -0,0 +1,14 @@
v.net.iso
Splits network by cost isolines.
Vector (v.*)
ParameterVector|input|Input vector line layer (arcs)|1|False
ParameterVector|points|Centers point layer (nodes)|0|False
ParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|0.0|None|50.0|False
*ParameterSelection|arc_type|Arc type|line;boundary;line,boundary|2
*ParameterString|center_cats|Category values|1-100000|False|False
ParameterString|costs|Costs for isolines|1000,2000,3000|False|False
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (number)|input|0|True
*ParameterTableField|arc_backward_column|Arc backward direction cost column (number)|input|0|True
*ParameterTableField|node_column|Node cost column (number)|points|0|True
*ParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
OutputVector|output|Network_Iso

View File

@ -0,0 +1,7 @@
v.net
v.net.nodes - Creates points for each network arcs
Vector (v.*)
ParameterVector|input|Input vector line layer (arcs)|1|False
Hardcoded|operation=nodes
*ParameterBoolean|-c|Assign unique categories to new points (for operation 'nodes')|False
OutputVector|output|Nodes

View File

@ -0,0 +1,7 @@
v.net
v.net.nreport - Reports nodes information of a network
Vector (v.*)
ParameterVector|input|Input vector line layer (arcs)|1|False
Hardcoded|operation=nreport
OutputFile|output|NReport

View File

@ -0,0 +1,16 @@
v.net.path
Finds shortest path on vector network
Vector (v.*)
ParameterVector|input|Input vector line layer (arcs)|1|False
ParameterVector|points|Centers point layer (nodes)|0|False
ParameterFile|file|Name of file containing start and end points|False|False
ParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|0.0|None|50.0|False
*ParameterSelection|type|Arc type|line;boundary;line,boundary|2
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (number)|input|0|True
*ParameterTableField|arc_backward_column|Arc backward direction cost column (number)|input|0|True
*ParameterTableField|node_column|Node cost column (number)|points|0|True
*ParameterNumber|dmax|Maximum distance to the network|None|None|1000.0|True
*ParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
*ParameterBoolean|-s|Write output as original input segments, not each path as one line|False|True
OutputVector|output|Network_Path

View File

@ -0,0 +1,7 @@
v.net
v.net.report - Reports lines information of a network
Vector (v.*)
ParameterVector|input|Input vector line layer (arcs)|1|False
Hardcoded|operation=report
OutputFile|output|Report

View File

@ -0,0 +1,13 @@
v.net.salesman
Creates a cycle connecting given nodes (Traveling salesman problem)
Vector (v.*)
ParameterVector|input|Input vector line layer (arcs)|1|False
ParameterVector|points|Centers point layer (nodes)|0|False
ParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|0.0|None|50.0|False
*ParameterSelection|arc_type|Arc type|line;boundary;line,boundary|2
*ParameterString|center_cats|Category values|1-100000|False|False
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (number)|input|0|True
*ParameterTableField|arc_backward_column|Arc backward direction cost column (number)|input|0|True
*ParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
OutputVector|output|Network_Salesman
OutputTable|sequence|Salesman_Nodes_Sequence

View File

@ -0,0 +1,7 @@
v.net.spanningtree
Computes minimum spanning tree for the network.
Vector (v.*)
ParameterVector|input|Input vector line layer (arcs)|1|False
*ParameterTableField|arc_column|Arc forward/both direction(s) cost column (number)|input|0|True
*ParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
OutputVector|output|SpanningTree

View File

@ -0,0 +1,12 @@
v.net.steiner
Creates Steiner tree for the network and given terminals
Vector (v.*)
ParameterVector|input|Input vector line layer (arcs)|1|False
ParameterVector|points|Centers point layer (nodes)|0|False
ParameterNumber|threshold|Threshold for connecting centers to the network (in map unit)|0.0|None|50.0|False
*ParameterSelection|arc_type|Arc type|line;boundary;line,boundary|2
*ParameterString|terminal_cats|Category values|1-100000|False|False
*ParameterTableField|acolumn|Arc forward/both direction(s) cost column (number)|input|0|True
*ParameterNumber|npoints|Maximum distance to the network|None|None|-1|True
*ParameterBoolean|-g|Use geodesic calculation for longitude-latitude locations|False|True
OutputVector|output|Network_Steiner

View File

@ -0,0 +1,7 @@
v.net.visibility
Performs visibility graph construction.
Vector (v.*)
ParameterVector|input|Input vector line layer (arcs)|-1|False
*ParameterString|coordinates|Coordinates||False|True
*ParameterVector|visibility|Input vector line layer containing visable points|1|True
OutputVector|output|Network_Visibility

View File

@ -0,0 +1,3 @@
FILE(GLOB PY_FILES *.py)
PLUGIN_INSTALL(processing algs/grass7/ext ${PY_FILES})

View File

@ -0,0 +1,105 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net.py
--------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
This Python module handles pre-treatment operations for v.net.* GRASS7 modules.
Before using a v.net module you often have to incorporate a points layer into
the network vector map.
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
from processing.core.parameters import getParameterFromString, ParameterVector, ParameterNumber, ParameterBoolean, ParameterString
def incorporatePoints(alg, pointLayerName=u'points', networkLayerName=u'input'):
"""
incorporate points with lines to form a GRASS network
"""
paramsToDelete = []
# Create an intermediate GRASS layer which is the combination of network + centers
intLayer = alg.getTempFilename()
# Grab the point layer and delete this parameter (not used by v.net.alloc)
pointLayer = alg.getParameterValue(pointLayerName)
if pointLayer:
pointLayer = alg.exportedLayers[pointLayer]
paramsToDelete.append(alg.getParameterFromName(u'points'))
# Grab the network layer and tell to v.net.alloc to use the temp layer instead
lineLayer = alg.getParameterValue(networkLayerName)
if lineLayer:
lineLayer = alg.exportedLayers[lineLayer]
alg.setParameterValue(networkLayerName, intLayer)
threshold = alg.getParameterValue(u'threshold')
paramsToDelete.append(alg.getParameterFromName(u'threshold'))
# Create the v.net connect command for point layer integration
command = u"v.net -s input={} points={} out={} op=connect threshold={}".format(
lineLayer, pointLayer, intLayer, threshold)
alg.commands.append(command)
# Connect the point layer database to the layer 2 of the network
command = u"v.db.connect -o map={} table={} layer=2".format(intLayer, pointLayer)
alg.commands.append(command)
# Delete some unnecessary parameters
for param in paramsToDelete:
alg.parameters.remove(param)
alg.processCommand()
# Bring back the parameters:
for param in paramsToDelete:
alg.parameters.append(param)
def variableOutput(alg, params, nocats=True):
""" Handle variable data output for v.net modules:
params is like:
{ u"output": [u"point", 1], # One output of type point from layer 1
u"output2": [u"line", 1], # One output of type line from layer 1
u"output3: [u"point", 2] # one output of type point from layer 2
}
"""
# Build the v.out.ogr commands
for outputName, typeList in params.iteritems():
if not isinstance(typeList, list):
continue
out = alg.getOutputValue(outputName)
command = u"v.out.ogr {} type={} layer={} -s -e input={} output=\"{}\" format=ESRI_Shapefile output_layer={}".format(
u"" if typeList[0] == u"line" and nocats else u"-c",
typeList[0],
typeList[1],
alg.exportedLayers[out],
os.path.dirname(out),
os.path.basename(out)[:-4]
)
alg.commands.append(command)
alg.outputCommands.append(command)

View File

@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_alloc.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import incorporatePoints, variableOutput
def processCommand(alg):
incorporatePoints(alg)
def processOutputs(alg):
outputParameter = {u"output": [u"line", 1]}
variableOutput(alg, outputParameter, False)

View File

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_allpairs.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import incorporatePoints
def processCommand(alg):
incorporatePoints(alg)

View File

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_arcs.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import variableOutput
def processOutputs(alg):
outputParameter = {u"output": [u"line", 1]}
variableOutput(alg, outputParameter)

View File

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_articulation.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import variableOutput
def processOutputs(alg):
outputParameter = {u"output": [u"point", 1]}
variableOutput(alg, outputParameter)

View File

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_bridge.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import variableOutput
def processOutputs(alg):
outputParameter = {u"output": [u"line", 1]}
variableOutput(alg, outputParameter)

View File

@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_components.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import variableOutput
def processCommand(alg):
# remove the output for point
outLine = alg.getOutputFromName(u'output')
outPoint = alg.getOutputFromName(u'output_point')
alg.exportedLayers[outPoint.value] = outLine.name + alg.uniqueSufix
alg.removeOutputFromName(u'output_point')
alg.processCommand()
# Re-add output for point
alg.addOutput(outPoint)
def processOutputs(alg):
outputParameter = {u"output": [u"line", 1],
u"output_point": [u"point", 2]}
variableOutput(alg, outputParameter)

View File

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_connect.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
def processOutputs(alg):
out = alg.getOutputValue(u"output")
command = u"v.out.ogr -c type=line layer=1 -e input={} output=\"{}\" format=ESRI_Shapefile output_layer={}".format(
alg.exportedLayers[out],
os.path.dirname(out),
os.path.basename(out)[:-4]
)
alg.commands.append(command)
alg.outputCommands.append(command)

View File

@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_connectivity.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import incorporatePoints, variableOutput
def checkParameterValuesBeforeExecuting(alg):
""" Verify if we have the right parameters """
params = [u'where', u'cats']
values = []
for param in params:
for i in range(1, 3):
values.append(alg.getParameterValue(u'set{}_{}'.format(i, param)))
if (values[0] or values[2]) and (values[1] or values[3]):
return None
return alg.tr("You need to set at least setX_where or setX_cats parameters for each set !")
def processCommand(alg):
incorporatePoints(alg)
def processOutputs(alg):
outputParameter = {u"output": [u"point", 2]}
variableOutput(alg, outputParameter)

View File

@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_distance.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from processing.core.parameters import getParameterFromString
def processCommand(alg):
""" Handle data preparation for v.net.distance:
* Integrate point layers into network vector map.
* Make v.net.distance use those layers.
* Delete the threshold parameter.
* If where statement, connect to the db
"""
paramsToDelete = []
# Grab the threshold value for our v.net connect command
threshold = alg.getParameterValue(u'threshold')
if threshold:
paramsToDelete.append(alg.getParameterFromName(u'threshold'))
# Grab the network layer and tell to v.net.alloc to use the temp layer instead
line_layer = alg.getParameterValue(u'input')
if line_layer:
line_layer = alg.exportedLayers[line_layer]
# import the two point layers into the network
for i, layer in enumerate([u'from', u'to']):
# Get a temp layer name
intLayer = alg.getTempFilename()
# Grab the from point layer and delete this parameter (not used by v.net.distance)
point_layer = alg.getParameterValue(layer + u'_points')
if point_layer:
point_layer = alg.exportedLayers[point_layer]
paramsToDelete.append(alg.getParameterFromName(layer + u'_points'))
# Create the v.net connect command for point layer integration
command = u"v.net -s input={} points={} out={} op=connect threshold={} arc_layer=1 node_layer={}".format(line_layer, point_layer, intLayer, threshold, i + 2)
alg.commands.append(command)
line_layer = intLayer
# Add the parameter to the algorithm
parameter = alg.getParameterFromName(u'{}_layer'.format(layer))
if not parameter:
parameter = getParameterFromString(u'ParameterNumber|{0}_layer|{0} layer number|1|3|2|False'.format(layer))
alg.addParameter(parameter)
parameter.setValue(i + 2)
# Make the connection with attribute table
command = u"v.db.connect -o map={} table={} layer={}".format(line_layer, point_layer, i + 2)
alg.commands.append(command)
alg.setParameterValue(u'input', line_layer)
# Delete some unnecessary parameters
for param in paramsToDelete:
alg.parameters.remove(param)
alg.processCommand()
# Bring back the parameters:
for param in paramsToDelete:
alg.parameters.append(param)
# Delete from_layer and to_layer
for word in [u'from', u'to']:
alg.parameters.remove(alg.getParameterFromName(u'{}_layer'.format(word)))

View File

@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_flow.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import incorporatePoints, variableOutput
def checkParameterValuesBeforeExecuting(alg):
""" Verify if we have the right parameters """
params = [u'where', u'cats']
values = []
for param in params:
for i in [u'source', u'sink']:
values.append(alg.getParameterValue(u'{}_{}'.format(i, param)))
if (values[0] or values[2]) and (values[1] or values[3]):
return None
return alg.tr("You need to set at least source/sink_where or source/sink_cats parameters for each set !")
def processCommand(alg):
incorporatePoints(alg)
def processOutputs(alg):
outputParameter = {u"output": [u"line", 1],
u"cut": [u"line", 1]}
variableOutput(alg, outputParameter)

View File

@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_iso.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import incorporatePoints
def processCommand(alg):
incorporatePoints(alg)

View File

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_nodes.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import variableOutput
def processOutputs(alg):
outputParameter = {u"output": [u"point", 2]}
variableOutput(alg, outputParameter)

View File

@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_path.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import incorporatePoints
def processCommand(alg):
incorporatePoints(alg)

View File

@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_salesman.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from processing.core.parameters import getParameterFromString
from v_net import incorporatePoints
def processCommand(alg):
# We temporary remove the output 'sequence'
sequence = alg.getOutputFromName(u'sequence')
sequenceFile = alg.getOutputValue(u'sequence')
alg.exportedLayers[sequence.value] = sequence.name + alg.uniqueSufix
alg.removeOutputFromName(u'sequence')
# We create a new parameter with the same name
param = getParameterFromString(u"ParameterString|sequence|sequence|None|False|False")
param.setValue(sequenceFile)
alg.addParameter(param)
# Let's do the incorporation and command generation
incorporatePoints(alg)
# then we delete the input parameter and add the old output
alg.parameters.remove(param)
alg.addOutput(sequence)

View File

@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_steiner.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import incorporatePoints
def processCommand(alg):
incorporatePoints(alg)

View File

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
v_net_visibility.py
---------------------
Date : December 2015
Copyright : (C) 2015 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'December 2015'
__copyright__ = '(C) 2015, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from v_net import variableOutput
def processOutputs(alg):
outputParameter = {u"output": [u"line", 1]}
variableOutput(alg, outputParameter, False)