feature.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from django.contrib.gis.gdal.base import GDALBase
  2. from django.contrib.gis.gdal.error import GDALException
  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
  6. from django.contrib.gis.gdal.prototypes import geom as geom_api
  7. from django.utils.encoding import force_bytes, force_str
  8. # For more information, see the OGR C API source code:
  9. # https://gdal.org/api/vector_c_api.html
  10. #
  11. # The OGR_F_* routines are relevant here.
  12. class Feature(GDALBase):
  13. """
  14. This class that wraps an OGR Feature, needs to be instantiated
  15. from a Layer object.
  16. """
  17. destructor = capi.destroy_feature
  18. def __init__(self, feat, layer):
  19. """
  20. Initialize 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 __getitem__(self, index):
  27. """
  28. Get the Field object at the specified index, which may be either
  29. an integer or the Field's string label. Note that the Field object
  30. is not the field's _value_ -- use the `get` method instead to
  31. retrieve the value (e.g. an integer) instead of a Field instance.
  32. """
  33. if isinstance(index, str):
  34. i = self.index(index)
  35. elif 0 <= index < self.num_fields:
  36. i = index
  37. else:
  38. raise IndexError(
  39. "Index out of range when accessing field in a feature: %s." % index
  40. )
  41. return Field(self, i)
  42. def __len__(self):
  43. "Return the count of fields in this feature."
  44. return self.num_fields
  45. def __str__(self):
  46. "The string name of the feature."
  47. return "Feature FID %d in Layer<%s>" % (self.fid, self.layer_name)
  48. def __eq__(self, other):
  49. "Do equivalence testing on the features."
  50. return bool(capi.feature_equal(self.ptr, other._ptr))
  51. # #### Feature Properties ####
  52. @property
  53. def encoding(self):
  54. return self._layer._ds.encoding
  55. @property
  56. def fid(self):
  57. "Return the feature identifier."
  58. return capi.get_fid(self.ptr)
  59. @property
  60. def layer_name(self):
  61. "Return the name of the layer for the feature."
  62. name = capi.get_feat_name(self._layer._ldefn)
  63. return force_str(name, self.encoding, strings_only=True)
  64. @property
  65. def num_fields(self):
  66. "Return the number of fields in the Feature."
  67. return capi.get_feat_field_count(self.ptr)
  68. @property
  69. def fields(self):
  70. "Return a list of fields in the Feature."
  71. return [
  72. force_str(
  73. capi.get_field_name(capi.get_field_defn(self._layer._ldefn, i)),
  74. self.encoding,
  75. strings_only=True,
  76. )
  77. for i in range(self.num_fields)
  78. ]
  79. @property
  80. def geom(self):
  81. "Return 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. "Return the OGR Geometry Type for this Feature."
  88. return OGRGeomType(capi.get_fd_geom_type(self._layer._ldefn))
  89. # #### Feature Methods ####
  90. def get(self, field):
  91. """
  92. Return 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. "Return 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 IndexError("Invalid OFT field name given: %s." % field_name)
  103. return i