2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
This module includes some utility functions for inspecting the layout
|
|
|
|
of a GDAL data source -- the functionality is analogous to the output
|
|
|
|
produced by the `ogrinfo` utility.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.contrib.gis.gdal import DataSource
|
|
|
|
from django.contrib.gis.gdal.geometries import GEO_CLASSES
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def ogrinfo(data_source, num_features=10):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Walk the available layers in the supplied `data_source`, displaying
|
2008-08-06 02:13:06 +08:00
|
|
|
the fields for the first `num_features` features.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Checking the parameters.
|
|
|
|
if isinstance(data_source, str):
|
|
|
|
data_source = DataSource(data_source)
|
|
|
|
elif isinstance(data_source, DataSource):
|
|
|
|
pass
|
|
|
|
else:
|
2010-01-11 02:36:20 +08:00
|
|
|
raise Exception('Data source parameter must be a string or a DataSource object.')
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
for i, layer in enumerate(data_source):
|
2012-04-29 00:02:01 +08:00
|
|
|
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)
|
2008-08-06 02:13:06 +08:00
|
|
|
extent_tup = layer.extent.tuple
|
2012-04-29 00:02:01 +08:00
|
|
|
print(" extent: %s - %s" % (extent_tup[0:2], extent_tup[2:4]))
|
|
|
|
print("Displaying the first %s features ====" % num_features)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-10-27 01:50:40 +08:00
|
|
|
width = max(*map(len, layer.fields))
|
2008-08-06 02:13:06 +08:00
|
|
|
fmt = " %%%ss: %%s" % width
|
|
|
|
for j, feature in enumerate(layer[:num_features]):
|
2012-04-29 00:02:01 +08:00
|
|
|
print("=== Feature %s" % j)
|
2008-08-06 02:13:06 +08:00
|
|
|
for fld_name in layer.fields:
|
|
|
|
type_name = feature[fld_name].type_name
|
|
|
|
output = fmt % (fld_name, type_name)
|
|
|
|
val = feature.get(fld_name)
|
|
|
|
if val:
|
|
|
|
if isinstance(val, str):
|
|
|
|
val_fmt = ' ("%s")'
|
|
|
|
else:
|
|
|
|
val_fmt = ' (%s)'
|
|
|
|
output += val_fmt % val
|
|
|
|
else:
|
|
|
|
output += ' (None)'
|
2012-04-29 00:02:01 +08:00
|
|
|
print(output)
|