Debian Codegraph @

django/contrib/gis/gdal/feature.py

raw
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
9
10 # For more information, see the OGR C API source code:
11 # https://gdal.org/api/vector_c_api.html
12 #
13 # The OGR_F_* routines are relevant here.
14 class Feature(GDALBase):
15 """
16 This class that wraps an OGR Feature, needs to be instantiated
17 from a Layer object.
18 """
19
20 destructor = capi.destroy_feature
21
22 def __init__(self, feat, layer):
23 """
24 Initialize Feature from a pointer and its Layer object.
25 """
26 if not feat:
27 raise GDALException("Cannot create OGR Feature, invalid pointer given.")
28 self.ptr = feat
29 self._layer = layer
30
31 def __getitem__(self, index):
32 """
33 Get the Field object at the specified index, which may be either
34 an integer or the Field's string label. Note that the Field object
35 is not the field's _value_ -- use the `get` method instead to
36 retrieve the value (e.g. an integer) instead of a Field instance.
37 """
38 if isinstance(index, str):
39 i = self.index(index)
40 elif 0 <= index < self.num_fields:
41 i = index
42 else:
43 raise IndexError(
44 "Index out of range when accessing field in a feature: %s." % index
45 )
46 return Field(self, i)
47
48 def __len__(self):
49 "Return the count of fields in this feature."
50 return self.num_fields
51
52 def __str__(self):
53 "The string name of the feature."
54 return "Feature FID %d in Layer<%s>" % (self.fid, self.layer_name)
55
56 def __eq__(self, other):
57 "Do equivalence testing on the features."
58 return bool(capi.feature_equal(self.ptr, other._ptr))
59
60 # #### Feature Properties ####
61 @property
62 def encoding(self):
63 return self._layer._ds.encoding
64
65 @property
66 def fid(self):
67 "Return the feature identifier."
68 return capi.get_fid(self.ptr)
69
70 @property
71 def layer_name(self):
72 "Return the name of the layer for the feature."
73 name = capi.get_feat_name(self._layer._ldefn)
74 return force_str(name, self.encoding, strings_only=True)
75
76 @property
77 def num_fields(self):
78 "Return the number of fields in the Feature."
79 return capi.get_feat_field_count(self.ptr)
80
81 @property
82 def fields(self):
83 "Return a list of fields in the Feature."
84 return [
85 force_str(
86 capi.get_field_name(capi.get_field_defn(self._layer._ldefn, i)),
87 self.encoding,
88 strings_only=True,
89 )
90 for i in range(self.num_fields)
91 ]
92
93 @property
94 def geom(self):
95 "Return the OGR Geometry for this Feature."
96 # Retrieving the geometry pointer for the feature.
97 geom_ptr = capi.get_feat_geom_ref(self.ptr)
98 return OGRGeometry(geom_api.clone_geom(geom_ptr))
99
100 @property
101 def geom_type(self):
102 "Return the OGR Geometry Type for this Feature."
103 return OGRGeomType(capi.get_fd_geom_type(self._layer._ldefn))
104
105 # #### Feature Methods ####
106 def get(self, field):
107 """
108 Return the value of the field, instead of an instance of the Field
109 object. May take a string of the field name or a Field object as
110 parameters.
111 """
112 field_name = getattr(field, "name", field)
113 return self[field_name].value
114
115 def index(self, field_name):
116 "Return the index of the given field name."
117 i = capi.get_field_index(self.ptr, force_bytes(field_name))
118 if i < 0:
119 raise IndexError("Invalid OFT field name given: %s." % field_name)
120 return i