Wrap an OGR Field. Needs to be instantiated from a Feature object.
| 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 |