Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

gis: Added utils.inspect_data.sample, which allows a shows a convenie…

…nt bit of the supplied data_source

Added support for feature slicing and negative indices.
Handled missing Feature field values.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@5605 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a925a3afe29922c0817fb9e4ae9bef7dc24b1cac 1 parent a183dd8
@jdunck jdunck authored
View
14 django/contrib/gis/gdal/Field.py
@@ -48,14 +48,24 @@ class OFTInteger(Field):
@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
View
28 django/contrib/gis/gdal/Layer.py
@@ -35,20 +35,26 @@ def __init__(self, l):
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."
-
- # 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)
+ return self.__getitem__(slice(self.num_feat))
def __len__(self):
"The length is the number of features."
View
7 django/contrib/gis/tests/test_gdal_ds.py
@@ -65,10 +65,13 @@ def test03_layers(self):
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 @@ def test04_features(self):
# 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.
View
27 django/contrib/gis/utils/inspect_data.py
@@ -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))
Please sign in to comment.
Something went wrong with that request. Please try again.