Skip to content

Commit

Permalink
Merge pull request #2787 from medspx/processing_vdistance
Browse files Browse the repository at this point in the history
[processing] add GRASS7 v.distance (fix #12817)
  • Loading branch information
alexbruy committed Mar 2, 2016
2 parents 0061dcc + 4196293 commit 61e5799
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
14 changes: 14 additions & 0 deletions python/plugins/processing/algs/grass7/description/v.distance.txt
@@ -0,0 +1,14 @@
v.distance
Finds the nearest element in vector map 'to' for elements in vector map 'from'.
Vector (v.*)
ParameterVector|from|'from' vector map|-1|False
*ParameterString|from_type|'from' feature type (point,line,boundary,centroid,area)|point,line,area|False|False
ParameterVector|to|'to' vector map|-1|False
*ParameterString|to_type|'to' feature type (point,line,boundary,centroid,area)|point,line,area|False|False
ParameterNumber|dmax|Maximum distance or -1.0 for no limit|-1.0|None|-1.0|False
ParameterNumber|dmin|Minimum distance or -1.0 for no limit|-1.0|None|-1.0|False
ParameterString|upload|'upload': Values describing the relation between two nearest features (cat,dist,to_x,to_y,to_along,to_angle,to_attr)|cat|False|True
ParameterString|column|Column name(s) where values specified by 'upload' option will be uploaded|None|False|True
ParameterTableField|to_column|Column name of nearest feature (used with upload=to_attr)|to|-1|True
OutputVector|from_output|Nearest
OutputVector|output|Distance
77 changes: 77 additions & 0 deletions python/plugins/processing/algs/grass7/ext/v_distance.py
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
v_distance.py
-------------
Date : February 2016
Copyright : (C) 2016 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__ = 'February 2016'
__copyright__ = '(C) 2016, Médéric Ribreux'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import os


def checkParameterValuesBeforeExecuting(alg):
""" Verify if we have the right parameters """
# Verify upload value
upload = alg.getParameterValue(u'upload')
if upload:
uploadList = [f for f in upload.split(",") if f not in ['cat', 'dist', 'to_x', 'to_y', 'to_along', 'to_angle', 'to_attr']]
if len(uploadList) > 0:
return alg.tr(u"Upload parameters should be a list of elements taken in the following values:\n'cat', 'dist', 'to_x', 'to_y', 'to_along', 'to_angle', 'to_attr'")
# Verifiy that we have the good number of columns
column = alg.getParameterValue(u'column')
if ((column is None or len(column) == 0) and upload) or (len(column.split(",")) != len(upload.split(","))):
return alg.tr(u"The number of columns and the number of upload parameters should be equal !")

# Verify from_type and to_type values
for geom in [u'from', u'to']:
geoType = alg.getParameterValue(u'{}_type'.format(geom))
if geoType:
geoTypeList = [f for f in geoType.split(",") if f not in ['point', 'line', 'boundary', 'centroid', 'area']]
if len(geoTypeList) > 0:
return alg.tr(u"Feature type for '{}' should be a list of elements taken in the following values:\n'point', 'line', 'boundary', 'centroid', 'area'".format(geom))

return None


def processCommand(alg):
# We temporary remove the output 'from_output'
fromOutput = alg.getOutputFromName(u'from_output')
fromParam = alg.getParameterValue(u'from')
alg.exportedLayers[fromOutput.value] = alg.exportedLayers[fromParam]
alg.removeOutputFromName(u'from_output')
alg.processCommand()

# We re-add the new output
alg.addOutput(fromOutput)


def processOutputs(alg):
# Output results ('from' table and output table)
for output in [u'from_output', u'output']:
out = alg.getOutputValue(output)
command = u"v.out.ogr -c type=auto -s -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)

0 comments on commit 61e5799

Please sign in to comment.