[GRASS-SVN] r63471 - grass/trunk/lib/python/pygrass/vector

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Dec 10 03:21:38 PST 2014


Author: zarch
Date: 2014-12-10 03:21:38 -0800 (Wed, 10 Dec 2014)
New Revision: 63471

Modified:
   grass/trunk/lib/python/pygrass/vector/geometry.py
Log:
pygrass: Fix a bug in the lines method of the class Node and improve documentation of the distance method of the Line class and change the returned value from tuple to namedtuple

Modified: grass/trunk/lib/python/pygrass/vector/geometry.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/geometry.py	2014-12-10 10:46:07 UTC (rev 63470)
+++ grass/trunk/lib/python/pygrass/vector/geometry.py	2014-12-10 11:21:38 UTC (rev 63471)
@@ -7,6 +7,7 @@
 """
 import ctypes
 import re
+from collections import namedtuple
 
 import numpy as np
 
@@ -19,6 +20,8 @@
 from grass.pygrass.vector import sql
 
 
+LineDist = namedtuple('LineDist', 'point dist spdist sldist')
+
 WKT = {'POINT\((.*)\)': 'point',  # 'POINT\(\s*([+-]*\d+\.*\d*)+\s*\)'
        'LINESTRING\((.*)\)': 'line'}
 
@@ -763,19 +766,20 @@
         :param pnt: the point to calculate distance
         :type pnt: a Point object or a tuple with the coordinates
 
-        Return a tuple with:
+        Return a namedtuple with:
 
-            * the closest point on the line,
-            * the distance between these two points,
-            * distance of point from segment beginning
-            * distance of point from line
+            * point: the closest point on the line,
+            * dist: the distance between these two points,
+            * spdist: distance to point on line from segment beginning
+            * sldist: distance to point on line form line beginning along line
 
         The distance is compute using the ``Vect_line_distance`` C function.
 
-            >>> line = Line([(0, 0), (0, 2)])
-            >>> line.distance(Point(1, 1))
-            (Point(0.000000, 1.000000), 1.0, 1.0, 1.0)
-
+            >>> point = Point(2.3, 0.5)
+            >>> line = Line([(0, 0), (2, 0), (3, 0)])
+            >>> line.distance(point)           #doctest: +NORMALIZE_WHITESPACE
+            LineDist(point=Point(2.300000, 0.000000),
+                     dist=0.5, spdist=0.2999999999999998, sldist=2.3)
         """
         # instantite outputs
         cx = ctypes.c_double(0)
@@ -795,7 +799,7 @@
         # instantiate the Point class
         point = Point(cx.value, cy.value, cz.value)
         point.is2D = self.is2D
-        return point, dist.value, sp_dist.value, lp_dist.value
+        return LineDist(point, dist.value, sp_dist.value, lp_dist.value)
 
     def get_first_cat(self):
         """Fetches FIRST category number for given vector line and field, using
@@ -1142,7 +1146,7 @@
         :type only_out: bool
         """
         for iline in self.ilines(only_in, only_out):
-            yield Line(id=abs(iline), c_mapinfo=self.c_mapinfo)
+            yield Line(v_id=abs(iline), c_mapinfo=self.c_mapinfo)
 
     def angles(self):
         """Return a generator with all lines angles in a node."""



More information about the grass-commit mailing list