PageRenderTime 32ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/gdal/datasource.py

https://code.google.com/p/mango-py/
Python | 128 lines | 115 code | 0 blank | 13 comment | 0 complexity | c739981dc53340c46959989ca87aa484 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. DataSource is a wrapper for the OGR Data Source object, which provides
  3. an interface for reading vector geometry data from many different file
  4. formats (including ESRI shapefiles).
  5. When instantiating a DataSource object, use the filename of a
  6. GDAL-supported data source. For example, a SHP file or a
  7. TIGER/Line file from the government.
  8. The ds_driver keyword is used internally when a ctypes pointer
  9. is passed in directly.
  10. Example:
  11. ds = DataSource('/home/foo/bar.shp')
  12. for layer in ds:
  13. for feature in layer:
  14. # Getting the geometry for the feature.
  15. g = feature.geom
  16. # Getting the 'description' field for the feature.
  17. desc = feature['description']
  18. # We can also increment through all of the fields
  19. # attached to this feature.
  20. for field in feature:
  21. # Get the name of the field (e.g. 'description')
  22. nm = field.name
  23. # Get the type (integer) of the field, e.g. 0 => OFTInteger
  24. t = field.type
  25. # Returns the value the field; OFTIntegers return ints,
  26. # OFTReal returns floats, all else returns string.
  27. val = field.value
  28. """
  29. # ctypes prerequisites.
  30. from ctypes import byref, c_void_p
  31. # The GDAL C library, OGR exceptions, and the Layer object.
  32. from django.contrib.gis.gdal.base import GDALBase
  33. from django.contrib.gis.gdal.driver import Driver
  34. from django.contrib.gis.gdal.error import OGRException, OGRIndexError
  35. from django.contrib.gis.gdal.layer import Layer
  36. # Getting the ctypes prototypes for the DataSource.
  37. from django.contrib.gis.gdal.prototypes import ds as capi
  38. # For more information, see the OGR C API source code:
  39. # http://www.gdal.org/ogr/ogr__api_8h.html
  40. #
  41. # The OGR_DS_* routines are relevant here.
  42. class DataSource(GDALBase):
  43. "Wraps an OGR Data Source object."
  44. #### Python 'magic' routines ####
  45. def __init__(self, ds_input, ds_driver=False, write=False):
  46. # The write flag.
  47. if write:
  48. self._write = 1
  49. else:
  50. self._write = 0
  51. # Registering all the drivers, this needs to be done
  52. # _before_ we try to open up a data source.
  53. if not capi.get_driver_count():
  54. capi.register_all()
  55. if isinstance(ds_input, basestring):
  56. # The data source driver is a void pointer.
  57. ds_driver = Driver.ptr_type()
  58. try:
  59. # OGROpen will auto-detect the data source type.
  60. ds = capi.open_ds(ds_input, self._write, byref(ds_driver))
  61. except OGRException:
  62. # Making the error message more clear rather than something
  63. # like "Invalid pointer returned from OGROpen".
  64. raise OGRException('Could not open the datasource at "%s"' % ds_input)
  65. elif isinstance(ds_input, self.ptr_type) and isinstance(ds_driver, Driver.ptr_type):
  66. ds = ds_input
  67. else:
  68. raise OGRException('Invalid data source input type: %s' % type(ds_input))
  69. if bool(ds):
  70. self.ptr = ds
  71. self.driver = Driver(ds_driver)
  72. else:
  73. # Raise an exception if the returned pointer is NULL
  74. raise OGRException('Invalid data source file "%s"' % ds_input)
  75. def __del__(self):
  76. "Destroys this DataStructure object."
  77. if self._ptr: capi.destroy_ds(self._ptr)
  78. def __iter__(self):
  79. "Allows for iteration over the layers in a data source."
  80. for i in xrange(self.layer_count):
  81. yield self[i]
  82. def __getitem__(self, index):
  83. "Allows use of the index [] operator to get a layer at the index."
  84. if isinstance(index, basestring):
  85. l = capi.get_layer_by_name(self.ptr, index)
  86. if not l: raise OGRIndexError('invalid OGR Layer name given: "%s"' % index)
  87. elif isinstance(index, int):
  88. if index < 0 or index >= self.layer_count:
  89. raise OGRIndexError('index out of range')
  90. l = capi.get_layer(self._ptr, index)
  91. else:
  92. raise TypeError('Invalid index type: %s' % type(index))
  93. return Layer(l, self)
  94. def __len__(self):
  95. "Returns the number of layers within the data source."
  96. return self.layer_count
  97. def __str__(self):
  98. "Returns OGR GetName and Driver for the Data Source."
  99. return '%s (%s)' % (self.name, str(self.driver))
  100. @property
  101. def layer_count(self):
  102. "Returns the number of layers in the data source."
  103. return capi.get_layer_count(self._ptr)
  104. @property
  105. def name(self):
  106. "Returns the name of the data source."
  107. return capi.get_ds_name(self._ptr)