mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +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
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
"Returns an integer contained in this field."
|
"Returns an integer contained in this field."
|
||||||
|
try:
|
||||||
return int(self._val)
|
return int(self._val)
|
||||||
|
except ValueError:
|
||||||
|
return 0
|
||||||
|
|
||||||
class OFTIntegerList(Field): pass
|
class OFTIntegerList(Field): pass
|
||||||
class OFTReal(Field):
|
class OFTReal(Field):
|
||||||
@property
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
"Returns a float contained in this field."
|
"Returns a float contained in this field."
|
||||||
|
|
||||||
|
try:
|
||||||
return float(self._val)
|
return float(self._val)
|
||||||
|
except ValueError:
|
||||||
|
#FIXME: 0? None?
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class OFTRealList(Field): pass
|
class OFTRealList(Field): pass
|
||||||
class OFTString(Field): pass
|
class OFTString(Field): pass
|
||||||
|
@ -35,20 +35,26 @@ class Layer(object):
|
|||||||
|
|
||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
"Gets the Feature at the specified index."
|
"Gets the Feature at the specified 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:
|
if index < 0 or index >= self.num_feat:
|
||||||
raise IndexError, 'index out of range'
|
raise IndexError, 'index out of range'
|
||||||
return Feature(lgdal.OGR_L_GetFeature(self._layer, c_long(index)))
|
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):
|
def __iter__(self):
|
||||||
"Iterates over each Feature in the Layer."
|
"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):
|
def __len__(self):
|
||||||
"The length is the number of features."
|
"The length is the number of features."
|
||||||
|
@ -69,6 +69,9 @@ class DataSourceTest(unittest.TestCase):
|
|||||||
# Making sure we get the number of features we expect
|
# Making sure we get the number of features we expect
|
||||||
self.assertEqual(len(layer), source.nfeat)
|
self.assertEqual(len(layer), source.nfeat)
|
||||||
|
|
||||||
|
layer[0] #can index
|
||||||
|
layer[:1] #can slice
|
||||||
|
|
||||||
# Making sure we get the number of fields we expect
|
# Making sure we get the number of fields we expect
|
||||||
self.assertEqual(source.nfld, layer.num_fields)
|
self.assertEqual(source.nfld, layer.num_fields)
|
||||||
self.assertEqual(source.nfld, len(layer.fields))
|
self.assertEqual(source.nfld, len(layer.fields))
|
||||||
@ -94,7 +97,7 @@ class DataSourceTest(unittest.TestCase):
|
|||||||
# Incrementing through each feature in the layer
|
# Incrementing through each feature in the layer
|
||||||
for feat in layer:
|
for feat in layer:
|
||||||
# Making sure the number of fields is what's expected.
|
# 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)
|
self.assertEqual(source.gtype, feat.geom_type)
|
||||||
|
|
||||||
# Making sure the fields match to an appropriate OFT 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