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.

libgdal.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import logging
  2. import os
  3. import re
  4. from ctypes import CDLL, CFUNCTYPE, c_char_p, c_int
  5. from ctypes.util import find_library
  6. from django.contrib.gis.gdal.error import GDALException
  7. from django.core.exceptions import ImproperlyConfigured
  8. logger = logging.getLogger('django.contrib.gis')
  9. # Custom library path set?
  10. try:
  11. from django.conf import settings
  12. lib_path = settings.GDAL_LIBRARY_PATH
  13. except (AttributeError, EnvironmentError,
  14. ImportError, ImproperlyConfigured):
  15. lib_path = None
  16. if lib_path:
  17. lib_names = None
  18. elif os.name == 'nt':
  19. # Windows NT shared libraries
  20. lib_names = ['gdal203', 'gdal202', 'gdal201', 'gdal20', 'gdal111']
  21. elif os.name == 'posix':
  22. # *NIX library names.
  23. lib_names = ['gdal', 'GDAL', 'gdal2.3.0', 'gdal2.2.0', 'gdal2.1.0', 'gdal2.0.0', 'gdal1.11.0']
  24. else:
  25. raise ImproperlyConfigured('GDAL is unsupported on OS "%s".' % os.name)
  26. # Using the ctypes `find_library` utility to find the
  27. # path to the GDAL library from the list of library names.
  28. if lib_names:
  29. for lib_name in lib_names:
  30. lib_path = find_library(lib_name)
  31. if lib_path is not None:
  32. break
  33. if lib_path is None:
  34. raise ImproperlyConfigured(
  35. 'Could not find the GDAL library (tried "%s"). Is GDAL installed? '
  36. 'If it is, try setting GDAL_LIBRARY_PATH in your settings.'
  37. % '", "'.join(lib_names)
  38. )
  39. # This loads the GDAL/OGR C library
  40. lgdal = CDLL(lib_path)
  41. # On Windows, the GDAL binaries have some OSR routines exported with
  42. # STDCALL, while others are not. Thus, the library will also need to
  43. # be loaded up as WinDLL for said OSR functions that require the
  44. # different calling convention.
  45. if os.name == 'nt':
  46. from ctypes import WinDLL
  47. lwingdal = WinDLL(lib_path)
  48. def std_call(func):
  49. """
  50. Return the correct STDCALL function for certain OSR routines on Win32
  51. platforms.
  52. """
  53. if os.name == 'nt':
  54. return lwingdal[func]
  55. else:
  56. return lgdal[func]
  57. # #### Version-information functions. ####
  58. # Return GDAL library version information with the given key.
  59. _version_info = std_call('GDALVersionInfo')
  60. _version_info.argtypes = [c_char_p]
  61. _version_info.restype = c_char_p
  62. def gdal_version():
  63. "Return only the GDAL version number information."
  64. return _version_info(b'RELEASE_NAME')
  65. def gdal_full_version():
  66. "Return the full GDAL version information."
  67. return _version_info('')
  68. version_regex = re.compile(r'^(?P<major>\d+)\.(?P<minor>\d+)(\.(?P<subminor>\d+))?')
  69. def gdal_version_info():
  70. ver = gdal_version().decode()
  71. m = version_regex.match(ver)
  72. if not m:
  73. raise GDALException('Could not parse GDAL version string "%s"' % ver)
  74. return {key: m.group(key) for key in ('major', 'minor', 'subminor')}
  75. _verinfo = gdal_version_info()
  76. GDAL_MAJOR_VERSION = int(_verinfo['major'])
  77. GDAL_MINOR_VERSION = int(_verinfo['minor'])
  78. GDAL_SUBMINOR_VERSION = _verinfo['subminor'] and int(_verinfo['subminor'])
  79. GDAL_VERSION = (GDAL_MAJOR_VERSION, GDAL_MINOR_VERSION, GDAL_SUBMINOR_VERSION)
  80. del _verinfo
  81. # Set library error handling so as errors are logged
  82. CPLErrorHandler = CFUNCTYPE(None, c_int, c_int, c_char_p)
  83. def err_handler(error_class, error_number, message):
  84. logger.error('GDAL_ERROR %d: %s', error_number, message)
  85. err_handler = CPLErrorHandler(err_handler)
  86. def function(name, args, restype):
  87. func = std_call(name)
  88. func.argtypes = args
  89. func.restype = restype
  90. return func
  91. set_error_handler = function('CPLSetErrorHandler', [CPLErrorHandler], CPLErrorHandler)
  92. set_error_handler(err_handler)