Add Django-1.2.1
[frozenviper.git] / Django-1.2.1 / django / contrib / gis / utils / ogrinfo.py
blob1e4c42daa541fbc812f7698d73e717fe67ef9a79
1 """
2 This module includes some utility functions for inspecting the layout
3 of a GDAL data source -- the functionality is analogous to the output
4 produced by the `ogrinfo` utility.
5 """
7 from django.contrib.gis.gdal import DataSource
8 from django.contrib.gis.gdal.geometries import GEO_CLASSES
10 def ogrinfo(data_source, num_features=10):
11 """
12 Walks the available layers in the supplied `data_source`, displaying
13 the fields for the first `num_features` features.
14 """
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.')
24 for i, layer in enumerate(data_source):
25 print "data source : %s" % data_source.name
26 print "==== layer %s" % i
27 print " shape type: %s" % GEO_CLASSES[layer.geom_type.num].__name__
28 print " # features: %s" % len(layer)
29 print " srs: %s" % layer.srs
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
34 width = max(*map(len,layer.fields))
35 fmt = " %%%ss: %%s" % width
36 for j, feature in enumerate(layer[:num_features]):
37 print "=== Feature %s" % j
38 for fld_name in layer.fields:
39 type_name = feature[fld_name].type_name
40 output = fmt % (fld_name, type_name)
41 val = feature.get(fld_name)
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
52 # For backwards compatibility.
53 sample = ogrinfo