2008-08-06 02:13:06 +08:00
|
|
|
from ctypes import byref, c_int
|
|
|
|
from datetime import date, datetime, time
|
2015-01-28 20:35:27 +08:00
|
|
|
|
2009-03-08 07:02:48 +08:00
|
|
|
from django.contrib.gis.gdal.base import GDALBase
|
2014-12-18 05:00:05 +08:00
|
|
|
from django.contrib.gis.gdal.error import GDALException
|
2009-03-08 07:02:48 +08:00
|
|
|
from django.contrib.gis.gdal.prototypes import ds as capi
|
2012-10-07 04:56:47 +08:00
|
|
|
from django.utils.encoding import force_text
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# For more information, see the OGR C API source code:
|
2016-06-16 09:20:23 +08:00
|
|
|
# http://www.gdal.org/ogr__api_8h.html
|
2008-08-06 02:13:06 +08:00
|
|
|
#
|
|
|
|
# The OGR_Fld_* routines are relevant here.
|
2009-03-08 07:02:48 +08:00
|
|
|
class Field(GDALBase):
|
2012-10-07 22:20:29 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Wrap an OGR Field. Needs to be instantiated from a Feature object.
|
2012-10-07 22:20:29 +08:00
|
|
|
"""
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def __init__(self, feat, index):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Initialize on the feature object and the integer index of
|
2008-08-06 02:13:06 +08:00
|
|
|
the field within the feature.
|
|
|
|
"""
|
|
|
|
# Setting the feature pointer and index.
|
|
|
|
self._feat = feat
|
|
|
|
self._index = index
|
2011-03-31 00:56:53 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Getting the pointer for this field.
|
2012-10-07 22:20:29 +08:00
|
|
|
fld_ptr = capi.get_feat_field_defn(feat.ptr, index)
|
2009-03-08 07:02:48 +08:00
|
|
|
if not fld_ptr:
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Cannot create OGR Field, invalid pointer given.')
|
2009-03-08 07:02:48 +08:00
|
|
|
self.ptr = fld_ptr
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Setting the class depending upon the OGR Field Type (OFT)
|
2009-03-08 07:02:48 +08:00
|
|
|
self.__class__ = OGRFieldTypes[self.type]
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# OFTReal with no precision should be an OFTInteger.
|
|
|
|
if isinstance(self, OFTReal) and self.precision == 0:
|
|
|
|
self.__class__ = OFTInteger
|
2011-03-31 00:56:53 +08:00
|
|
|
self._double = True
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the string representation of the Field."
|
2008-08-06 02:13:06 +08:00
|
|
|
return str(self.value).strip()
|
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# #### Field Methods ####
|
2008-08-06 02:13:06 +08:00
|
|
|
def as_double(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Retrieve the Field's value as a double (float)."
|
2012-10-07 22:20:29 +08:00
|
|
|
return capi.get_field_as_double(self._feat.ptr, self._index)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-06-20 17:31:22 +08:00
|
|
|
def as_int(self, is_64=False):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Retrieve the Field's value as an integer."
|
2015-06-20 17:31:22 +08:00
|
|
|
if is_64:
|
|
|
|
return capi.get_field_as_integer64(self._feat.ptr, self._index)
|
|
|
|
else:
|
|
|
|
return capi.get_field_as_integer(self._feat.ptr, self._index)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def as_string(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Retrieve the Field's value as a string."
|
2012-10-07 04:56:47 +08:00
|
|
|
string = capi.get_field_as_string(self._feat.ptr, self._index)
|
|
|
|
return force_text(string, encoding=self._feat.encoding, strings_only=True)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def as_datetime(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Retrieve the Field's value as a tuple of date & time components."
|
2008-08-06 02:13:06 +08:00
|
|
|
yy, mm, dd, hh, mn, ss, tz = [c_int() for i in range(7)]
|
2012-10-07 22:20:29 +08:00
|
|
|
status = capi.get_field_as_datetime(
|
|
|
|
self._feat.ptr, self._index, byref(yy), byref(mm), byref(dd),
|
|
|
|
byref(hh), byref(mn), byref(ss), byref(tz))
|
2008-08-06 02:13:06 +08:00
|
|
|
if status:
|
|
|
|
return (yy, mm, dd, hh, mn, ss, tz)
|
|
|
|
else:
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Unable to retrieve date & time information from the field.')
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# #### Field Properties ####
|
2008-08-06 02:13:06 +08:00
|
|
|
@property
|
|
|
|
def name(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the name of this Field."
|
2012-10-07 04:56:47 +08:00
|
|
|
name = capi.get_field_name(self.ptr)
|
|
|
|
return force_text(name, encoding=self._feat.encoding, strings_only=True)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def precision(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the precision of this Field."
|
2009-03-08 07:02:48 +08:00
|
|
|
return capi.get_field_precision(self.ptr)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the OGR type of this Field."
|
2009-03-08 07:02:48 +08:00
|
|
|
return capi.get_field_type(self.ptr)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type_name(self):
|
|
|
|
"Return the OGR field type name for this Field."
|
2009-03-08 07:02:48 +08:00
|
|
|
return capi.get_field_type_name(self.type)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def value(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the value of this Field."
|
2008-08-06 02:13:06 +08:00
|
|
|
# Default is to get the field as a string.
|
|
|
|
return self.as_string()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def width(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the width of this Field."
|
2009-03-08 07:02:48 +08:00
|
|
|
return capi.get_field_width(self.ptr)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# ### The Field sub-classes for each OGR Field type. ###
|
2008-08-06 02:13:06 +08:00
|
|
|
class OFTInteger(Field):
|
2011-03-31 00:56:53 +08:00
|
|
|
_double = False
|
2015-06-20 17:31:22 +08:00
|
|
|
_bit64 = False
|
2011-03-31 00:56:53 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
@property
|
|
|
|
def value(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return an integer contained in this field."
|
2011-03-31 00:56:53 +08:00
|
|
|
if self._double:
|
|
|
|
# If this is really from an OFTReal field with no precision,
|
|
|
|
# read as a double and cast as Python int (to prevent overflow).
|
|
|
|
return int(self.as_double())
|
|
|
|
else:
|
2015-06-20 17:31:22 +08:00
|
|
|
return self.as_int(self._bit64)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
"""
|
|
|
|
GDAL uses OFTReals to represent OFTIntegers in created
|
|
|
|
shapefiles -- forcing the type here since the underlying field
|
|
|
|
type may actually be OFTReal.
|
|
|
|
"""
|
|
|
|
return 0
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class OFTReal(Field):
|
|
|
|
@property
|
|
|
|
def value(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return a float contained in this field."
|
2008-08-06 02:13:06 +08:00
|
|
|
return self.as_double()
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# String & Binary fields, just subclasses
|
2013-10-17 16:17:41 +08:00
|
|
|
class OFTString(Field):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-10-17 16:17:41 +08:00
|
|
|
class OFTWideString(Field):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-10-17 16:17:41 +08:00
|
|
|
class OFTBinary(Field):
|
|
|
|
pass
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# OFTDate, OFTTime, OFTDateTime fields.
|
|
|
|
class OFTDate(Field):
|
|
|
|
@property
|
|
|
|
def value(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return a Python `date` object for the OFTDate field."
|
2008-08-06 02:13:06 +08:00
|
|
|
try:
|
2009-01-08 04:21:19 +08:00
|
|
|
yy, mm, dd, hh, mn, ss, tz = self.as_datetime()
|
2008-08-06 02:13:06 +08:00
|
|
|
return date(yy.value, mm.value, dd.value)
|
2014-12-18 05:00:05 +08:00
|
|
|
except (ValueError, GDALException):
|
2008-08-06 02:13:06 +08:00
|
|
|
return None
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class OFTDateTime(Field):
|
|
|
|
@property
|
|
|
|
def value(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return a Python `datetime` object for this OFTDateTime field."
|
2008-08-06 02:13:06 +08:00
|
|
|
# TODO: Adapt timezone information.
|
2011-11-01 02:54:24 +08:00
|
|
|
# See http://lists.osgeo.org/pipermail/gdal-dev/2006-February/007990.html
|
2011-03-31 00:56:53 +08:00
|
|
|
# The `tz` variable has values of: 0=unknown, 1=localtime (ambiguous),
|
2008-08-06 02:13:06 +08:00
|
|
|
# 100=GMT, 104=GMT+1, 80=GMT-5, etc.
|
|
|
|
try:
|
2009-01-08 04:21:19 +08:00
|
|
|
yy, mm, dd, hh, mn, ss, tz = self.as_datetime()
|
2008-08-06 02:13:06 +08:00
|
|
|
return datetime(yy.value, mm.value, dd.value, hh.value, mn.value, ss.value)
|
2014-12-18 05:00:05 +08:00
|
|
|
except (ValueError, GDALException):
|
2008-08-06 02:13:06 +08:00
|
|
|
return None
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class OFTTime(Field):
|
|
|
|
@property
|
|
|
|
def value(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return a Python `time` object for this OFTTime field."
|
2008-08-06 02:13:06 +08:00
|
|
|
try:
|
2009-01-08 04:21:19 +08:00
|
|
|
yy, mm, dd, hh, mn, ss, tz = self.as_datetime()
|
2008-08-06 02:13:06 +08:00
|
|
|
return time(hh.value, mn.value, ss.value)
|
2014-12-18 05:00:05 +08:00
|
|
|
except (ValueError, GDALException):
|
2008-08-06 02:13:06 +08:00
|
|
|
return None
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2015-06-20 17:31:22 +08:00
|
|
|
class OFTInteger64(OFTInteger):
|
|
|
|
_bit64 = True
|
|
|
|
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# List fields are also just subclasses
|
2013-10-17 16:17:41 +08:00
|
|
|
class OFTIntegerList(Field):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-10-17 16:17:41 +08:00
|
|
|
class OFTRealList(Field):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-10-17 16:17:41 +08:00
|
|
|
class OFTStringList(Field):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-10-17 16:17:41 +08:00
|
|
|
class OFTWideStringList(Field):
|
|
|
|
pass
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-06-20 17:31:22 +08:00
|
|
|
|
|
|
|
class OFTInteger64List(Field):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2009-03-08 07:02:48 +08:00
|
|
|
# Class mapping dictionary for OFT Types and reverse mapping.
|
2013-10-15 03:13:14 +08:00
|
|
|
OGRFieldTypes = {
|
|
|
|
0: OFTInteger,
|
|
|
|
1: OFTIntegerList,
|
|
|
|
2: OFTReal,
|
|
|
|
3: OFTRealList,
|
|
|
|
4: OFTString,
|
|
|
|
5: OFTStringList,
|
|
|
|
6: OFTWideString,
|
|
|
|
7: OFTWideStringList,
|
|
|
|
8: OFTBinary,
|
|
|
|
9: OFTDate,
|
|
|
|
10: OFTTime,
|
|
|
|
11: OFTDateTime,
|
2015-06-20 17:31:22 +08:00
|
|
|
# New 64-bit integer types in GDAL 2
|
|
|
|
12: OFTInteger64,
|
|
|
|
13: OFTInteger64List,
|
2013-10-15 03:13:14 +08:00
|
|
|
}
|
2014-12-07 05:00:09 +08:00
|
|
|
ROGRFieldTypes = {cls: num for num, cls in OGRFieldTypes.items()}
|