Ticket #18640: 18640-1.diff

File 18640-1.diff, 4.5 KB (added by Claude Paroz, 12 years ago)

Add parent references to GDAL Feature/Field

  • django/contrib/gis/gdal/feature.py

    diff --git a/django/contrib/gis/gdal/feature.py b/django/contrib/gis/gdal/feature.py
    index 47fd9e5..d08e786 100644
    a b class Feature(GDALBase):  
    1515    "A class that wraps an OGR Feature, needs to be instantiated from a Layer object."
    1616
    1717    #### Python 'magic' routines ####
    18     def __init__(self, feat, fdefn):
     18    def __init__(self, feat, fdefn, layer):
    1919        "Initializes on the pointers for the feature and the layer definition."
    2020        if not feat or not fdefn:
    2121            raise OGRException('Cannot create OGR Feature, invalid pointer given.')
    2222        self.ptr = feat
    2323        self._fdefn = fdefn
     24        self._layer = layer
    2425
    2526    def __del__(self):
    2627        "Releases a reference to this object."
    class Feature(GDALBase):  
    3940            if index < 0 or index > self.num_fields:
    4041                raise OGRIndexError('index out of range')
    4142            i = index
    42         return Field(self.ptr, i)
     43        return Field(self, i)
    4344   
    4445    def __iter__(self):
    4546        "Iterates over each field in the Feature."
  • django/contrib/gis/gdal/field.py

    diff --git a/django/contrib/gis/gdal/field.py b/django/contrib/gis/gdal/field.py
    index 12dc8b9..dc4d2b3 100644
    a b class Field(GDALBase):  
    2222        self._index = index
    2323
    2424        # Getting the pointer for this field.
    25         fld_ptr = capi.get_feat_field_defn(feat, index)
     25        fld_ptr = capi.get_feat_field_defn(feat.ptr, index)
    2626        if not fld_ptr:
    2727            raise OGRException('Cannot create OGR Field, invalid pointer given.')
    2828        self.ptr = fld_ptr
    class Field(GDALBase):  
    4242    #### Field Methods ####
    4343    def as_double(self):
    4444        "Retrieves the Field's value as a double (float)."
    45         return capi.get_field_as_double(self._feat, self._index)
     45        return capi.get_field_as_double(self._feat.ptr, self._index)
    4646
    4747    def as_int(self):
    4848        "Retrieves the Field's value as an integer."
    49         return capi.get_field_as_integer(self._feat, self._index)
     49        return capi.get_field_as_integer(self._feat.ptr, self._index)
    5050
    5151    def as_string(self):
    5252        "Retrieves the Field's value as a string."
    53         return capi.get_field_as_string(self._feat, self._index)
     53        return capi.get_field_as_string(self._feat.ptr, self._index)
    5454
    5555    def as_datetime(self):
    5656        "Retrieves the Field's value as a tuple of date & time components."
    5757        yy, mm, dd, hh, mn, ss, tz = [c_int() for i in range(7)]
    58         status = capi.get_field_as_datetime(self._feat, self._index, byref(yy), byref(mm), byref(dd),
     58        status = capi.get_field_as_datetime(self._feat.ptr, self._index, byref(yy), byref(mm), byref(dd),
    5959                                            byref(hh), byref(mn), byref(ss), byref(tz))
    6060        if status:
    6161            return (yy, mm, dd, hh, mn, ss, tz)
  • django/contrib/gis/gdal/layer.py

    diff --git a/django/contrib/gis/gdal/layer.py b/django/contrib/gis/gdal/layer.py
    index a2163bc..b2fef51 100644
    a b class Layer(GDALBase):  
    5757        # ResetReading() must be called before iteration is to begin.
    5858        capi.reset_reading(self._ptr)
    5959        for i in xrange(self.num_feat):
    60             yield Feature(capi.get_next_feature(self._ptr), self._ldefn)
     60            yield Feature(capi.get_next_feature(self._ptr), self._ldefn, self)
    6161
    6262    def __len__(self):
    6363        "The length is the number of features."
    class Layer(GDALBase):  
    7777        if self._random_read:
    7878            # If the Layer supports random reading, return.
    7979            try:
    80                 return Feature(capi.get_feature(self.ptr, feat_id), self._ldefn)
     80                return Feature(capi.get_feature(self.ptr, feat_id), self._ldefn, self)
    8181            except OGRException:
    8282                pass
    8383        else:
  • django/contrib/gis/gdal/tests/test_ds.py

    diff --git a/django/contrib/gis/gdal/tests/test_ds.py b/django/contrib/gis/gdal/tests/test_ds.py
    index 71d22a0..34f0399 100644
    a b class DataSourceTest(unittest.TestCase):  
    141141        lyr = get_layer()
    142142        self.assertEqual(source.nfeat, len(lyr))
    143143        self.assertEqual(source.gtype, lyr.geom_type.num)
     144        # Same issue for Feature/Field objects, see #18640
     145        self.assertEqual(str(lyr[0]['str']), "1")
    144146
    145147    def test04_features(self):
    146148        "Testing Data Source Features."
Back to Top