Changeset 6464
- Timestamp:
- 10/07/07 15:29:59 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/gdal/feature.py
r6436 r6464 1 1 # types and ctypes 2 2 from types import StringType 3 from ctypes import c_char_p, c_int, string_at3 from ctypes import c_char_p, c_int, c_void_p, string_at 4 4 5 5 # The GDAL C library, OGR exception, and the Field object … … 99 99 100 100 # Geometry is cloned so the feature isn't invalidated. 101 return OGRGeometry( lgdal.OGR_G_Clone(geom_ptr), srs)101 return OGRGeometry(c_void_p(lgdal.OGR_G_Clone(geom_ptr)), srs) 102 102 103 103 @property django/branches/gis/django/contrib/gis/gdal/geometries.py
r6460 r6464 39 39 True 40 40 """ 41 # types & ctypes 42 from types import IntType, StringType, UnicodeType 43 from ctypes import byref, string_at, c_char_p, c_double, c_int, c_void_p 41 # Python library imports 42 import re, sys 43 from binascii import a2b_hex, b2a_hex 44 from ctypes import byref, create_string_buffer, string_at, c_char_p, c_double, c_int, c_void_p 45 from types import BufferType, IntType, StringType, UnicodeType 44 46 45 47 # Getting GDAL prerequisites … … 71 73 get_area.argtypes = [c_void_p] 72 74 75 # Regular expression for determining whether the input is HEXEWKB. 76 hex_regex = re.compile(r'^[0-9A-F]+$', re.I) 77 73 78 #### OGRGeometry Class #### 74 79 class OGRGeometry(object): … … 78 83 "Initializes Geometry on either WKT or an OGR pointer as input." 79 84 80 self._g = 0 # Initially NULL 85 self._g = c_void_p(None) # Initially NULL 86 87 # Checking if unicode 88 if isinstance(geom_input, UnicodeType): 89 # Encoding to ASCII, WKT or HEX doesn't need any more. 90 geo_input = geo_input.encode('ascii') 91 92 # If HEX, unpack input to to a binary buffer. 93 if isinstance(geom_input, StringType) and hex_regex.match(geom_input): 94 geom_input = buffer(a2b_hex(geom_input.upper())) 81 95 82 96 if isinstance(geom_input, StringType): … … 94 108 except: 95 109 raise OGRException('Could not initialize OGR Geometry from: %s' % geom_input) 110 elif isinstance(geom_input, BufferType): 111 # WKB was passed in 112 g = c_void_p() 113 check_err(lgdal.OGR_G_CreateFromWkb(c_char_p(str(geom_input)), c_void_p(), byref(g), len(geom_input))) 96 114 elif isinstance(geom_input, OGRGeomType): 115 # OGRGeomType was passed in, an empty geometry will be created. 97 116 g = lgdal.OGR_G_CreateGeometry(geom_input.num) 98 elif isinstance(geom_input, IntType):99 # OGR Pointer (integer) was the input117 elif isinstance(geom_input, c_void_p): 118 # OGR pointer (c_void_p) was the input. 100 119 g = geom_input 101 120 else: … … 182 201 return self.point_count 183 202 203 @property 204 def geom_type(self): 205 "Returns the Type for this Geometry." 206 return OGRGeomType(lgdal.OGR_G_GetGeometryType(self._g)) 207 208 @property 209 def geom_name(self): 210 "Returns the Name of this Geometry." 211 return string_at(lgdal.OGR_G_GetGeometryName(self._g)) 212 213 @property 214 def area(self): 215 "Returns the area for a LinearRing, Polygon, or MultiPolygon; 0 otherwise." 216 return get_area(self._g) 217 218 @property 219 def envelope(self): 220 "Returns the envelope for this Geometry." 221 env = OGREnvelope() 222 lgdal.OGR_G_GetEnvelope(self._g, byref(env)) 223 return Envelope(env) 224 225 #### SpatialReference-related Properties #### 226 184 227 # The SRS property 185 228 def get_srs(self): … … 217 260 srid = property(get_srid, set_srid) 218 261 219 @property220 def geom_type(self):221 "Returns the Type for this Geometry."222 return OGRGeomType(lgdal.OGR_G_GetGeometryType(self._g))223 224 @property225 def geom_name(self):226 "Returns the Name of this Geometry."227 return string_at(lgdal.OGR_G_GetGeometryName(self._g))228 229 @property230 def area(self):231 "Returns the area for a LinearRing, Polygon, or MultiPolygon; 0 otherwise."232 return get_area(self._g)233 234 @property235 def envelope(self):236 "Returns the envelope for this Geometry."237 env = OGREnvelope()238 lgdal.OGR_G_GetEnvelope(self._g, byref(env))239 return Envelope(env)240 241 262 #### Output Methods #### 242 263 @property … … 246 267 if buf: return string_at(buf) 247 268 else: return None 269 270 @property 271 def hex(self): 272 "Returns the hexadecimal representation of the WKB (a string)." 273 return b2a_hex(self.wkb).upper() 274 275 @property 276 def wkb_size(self): 277 "Returns the size of the WKB buffer." 278 return lgdal.OGR_G_WkbSize(self._g) 279 280 @property 281 def wkb(self): 282 "Returns the WKB representation of the Geometry." 283 if sys.byteorder == 'little': 284 byteorder = c_int(1) # wkbNDR (from ogr_core.h) 285 else: 286 byteorder = c_int(0) # wkbXDR (from ogr_core.h) 287 # Creating a mutable string buffer of the given size, exporting 288 # to WKB, and returning a Python buffer of the WKB. 289 sz = self.wkb_size 290 wkb = create_string_buffer(sz) 291 check_err(lgdal.OGR_G_ExportToWkb(self._g, byteorder, byref(wkb))) 292 return buffer(string_at(wkb, sz)) 248 293 249 294 @property … … 257 302 def clone(self): 258 303 "Clones this OGR Geometry." 259 return OGRGeometry( lgdal.OGR_G_Clone(self._g))304 return OGRGeometry(c_void_p(lgdal.OGR_G_Clone(self._g))) 260 305 261 306 def close_rings(self): … … 328 373 "A helper routine for the OGR routines that generate geometries." 329 374 if isinstance(other, OGRGeometry): 330 return OGRGeometry( gen_func(self._g, other._g))331 else: 332 return OGRGeometry( gen_func(self._g))375 return OGRGeometry(c_void_p(gen_func(self._g, other._g))) 376 else: 377 return OGRGeometry(c_void_p(gen_func(self._g))) 333 378 334 379 @property … … 383 428 def tuple(self): 384 429 "Returns the tuple of this point." 385 if self.coord_dim == 1: 386 return (self.x,) 387 elif self.coord_dim == 2: 430 if self.coord_dim == 2: 388 431 return (self.x, self.y) 389 432 elif self.coord_dim == 3: … … 442 485 raise OGRIndexError('index out of range: %s' % str(index)) 443 486 else: 444 return OGRGeometry( lgdal.OGR_G_Clone(lgdal.OGR_G_GetGeometryRef(self._g, c_int(index))), self.srs)487 return OGRGeometry(c_void_p(lgdal.OGR_G_Clone(lgdal.OGR_G_GetGeometryRef(self._g, c_int(index)))), self.srs) 445 488 446 489 # Polygon Properties … … 478 521 raise OGRIndexError('index out of range: %s' % str(index)) 479 522 else: 480 return OGRGeometry( lgdal.OGR_G_Clone(lgdal.OGR_G_GetGeometryRef(self._g, c_int(index))), self.srs)523 return OGRGeometry(c_void_p(lgdal.OGR_G_Clone(lgdal.OGR_G_GetGeometryRef(self._g, c_int(index)))), self.srs) 481 524 482 525 def __iter__(self): … … 493 536 if isinstance(geom, OGRGeometry): 494 537 ptr = geom._g 495 elif isinstance(geom, StringType):538 elif isinstance(geom, (StringType, UnicodeType)): 496 539 tmp = OGRGeometry(geom) 497 540 ptr = tmp._g django/branches/gis/django/contrib/gis/tests/test_gdal_geom.py
r6460 r6464 1 1 import unittest 2 2 from django.contrib.gis.gdal import OGRGeometry, OGRGeomType, OGRException, SpatialReference 3 from geometries import *3 from django.contrib.gis.tests.geometries import * 4 4 5 5 class OGRGeomTest(unittest.TestCase): … … 42 42 geom = OGRGeometry(g.wkt) 43 43 self.assertEqual(g.gml, geom.gml) 44 45 def test01c_hex(self): 46 "Testing HEX input/output." 47 for g in hex_wkt: 48 geom1 = OGRGeometry(g.wkt) 49 self.assertEqual(g.hex, geom1.hex) 50 # Constructing w/HEX 51 geom2 = OGRGeometry(g.hex) 52 self.assertEqual(geom1, geom2) 53 54 def test01d_wkb(self): 55 "Testing WKB input/output." 56 from binascii import b2a_hex 57 for g in hex_wkt: 58 geom1 = OGRGeometry(g.wkt) 59 wkb = geom1.wkb 60 self.assertEqual(b2a_hex(wkb).upper(), g.hex) 61 # Constructing w/WKB. 62 geom2 = OGRGeometry(wkb) 63 self.assertEqual(geom1, geom2) 44 64 45 65 def test02_points(self):
