mirror of
https://github.com/django/django.git
synced 2025-07-04 01:39:20 +00:00
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. git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@5605 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
a183dd8225
commit
a925a3afe2
@ -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
|
||||
|
@ -35,20 +35,26 @@ class Layer(object):
|
||||
|
||||
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."
|
||||
|
@ -65,10 +65,13 @@ class DataSourceTest(unittest.TestCase):
|
||||
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 @@ class DataSourceTest(unittest.TestCase):
|
||||
# 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.
|
||||
|
27
django/contrib/gis/utils/inspect_data.py
Normal file
27
django/contrib/gis/utils/inspect_data.py
Normal file
@ -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))
|
Loading…
x
Reference in New Issue
Block a user