Skip to content

Commit 4196293

Browse files
author
Médéric Ribreux
committed
[Processing] GRASS7 v.distance (implements Redmine #12817)
1 parent 9e0043c commit 4196293

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
v.distance
2+
Finds the nearest element in vector map 'to' for elements in vector map 'from'.
3+
Vector (v.*)
4+
ParameterVector|from|'from' vector map|-1|False
5+
*ParameterString|from_type|'from' feature type (point,line,boundary,centroid,area)|point,line,area|False|False
6+
ParameterVector|to|'to' vector map|-1|False
7+
*ParameterString|to_type|'to' feature type (point,line,boundary,centroid,area)|point,line,area|False|False
8+
ParameterNumber|dmax|Maximum distance or -1.0 for no limit|-1.0|None|-1.0|False
9+
ParameterNumber|dmin|Minimum distance or -1.0 for no limit|-1.0|None|-1.0|False
10+
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
11+
ParameterString|column|Column name(s) where values specified by 'upload' option will be uploaded|None|False|True
12+
ParameterTableField|to_column|Column name of nearest feature (used with upload=to_attr)|to|-1|True
13+
OutputVector|from_output|Nearest
14+
OutputVector|output|Distance
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
v_distance.py
6+
-------------
7+
Date : February 2016
8+
Copyright : (C) 2016 by Médéric Ribreux
9+
Email : medspx at medspx dot fr
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Médéric Ribreux'
21+
__date__ = 'February 2016'
22+
__copyright__ = '(C) 2016, Médéric Ribreux'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
30+
31+
def checkParameterValuesBeforeExecuting(alg):
32+
""" Verify if we have the right parameters """
33+
# Verify upload value
34+
upload = alg.getParameterValue(u'upload')
35+
if upload:
36+
uploadList = [f for f in upload.split(",") if f not in ['cat', 'dist', 'to_x', 'to_y', 'to_along', 'to_angle', 'to_attr']]
37+
if len(uploadList) > 0:
38+
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'")
39+
# Verifiy that we have the good number of columns
40+
column = alg.getParameterValue(u'column')
41+
if ((column is None or len(column) == 0) and upload) or (len(column.split(",")) != len(upload.split(","))):
42+
return alg.tr(u"The number of columns and the number of upload parameters should be equal !")
43+
44+
# Verify from_type and to_type values
45+
for geom in [u'from', u'to']:
46+
geoType = alg.getParameterValue(u'{}_type'.format(geom))
47+
if geoType:
48+
geoTypeList = [f for f in geoType.split(",") if f not in ['point', 'line', 'boundary', 'centroid', 'area']]
49+
if len(geoTypeList) > 0:
50+
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))
51+
52+
return None
53+
54+
55+
def processCommand(alg):
56+
# We temporary remove the output 'from_output'
57+
fromOutput = alg.getOutputFromName(u'from_output')
58+
fromParam = alg.getParameterValue(u'from')
59+
alg.exportedLayers[fromOutput.value] = alg.exportedLayers[fromParam]
60+
alg.removeOutputFromName(u'from_output')
61+
alg.processCommand()
62+
63+
# We re-add the new output
64+
alg.addOutput(fromOutput)
65+
66+
67+
def processOutputs(alg):
68+
# Output results ('from' table and output table)
69+
for output in [u'from_output', u'output']:
70+
out = alg.getOutputValue(output)
71+
command = u"v.out.ogr -c type=auto -s -e input={} output=\"{}\" format=ESRI_Shapefile output_layer={}".format(
72+
alg.exportedLayers[out],
73+
os.path.dirname(out),
74+
os.path.basename(out)[:-4]
75+
)
76+
alg.commands.append(command)
77+
alg.outputCommands.append(command)

0 commit comments

Comments
 (0)