You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

feature.py 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from django.contrib.gis.gdal.base import GDALBase
  2. from django.contrib.gis.gdal.error import GDALException, OGRIndexError
  3. from django.contrib.gis.gdal.field import Field
  4. from django.contrib.gis.gdal.geometries import OGRGeometry, OGRGeomType
  5. from django.contrib.gis.gdal.prototypes import ds as capi, geom as geom_api
  6. from django.utils import six
  7. from django.utils.encoding import force_bytes, force_text
  8. from django.utils.six.moves import range
  9. # For more information, see the OGR C API source code:
  10. # http://www.gdal.org/ogr__api_8h.html
  11. #
  12. # The OGR_F_* routines are relevant here.
  13. class Feature(GDALBase):
  14. """
  15. This class that wraps an OGR Feature, needs to be instantiated
  16. from a Layer object.
  17. """
  18. def __init__(self, feat, layer):
  19. """
  20. Initializes Feature from a pointer and its Layer object.
  21. """
  22. if not feat:
  23. raise GDALException('Cannot create OGR Feature, invalid pointer given.')
  24. self.ptr = feat
  25. self._layer = layer
  26. def __del__(self):
  27. "Releases a reference to this object."
  28. if self._ptr and capi:
  29. capi.destroy_feature(self._ptr)
  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, six.string_types):
  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, i)
  44. def __iter__(self):
  45. "Iterates over each field in the Feature."
  46. for i in range(self.num_fields):
  47. yield self[i]
  48. def __len__(self):
  49. "Returns the count of fields in this feature."
  50. return self.num_fields
  51. def __str__(self):
  52. "The string name of the feature."
  53. return 'Feature FID %d in Layer<%s>' % (self.fid, self.layer_name)
  54. def __eq__(self, other):
  55. "Does equivalence testing on the features."
  56. return bool(capi.feature_equal(self.ptr, other._ptr))
  57. # #### Feature Properties ####
  58. @property
  59. def encoding(self):
  60. return self._layer._ds.encoding
  61. @property
  62. def fid(self):
  63. "Returns the feature identifier."
  64. return capi.get_fid(self.ptr)
  65. @property
  66. def layer_name(self):
  67. "Returns the name of the layer for the feature."
  68. name = capi.get_feat_name(self._layer._ldefn)
  69. return force_text(name, self.encoding, strings_only=True)
  70. @property
  71. def num_fields(self):
  72. "Returns the number of fields in the Feature."
  73. return capi.get_feat_field_count(self.ptr)
  74. @property
  75. def fields(self):
  76. "Returns a list of fields in the Feature."
  77. return [capi.get_field_name(capi.get_field_defn(self._layer._ldefn, i))
  78. for i in range(self.num_fields)]
  79. @property
  80. def geom(self):
  81. "Returns the OGR Geometry for this Feature."
  82. # Retrieving the geometry pointer for the feature.
  83. geom_ptr = capi.get_feat_geom_ref(self.ptr)
  84. return OGRGeometry(geom_api.clone_geom(geom_ptr))
  85. @property
  86. def geom_type(self):
  87. "Returns the OGR Geometry Type for this Feture."
  88. return OGRGeomType(capi.get_fd_geom_type(self._layer._ldefn))
  89. # #### Feature Methods ####
  90. def get(self, field):
  91. """
  92. Returns the value of the field, instead of an instance of the Field
  93. object. May take a string of the field name or a Field object as
  94. parameters.
  95. """
  96. field_name = getattr(field, 'name', field)
  97. return self[field_name].value
  98. def index(self, field_name):
  99. "Returns the index of the given field name."
  100. i = capi.get_field_index(self.ptr, force_bytes(field_name))
  101. if i < 0:
  102. raise OGRIndexError('invalid OFT field name given: "%s"' % field_name)
  103. return i