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.2KB

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