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