From e8358686215c34427d7ae0962fc5e1c70b413e82 Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Tue, 25 Sep 2007 06:14:01 +0000 Subject: [PATCH] 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 --- django/contrib/gis/utils/__init__.py | 6 +-- django/contrib/gis/utils/inspect_data.py | 27 ------------ django/contrib/gis/utils/ogrinfo.py | 53 ++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 30 deletions(-) delete mode 100644 django/contrib/gis/utils/inspect_data.py create mode 100644 django/contrib/gis/utils/ogrinfo.py diff --git a/django/contrib/gis/utils/__init__.py b/django/contrib/gis/utils/__init__.py index 65a9987363..67ca8c5d3c 100644 --- a/django/contrib/gis/utils/__init__.py +++ b/django/contrib/gis/utils/__init__.py @@ -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 diff --git a/django/contrib/gis/utils/inspect_data.py b/django/contrib/gis/utils/inspect_data.py deleted file mode 100644 index 11303d9306..0000000000 --- a/django/contrib/gis/utils/inspect_data.py +++ /dev/null @@ -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)) diff --git a/django/contrib/gis/utils/ogrinfo.py b/django/contrib/gis/utils/ogrinfo.py new file mode 100644 index 0000000000..fa08f7e729 --- /dev/null +++ b/django/contrib/gis/utils/ogrinfo.py @@ -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