Code

Ticket #18640: 18640-1.diff

File 18640-1.diff, 4.5 KB (added by claudep, 2 years ago)

Add parent references to GDAL Feature/Field

Line 
1diff --git a/django/contrib/gis/gdal/feature.py b/django/contrib/gis/gdal/feature.py
2index 47fd9e5..d08e786 100644
3--- a/django/contrib/gis/gdal/feature.py
4+++ b/django/contrib/gis/gdal/feature.py
5@@ -15,12 +15,13 @@ class Feature(GDALBase):
6     "A class that wraps an OGR Feature, needs to be instantiated from a Layer object."
7 
8     #### Python 'magic' routines ####
9-    def __init__(self, feat, fdefn):
10+    def __init__(self, feat, fdefn, layer):
11         "Initializes on the pointers for the feature and the layer definition."
12         if not feat or not fdefn:
13             raise OGRException('Cannot create OGR Feature, invalid pointer given.')
14         self.ptr = feat
15         self._fdefn = fdefn
16+        self._layer = layer
17 
18     def __del__(self):
19         "Releases a reference to this object."
20@@ -39,7 +40,7 @@ class Feature(GDALBase):
21             if index < 0 or index > self.num_fields:
22                 raise OGRIndexError('index out of range')
23             i = index
24-        return Field(self.ptr, i)
25+        return Field(self, i)
26     
27     def __iter__(self):
28         "Iterates over each field in the Feature."
29diff --git a/django/contrib/gis/gdal/field.py b/django/contrib/gis/gdal/field.py
30index 12dc8b9..dc4d2b3 100644
31--- a/django/contrib/gis/gdal/field.py
32+++ b/django/contrib/gis/gdal/field.py
33@@ -22,7 +22,7 @@ class Field(GDALBase):
34         self._index = index
35 
36         # Getting the pointer for this field.
37-        fld_ptr = capi.get_feat_field_defn(feat, index)
38+        fld_ptr = capi.get_feat_field_defn(feat.ptr, index)
39         if not fld_ptr:
40             raise OGRException('Cannot create OGR Field, invalid pointer given.')
41         self.ptr = fld_ptr
42@@ -42,20 +42,20 @@ class Field(GDALBase):
43     #### Field Methods ####
44     def as_double(self):
45         "Retrieves the Field's value as a double (float)."
46-        return capi.get_field_as_double(self._feat, self._index)
47+        return capi.get_field_as_double(self._feat.ptr, self._index)
48 
49     def as_int(self):
50         "Retrieves the Field's value as an integer."
51-        return capi.get_field_as_integer(self._feat, self._index)
52+        return capi.get_field_as_integer(self._feat.ptr, self._index)
53 
54     def as_string(self):
55         "Retrieves the Field's value as a string."
56-        return capi.get_field_as_string(self._feat, self._index)
57+        return capi.get_field_as_string(self._feat.ptr, self._index)
58 
59     def as_datetime(self):
60         "Retrieves the Field's value as a tuple of date & time components."
61         yy, mm, dd, hh, mn, ss, tz = [c_int() for i in range(7)]
62-        status = capi.get_field_as_datetime(self._feat, self._index, byref(yy), byref(mm), byref(dd),
63+        status = capi.get_field_as_datetime(self._feat.ptr, self._index, byref(yy), byref(mm), byref(dd),
64                                             byref(hh), byref(mn), byref(ss), byref(tz))
65         if status:
66             return (yy, mm, dd, hh, mn, ss, tz)
67diff --git a/django/contrib/gis/gdal/layer.py b/django/contrib/gis/gdal/layer.py
68index a2163bc..b2fef51 100644
69--- a/django/contrib/gis/gdal/layer.py
70+++ b/django/contrib/gis/gdal/layer.py
71@@ -57,7 +57,7 @@ class Layer(GDALBase):
72         # ResetReading() must be called before iteration is to begin.
73         capi.reset_reading(self._ptr)
74         for i in xrange(self.num_feat):
75-            yield Feature(capi.get_next_feature(self._ptr), self._ldefn)
76+            yield Feature(capi.get_next_feature(self._ptr), self._ldefn, self)
77 
78     def __len__(self):
79         "The length is the number of features."
80@@ -77,7 +77,7 @@ class Layer(GDALBase):
81         if self._random_read:
82             # If the Layer supports random reading, return.
83             try:
84-                return Feature(capi.get_feature(self.ptr, feat_id), self._ldefn)
85+                return Feature(capi.get_feature(self.ptr, feat_id), self._ldefn, self)
86             except OGRException:
87                 pass
88         else:
89diff --git a/django/contrib/gis/gdal/tests/test_ds.py b/django/contrib/gis/gdal/tests/test_ds.py
90index 71d22a0..34f0399 100644
91--- a/django/contrib/gis/gdal/tests/test_ds.py
92+++ b/django/contrib/gis/gdal/tests/test_ds.py
93@@ -141,6 +141,8 @@ class DataSourceTest(unittest.TestCase):
94         lyr = get_layer()
95         self.assertEqual(source.nfeat, len(lyr))
96         self.assertEqual(source.gtype, lyr.geom_type.num)
97+        # Same issue for Feature/Field objects, see #18640
98+        self.assertEqual(str(lyr[0]['str']), "1")
99 
100     def test04_features(self):
101         "Testing Data Source Features."