2009-03-08 07:02:48 +08:00
|
|
|
from django.contrib.gis.gdal.base import GDALBase
|
2017-09-04 09:19:37 +08:00
|
|
|
from django.contrib.gis.gdal.error import GDALException
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.contrib.gis.gdal.field import Field
|
|
|
|
from django.contrib.gis.gdal.geometries import OGRGeometry, OGRGeomType
|
2009-03-08 07:02:48 +08:00
|
|
|
from django.contrib.gis.gdal.prototypes import ds as capi, geom as geom_api
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.encoding import force_bytes, force_text
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
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_F_* routines are relevant here.
|
2009-03-08 07:02:48 +08:00
|
|
|
class Feature(GDALBase):
|
2012-10-07 22:20:29 +08:00
|
|
|
"""
|
|
|
|
This class that wraps an OGR Feature, needs to be instantiated
|
|
|
|
from a Layer object.
|
|
|
|
"""
|
2016-12-16 05:59:08 +08:00
|
|
|
destructor = capi.destroy_feature
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-10-07 22:20:29 +08:00
|
|
|
def __init__(self, feat, layer):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Initialize Feature from a pointer and its Layer object.
|
2012-10-07 22:20:29 +08:00
|
|
|
"""
|
|
|
|
if not feat:
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Cannot create OGR Feature, invalid pointer given.')
|
2009-03-08 07:02:48 +08:00
|
|
|
self.ptr = feat
|
2012-10-07 22:20:29 +08:00
|
|
|
self._layer = layer
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Get the Field object at the specified index, which may be either
|
2008-08-06 02:13:06 +08:00
|
|
|
an integer or the Field's string label. Note that the Field object
|
2012-07-20 20:22:00 +08:00
|
|
|
is not the field's _value_ -- use the `get` method instead to
|
2008-08-06 02:13:06 +08:00
|
|
|
retrieve the value (e.g. an integer) instead of a Field instance.
|
|
|
|
"""
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(index, str):
|
2008-08-06 02:13:06 +08:00
|
|
|
i = self.index(index)
|
2017-09-01 22:48:43 +08:00
|
|
|
elif 0 <= index < self.num_fields:
|
2008-08-06 02:13:06 +08:00
|
|
|
i = index
|
2017-09-01 22:48:43 +08:00
|
|
|
else:
|
2017-09-02 15:38:17 +08:00
|
|
|
raise IndexError('Index out of range when accessing field in a feature: %s.' % index)
|
2012-10-07 22:20:29 +08:00
|
|
|
return Field(self, i)
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def __len__(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the count of fields in this feature."
|
2008-08-06 02:13:06 +08:00
|
|
|
return self.num_fields
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def __str__(self):
|
|
|
|
"The string name of the feature."
|
|
|
|
return 'Feature FID %d in Layer<%s>' % (self.fid, self.layer_name)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Do equivalence testing on the features."
|
2009-03-08 07:02:48 +08:00
|
|
|
return bool(capi.feature_equal(self.ptr, other._ptr))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# #### Feature Properties ####
|
2012-10-07 04:56:47 +08:00
|
|
|
@property
|
|
|
|
def encoding(self):
|
|
|
|
return self._layer._ds.encoding
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
@property
|
|
|
|
def fid(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the feature identifier."
|
2009-03-08 07:02:48 +08:00
|
|
|
return capi.get_fid(self.ptr)
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
@property
|
|
|
|
def layer_name(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the name of the layer for the feature."
|
2012-10-07 04:56:47 +08:00
|
|
|
name = capi.get_feat_name(self._layer._ldefn)
|
|
|
|
return force_text(name, self.encoding, strings_only=True)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def num_fields(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the number of fields in the Feature."
|
2009-03-08 07:02:48 +08:00
|
|
|
return capi.get_feat_field_count(self.ptr)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def fields(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return a list of fields in the Feature."
|
2017-08-14 17:32:37 +08:00
|
|
|
return [
|
|
|
|
force_text(
|
|
|
|
capi.get_field_name(capi.get_field_defn(self._layer._ldefn, i)),
|
|
|
|
self.encoding,
|
|
|
|
strings_only=True
|
|
|
|
) for i in range(self.num_fields)
|
|
|
|
]
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def geom(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the OGR Geometry for this Feature."
|
2008-08-06 02:13:06 +08:00
|
|
|
# Retrieving the geometry pointer for the feature.
|
2009-03-08 07:02:48 +08:00
|
|
|
geom_ptr = capi.get_feat_geom_ref(self.ptr)
|
|
|
|
return OGRGeometry(geom_api.clone_geom(geom_ptr))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def geom_type(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the OGR Geometry Type for this Feture."
|
2012-10-07 22:20:29 +08:00
|
|
|
return OGRGeomType(capi.get_fd_geom_type(self._layer._ldefn))
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# #### Feature Methods ####
|
2008-08-06 02:13:06 +08:00
|
|
|
def get(self, field):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the value of the field, instead of an instance of the Field
|
2008-08-06 02:13:06 +08:00
|
|
|
object. May take a string of the field name or a Field object as
|
|
|
|
parameters.
|
|
|
|
"""
|
|
|
|
field_name = getattr(field, 'name', field)
|
|
|
|
return self[field_name].value
|
|
|
|
|
|
|
|
def index(self, field_name):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the index of the given field name."
|
2012-09-24 01:59:27 +08:00
|
|
|
i = capi.get_field_index(self.ptr, force_bytes(field_name))
|
|
|
|
if i < 0:
|
2017-09-02 15:38:17 +08:00
|
|
|
raise IndexError('Invalid OFT field name given: %s.' % field_name)
|
2008-08-06 02:13:06 +08:00
|
|
|
return i
|