Ticket #16594: 16594-1.diff

File 16594-1.diff, 3.3 KB (added by claudep, 3 years ago)

Added 3d support to wkt property

  • django/contrib/gis/geos/geometry.py

    diff --git a/django/contrib/gis/geos/geometry.py b/django/contrib/gis/geos/geometry.py
    index df396bd..7ae5788 100644
    a b class GEOSGeometry(GEOSBase, ListMixin): 
    382382    @property
    383383    def wkt(self):
    384384        "Returns the WKT (Well-Known Text) representation of this Geometry."
    385         return wkt_w().write(self).decode()
     385        return wkt_w(self.hasz and 3 or 2).write(self).decode()
    386386
    387387    @property
    388388    def hex(self):
  • django/contrib/gis/geos/prototypes/io.py

    diff --git a/django/contrib/gis/geos/prototypes/io.py b/django/contrib/gis/geos/prototypes/io.py
    index 1be7da8..1e80351 100644
    a b wkt_writer_write.argtypes = [WKT_WRITE_PTR, GEOM_PTR] 
    4545wkt_writer_write.restype = geos_char_p
    4646wkt_writer_write.errcheck = check_string
    4747
     48try:
     49    wkt_writer_get_outdim = GEOSFunc('GEOSWKTWriter_getOutputDimension')
     50    wkt_writer_get_outdim.argtypes = [WKT_WRITE_PTR]
     51    wkt_writer_get_outdim.restype = c_int
     52    wkt_writer_set_outdim = GEOSFunc('GEOSWKTWriter_setOutputDimension')
     53    wkt_writer_set_outdim.argtypes = [WKT_WRITE_PTR, c_int]
     54except AttributeError:
     55    # GEOSWKTWriter_get/setOutputDimension has been introduced in GEOS 3.3.0
     56    # Always return 2 if not available
     57    wkt_writer_get_outdim = lambda ptr: 2
     58    wkt_writer_set_outdim = lambda ptr, dim: None
     59
    4860### WKBReader routines ###
    4961wkb_reader_create = GEOSFunc('GEOSWKBReader_create')
    5062wkb_reader_create.restype = WKB_READ_PTR
    class WKTWriter(IOBase): 
    151163        "Returns the WKT representation of the given geometry."
    152164        return wkt_writer_write(self.ptr, geom.ptr)
    153165
     166    @property
     167    def outdim(self):
     168        return wkt_writer_get_outdim(self.ptr)
     169
     170    @outdim.setter
     171    def outdim(self, new_dim):
     172        if not new_dim in (2, 3):
     173            raise ValueError('WKT output dimension must be 2 or 3')
     174        wkt_writer_set_outdim(self.ptr, new_dim)
     175
     176
    154177class WKBWriter(IOBase):
    155178    _constructor = wkb_writer_create
    156179    _destructor = wkb_writer_destroy
    def wkt_r(): 
    217240        thread_context.wkt_r = _WKTReader()
    218241    return thread_context.wkt_r
    219242
    220 def wkt_w():
     243def wkt_w(dim=2):
    221244    if not thread_context.wkt_w:
    222245        thread_context.wkt_w = WKTWriter()
     246    thread_context.wkt_w.outdim = dim
    223247    return thread_context.wkt_w
    224248
    225249def wkb_r():
  • django/contrib/gis/geos/tests/test_geos.py

    diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py
    index cbe5136..e18f809 100644
    a b class GEOSTest(unittest.TestCase, TestDataMixin): 
    8080        "Testing WKT output."
    8181        for g in self.geometries.wkt_out:
    8282            geom = fromstr(g.wkt)
    83             self.assertEqual(g.ewkt, geom.wkt)
     83            if geom.hasz and geos_version_info()['version'] >= '3.3.0':
     84                self.assertEqual(g.ewkt, geom.wkt)
    8485
    8586    def test_hex(self):
    8687        "Testing HEX output."
Back to Top