driver.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 capi
  5. from django.utils.encoding import force_bytes, force_str
  6. class Driver(GDALBase):
  7. """
  8. Wrap a GDAL/OGR Data Source Driver.
  9. For more information, see the C API documentation:
  10. https://gdal.org/api/vector_c_api.html
  11. https://gdal.org/api/raster_c_api.html
  12. """
  13. # Case-insensitive aliases for some GDAL/OGR Drivers.
  14. # For a complete list of original driver names see
  15. # https://gdal.org/drivers/vector/
  16. # https://gdal.org/drivers/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. Initialize an GDAL/OGR driver on either a string or integer input.
  33. """
  34. if isinstance(dr_input, str):
  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. driver = c_void_p(capi.get_driver_by_name(force_bytes(name)))
  45. elif isinstance(dr_input, int):
  46. self.ensure_registered()
  47. driver = capi.get_driver(dr_input)
  48. elif isinstance(dr_input, c_void_p):
  49. driver = dr_input
  50. else:
  51. raise GDALException(
  52. "Unrecognized input type for GDAL/OGR Driver: %s" % type(dr_input)
  53. )
  54. # Making sure we get a valid pointer to the OGR Driver
  55. if not driver:
  56. raise GDALException(
  57. "Could not initialize GDAL/OGR Driver on input: %s" % dr_input
  58. )
  59. self.ptr = driver
  60. def __str__(self):
  61. return self.name
  62. @classmethod
  63. def ensure_registered(cls):
  64. """
  65. Attempt to register all the data source drivers.
  66. """
  67. # Only register all if the driver count is 0 (or else all drivers will
  68. # be registered over and over again).
  69. if not capi.get_driver_count():
  70. capi.register_all()
  71. @classmethod
  72. def driver_count(cls):
  73. """
  74. Return the number of GDAL/OGR data source drivers registered.
  75. """
  76. return capi.get_driver_count()
  77. @property
  78. def name(self):
  79. """
  80. Return description/name string for this driver.
  81. """
  82. return force_str(capi.get_driver_description(self.ptr))