Skip to content

Commit

Permalink
Merge pull request #2628 from medspx/work_vnet_processing
Browse files Browse the repository at this point in the history
[Feature][Processing] GRASS v.net modules
  • Loading branch information
alexbruy committed Jan 13, 2016
2 parents ea92e11 + 89a1ad5 commit 9551ca7
Show file tree
Hide file tree
Showing 41 changed files with 1,106 additions and 82 deletions.
2 changes: 2 additions & 0 deletions python/plugins/processing/algs/grass7/CMakeLists.txt
Expand Up @@ -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})
193 changes: 111 additions & 82 deletions python/plugins/processing/algs/grass7/Grass7Algorithm.py

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions python/plugins/processing/algs/grass7/description/v.net.alloc.txt
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
15 changes: 15 additions & 0 deletions python/plugins/processing/algs/grass7/description/v.net.flow.txt
@@ -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
14 changes: 14 additions & 0 deletions python/plugins/processing/algs/grass7/description/v.net.iso.txt
@@ -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
@@ -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
@@ -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

16 changes: 16 additions & 0 deletions python/plugins/processing/algs/grass7/description/v.net.path.txt
@@ -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

@@ -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

@@ -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
@@ -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
@@ -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
@@ -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
3 changes: 3 additions & 0 deletions python/plugins/processing/algs/grass7/ext/CMakeLists.txt
@@ -0,0 +1,3 @@
FILE(GLOB PY_FILES *.py)

PLUGIN_INSTALL(processing algs/grass7/ext ${PY_FILES})
File renamed without changes.
105 changes: 105 additions & 0 deletions python/plugins/processing/algs/grass7/ext/v_net.py
@@ -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)
38 changes: 38 additions & 0 deletions python/plugins/processing/algs/grass7/ext/v_net_alloc.py
@@ -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)

0 comments on commit 9551ca7

Please sign in to comment.