PageRenderTime 30ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/utils/ogrinfo.py

https://code.google.com/p/mango-py/
Python | 53 lines | 36 code | 6 blank | 11 comment | 10 complexity | 11fb47c5fa8d221bb55556e2bc99113e MD5 | raw file
Possible License(s): BSD-3-Clause
  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. Walks 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
  46. # For backwards compatibility.
  47. sample = ogrinfo