Ticket #18640: 18640-2.diff

File 18640-2.diff, 7.4 KB (added by jbronn, 12 years ago)
  • django/contrib/gis/gdal/feature.py

    diff --git a/django/contrib/gis/gdal/feature.py b/django/contrib/gis/gdal/feature.py
    index 2920048..db40357 100644
    a b from django.utils.six.moves import xrange  
    1515#
    1616# The OGR_F_* routines are relevant here.
    1717class Feature(GDALBase):
    18     "A class that wraps an OGR Feature, needs to be instantiated from a Layer object."
     18    """
     19    This class that wraps an OGR Feature, needs to be instantiated
     20    from a Layer object.
     21    """
    1922
    2023    #### Python 'magic' routines ####
    21     def __init__(self, feat, fdefn):
    22         "Initializes on the pointers for the feature and the layer definition."
    23         if not feat or not fdefn:
     24    def __init__(self, feat, layer):
     25        """
     26        Initializes on the feature pointers for the feature and the layer
     27        definition, as well as the Layer
     28        """
     29        if not feat or not layer:
    2430            raise OGRException('Cannot create OGR Feature, invalid pointer given.')
    2531        self.ptr = feat
    26         self._fdefn = fdefn
     32        self._layer = layer
    2733
    2834    def __del__(self):
    2935        "Releases a reference to this object."
    class Feature(GDALBase):  
    4248            if index < 0 or index > self.num_fields:
    4349                raise OGRIndexError('index out of range')
    4450            i = index
    45         return Field(self.ptr, i)
     51        return Field(self, i)
    4652
    4753    def __iter__(self):
    4854        "Iterates over each field in the Feature."
    class Feature(GDALBase):  
    7076    @property
    7177    def layer_name(self):
    7278        "Returns the name of the layer for the feature."
    73         return capi.get_feat_name(self._fdefn)
     79        return capi.get_feat_name(self._layer._ldefn)
    7480
    7581    @property
    7682    def num_fields(self):
    class Feature(GDALBase):  
    8086    @property
    8187    def fields(self):
    8288        "Returns a list of fields in the Feature."
    83         return [capi.get_field_name(capi.get_field_defn(self._fdefn, i))
     89        return [capi.get_field_name(capi.get_field_defn(self._layer._ldefn, i))
    8490                for i in xrange(self.num_fields)]
    8591
    8692    @property
    class Feature(GDALBase):  
    9399    @property
    94100    def geom_type(self):
    95101        "Returns the OGR Geometry Type for this Feture."
    96         return OGRGeomType(capi.get_fd_geom_type(self._fdefn))
     102        return OGRGeomType(capi.get_fd_geom_type(self._layer._ldefn))
    97103
    98104    #### Feature Methods ####
    99105    def get(self, field):
  • django/contrib/gis/gdal/field.py

    diff --git a/django/contrib/gis/gdal/field.py b/django/contrib/gis/gdal/field.py
    index 12dc8b9..16230af 100644
    a b from django.contrib.gis.gdal.prototypes import ds as capi  
    99#
    1010# The OGR_Fld_* routines are relevant here.
    1111class Field(GDALBase):
    12     "A class that wraps an OGR Field, needs to be instantiated from a Feature object."
     12    """
     13    This class wraps an OGR Field, and needs to be instantiated
     14    from a Feature object.
     15    """
    1316
    1417    #### Python 'magic' routines ####
    1518    def __init__(self, feat, index):
    1619        """
    17         Initializes on the feature pointer and the integer index of
     20        Initializes on the feature object and the integer index of
    1821        the field within the feature.
    1922        """
    2023        # Setting the feature pointer and index.
    class Field(GDALBase):  
    2225        self._index = index
    2326
    2427        # Getting the pointer for this field.
    25         fld_ptr = capi.get_feat_field_defn(feat, index)
     28        fld_ptr = capi.get_feat_field_defn(feat.ptr, index)
    2629        if not fld_ptr:
    2730            raise OGRException('Cannot create OGR Field, invalid pointer given.')
    2831        self.ptr = fld_ptr
    class Field(GDALBase):  
    4245    #### Field Methods ####
    4346    def as_double(self):
    4447        "Retrieves the Field's value as a double (float)."
    45         return capi.get_field_as_double(self._feat, self._index)
     48        return capi.get_field_as_double(self._feat.ptr, self._index)
    4649
    4750    def as_int(self):
    4851        "Retrieves the Field's value as an integer."
    49         return capi.get_field_as_integer(self._feat, self._index)
     52        return capi.get_field_as_integer(self._feat.ptr, self._index)
    5053
    5154    def as_string(self):
    5255        "Retrieves the Field's value as a string."
    53         return capi.get_field_as_string(self._feat, self._index)
     56        return capi.get_field_as_string(self._feat.ptr, self._index)
    5457
    5558    def as_datetime(self):
    5659        "Retrieves the Field's value as a tuple of date & time components."
    5760        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),
    59                                             byref(hh), byref(mn), byref(ss), byref(tz))
     61        status = capi.get_field_as_datetime(
     62            self._feat.ptr, self._index, byref(yy), byref(mm), byref(dd),
     63            byref(hh), byref(mn), byref(ss), byref(tz))
    6064        if status:
    6165            return (yy, mm, dd, hh, mn, ss, tz)
    6266        else:
  • django/contrib/gis/gdal/layer.py

    diff --git a/django/contrib/gis/gdal/layer.py b/django/contrib/gis/gdal/layer.py
    index 2357fbb..8c53831 100644
    a b from django.utils.six.moves import xrange  
    2222#
    2323# The OGR_L_* routines are relevant here.
    2424class Layer(GDALBase):
    25     "A class that wraps an OGR Layer, needs to be instantiated from a DataSource object."
     25    """
     26    This class wraps an OGR Layer, and needs to be instantiated
     27    from a DataSource object.
     28    """
    2629
    2730    #### Python 'magic' routines ####
    2831    def __init__(self, layer_ptr, ds):
    class Layer(GDALBase):  
    6063        # ResetReading() must be called before iteration is to begin.
    6164        capi.reset_reading(self._ptr)
    6265        for i in xrange(self.num_feat):
    63             yield Feature(capi.get_next_feature(self._ptr), self._ldefn)
     66            yield Feature(capi.get_next_feature(self._ptr), self)
    6467
    6568    def __len__(self):
    6669        "The length is the number of features."
    class Layer(GDALBase):  
    8083        if self._random_read:
    8184            # If the Layer supports random reading, return.
    8285            try:
    83                 return Feature(capi.get_feature(self.ptr, feat_id), self._ldefn)
     86                return Feature(capi.get_feature(self.ptr, feat_id), self)
    8487            except OGRException:
    8588                pass
    8689        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..4d7d075 100644
    a b class DataSourceTest(unittest.TestCase):  
    126126            self.assertEqual(control_vals, test_vals)
    127127
    128128    def test03c_layer_references(self):
    129         "Test to make sure Layer access is still available without the DataSource."
     129        """
     130        Test to make sure Layer/Feature access is still available without
     131        the DataSource/Feature.
     132        """
    130133        source = ds_list[0]
    131134
    132135        # See ticket #9448.
    class DataSourceTest(unittest.TestCase):  
    142145        self.assertEqual(source.nfeat, len(lyr))
    143146        self.assertEqual(source.gtype, lyr.geom_type.num)
    144147
     148        # Same issue for Feature/Field objects, see #18640
     149        self.assertEqual(str(lyr[0]['str']), "1")
     150
    145151    def test04_features(self):
    146152        "Testing Data Source Features."
    147153        for source in ds_list:
Back to Top