PageRenderTime 22ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/gdal/feature.py

https://code.google.com/p/mango-py/
Python | 110 lines | 96 code | 5 blank | 9 comment | 7 complexity | d3700e6e05ab1e614345cfff0c8086f3 MD5 | raw file
  1# The GDAL C library, OGR exception, and the Field object
  2from django.contrib.gis.gdal.base import GDALBase
  3from django.contrib.gis.gdal.error import OGRException, OGRIndexError
  4from django.contrib.gis.gdal.field import Field
  5from django.contrib.gis.gdal.geometries import OGRGeometry, OGRGeomType
  6from django.contrib.gis.gdal.srs import SpatialReference
  7
  8# ctypes function prototypes
  9from django.contrib.gis.gdal.prototypes import ds as capi, geom as geom_api
 10
 11# For more information, see the OGR C API source code:
 12#  http://www.gdal.org/ogr/ogr__api_8h.html
 13#
 14# The OGR_F_* routines are relevant here.
 15class Feature(GDALBase):
 16    "A class that wraps an OGR Feature, needs to be instantiated from a Layer object."
 17
 18    #### Python 'magic' routines ####
 19    def __init__(self, feat, fdefn):
 20        "Initializes on the pointers for the feature and the layer definition."
 21        if not feat or not fdefn:
 22            raise OGRException('Cannot create OGR Feature, invalid pointer given.')
 23        self.ptr = feat
 24        self._fdefn = fdefn
 25
 26    def __del__(self):
 27        "Releases a reference to this object."
 28        if self._ptr: capi.destroy_feature(self._ptr)
 29
 30    def __getitem__(self, index):
 31        """
 32        Gets the Field object at the specified index, which may be either
 33        an integer or the Field's string label.  Note that the Field object
 34        is not the field's _value_ -- use the `get` method instead to 
 35        retrieve the value (e.g. an integer) instead of a Field instance.
 36        """
 37        if isinstance(index, basestring):
 38            i = self.index(index)
 39        else:
 40            if index < 0 or index > self.num_fields:
 41                raise OGRIndexError('index out of range')
 42            i = index
 43        return Field(self.ptr, i)
 44    
 45    def __iter__(self):
 46        "Iterates over each field in the Feature."
 47        for i in xrange(self.num_fields):
 48            yield self[i]
 49
 50    def __len__(self):
 51        "Returns the count of fields in this feature."
 52        return self.num_fields
 53        
 54    def __str__(self):
 55        "The string name of the feature."
 56        return 'Feature FID %d in Layer<%s>' % (self.fid, self.layer_name)
 57
 58    def __eq__(self, other):
 59        "Does equivalence testing on the features."
 60        return bool(capi.feature_equal(self.ptr, other._ptr))
 61
 62    #### Feature Properties ####
 63    @property
 64    def fid(self):
 65        "Returns the feature identifier."
 66        return capi.get_fid(self.ptr)
 67        
 68    @property
 69    def layer_name(self):
 70        "Returns the name of the layer for the feature."
 71        return capi.get_feat_name(self._fdefn)
 72
 73    @property
 74    def num_fields(self):
 75        "Returns the number of fields in the Feature."
 76        return capi.get_feat_field_count(self.ptr)
 77
 78    @property
 79    def fields(self):
 80        "Returns a list of fields in the Feature."
 81        return [capi.get_field_name(capi.get_field_defn(self._fdefn, i)) 
 82                for i in xrange(self.num_fields)]
 83
 84    @property
 85    def geom(self):
 86        "Returns the OGR Geometry for this Feature."
 87        # Retrieving the geometry pointer for the feature.
 88        geom_ptr = capi.get_feat_geom_ref(self.ptr)
 89        return OGRGeometry(geom_api.clone_geom(geom_ptr))
 90
 91    @property
 92    def geom_type(self):
 93        "Returns the OGR Geometry Type for this Feture."
 94        return OGRGeomType(capi.get_fd_geom_type(self._fdefn))
 95    
 96    #### Feature Methods ####
 97    def get(self, field):
 98        """
 99        Returns the value of the field, instead of an instance of the Field
100        object.  May take a string of the field name or a Field object as
101        parameters.
102        """
103        field_name = getattr(field, 'name', field)
104        return self[field_name].value
105
106    def index(self, field_name):
107        "Returns the index of the given field name."
108        i = capi.get_field_index(self.ptr, field_name)
109        if i < 0: raise OGRIndexError('invalid OFT field name given: "%s"' % field_name)
110        return i