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.

driver.py 3.2 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from ctypes import c_void_p
  2. from django.contrib.gis.gdal.base import GDALBase
  3. from django.contrib.gis.gdal.error import GDALException
  4. from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi
  5. from django.utils import six
  6. from django.utils.encoding import force_bytes, force_text
  7. class Driver(GDALBase):
  8. """
  9. Wraps a GDAL/OGR Data Source Driver.
  10. For more information, see the C API source code:
  11. http://www.gdal.org/gdal_8h.html - http://www.gdal.org/ogr__api_8h.html
  12. """
  13. # Case-insensitive aliases for some GDAL/OGR Drivers.
  14. # For a complete list of original driver names see
  15. # http://www.gdal.org/ogr_formats.html (vector)
  16. # http://www.gdal.org/formats_list.html (raster)
  17. _alias = {
  18. # vector
  19. 'esri': 'ESRI Shapefile',
  20. 'shp': 'ESRI Shapefile',
  21. 'shape': 'ESRI Shapefile',
  22. 'tiger': 'TIGER',
  23. 'tiger/line': 'TIGER',
  24. # raster
  25. 'tiff': 'GTiff',
  26. 'tif': 'GTiff',
  27. 'jpeg': 'JPEG',
  28. 'jpg': 'JPEG',
  29. }
  30. def __init__(self, dr_input):
  31. """
  32. Initializes an GDAL/OGR driver on either a string or integer input.
  33. """
  34. if isinstance(dr_input, six.string_types):
  35. # If a string name of the driver was passed in
  36. self.ensure_registered()
  37. # Checking the alias dictionary (case-insensitive) to see if an
  38. # alias exists for the given driver.
  39. if dr_input.lower() in self._alias:
  40. name = self._alias[dr_input.lower()]
  41. else:
  42. name = dr_input
  43. # Attempting to get the GDAL/OGR driver by the string name.
  44. for iface in (vcapi, rcapi):
  45. driver = iface.get_driver_by_name(force_bytes(name))
  46. if driver:
  47. break
  48. elif isinstance(dr_input, int):
  49. self.ensure_registered()
  50. for iface in (vcapi, rcapi):
  51. driver = iface.get_driver(dr_input)
  52. if driver:
  53. break
  54. elif isinstance(dr_input, c_void_p):
  55. driver = dr_input
  56. else:
  57. raise GDALException('Unrecognized input type for GDAL/OGR Driver: %s' % str(type(dr_input)))
  58. # Making sure we get a valid pointer to the OGR Driver
  59. if not driver:
  60. raise GDALException('Could not initialize GDAL/OGR Driver on input: %s' % str(dr_input))
  61. self.ptr = driver
  62. def __str__(self):
  63. return self.name
  64. @classmethod
  65. def ensure_registered(cls):
  66. """
  67. Attempts to register all the data source drivers.
  68. """
  69. # Only register all if the driver count is 0 (or else all drivers
  70. # will be registered over and over again)
  71. if not cls.driver_count():
  72. vcapi.register_all()
  73. rcapi.register_all()
  74. @classmethod
  75. def driver_count(cls):
  76. """
  77. Returns the number of GDAL/OGR data source drivers registered.
  78. """
  79. return vcapi.get_driver_count() + rcapi.get_driver_count()
  80. @property
  81. def name(self):
  82. """
  83. Returns description/name string for this driver.
  84. """
  85. return force_text(rcapi.get_driver_description(self.ptr))