Django

Code

Changeset 6314

Show
Ignore:
Timestamp:
09/15/07 13:56:35 (1 year ago)
Author:
jbronn
Message:

gis: Fixed #5440 with patches from rcoup; cleaned up notice handler in libgeos.

Files:

Legend:

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

    r6024 r6314  
    77     byref, string_at, create_string_buffer, pointer, \ 
    88     c_char_p, c_double, c_int, c_size_t 
    9 from types import StringType, UnicodeType, IntType, FloatType 
     9from types import StringType, UnicodeType, IntType, FloatType, BufferType 
    1010 
    1111# Python and GEOS-related dependencies. 
     
    3131    def __init__(self, geo_input, srid=None): 
    3232        """ 
    33         The base constructor for GEOS geometry objects, and may take the following 
    34          string inputs: WKT and HEXEWKB (a PostGIS-specific canonical form). 
    35  
     33        The base constructor for GEOS geometry objects, and may take the  
     34         following inputs: 
     35          
     36         * string: WKT 
     37         * string: HEXEWKB (a PostGIS-specific canonical form) 
     38         * buffer: WKB 
     39         
    3640        The `srid` keyword is used to specify the Source Reference Identifier 
    3741         (SRID) number for this Geometry.  If not set, the SRID will be None. 
     
    5660            #  GEOSPointer object. 
    5761            g = geo_input 
     62        elif isinstance(geo_input, BufferType): 
     63            # When the input is a buffer (WKB). 
     64            wkb_input = str(geo_input) 
     65            sz = c_size_t(len(wkb_input)) 
     66            g = lgeos.GEOSGeomFromWKB_buf(c_char_p(wkb_input), sz) 
    5867        else: 
    5968            # Invalid geometry type. 
     
    413422        h = lgeos.GEOSGeomToHEX_buf(self._ptr(), byref(sz)) 
    414423        return string_at(h, sz.value) 
     424 
     425    @property 
     426    def wkb(self): 
     427        "Returns the WKB of the Geometry as a buffer." 
     428        sz = c_size_t() 
     429        h = lgeos.GEOSGeomToWKB_buf(self._ptr(), byref(sz)) 
     430        return buffer(string_at(h, sz.value)) 
    415431 
    416432    @property 
  • django/branches/gis/django/contrib/gis/geos/libgeos.py

    r6024 r6314  
    5353#  "typedef void (*GEOSMessageHandler)(const char *fmt, ...);" 
    5454NOTICEFUNC = CFUNCTYPE(None, c_char_p, c_char_p) 
    55 def notice_h(fmt, list, output_h=sys.stdout): 
    56     output_h.write('GEOS_NOTICE: %s\n' % (fmt % list)) 
     55def notice_h(fmt, lst, output_h=sys.stdout): 
     56    try: 
     57        warn_msg = fmt % lst 
     58    except: 
     59        warn_msg = fmt  
     60    output_h.write('GEOS_NOTICE: %s\n' % warn_msg) 
    5761notice_h = NOTICEFUNC(notice_h) 
    5862 
  • django/branches/gis/django/contrib/gis/tests/geometries.py

    r5805 r6314  
    5050          TestGeom('AAABBBDDDAAD##@#1113511111-098111111111111111533333333333333', bad=True, hex=True), 
    5151          TestGeom('FFFFFFFFFFFFFFFFF1355555555555555555565111', bad=True, hex=True), 
     52          TestGeom('', bad=True, hex=False), 
    5253          ) 
    5354 
  • django/branches/gis/django/contrib/gis/tests/test_geos.py

    r6024 r6314  
    3232    def test01d_errors(self): 
    3333        "Testing the Error handlers." 
     34        # string-based 
    3435        print "\nBEGIN - expecting GEOS_ERROR; safe to ignore.\n" 
    3536        for err in errors: 
    36             if err.hex: 
    37                 self.assertRaises(GEOSException, fromstr, err.wkt) 
    38             else: 
    39                 self.assertRaises(GEOSException, fromstr, err.wkt) 
     37            self.assertRaises(GEOSException, fromstr, err.wkt) 
    4038        print "\nEND - expecting GEOS_ERROR; safe to ignore.\n" 
     39         
     40        class NotAGeometry(object): 
     41            pass 
     42         
     43        # Some other object 
     44        self.assertRaises(TypeError, GEOSGeometry, NotAGeometry()) 
     45        # None 
     46        self.assertRaises(TypeError, GEOSGeometry, None) 
     47        # Bad WKB 
     48        self.assertRaises(GEOSException, GEOSGeometry, buffer('0')) 
    4149                 
     50    def test01e_wkb(self): 
     51        "Testing WKB output." 
     52        from binascii import b2a_hex 
     53        for g in hex_wkt: 
     54            geom = fromstr(g.wkt) 
     55            wkb = geom.wkb 
     56            self.assertEqual(b2a_hex(wkb).upper(), g.hex) 
     57 
     58    def test01f_create_hex(self): 
     59        "Testing creation from HEX." 
     60        for g in hex_wkt: 
     61            geom_h = GEOSGeometry(g.hex) 
     62            # we need to do this so decimal places get normalised 
     63            geom_t = fromstr(g.wkt) 
     64            self.assertEqual(geom_t.wkt, geom_h.wkt) 
     65 
     66    def test01g_create_wkb(self): 
     67        "Testing creation from WKB." 
     68        from binascii import a2b_hex 
     69        for g in hex_wkt: 
     70            wkb = buffer(a2b_hex(g.hex)) 
     71            geom_h = GEOSGeometry(wkb) 
     72            # we need to do this so decimal places get normalised 
     73            geom_t = fromstr(g.wkt) 
     74            self.assertEqual(geom_t.wkt, geom_h.wkt) 
     75     
    4276    def test02a_points(self): 
    4377        "Testing Point objects."