Debian Codegraph @

django/contrib/gis/gdal/field.py

raw
1 from ctypes import byref, c_int
2 from datetime import date, datetime, time
3
4 from django.contrib.gis.gdal.base import GDALBase
5 from django.contrib.gis.gdal.error import GDALException
6 from django.contrib.gis.gdal.prototypes import ds as capi
7 from django.utils.encoding import 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_Fld_* routines are relevant here.
14 class Field(GDALBase):
15 """
16 Wrap an OGR Field. Needs to be instantiated from a Feature object.
17 """
18
19 def __init__(self, feat, index):
20 """
21 Initialize on the feature object and the integer index of
22 the field within the feature.
23 """
24 # Setting the feature pointer and index.
25 self._feat = feat
26 self._index = index
27
28 # Getting the pointer for this field.
29 fld_ptr = capi.get_feat_field_defn(feat.ptr, index)
30 if not fld_ptr:
31 raise GDALException("Cannot create OGR Field, invalid pointer given.")
32 self.ptr = fld_ptr
33
34 # Setting the class depending upon the OGR Field Type (OFT)
35 self.__class__ = OGRFieldTypes[self.type]
36
37 def __str__(self):
38 "Return the string representation of the Field."
39 return str(self.value).strip()
40
41 # #### Field Methods ####
42 def as_double(self):
43 "Retrieve the Field's value as a double (float)."
44 return (
45 capi.get_field_as_double(self._feat.ptr, self._index)
46 if self.is_set
47 else None
48 )
49
50 def as_int(self, is_64=False):
51 "Retrieve the Field's value as an integer."
52 if is_64:
53 return (
54 capi.get_field_as_integer64(self._feat.ptr, self._index)
55 if self.is_set
56 else None
57 )
58 else:
59 return (
60 capi.get_field_as_integer(self._feat.ptr, self._index)
61 if self.is_set
62 else None
63 )
64
65 def as_string(self):
66 "Retrieve the Field's value as a string."
67 if not self.is_set:
68 return None
69 string = capi.get_field_as_string(self._feat.ptr, self._index)
70 return force_str(string, encoding=self._feat.encoding, strings_only=True)
71
72 def as_datetime(self):
73 "Retrieve the Field's value as a tuple of date & time components."
74 if not self.is_set:
75 return None
76 yy, mm, dd, hh, mn, ss, tz = [c_int() for i in range(7)]
77 status = capi.get_field_as_datetime(
78 self._feat.ptr,
79 self._index,
80 byref(yy),
81 byref(mm),
82 byref(dd),
83 byref(hh),
84 byref(mn),
85 byref(ss),
86 byref(tz),
87 )
88 if status:
89 return (yy, mm, dd, hh, mn, ss, tz)
90 else:
91 raise GDALException(
92 "Unable to retrieve date & time information from the field."
93 )
94
95 # #### Field Properties ####
96 @property
97 def is_set(self):
98 "Return True if the value of this field isn't null, False otherwise."
99 return capi.is_field_set(self._feat.ptr, self._index)
100
101 @property
102 def name(self):
103 "Return the name of this Field."
104 name = capi.get_field_name(self.ptr)
105 return force_str(name, encoding=self._feat.encoding, strings_only=True)
106
107 @property
108 def precision(self):
109 "Return the precision of this Field."
110 return capi.get_field_precision(self.ptr)
111
112 @property
113 def type(self):
114 "Return the OGR type of this Field."
115 return capi.get_field_type(self.ptr)
116
117 @property
118 def type_name(self):
119 "Return the OGR field type name for this Field."
120 return capi.get_field_type_name(self.type)
121
122 @property
123 def value(self):
124 "Return the value of this Field."
125 # Default is to get the field as a string.
126 return self.as_string()
127
128 @property
129 def width(self):
130 "Return the width of this Field."
131 return capi.get_field_width(self.ptr)
132
133
134 # ### The Field sub-classes for each OGR Field type. ###
135 class OFTInteger(Field):
136 _bit64 = False
137
138 @property
139 def value(self):
140 "Return an integer contained in this field."
141 return self.as_int(self._bit64)
142
143 @property
144 def type(self):
145 """
146 GDAL uses OFTReals to represent OFTIntegers in created
147 shapefiles -- forcing the type here since the underlying field
148 type may actually be OFTReal.
149 """
150 return 0
151
152
153 class OFTReal(Field):
154 @property
155 def value(self):
156 "Return a float contained in this field."
157 return self.as_double()
158
159
160 # String & Binary fields, just subclasses
161 class OFTString(Field):
162 pass
163
164
165 class OFTWideString(Field):
166 pass
167
168
169 class OFTBinary(Field):
170 pass
171
172
173 # OFTDate, OFTTime, OFTDateTime fields.
174 class OFTDate(Field):
175 @property
176 def value(self):
177 "Return a Python `date` object for the OFTDate field."
178 try:
179 yy, mm, dd, hh, mn, ss, tz = self.as_datetime()
180 return date(yy.value, mm.value, dd.value)
181 except (TypeError, ValueError, GDALException):
182 return None
183
184
185 class OFTDateTime(Field):
186 @property
187 def value(self):
188 "Return a Python `datetime` object for this OFTDateTime field."
189 # TODO: Adapt timezone information.
190 # See https://lists.osgeo.org/pipermail/gdal-dev/2006-February/007990.html
191 # The `tz` variable has values of: 0=unknown, 1=localtime (ambiguous),
192 # 100=GMT, 104=GMT+1, 80=GMT-5, etc.
193 try:
194 yy, mm, dd, hh, mn, ss, tz = self.as_datetime()
195 return datetime(yy.value, mm.value, dd.value, hh.value, mn.value, ss.value)
196 except (TypeError, ValueError, GDALException):
197 return None
198
199
200 class OFTTime(Field):
201 @property
202 def value(self):
203 "Return a Python `time` object for this OFTTime field."
204 try:
205 yy, mm, dd, hh, mn, ss, tz = self.as_datetime()
206 return time(hh.value, mn.value, ss.value)
207 except (ValueError, GDALException):
208 return None
209
210
211 class OFTInteger64(OFTInteger):
212 _bit64 = True
213
214
215 # List fields are also just subclasses
216 class OFTIntegerList(Field):
217 pass
218
219
220 class OFTRealList(Field):
221 pass
222
223
224 class OFTStringList(Field):
225 pass
226
227
228 class OFTWideStringList(Field):
229 pass
230
231
232 class OFTInteger64List(Field):
233 pass
234
235
236 # Class mapping dictionary for OFT Types and reverse mapping.
237 OGRFieldTypes = {
238 0: OFTInteger,
239 1: OFTIntegerList,
240 2: OFTReal,
241 3: OFTRealList,
242 4: OFTString,
243 5: OFTStringList,
244 6: OFTWideString,
245 7: OFTWideStringList,
246 8: OFTBinary,
247 9: OFTDate,
248 10: OFTTime,
249 11: OFTDateTime,
250 12: OFTInteger64,
251 13: OFTInteger64List,
252 }
253 ROGRFieldTypes = {cls: num for num, cls in OGRFieldTypes.items()}