Commit 8cdc8472 authored by Claude Paroz's avatar Claude Paroz
Browse files

[py3] Added buffer/memoryview compatibility

Even if buffer and memoryview are not strictly identical, it should
be safe to consider them equivalent for GIS support.
Thanks Aymeric Augustin for the review.
parent 3174b5f2
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
from django.utils import six

if six.PY3:
    memoryview = memoryview
else:
    memoryview = buffer
+2 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ corresponding to geographic model fields.

Thanks to Robert Coup for providing this functionality (see #4322).
"""
from django.contrib.gis import memoryview
from django.utils import six

class GeometryProxy(object):
@@ -54,7 +55,7 @@ class GeometryProxy(object):
        if isinstance(value, self._klass) and (str(value.geom_type).upper() == gtype or gtype == 'GEOMETRY'):
            # Assigning the SRID to the geometry.
            if value.srid is None: value.srid = self._field.srid
        elif value is None or isinstance(value, six.string_types + (buffer,)):
        elif value is None or isinstance(value, six.string_types + (memoryview,)):
            # Set with None, WKT, HEX, or WKB
            pass
        else:
+2 −1
Original line number Diff line number Diff line
from django.db import connections
from django.db.models.query import QuerySet, ValuesQuerySet, ValuesListQuerySet

from django.contrib.gis import memoryview
from django.contrib.gis.db.models import aggregates
from django.contrib.gis.db.models.fields import get_srid_info, PointField, LineStringField
from django.contrib.gis.db.models.sql import AreaField, DistanceField, GeomField, GeoQuery
@@ -676,7 +677,7 @@ class GeoQuerySet(QuerySet):
                    if not backend.geography:
                        if not isinstance(geo_field, PointField):
                            raise ValueError('Spherical distance calculation only supported on PointFields.')
                        if not str(Geometry(buffer(params[0].ewkb)).geom_type) == 'Point':
                        if not str(Geometry(memoryview(params[0].ewkb)).geom_type) == 'Point':
                            raise ValueError('Spherical distance calculation only supported with Point Geometry parameters')
                    # The `function` procedure argument needs to be set differently for
                    # geodetic distance calculations.
+5 −3
Original line number Diff line number Diff line
@@ -43,6 +43,8 @@ import sys
from binascii import a2b_hex, b2a_hex
from ctypes import byref, string_at, c_char_p, c_double, c_ubyte, c_void_p

from django.contrib.gis import memoryview

# Getting GDAL prerequisites
from django.contrib.gis.gdal.base import GDALBase
from django.contrib.gis.gdal.envelope import Envelope, OGREnvelope
@@ -76,7 +78,7 @@ class OGRGeometry(GDALBase):

        # If HEX, unpack input to to a binary buffer.
        if str_instance and hex_regex.match(geom_input):
            geom_input = buffer(a2b_hex(geom_input.upper()))
            geom_input = memoryview(a2b_hex(geom_input.upper()))
            str_instance = False

        # Constructing the geometry,
@@ -106,7 +108,7 @@ class OGRGeometry(GDALBase):
                # (e.g., 'Point', 'POLYGON').
                ogr_t = OGRGeomType(geom_input)
                g = capi.create_geom(OGRGeomType(geom_input).num)
        elif isinstance(geom_input, buffer):
        elif isinstance(geom_input, memoryview):
            # WKB was passed in
            g = capi.from_wkb(str(geom_input), None, byref(c_void_p()), len(geom_input))
        elif isinstance(geom_input, OGRGeomType):
@@ -354,7 +356,7 @@ class OGRGeometry(GDALBase):
        buf = (c_ubyte * sz)()
        wkb = capi.to_wkb(self.ptr, byteorder, byref(buf))
        # Returning a buffer of the string at the pointer.
        return buffer(string_at(buf, sz))
        return memoryview(string_at(buf, sz))

    @property
    def wkt(self):
+2 −1
Original line number Diff line number Diff line
from django.contrib.gis import memoryview
from django.contrib.gis.geos.geometry import GEOSGeometry, wkt_regex, hex_regex

from django.utils import six
@@ -18,7 +19,7 @@ def fromfile(file_h):
    if wkt_regex.match(buf) or hex_regex.match(buf):
        return GEOSGeometry(buf)
    else:
        return GEOSGeometry(buffer(buf))
        return GEOSGeometry(memoryview(buf))

def fromstr(string, **kwargs):
    "Given a string value, returns a GEOSGeometry object."
Loading