2008-08-06 02:13:06 +08:00
|
|
|
# The GDAL C library, OGR exception, and the Field object
|
2009-03-08 07:02:48 +08:00
|
|
|
from django.contrib.gis.gdal.base import GDALBase
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.contrib.gis.gdal.error import OGRException, OGRIndexError
|
|
|
|
from django.contrib.gis.gdal.field import Field
|
|
|
|
from django.contrib.gis.gdal.geometries import OGRGeometry, OGRGeomType
|
|
|
|
|
|
|
|
# ctypes function prototypes
|
2009-03-08 07:02:48 +08:00
|
|
|
from django.contrib.gis.gdal.prototypes import ds as capi, geom as geom_api
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# For more information, see the OGR C API source code:
|
|
|
|
# http://www.gdal.org/ogr/ogr__api_8h.html
|
|
|
|
#
|
|
|
|
# The OGR_F_* routines are relevant here.
|
2009-03-08 07:02:48 +08:00
|
|
|
class Feature(GDALBase):
|
2008-08-06 02:13:06 +08:00
|
|
|
"A class that wraps an OGR Feature, needs to be instantiated from a Layer object."
|
|
|
|
|
|
|
|
#### Python 'magic' routines ####
|
|
|
|
def __init__(self, feat, fdefn):
|
|
|
|
"Initializes on the pointers for the feature and the layer definition."
|
|
|
|
if not feat or not fdefn:
|
|
|
|
raise OGRException('Cannot create OGR Feature, invalid pointer given.')
|
2009-03-08 07:02:48 +08:00
|
|
|
self.ptr = feat
|
2008-08-06 02:13:06 +08:00
|
|
|
self._fdefn = fdefn
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
"Releases a reference to this object."
|
2009-03-08 07:02:48 +08:00
|
|
|
if self._ptr: capi.destroy_feature(self._ptr)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
"""
|
|
|
|
Gets the Field object at the specified index, which may be either
|
|
|
|
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.
|
|
|
|
"""
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(index, six.string_types):
|
2008-08-06 02:13:06 +08:00
|
|
|
i = self.index(index)
|
|
|
|
else:
|
|
|
|
if index < 0 or index > self.num_fields:
|
|
|
|
raise OGRIndexError('index out of range')
|
|
|
|
i = index
|
2009-03-08 07:02:48 +08:00
|
|
|
return Field(self.ptr, i)
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def __iter__(self):
|
|
|
|
"Iterates over each field in the Feature."
|
|
|
|
for i in xrange(self.num_fields):
|
|
|
|
yield self[i]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
"Returns the count of fields in this feature."
|
|
|
|
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):
|
|
|
|
"Does 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
|
|
|
|
|
|
|
#### Feature Properties ####
|
|
|
|
@property
|
|
|
|
def fid(self):
|
|
|
|
"Returns 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):
|
|
|
|
"Returns the name of the layer for the feature."
|
2009-03-08 07:02:48 +08:00
|
|
|
return capi.get_feat_name(self._fdefn)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def num_fields(self):
|
|
|
|
"Returns 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):
|
|
|
|
"Returns a list of fields in the Feature."
|
2012-07-20 20:22:00 +08:00
|
|
|
return [capi.get_field_name(capi.get_field_defn(self._fdefn, i))
|
2008-08-06 02:13:06 +08:00
|
|
|
for i in xrange(self.num_fields)]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def geom(self):
|
|
|
|
"Returns the OGR Geometry for this Feature."
|
|
|
|
# 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):
|
|
|
|
"Returns the OGR Geometry Type for this Feture."
|
2009-03-08 07:02:48 +08:00
|
|
|
return OGRGeomType(capi.get_fd_geom_type(self._fdefn))
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
#### Feature Methods ####
|
|
|
|
def get(self, field):
|
|
|
|
"""
|
|
|
|
Returns the value of the field, instead of an instance of the Field
|
|
|
|
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):
|
|
|
|
"Returns the index of the given field name."
|
2009-03-08 07:02:48 +08:00
|
|
|
i = capi.get_field_index(self.ptr, field_name)
|
2008-08-06 02:13:06 +08:00
|
|
|
if i < 0: raise OGRIndexError('invalid OFT field name given: "%s"' % field_name)
|
|
|
|
return i
|