Author: jdunck
Date: 2007-07-04 01:55:32 -0500 (Wed, 04 Jul 2007)
New Revision: 5605

Added:
   django/branches/gis/django/contrib/gis/utils/inspect_data.py
Modified:
   django/branches/gis/django/contrib/gis/gdal/Field.py
   django/branches/gis/django/contrib/gis/gdal/Layer.py
   django/branches/gis/django/contrib/gis/tests/test_gdal_ds.py
Log:
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.

Modified: django/branches/gis/django/contrib/gis/gdal/Field.py
===================================================================
--- django/branches/gis/django/contrib/gis/gdal/Field.py        2007-07-04 
06:52:40 UTC (rev 5604)
+++ django/branches/gis/django/contrib/gis/gdal/Field.py        2007-07-04 
06:55:32 UTC (rev 5605)
@@ -48,15 +48,25 @@
     @property
     def value(self):
         "Returns an integer contained in this field."
-        return int(self._val)
+        try:
+            return int(self._val)
+        except ValueError:
+            return 0
     
 class OFTIntegerList(Field): pass
 class OFTReal(Field):
     @property
     def value(self):
         "Returns a float contained in this field."
-        return float(self._val)
 
+        try:
+            return float(self._val)
+        except ValueError:
+            #FIXME: 0?  None?
+            return 0
+
+        
+
 class OFTRealList(Field): pass
 class OFTString(Field): pass
 class OFTStringList(Field): pass

Modified: django/branches/gis/django/contrib/gis/gdal/Layer.py
===================================================================
--- django/branches/gis/django/contrib/gis/gdal/Layer.py        2007-07-04 
06:52:40 UTC (rev 5604)
+++ django/branches/gis/django/contrib/gis/gdal/Layer.py        2007-07-04 
06:55:32 UTC (rev 5605)
@@ -35,21 +35,27 @@
 
     def __getitem__(self, index):
         "Gets the Feature at the specified index."
-        if index < 0 or index >= self.num_feat:
-            raise IndexError, 'index out of range'
-        return Feature(lgdal.OGR_L_GetFeature(self._layer, c_long(index)))
+        def make_feature(offset):
+            return Feature(lgdal.OGR_L_GetFeature(self._layer,
+                                                  c_long(offset)))
+        end = self.num_feat
+        if not isinstance(index, (slice, int)):
+            raise TypeError
+        if isinstance(index,int):
+            if index < 0:
+                index = end - index
+            if index < 0 or index >= self.num_feat:
+                raise IndexError, 'index out of range'
+            yield make_feature(index)
+        else: #isinstance(index,slice)
+            start, stop, stride = index.indices(end)
+            for offset in xrange(start,stop,stride):
+                yield make_feature(offset)
 
     def __iter__(self):
         "Iterates over each Feature in the Layer."
+        return self.__getitem__(slice(self.num_feat))
 
-        # Resetting the Layer before beginning iteration
-        lgdal.OGR_L_ResetReading(self._layer)
-
-        # Incrementing over each feature in the layer, and yielding
-        #  to the caller of the function.
-        for i in xrange(self.num_feat):
-            yield self.__getitem__(i)
-
     def __len__(self):
         "The length is the number of features."
         return self.num_feat

Modified: django/branches/gis/django/contrib/gis/tests/test_gdal_ds.py
===================================================================
--- django/branches/gis/django/contrib/gis/tests/test_gdal_ds.py        
2007-07-04 06:52:40 UTC (rev 5604)
+++ django/branches/gis/django/contrib/gis/tests/test_gdal_ds.py        
2007-07-04 06:55:32 UTC (rev 5605)
@@ -65,10 +65,13 @@
             ds = DataSource(source.ds)
 
             # Incrementing through each layer, this tests __iter__
-            for layer in ds:
+            for layer in ds:                
                 # Making sure we get the number of features we expect
                 self.assertEqual(len(layer), source.nfeat)
 
+                layer[0] #can index
+                layer[:1] #can slice
+
                 # Making sure we get the number of fields we expect
                 self.assertEqual(source.nfld, layer.num_fields)
                 self.assertEqual(source.nfld, len(layer.fields))
@@ -94,7 +97,7 @@
                 # Incrementing through each feature in the layer
                 for feat in layer:
                     # Making sure the number of fields is what's expected.
-                    self.assertEqual(source.nfld, len(feat))
+                    self.assertEqual(source.nfld, len(list(feat)))
                     self.assertEqual(source.gtype, feat.geom_type)
 
                     # Making sure the fields match to an appropriate OFT type.

Added: django/branches/gis/django/contrib/gis/utils/inspect_data.py
===================================================================
--- django/branches/gis/django/contrib/gis/utils/inspect_data.py                
                (rev 0)
+++ django/branches/gis/django/contrib/gis/utils/inspect_data.py        
2007-07-04 06:55:32 UTC (rev 5605)
@@ -0,0 +1,27 @@
+"""
+This module includes some utility functions for inspecting the layout
+of a gdal.DataSource.
+"""
+
+from django.contrib.gis.gdal.OGRGeometry import GEO_CLASSES
+
+def sample(data_source, num_features=10, gcs_file=None):
+    """
+    Walks the available layers in the supplied ``data_source``, displaying
+    the fields for the first ``num_features`` features.
+    """
+
+    for i, layer in enumerate(data_source):
+        print "data source : %s" % data_source.name
+        print "==== layer %s" % i
+        print "  shape type: %s" % GEO_CLASSES[layer.geom_type.num].__name__
+        print "  # features: %s" % len(layer)
+        print "         srs: %s" % layer.srs
+        print "Showing first %s features ========" % num_features
+
+        width = max(*map(len,layer.fields))
+        fmt = " %%%ss:%%s" % width
+        for i, feature in enumerate(layer[:num_features]):
+            print "======== Feature %s" % i
+            for field in layer.fields:
+                print fmt % (field, feature.get(field))


Property changes on: 
django/branches/gis/django/contrib/gis/utils/inspect_data.py
___________________________________________________________________
Name: svn:eol-style
   + native


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to