Changeset 5030
- Timestamp:
- 04/19/07 07:49:40 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/geos/GEOSGeometry.py
r5023 r5030 83 83 raise GEOSException, 'Unsupported OS "%s"' % os.name 84 84 85 # The GEOSException class 86 class GEOSException(Exception): pass 87 85 88 # Getting the GEOS C library. The C interface (CDLL) is used for 86 89 # both *NIX and Windows. … … 99 102 ERRORFUNC = CFUNCTYPE(None, c_char_p, c_char_p) 100 103 def error_h(fmt, list): 101 # TODO: Figure out why improper format code given w/some GEOS errors 102 if not list: 103 sys.stderr.write(fmt) 104 if list: 105 err_msg = fmt % list 104 106 else: 105 sys.stderr.write('ERROR: %s' % str(list)) 107 err_msg = fmt 108 sys.stderr.write(err_msg) 106 109 error_h = ERRORFUNC(error_h) 107 110 … … 112 115 lgeos.initGEOS(notice_h, error_h) 113 116 114 class GEOSException:115 "Exception class for any GEOS-related errors."116 def __init__(self, msg):117 self.msg = msg118 def __str__(self):119 return repr(self.msg)120 121 117 class GEOSGeometry: 122 118 "A class that, generally, encapsulates a GEOS geometry." … … 125 121 def __init__(self, input, geom_type='wkt'): 126 122 "Takes an input and the type of the input for initialization." 123 127 124 if geom_type == 'wkt': 128 125 # If the geometry is in WKT form 129 self._g = lgeos.GEOSGeomFromWKT(c_char_p(input)) 126 buf = create_string_buffer(input) 127 g = lgeos.GEOSGeomFromWKT(buf) 130 128 elif geom_type == 'hex': 131 # If the geometry is in EWHEX form.129 # If the geometry is in HEX form. 132 130 sz = c_size_t(len(input)) 133 131 buf = create_string_buffer(input) 134 self._g = lgeos.GEOSGeomFromHEX_buf(buf, sz)132 g = lgeos.GEOSGeomFromHEX_buf(buf, sz) 135 133 elif geom_type == 'geos': 136 134 # When the input is a C pointer (Python integer) 137 self._g = input135 g = input 138 136 else: 139 137 # Invalid geometry type. 140 138 raise GEOSException, 'Improper geometry input type!' 141 139 140 # If the geometry pointer is NULL (0), then raise an exception. 141 if not g: 142 self._g = False # Setting this before raising the exception 143 raise GEOSException, 'Invalid %s given!' % geom_type.upper() 144 else: 145 self._g = g 146 142 147 # Setting the class type (e.g. 'Point', 'Polygon', etc.) 143 148 self.__class__ = GEO_CLASSES[self.geom_type] 144 149 145 # If the geometry pointer is NULL (0), then raise an exception.146 if not self._g:147 raise GEOSException, 'Could not initialize on input!'148 149 150 def __del__(self): 150 151 "This cleans up the memory allocated for the geometry." 151 lgeos.GEOSGeom_destroy(self._g)152 if self._g: lgeos.GEOSGeom_destroy(self._g) 152 153 153 154 def __str__(self): … … 413 414 414 415 def __del__(self): 415 lgeos.GEOSCoordSeq_destroy(self._cs)416 if self._cs: lgeos.GEOSCoordSeq_destroy(self._cs) 416 417 417 418 def __iter__(self): … … 488 489 idx = c_uint(index) 489 490 490 # 'd' is the value of the point 491 # 'd' is the value of the point, passed in by reference 491 492 d = c_double() 492 493 status = lgeos.GEOSCoordSeq_getOrdinate(self._cs, idx, dim, byref(d)) django/branches/gis/django/contrib/gis/tests/geometries.py
r5008 r5030 8 8 self.wkt = wkt 9 9 10 m = wkt_regex.match(wkt) 11 if not m: 12 raise Exception, 'Improper WKT: "%s"' % wkt 13 self.geo_type = m.group('type') 10 self.bad = kwargs.pop('bad', False) 14 11 12 if not self.bad: 13 m = wkt_regex.match(wkt) 14 if not m: 15 raise Exception, 'Improper WKT: "%s"' % wkt 16 self.geo_type = m.group('type') 15 17 16 18 for key, value in kwargs.items(): … … 42 44 ) 43 45 46 # Errors 47 errors = (TestGeom('GEOMETR##!@#%#............a32515', bad=True, hex=False), 48 TestGeom('Foo.Bar', bad=True, hex=False), 49 TestGeom('POINT (5, 23)', bad=True, hex=False), 50 TestGeom('AAABBBDDDAAD##@#1113511111-098111111111111111533333333333333', bad=True, hex=True), 51 TestGeom('FFFFFFFFFFFFFFFFF1355555555555555555565111', bad=True, hex=True), 52 ) 53 44 54 # Polygons 45 55 polygons = (TestGeom('POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0), (10 10, 10 90, 90 90, 90 10, 10 10) ))', … … 52 62 n_i=0, ext_ring_cs=False 53 63 ), 54 55 64 ) 56 65 django/branches/gis/django/contrib/gis/tests/test_geos.py
r5023 r5030 6 6 class GeosTest2(unittest.TestCase): 7 7 8 def test010 _wkt(self):8 def test0100_wkt(self): 9 9 "Testing WKT output." 10 10 for g in wkt_out: … … 12 12 self.assertEqual(g.ewkt, geom.wkt) 13 13 14 def test01 1_hex(self):14 def test0101_hex(self): 15 15 "Testing HEX output." 16 16 for g in hex_wkt: … … 18 18 self.assertEqual(g.hex, geom.hex) 19 19 20 def test0102_errors(self): 21 "Testing the Error handlers." 22 for err in errors: 23 if err.hex: 24 self.assertRaises(GEOSException, GEOSGeometry, err.wkt, 'hex') 25 else: 26 self.assertRaises(GEOSException, GEOSGeometry, err.wkt) 27 20 28 def test02_points(self): 21 29 "Testing Point objects."
