driver.py 3.3 KB

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