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.
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):
12 Walks the available layers in the supplied `data_source`, displaying
13 the fields for the first `num_features` features.
16 # Checking the parameters.
17 if isinstance(data_source
, str):
18 data_source
= DataSource(data_source
)
19 elif isinstance(data_source
, DataSource
):
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
)
43 if isinstance(val
, str):
47 output
+= val_fmt
% val
52 # For backwards compatibility.