Ticket #5440: geos_wkb_support.patch

File geos_wkb_support.patch, 4.8 KB (added by Robert Coup, 17 years ago)
  • django/contrib/gis/geos/base.py

     
    66from ctypes import \
    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.
    1212import re
     
    3030    #### Python 'magic' routines ####
    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.
    3842        """
     
    5559            # When the input is either a memory address (an integer), or a
    5660            #  GEOSPointer object.
    5761            g = geo_input
     62        elif isinstance(geo_input, BufferType):
     63            # When the input is a buffer (WKB).
     64            s_geo_input = str(geo_input)
     65            sz = c_size_t(len(s_geo_input))
     66            g = lgeos.GEOSGeomFromWKB_buf(s_geo_input, sz)
     67                       
    5868        else:
    5969            # Invalid geometry type.
    6070            raise TypeError, 'Improper geometry input type: %s' % str(type(geo_input))
     
    414424        return string_at(h, sz.value)
    415425
    416426    @property
     427    def wkb(self):
     428        "Returns the WKB of the Geometry as a buffer."
     429        sz = c_size_t()
     430        h = lgeos.GEOSGeomToWKB_buf(self._ptr(), byref(sz))
     431        return buffer(string_at(h, sz.value))
     432
     433    @property
    417434    def kml(self):
    418435        "Returns the KML representation of this Geometry."
    419436        gtype = self.geom_type
  • django/contrib/gis/tests/geometries.py

     
    4949          TestGeom('POINT (5, 23)', bad=True, hex=False),
    5050          TestGeom('AAABBBDDDAAD##@#1113511111-098111111111111111533333333333333', bad=True, hex=True),
    5151          TestGeom('FFFFFFFFFFFFFFFFF1355555555555555555565111', bad=True, hex=True),
     52          TestGeom('', bad=True, hex=False),
    5253          )
    5354
    5455# Polygons
  • django/contrib/gis/tests/test_geos.py

     
    3131
    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."
    4478        prev = fromstr('POINT(0 0)')
Back to Top