1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

gis: renamed inspect_data and sample() to ogrinfo, and now displays the extent and field type information.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6423 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2007-09-25 06:14:01 +00:00
parent d0a4c6634f
commit e835868621
3 changed files with 56 additions and 30 deletions

View File

@ -2,11 +2,11 @@
This module contains useful utilities for GeoDjango.
"""
from django.contrib.gis.utils.inspect_data import sample
# Importing LayerMapping (will not be done if GDAL is not installed)
# Importing LayerMapping and ogrinfo (will not be done if GDAL is not
# installed)
from django.contrib.gis.gdal import HAS_GDAL
if HAS_GDAL:
from django.contrib.gis.utils.ogrinfo import ogrinfo, sample
from django.contrib.gis.utils.layermapping import LayerMapping
# Importing GeoIP

View File

@ -1,27 +0,0 @@
"""
This module includes some utility functions for inspecting the layout
of a gdal.DataSource.
"""
from django.contrib.gis.gdal.geometries 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))

View File

@ -0,0 +1,53 @@
"""
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
def ogrinfo(data_source, num_features=10):
"""
Walks the available layers in the supplied `data_source`, displaying
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:
raise Exception, 'Data source parameter must be a string or a DataSource object.'
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
extent_tup = layer.extent.tuple
print " extent: %s - %s" % (extent_tup[0:2], extent_tup[2:4])
print "Displaying the 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:
fld_typ = feature[field].__class__.__name__.replace('OFT', '')
output = fmt % (field, fld_typ)
val = feature.get(field)
if val:
if isinstance(val, str):
val_fmt = ' ("%s")'
else:
val_fmt = ' (%s)'
output += val_fmt % val
else:
output += ' (None)'
print output
# For backwards compatibility.
sample = ogrinfo