Changeset 5474
- Timestamp:
- 06/14/07 22:49:25 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/geos/GEOSGeometry.py
r5395 r5474 33 33 c_char_p, c_double, c_float, c_int, c_uint, c_size_t 34 34 import os, sys 35 from types import StringType, IntType, FloatType 35 36 36 37 """ … … 121 122 122 123 #### Python 'magic' routines #### 123 def __init__(self, input, geom_type='wkt'):124 def __init__(self, input, input_type='wkt'): 124 125 "Takes an input and the type of the input for initialization." 125 126 126 if geom_type == 'wkt': 127 # If the geometry is in WKT form 128 g = lgeos.GEOSGeomFromWKT(c_char_p(input)) 129 elif geom_type == 'hex': 130 # If the geometry is in HEX form. 131 sz = c_size_t(len(input)) 132 buf = create_string_buffer(input) 133 g = lgeos.GEOSGeomFromHEX_buf(buf, sz) 134 elif geom_type == 'geos': 127 if isinstance(input, StringType): 128 if input_type == 'wkt': 129 # If the geometry is in WKT form 130 g = lgeos.GEOSGeomFromWKT(c_char_p(input)) 131 elif input_type == 'hex': 132 # If the geometry is in HEX form. 133 sz = c_size_t(len(input)) 134 buf = create_string_buffer(input) 135 g = lgeos.GEOSGeomFromHEX_buf(buf, sz) 136 else: 137 raise GEOSException, 'GEOS input geometry type "%s" not supported!' % input_type 138 elif isinstance(input, IntType): 135 139 # When the input is a C pointer (Python integer) 136 140 g = input 137 141 else: 138 142 # Invalid geometry type. 139 raise GEOSException, 'Improper geometry input type !'140 141 # If the geometry pointer is NULL (0), thenraise an exception.143 raise GEOSException, 'Improper geometry input type: %s' % str(type(input)) 144 145 # If the geometry pointer is NULL (0), raise an exception. 142 146 if not g: 143 raise GEOSException, 'Invalid %s given!' % geom_type.upper()147 raise GEOSException, 'Invalid %s given!' % input_type.upper() 144 148 else: 145 149 self._g = g 146 150 147 151 # Setting the class type (e.g. 'Point', 'Polygon', etc.) 148 self.__class__ = GEO _CLASSES[self.geom_type]152 self.__class__ = GEOS_CLASSES[self.geom_type] 149 153 150 154 def __del__(self): … … 184 188 if n == -1: raise GEOSException, 'Error getting number of coordinates!' 185 189 else: return n 190 191 @property 192 def num_points(self): 193 "Returns the number points, or coordinates, in the geometry." 194 return self.num_coords 186 195 187 196 @property … … 328 337 (Text from PostGIS documentation at ch. 6.1.3) 329 338 """ 330 if type(width) != type(0.0):339 if not isinstance(width, FloatType): 331 340 raise TypeError, 'width parameter must be a float' 332 if type(quadsegs) != type(0):341 if not isinstance(quadsegs, IntType): 333 342 raise TypeError, 'quadsegs parameter must be an integer' 334 343 b = lgeos.GEOSBuffer(self._g, c_double(width), c_int(quadsegs)) 335 return GEOSGeometry(b , 'geos')344 return GEOSGeometry(b) 336 345 337 346 @property 338 347 def envelope(self): 339 348 "Return the geometries bounding box." 340 e = lgeos.GEOSEnvelope(self._g) 341 return GEOSGeometry(e, 'geos') 349 return GEOSGeometry(lgeos.GEOSEnvelope(self._g)) 342 350 343 351 @property … … 346 354 of highest dimension (since the lower-dimension geometries contribute zero 347 355 "weight" to the centroid).""" 348 g = lgeos.GEOSGetCentroid(self._g) 349 return GEOSGeometry(g, 'geos') 356 return GEOSGeometry(lgeos.GEOSGetCentroid(self._g)) 350 357 351 358 @property 352 359 def boundary(self): 353 360 "Returns the boundary as a newly allocated Geometry object." 354 g = lgeos.GEOSBoundary(self._g) 355 return GEOSGeometry(g, 'geos') 361 return GEOSGeometry(lgeos.GEOSBoundary(self._g)) 356 362 357 363 @property 358 364 def convex_hull(self): 359 365 "Returns the smallest convex Polygon that contains all the points in the Geometry." 360 g = lgeos.GEOSConvexHull(self._g) 361 return GEOSGeometry(g, 'geos') 366 return GEOSGeometry(lgeos.GEOSConvexHull(self._g)) 362 367 363 368 @property 364 369 def point_on_surface(self): 365 370 "Computes an interior point of this Geometry." 366 g = lgeos.GEOSPointOnSurface(self._g) 367 return GEOSGeometry(g, 'geos') 371 return GEOSGeometry(lgeos.GEOSPointOnSurface(self._g)) 368 372 369 373 def relate(self, other): … … 374 378 """Returns a Geometry representing the points making up this Geometry 375 379 that do not make up other.""" 376 d = lgeos.GEOSDifference(self._g, other._g) 377 return GEOSGeometry(d, 'geos') 380 return GEOSGeometry(lgeos.GEOSDifference(self._g, other._g)) 378 381 379 382 def sym_difference(self, other): 380 383 """Returns a set combining the points in this Geometry not in other, 381 384 and the points in other not in this Geometry.""" 382 d = lgeos.GEOSSymDifference(self._g, other._g) 383 return GEOSGeometry(d, 'geos') 385 return GEOSGeometry(lgeos.GEOSSymDifference(self._g, other._g)) 384 386 385 387 def intersection(self, other): 386 388 "Returns a Geometry representing the points shared by this Geometry and other." 387 i = lgeos.GEOSIntersection(self._g, other._g) 388 return GEOSGeometry(i, 'geos') 389 return GEOSGeometry(lgeos.GEOSIntersection(self._g, other._g)) 389 390 390 391 def union(self, other): 391 392 "Returns a Geometry representing all the points in this Geometry and other." 392 u = lgeos.GEOSUnion(self._g, other._g) 393 return GEOSGeometry(u, 'geos') 393 return GEOSGeometry(lgeos.GEOSUnion(self._g, other._g)) 394 394 395 395 #### Other Routines #### … … 641 641 "Returns the number of rings in this Polygon." 642 642 return self.num_interior_rings + 1 643 644 643 645 644 def get_interior_ring(self, ring_i): … … 699 698 "For indexing on the multiple geometries." 700 699 self._checkindex(index) 701 item = lgeos.GEOSGeom_clone(lgeos.GEOSGetGeometryN(self._g, c_int(index)))702 return GEOSGeometry( item, 'geos')700 # Cloning the GEOS Geometry first, before returning it. 701 return GEOSGeometry(lgeos.GEOSGeom_clone(lgeos.GEOSGetGeometryN(self._g, c_int(index)))) 703 702 704 703 def __len__(self): … … 712 711 713 712 # Class mapping dictionary 714 GEO _CLASSES = {'Point' : Point,715 'Polygon' : Polygon,716 'LineString' : LineString,717 'LinearRing' : LinearRing,718 'GeometryCollection' : GeometryCollection,719 'MultiPoint' : MultiPoint,720 'MultiLineString' : MultiLineString,721 'MultiPolygon' : MultiPolygon,722 }713 GEOS_CLASSES = {'Point' : Point, 714 'Polygon' : Polygon, 715 'LineString' : LineString, 716 'LinearRing' : LinearRing, 717 'GeometryCollection' : GeometryCollection, 718 'MultiPoint' : MultiPoint, 719 'MultiLineString' : MultiLineString, 720 'MultiPolygon' : MultiPolygon, 721 } django/branches/gis/django/contrib/gis/tests/test_geos.py
r5395 r5474 84 84 self.assertEqual(p.n_i, poly.num_interior_rings) 85 85 self.assertEqual(p.n_i + 1, len(poly)) # Testing __len__ 86 self.assertEqual(p.n_p, poly.num_points) 87 88 # Area & Centroid 89 self.assertAlmostEqual(p.area, poly.area, 9) 90 self.assertAlmostEqual(p.centroid[0], poly.centroid.tuple[0], 9) 91 self.assertAlmostEqual(p.centroid[1], poly.centroid.tuple[1], 9) 86 92 87 93 # Testing the geometry equivalence … … 145 151 self.assertAlmostEqual(l.centroid[0], ml.centroid.x, 9) 146 152 self.assertAlmostEqual(l.centroid[1], ml.centroid.y, 9) 147 148 153 149 154 self.assertEqual(True, ml == GEOSGeometry(l.wkt)) django/branches/gis/django/contrib/gis/tests/test_with_swig.py
r5008 r5474 4 4 from geometries import swig_geoms as geos_geoms 5 5 6 class GeosTest (unittest.TestCase):6 class GeosTestWithSwig(unittest.TestCase): 7 7 8 8 def test001_hex_to_wkt(self): … … 21 21 "Testing the centroid property." 22 22 for g in geos_geoms: 23 wkt1 = (centroid(g.wkt, geom_type='wkt')).wkt23 wkt1 = centroid(g.wkt, geom_type='wkt') 24 24 wkt2 = geomToWKT((geomFromWKT(g.wkt)).getCentroid()) 25 25 self.assertEqual(wkt1, wkt2) … … 61 61 def suite(): 62 62 s = unittest.TestSuite() 63 s.addTest(unittest.makeSuite(GeosTest ))63 s.addTest(unittest.makeSuite(GeosTestWithSwig)) 64 64 return s 65 65
