Django

Code

Changeset 5605

Show
Ignore:
Timestamp:
07/04/07 01:55:32 (1 year ago)
Author:
jdunck
Message:

gis: Added utils.inspect_data.sample, which allows a shows a convenient bit of the supplied data_source
Added support for feature slicing and negative indices.
Handled missing Feature field values.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/gdal/Field.py

    r5586 r5605  
    4949    def value(self): 
    5050        "Returns an integer contained in this field." 
    51         return int(self._val) 
     51        try: 
     52            return int(self._val) 
     53        except ValueError: 
     54            return 0 
    5255     
    5356class OFTIntegerList(Field): pass 
     
    5659    def value(self): 
    5760        "Returns a float contained in this field." 
    58         return float(self._val) 
     61 
     62        try: 
     63            return float(self._val) 
     64        except ValueError: 
     65            #FIXME: 0?  None? 
     66            return 0 
     67 
     68         
    5969 
    6070class OFTRealList(Field): pass 
  • django/branches/gis/django/contrib/gis/gdal/Layer.py

    r5587 r5605  
    3636    def __getitem__(self, index): 
    3737        "Gets the Feature at the specified index." 
    38         if index < 0 or index >= self.num_feat: 
    39             raise IndexError, 'index out of range' 
    40         return Feature(lgdal.OGR_L_GetFeature(self._layer, c_long(index))) 
     38        def make_feature(offset): 
     39            return Feature(lgdal.OGR_L_GetFeature(self._layer, 
     40                                                  c_long(offset))) 
     41        end = self.num_feat 
     42        if not isinstance(index, (slice, int)): 
     43            raise TypeError 
     44        if isinstance(index,int): 
     45            if index < 0: 
     46                index = end - index 
     47            if index < 0 or index >= self.num_feat: 
     48                raise IndexError, 'index out of range' 
     49            yield make_feature(index) 
     50        else: #isinstance(index,slice) 
     51            start, stop, stride = index.indices(end) 
     52            for offset in xrange(start,stop,stride): 
     53                yield make_feature(offset) 
    4154 
    4255    def __iter__(self): 
    4356        "Iterates over each Feature in the Layer." 
    44  
    45         # Resetting the Layer before beginning iteration 
    46         lgdal.OGR_L_ResetReading(self._layer) 
    47  
    48         # Incrementing over each feature in the layer, and yielding 
    49         #  to the caller of the function. 
    50         for i in xrange(self.num_feat): 
    51             yield self.__getitem__(i) 
     57        return self.__getitem__(slice(self.num_feat)) 
    5258 
    5359    def __len__(self): 
  • django/branches/gis/django/contrib/gis/tests/test_gdal_ds.py

    r5587 r5605  
    6666 
    6767            # Incrementing through each layer, this tests __iter__ 
    68             for layer in ds: 
     68            for layer in ds:                 
    6969                # Making sure we get the number of features we expect 
    7070                self.assertEqual(len(layer), source.nfeat) 
     71 
     72                layer[0] #can index 
     73                layer[:1] #can slice 
    7174 
    7275                # Making sure we get the number of fields we expect 
     
    9598                for feat in layer: 
    9699                    # Making sure the number of fields is what's expected. 
    97                     self.assertEqual(source.nfld, len(feat)) 
     100                    self.assertEqual(source.nfld, len(list(feat))) 
    98101                    self.assertEqual(source.gtype, feat.geom_type) 
    99102