Prechádzať zdrojové kódy

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

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@63471 15284696-431f-4ddb-bdfa-cd5b030d7da7
Pietro Zambelli 10 rokov pred
rodič
commit
12cfc9613e
1 zmenil súbory, kde vykonal 15 pridanie a 11 odobranie
  1. 15 11
      lib/python/pygrass/vector/geometry.py

+ 15 - 11
lib/python/pygrass/vector/geometry.py

@@ -7,6 +7,7 @@ Created on Wed Jul 18 10:46:25 2012
 """
 import ctypes
 import re
+from collections import namedtuple
 
 import numpy as np
 
@@ -19,6 +20,8 @@ from grass.pygrass.vector.basic import Ilist, Bbox, Cats
 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 @@ class Line(Geo):
         :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 @@ class Line(Geo):
         # 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 @@ class Node(object):
         :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."""