You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ogrinfo.py 1.9KB

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