Changeset 5991
- Timestamp:
- 08/20/07 22:24:22 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/geos/base.py
r5832 r5991 273 273 ## Internal for GEOS unary & binary predicate functions ## 274 274 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.""" 276 277 val = func(self._ptr()) 277 278 if val == 0: return False … … 280 281 281 282 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.""" 283 285 if not isinstance(other, GEOSGeometry): 284 286 raise TypeError, 'Binary predicate operation ("%s") requires another GEOSGeometry instance.' % func.__name__ … … 323 325 324 326 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****.""" 326 329 return self._binary_predicate(lgeos.GEOSDisjoint, other) 327 330 328 331 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****.""" 330 334 return self._binary_predicate(lgeos.GEOSTouches, other) 331 335 … … 335 339 336 340 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).""" 339 344 return self._binary_predicate(lgeos.GEOSCrosses, other) 340 345 341 346 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***.""" 343 349 return self._binary_predicate(lgeos.GEOSWithin, other) 344 350 … … 348 354 349 355 def overlaps(self, other): 350 """Returns true if the DE-9IM intersection matrix for the two Geometries is T*T***T** (for two points351 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).""" 352 358 return self._binary_predicate(lgeos.GEOSOverlaps, other) 353 359 354 360 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*.""" 356 363 return self._binary_predicate(lgeos.GEOSEquals, other) 357 364 358 365 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)) 362 370 363 371 #### SRID Routines #### … … 394 402 #### Topology Routines #### 395 403 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) 398 407 399 408 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) 402 412 403 413 def buffer(self, width, quadsegs=8): … … 433 443 @property 434 444 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.""" 436 447 return self._unary_topology(lgeos.GEOSConvexHull) 437 448 … … 469 480 a = c_double() 470 481 status = lgeos.GEOSArea(self._ptr(), byref(a)) 471 if not status: return None482 if status != 1: return None 472 483 else: return a.value 473 484 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 474 505 def clone(self): 475 506 "Clones this Geometry." django/branches/gis/django/contrib/gis/geos/__init__.py
r5760 r5991 30 30 """ 31 31 32 from base import GEOSGeometry33 from geometries import Point, LineString, LinearRing, Polygon, HAS_NUMPY34 from collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon35 from error import GEOSException, GEOSGeometryIndexError32 from django.contrib.gis.geos.base import GEOSGeometry 33 from django.contrib.gis.geos.geometries import Point, LineString, LinearRing, Polygon, HAS_NUMPY 34 from django.contrib.gis.geos.collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon 35 from django.contrib.gis.geos.error import GEOSException, GEOSGeometryIndexError 36 36 37 37 def fromstr(wkt_or_hex, **kwargs): django/branches/gis/django/contrib/gis/tests/test_geos.py
r5832 r5991 613 613 self.assertEqual((1.,2.,3.), ls[0]) 614 614 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) 615 651 616 652 def suite():
