Django

Code

Changeset 5991

Show
Ignore:
Timestamp:
08/20/07 22:24:22 (1 year ago)
Author:
jbronn
Message:

gis: geos: added length property and distance method; no more relative imports in geos module; tamed unruly docstrings in base.py.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/geos/base.py

    r5832 r5991  
    273273    ## Internal for GEOS unary & binary predicate functions ## 
    274274    def _unary_predicate(self, func): 
    275         "Returns the result, or raises an exception for the given unary predicate function." 
     275        """Returns the result, or raises an exception for the given unary  
     276        predicate function.""" 
    276277        val = func(self._ptr()) 
    277278        if val == 0: return False 
     
    280281 
    281282    def _binary_predicate(self, func, other, *args): 
    282         "Returns the result, or raises an exception for the given binary predicate function." 
     283        """Returns the result, or raises an exception for the given binary  
     284        predicate function.""" 
    283285        if not isinstance(other, GEOSGeometry): 
    284286            raise TypeError, 'Binary predicate operation ("%s") requires another GEOSGeometry instance.' % func.__name__ 
     
    323325 
    324326    def disjoint(self, other): 
    325         "Returns true if the DE-9IM intersection matrix for the two Geometries is FF*FF****." 
     327        """Returns true if the DE-9IM intersection matrix for the two Geometries  
     328        is FF*FF****.""" 
    326329        return self._binary_predicate(lgeos.GEOSDisjoint, other) 
    327330 
    328331    def touches(self, other): 
    329         "Returns true if the DE-9IM intersection matrix for the two Geometries is FT*******, F**T***** or F***T****." 
     332        """Returns true if the DE-9IM intersection matrix for the two Geometries  
     333        is FT*******, F**T***** or F***T****.""" 
    330334        return self._binary_predicate(lgeos.GEOSTouches, other) 
    331335 
     
    335339 
    336340    def crosses(self, other): 
    337         """Returns true if the DE-9IM intersection matrix for the two Geometries is T*T****** (for a point and a curve, 
    338         a point and an area or a line and an area) 0******** (for two curves).""" 
     341        """Returns true if the DE-9IM intersection matrix for the two Geometries 
     342        is T*T****** (for a point and a curve,a point and an area or a line and  
     343        an area) 0******** (for two curves).""" 
    339344        return self._binary_predicate(lgeos.GEOSCrosses, other) 
    340345 
    341346    def within(self, other): 
    342         "Returns true if the DE-9IM intersection matrix for the two Geometries is T*F**F***." 
     347        """Returns true if the DE-9IM intersection matrix for the two Geometries  
     348        is T*F**F***.""" 
    343349        return self._binary_predicate(lgeos.GEOSWithin, other) 
    344350 
     
    348354 
    349355    def overlaps(self, other): 
    350         """Returns true if the DE-9IM intersection matrix for the two Geometries is T*T***T** (for two points 
    351         or two surfaces) 1*T***T** (for two curves).""" 
     356        """Returns true if the DE-9IM intersection matrix for the two Geometries  
     357        is T*T***T** (for two points or two surfaces) 1*T***T** (for two curves).""" 
    352358        return self._binary_predicate(lgeos.GEOSOverlaps, other) 
    353359 
    354360    def equals(self, other): 
    355         "Returns true if the DE-9IM intersection matrix for the two Geometries is T*F**FFF*." 
     361        """Returns true if the DE-9IM intersection matrix for the two Geometries  
     362        is T*F**FFF*.""" 
    356363        return self._binary_predicate(lgeos.GEOSEquals, other) 
    357364 
    358365    def equals_exact(self, other, tolerance=0): 
    359         "Returns true if the two Geometries are exactly equal, up to a specified tolerance." 
    360         tol = c_double(tolerance) 
    361         return self._binary_predicate(lgeos.GEOSEqualsExact, other, tol) 
     366        """Returns true if the two Geometries are exactly equal, up to a  
     367        specified tolerance.""" 
     368        return self._binary_predicate(lgeos.GEOSEqualsExact, other,  
     369                                      c_double(tolerance)) 
    362370 
    363371    #### SRID Routines #### 
     
    394402    #### Topology Routines #### 
    395403    def _unary_topology(self, func, *args): 
    396         "Returns a GEOSGeometry for the given unary (only takes one geomtry as a paramter) topological operation." 
    397         return GEOSGeometry(func(self._ptr(), *args)) 
     404        """Returns a GEOSGeometry for the given unary (takes only one Geomtery  
     405        as a paramter) topological operation.""" 
     406        return GEOSGeometry(func(self._ptr(), *args), srid=self.srid) 
    398407 
    399408    def _binary_topology(self, func, other, *args): 
    400         "Returns a GEOSGeometry for the given binary (takes two geometries as parameters) topological operation." 
    401         return GEOSGeometry(func(self._ptr(), other._ptr(), *args)) 
     409        """Returns a GEOSGeometry for the given binary (takes two Geometries  
     410        as parameters) topological operation.""" 
     411        return GEOSGeometry(func(self._ptr(), other._ptr(), *args), srid=self.srid) 
    402412 
    403413    def buffer(self, width, quadsegs=8): 
     
    433443    @property 
    434444    def convex_hull(self): 
    435         "Returns the smallest convex Polygon that contains all the points in the Geometry." 
     445        """Returns the smallest convex Polygon that contains all the points  
     446        in the Geometry.""" 
    436447        return self._unary_topology(lgeos.GEOSConvexHull) 
    437448 
     
    469480        a = c_double() 
    470481        status = lgeos.GEOSArea(self._ptr(), byref(a)) 
    471         if not status: return None 
     482        if status != 1: return None 
    472483        else: return a.value 
    473484 
     485    def distance(self, other): 
     486        """Returns the distance between the closest points on this Geometry 
     487        and the other. Units will be in those of the coordinate system. of 
     488        the Geometry.""" 
     489        if not isinstance(other, GEOSGeometry):  
     490            raise TypeError, 'distance() works only on other GEOS Geometries.' 
     491        dist = c_double() 
     492        status = lgeos.GEOSDistance(self._ptr(), other._ptr(), byref(dist)) 
     493        if status != 1: return None 
     494        else: return dist.value 
     495 
     496    @property 
     497    def length(self): 
     498        """Returns the length of this Geometry (e.g., 0 for point, or the 
     499        circumfrence of a Polygon).""" 
     500        l = c_double() 
     501        status = lgeos.GEOSLength(self._ptr(), byref(l)) 
     502        if status != 1: return None 
     503        else: return l.value 
     504     
    474505    def clone(self): 
    475506        "Clones this Geometry." 
  • django/branches/gis/django/contrib/gis/geos/__init__.py

    r5760 r5991  
    3030""" 
    3131 
    32 from base import GEOSGeometry 
    33 from geometries import Point, LineString, LinearRing, Polygon, HAS_NUMPY 
    34 from collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon 
    35 from error import GEOSException, GEOSGeometryIndexError 
     32from django.contrib.gis.geos.base import GEOSGeometry 
     33from django.contrib.gis.geos.geometries import Point, LineString, LinearRing, Polygon, HAS_NUMPY 
     34from django.contrib.gis.geos.collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon 
     35from django.contrib.gis.geos.error import GEOSException, GEOSGeometryIndexError 
    3636 
    3737def fromstr(wkt_or_hex, **kwargs): 
  • django/branches/gis/django/contrib/gis/tests/test_geos.py

    r5832 r5991  
    613613        self.assertEqual((1.,2.,3.), ls[0]) 
    614614             
     615    def test18_distance(self): 
     616        "Testing the distance() function." 
     617        # Distance to self should be 0.  
     618        pnt = Point(0, 0) 
     619        self.assertEqual(0.0, pnt.distance(Point(0, 0))) 
     620     
     621        # Distance should be 1 
     622        self.assertEqual(1.0, pnt.distance(Point(0, 1))) 
     623 
     624        # Distance should be ~ sqrt(2) 
     625        self.assertAlmostEqual(1.41421356237, pnt.distance(Point(1, 1)), 11) 
     626 
     627        # Distances are from the closest vertex in each geometry -- 
     628        #  should be 3 (distance from (2, 2) to (5, 2)). 
     629        ls1 = LineString((0, 0), (1, 1), (2, 2)) 
     630        ls2 = LineString((5, 2), (6, 1), (7, 0)) 
     631        self.assertEqual(3, ls1.distance(ls2)) 
     632 
     633    def test19_length(self): 
     634        "Testing the length property." 
     635 
     636        # Points have 0 length. 
     637        pnt = Point(0, 0) 
     638        self.assertEqual(0.0, pnt.length) 
     639         
     640        # Should be ~ sqrt(2) 
     641        ls = LineString((0, 0), (1, 1)) 
     642        self.assertAlmostEqual(1.41421356237, ls.length, 11) 
     643 
     644        # Should be circumfrence of Polygon 
     645        poly = Polygon(LinearRing((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))) 
     646        self.assertEqual(4.0, poly.length) 
     647 
     648        # Should be sum of each element's length in collection. 
     649        mpoly = MultiPolygon(poly.clone(), poly) 
     650        self.assertEqual(8.0, mpoly.length) 
    615651 
    616652def suite():