Permalink
Browse files

Merge remote-tracking branch 'django/master' into t3011

  • Loading branch information...
2 parents 8a527dd + 54c81a1 commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91 @freakboy3742 freakboy3742 committed Sep 23, 2012
@@ -0,0 +1,6 @@
+from django.utils import six
+
+if six.PY3:
+ memoryview = memoryview
+else:
+ memoryview = buffer
@@ -1,6 +1,7 @@
"""
This object provides quoting for GEOS geometries into PostgreSQL/PostGIS.
"""
+from __future__ import unicode_literals
from psycopg2 import Binary
from psycopg2.extensions import ISQLQuote
@@ -10,7 +11,7 @@ def __init__(self, geom):
"Initializes on the geometry."
# Getting the WKB (in string form, to allow easy pickling of
# the adaptor) and the SRID from the geometry.
- self.ewkb = str(geom.ewkb)
+ self.ewkb = bytes(geom.ewkb)
self.srid = geom.srid
self._adapter = Binary(self.ewkb)
@@ -39,7 +40,7 @@ def prepare(self, conn):
def getquoted(self):
"Returns a properly quoted string for use in PostgreSQL/PostGIS."
# psycopg will figure out whether to use E'\\000' or '\000'
- return 'ST_GeomFromEWKB(%s)' % self._adapter.getquoted()
+ return str('ST_GeomFromEWKB(%s)' % self._adapter.getquoted().decode())
def prepare_database_save(self, unused):
return self
@@ -160,7 +160,7 @@ def get_prep_value(self, value):
# from the given string input.
if isinstance(geom, Geometry):
pass
- elif isinstance(geom, six.string_types) or hasattr(geom, '__geo_interface__'):
+ elif isinstance(geom, (bytes, six.string_types)) or hasattr(geom, '__geo_interface__'):
try:
geom = Geometry(geom)
except GeometryException:
@@ -5,6 +5,7 @@
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 @@ def __set__(self, obj, value):
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:
@@ -1,6 +1,7 @@
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 @@ def _distance_attribute(self, func, geom=None, tolerance=0.05, spheroid=False, *
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.
@@ -1,3 +1,8 @@
+try:
+ from itertools import zip_longest
+except ImportError:
+ from itertools import izip_longest as zip_longest
+
from django.utils.six.moves import zip
from django.db.backends.util import truncate_name, typecast_timestamp
@@ -190,7 +195,7 @@ def resolve_columns(self, row, fields=()):
if self.connection.ops.oracle or getattr(self.query, 'geo_values', False):
# We resolve the rest of the columns if we're on Oracle or if
# the `geo_values` attribute is defined.
- for value, field in map(None, row[index_start:], fields):
+ for value, field in zip_longest(row[index_start:], fields):
values.append(self.query.convert_values(value, field, self.connection))
else:
values.extend(row[index_start:])
@@ -41,7 +41,7 @@
from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform
from django.contrib.gis.gdal.geometries import OGRGeometry
HAS_GDAL = True
-except:
+except ImportError:
HAS_GDAL = False
try:
@@ -45,6 +45,7 @@
# Getting the ctypes prototypes for the DataSource.
from django.contrib.gis.gdal.prototypes import ds as capi
+from django.utils.encoding import force_bytes
from django.utils import six
from django.utils.six.moves import xrange
@@ -73,7 +74,7 @@ def __init__(self, ds_input, ds_driver=False, write=False):
ds_driver = Driver.ptr_type()
try:
# OGROpen will auto-detect the data source type.
- ds = capi.open_ds(ds_input, self._write, byref(ds_driver))
+ ds = capi.open_ds(force_bytes(ds_input), self._write, byref(ds_driver))
except OGRException:
# Making the error message more clear rather than something
# like "Invalid pointer returned from OGROpen".
@@ -102,7 +103,7 @@ def __iter__(self):
def __getitem__(self, index):
"Allows use of the index [] operator to get a layer at the index."
if isinstance(index, six.string_types):
- l = capi.get_layer_by_name(self.ptr, index)
+ l = capi.get_layer_by_name(self.ptr, force_bytes(index))
if not l: raise OGRIndexError('invalid OGR Layer name given: "%s"' % index)
elif isinstance(index, int):
if index < 0 or index >= self.layer_count:
@@ -5,6 +5,7 @@
from django.contrib.gis.gdal.prototypes import ds as capi
from django.utils import six
+from django.utils.encoding import force_bytes
# For more information, see the OGR C API source code:
# http://www.gdal.org/ogr/ogr__api_8h.html
@@ -36,7 +37,7 @@ def __init__(self, dr_input):
name = dr_input
# Attempting to get the OGR driver by the string name.
- dr = capi.get_driver_by_name(name)
+ dr = capi.get_driver_by_name(force_bytes(name))
elif isinstance(dr_input, int):
self._register()
dr = capi.get_driver(dr_input)
@@ -52,7 +52,7 @@ def __init__(self, *args):
elif len(args) == 4:
# Individual parameters passed in.
# Thanks to ww for the help
- self._from_sequence(map(float, args))
+ self._from_sequence([float(a) for a in args])
else:
raise OGRException('Incorrect number (%d) of arguments.' % len(args))
@@ -7,6 +7,7 @@
# ctypes function prototypes
from django.contrib.gis.gdal.prototypes import ds as capi, geom as geom_api
+from django.utils.encoding import force_bytes
from django.utils import six
from django.utils.six.moves import xrange
@@ -107,6 +108,7 @@ def get(self, field):
def index(self, field_name):
"Returns the index of the given field name."
- i = capi.get_field_index(self.ptr, field_name)
- if i < 0: raise OGRIndexError('invalid OFT field name given: "%s"' % field_name)
+ i = capi.get_field_index(self.ptr, force_bytes(field_name))
+ if i < 0:
+ raise OGRIndexError('invalid OFT field name given: "%s"' % field_name)
return i
@@ -43,6 +43,8 @@
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,16 +78,11 @@ def __init__(self, geom_input, srs=None):
# 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().encode()))
str_instance = False
# Constructing the geometry,
if str_instance:
- # Checking if unicode
- if isinstance(geom_input, six.text_type):
- # Encoding to ASCII, WKT or HEX doesn't need any more.
- geom_input = geom_input.encode('ascii')
-
wkt_m = wkt_regex.match(geom_input)
json_m = json_regex.match(geom_input)
if wkt_m:
@@ -96,19 +93,19 @@ def __init__(self, geom_input, srs=None):
# OGR_G_CreateFromWkt doesn't work with LINEARRING WKT.
# See http://trac.osgeo.org/gdal/ticket/1992.
g = capi.create_geom(OGRGeomType(wkt_m.group('type')).num)
- capi.import_wkt(g, byref(c_char_p(wkt_m.group('wkt'))))
+ capi.import_wkt(g, byref(c_char_p(wkt_m.group('wkt').encode())))
else:
- g = capi.from_wkt(byref(c_char_p(wkt_m.group('wkt'))), None, byref(c_void_p()))
+ g = capi.from_wkt(byref(c_char_p(wkt_m.group('wkt').encode())), None, byref(c_void_p()))
elif json_m:
- g = capi.from_json(geom_input)
+ g = capi.from_json(geom_input.encode())
else:
# Seeing if the input is a valid short-hand string
# (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))
+ g = capi.from_wkb(bytes(geom_input), None, byref(c_void_p()), len(geom_input))
elif isinstance(geom_input, OGRGeomType):
# OGRGeomType was passed in, an empty geometry will be created.
g = capi.create_geom(geom_input.num)
@@ -141,7 +138,7 @@ def __getstate__(self):
srs = srs.wkt
else:
srs = None
- return str(self.wkb), srs
+ return bytes(self.wkb), srs
def __setstate__(self, state):
wkb, srs = state
@@ -354,7 +351,7 @@ def wkb(self):
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):
@@ -14,6 +14,7 @@
# GDAL ctypes function prototypes.
from django.contrib.gis.gdal.prototypes import ds as capi, geom as geom_api, srs as srs_api
+from django.utils.encoding import force_bytes
from django.utils import six
from django.utils.six.moves import xrange
@@ -38,7 +39,7 @@ def __init__(self, layer_ptr, ds):
self._ds = ds
self._ldefn = capi.get_layer_defn(self._ptr)
# Does the Layer support random reading?
- self._random_read = self.test_capability('RandomRead')
+ self._random_read = self.test_capability(b'RandomRead')
def __getitem__(self, index):
"Gets the Feature at the specified index."
@@ -212,4 +213,4 @@ def test_capability(self, capability):
'FastFeatureCount', 'FastGetExtent', 'CreateField', 'Transactions',
'DeleteFeature', and 'FastSetNextByIndex'.
"""
- return bool(capi.test_capability(self.ptr, capability))
+ return bool(capi.test_capability(self.ptr, force_bytes(capability)))
@@ -1,3 +1,5 @@
+from __future__ import unicode_literals
+
import os
import re
from ctypes import c_char_p, CDLL
@@ -65,7 +67,7 @@ def std_call(func):
def gdal_version():
"Returns only the GDAL version number information."
- return _version_info('RELEASE_NAME')
+ return _version_info(b'RELEASE_NAME')
def gdal_full_version():
"Returns the full GDAL version information."
@@ -86,7 +88,7 @@ def gdal_release_date(date=False):
version_regex = re.compile(r'^(?P<major>\d+)\.(?P<minor>\d+)(\.(?P<subminor>\d+))?')
def gdal_version_info():
- ver = gdal_version()
+ ver = gdal_version().decode()
m = version_regex.match(ver)
if not m: raise OGRException('Could not parse GDAL version string "%s"' % ver)
return dict([(key, m.group(key)) for key in ('major', 'minor', 'subminor')])
@@ -30,9 +30,10 @@ def check_const_string(result, func, cargs, offset=None):
if offset:
check_err(result)
ptr = ptr_byref(cargs, offset)
- return ptr.value
+ return ptr.value.decode()
else:
- return result
+ if result is not None:
+ return result.decode()
def check_string(result, func, cargs, offset=-1, str_result=False):
"""
@@ -47,13 +48,13 @@ def check_string(result, func, cargs, offset=-1, str_result=False):
# For routines that return a string.
ptr = result
if not ptr: s = None
- else: s = string_at(result)
+ else: s = string_at(result).decode()
else:
# Error-code return specified.
check_err(result)
ptr = ptr_byref(cargs, offset)
# Getting the string value
- s = ptr.value
+ s = ptr.value.decode()
# Correctly freeing the allocated memory beind GDAL pointer
# w/the VSIFree routine.
if ptr: lgdal.VSIFree(ptr)
@@ -125,4 +126,4 @@ def check_str_arg(result, func, cargs):
"""
dbl = result
ptr = cargs[-1]._obj
- return dbl, ptr.value
+ return dbl, ptr.value.decode()
@@ -34,6 +34,8 @@
from django.contrib.gis.gdal.prototypes import srs as capi
from django.utils import six
+from django.utils.encoding import force_bytes, force_text
+
#### Spatial Reference class. ####
class SpatialReference(GDALBase):
@@ -51,7 +53,6 @@ def __init__(self, srs_input=''):
EPSG code, a PROJ.4 string, and/or a projection "well known" shorthand
string (one of 'WGS84', 'WGS72', 'NAD27', 'NAD83').
"""
- buf = c_char_p('')
srs_type = 'user'
if isinstance(srs_input, six.string_types):
@@ -79,6 +80,7 @@ def __init__(self, srs_input=''):
srs = srs_input
else:
# Creating a new SRS pointer, using the string buffer.
+ buf = c_char_p(b'')
srs = capi.new_srs(buf)
# If the pointer is NULL, throw an exception.
@@ -137,15 +139,16 @@ def attr_value(self, target, index=0):
"""
if not isinstance(target, six.string_types) or not isinstance(index, int):
raise TypeError
- return capi.get_attr_value(self.ptr, target, index)
+ value = capi.get_attr_value(self.ptr, force_bytes(target), index)
+ return force_text(value, 'ascii', strings_only=True)
def auth_name(self, target):
"Returns the authority name for the given string target node."
- return capi.get_auth_name(self.ptr, target)
+ return capi.get_auth_name(self.ptr, force_bytes(target))
def auth_code(self, target):
"Returns the authority code for the given string target node."
- return capi.get_auth_code(self.ptr, target)
+ return capi.get_auth_code(self.ptr, force_bytes(target))
def clone(self):
"Returns a clone of this SpatialReference object."
@@ -219,12 +222,14 @@ def units(self):
and will automatically determines whether to return the linear
or angular units.
"""
+ units, name = None, None
if self.projected or self.local:
- return capi.linear_units(self.ptr, byref(c_char_p()))
+ units, name = capi.linear_units(self.ptr, byref(c_char_p()))
elif self.geographic:
- return capi.angular_units(self.ptr, byref(c_char_p()))
- else:
- return (None, None)
+ units, name = capi.angular_units(self.ptr, byref(c_char_p()))
+ if name is not None:
+ name.decode()
+ return (units, name)
#### Spheroid/Ellipsoid Properties ####
@property
@@ -283,7 +288,7 @@ def import_proj(self, proj):
def import_user_input(self, user_input):
"Imports the Spatial Reference from the given user input string."
- capi.from_user_input(self.ptr, user_input)
+ capi.from_user_input(self.ptr, force_bytes(user_input))
def import_wkt(self, wkt):
"Imports the Spatial Reference from OGC WKT (string)"
Oops, something went wrong.

0 comments on commit 29d1abb

Please sign in to comment.