Changeset 6423
- Timestamp:
- 09/25/07 01:14:01 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/utils/__init__.py
r6414 r6423 3 3 """ 4 4 5 from django.contrib.gis.utils.inspect_data import sample 6 7 # Importing LayerMapping (will not be done if GDAL is not installed) 5 # Importing LayerMapping and ogrinfo (will not be done if GDAL is not 6 # installed) 8 7 from django.contrib.gis.gdal import HAS_GDAL 9 8 if HAS_GDAL: 9 from django.contrib.gis.utils.ogrinfo import ogrinfo, sample 10 10 from django.contrib.gis.utils.layermapping import LayerMapping 11 11 django/branches/gis/django/contrib/gis/utils/ogrinfo.py
r5755 r6423 1 1 """ 2 2 This module includes some utility functions for inspecting the layout 3 of a gdal.DataSource. 3 of a GDAL data source -- the functionality is analogous to the output 4 produced by the `ogrinfo` utility. 4 5 """ 5 6 7 from django.contrib.gis.gdal import DataSource 6 8 from django.contrib.gis.gdal.geometries import GEO_CLASSES 7 9 8 def sample(data_source, num_features=10, gcs_file=None):10 def ogrinfo(data_source, num_features=10): 9 11 """ 10 Walks the available layers in the supplied ` `data_source``, displaying11 the fields for the first ` `num_features`` features.12 Walks the available layers in the supplied `data_source`, displaying 13 the fields for the first `num_features` features. 12 14 """ 15 16 # Checking the parameters. 17 if isinstance(data_source, str): 18 data_source = DataSource(data_source) 19 elif isinstance(data_source, DataSource): 20 pass 21 else: 22 raise Exception, 'Data source parameter must be a string or a DataSource object.' 13 23 14 24 for i, layer in enumerate(data_source): … … 18 28 print " # features: %s" % len(layer) 19 29 print " srs: %s" % layer.srs 20 print "Showing first %s features ========" % num_features 30 extent_tup = layer.extent.tuple 31 print " extent: %s - %s" % (extent_tup[0:2], extent_tup[2:4]) 32 print "Displaying the first %s features ====" % num_features 21 33 22 34 width = max(*map(len,layer.fields)) 23 fmt = " %%%ss: %%s" % width35 fmt = " %%%ss: %%s" % width 24 36 for i, feature in enumerate(layer[:num_features]): 25 print "=== =====Feature %s" % i37 print "=== Feature %s" % i 26 38 for field in layer.fields: 27 print fmt % (field, feature.get(field)) 39 fld_typ = feature[field].__class__.__name__.replace('OFT', '') 40 output = fmt % (field, fld_typ) 41 val = feature.get(field) 42 if val: 43 if isinstance(val, str): 44 val_fmt = ' ("%s")' 45 else: 46 val_fmt = ' (%s)' 47 output += val_fmt % val 48 else: 49 output += ' (None)' 50 print output 51 52 # For backwards compatibility. 53 sample = ogrinfo
