Debian Codegraph @

django/contrib/gis/utils/ogrinfo.py

raw
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 """
6
7 from django.contrib.gis.gdal import DataSource
8 from django.contrib.gis.gdal.geometries import GEO_CLASSES
9
10
11 def ogrinfo(data_source, num_features=10):
12 """
13 Walk the available layers in the supplied `data_source`, displaying
14 the fields for the first `num_features` features.
15 """
16
17 # Checking the parameters.
18 if isinstance(data_source, str):
19 data_source = DataSource(data_source)
20 elif isinstance(data_source, DataSource):
21 pass
22 else:
23 raise Exception(
24 "Data source parameter must be a string or a DataSource object."
25 )
26
27 for i, layer in enumerate(data_source):
28 print("data source : %s" % data_source.name)
29 print("==== layer %s" % i)
30 print(" shape type: %s" % GEO_CLASSES[layer.geom_type.num].__name__)
31 print(" # features: %s" % len(layer))
32 print(" srs: %s" % layer.srs)
33 extent_tup = layer.extent.tuple
34 print(" extent: %s - %s" % (extent_tup[0:2], extent_tup[2:4]))
35 print("Displaying the first %s features ====" % num_features)
36
37 width = max(*map(len, layer.fields))
38 fmt = " %%%ss: %%s" % width
39 for j, feature in enumerate(layer[:num_features]):
40 print("=== Feature %s" % j)
41 for fld_name in layer.fields:
42 type_name = feature[fld_name].type_name
43 output = fmt % (fld_name, type_name)
44 val = feature.get(fld_name)
45 if val:
46 if isinstance(val, str):
47 val_fmt = ' ("%s")'
48 else:
49 val_fmt = " (%s)"
50 output += val_fmt % val
51 else:
52 output += " (None)"
53 print(output)