/django/contrib/gis/gdal/libgdal.py

https://code.google.com/p/mango-py/ · Python · 104 lines · 70 code · 16 blank · 18 comment · 20 complexity · eaf9e9436b4dd3654aa35225d0bcf754 MD5 · raw file

  1. import os, re, sys
  2. from ctypes import c_char_p, CDLL
  3. from ctypes.util import find_library
  4. from django.contrib.gis.gdal.error import OGRException
  5. # Custom library path set?
  6. try:
  7. from django.conf import settings
  8. lib_path = settings.GDAL_LIBRARY_PATH
  9. except (AttributeError, EnvironmentError, ImportError):
  10. lib_path = None
  11. if lib_path:
  12. lib_names = None
  13. elif os.name == 'nt':
  14. # Windows NT shared libraries
  15. lib_names = ['gdal18', 'gdal17', 'gdal16', 'gdal15']
  16. elif os.name == 'posix':
  17. # *NIX library names.
  18. lib_names = ['gdal', 'GDAL', 'gdal1.7.0', 'gdal1.6.0', 'gdal1.5.0', 'gdal1.4.0']
  19. else:
  20. raise OGRException('Unsupported OS "%s"' % os.name)
  21. # Using the ctypes `find_library` utility to find the
  22. # path to the GDAL library from the list of library names.
  23. if lib_names:
  24. for lib_name in lib_names:
  25. lib_path = find_library(lib_name)
  26. if not lib_path is None: break
  27. if lib_path is None:
  28. raise OGRException('Could not find the GDAL library (tried "%s"). '
  29. 'Try setting GDAL_LIBRARY_PATH in your settings.' %
  30. '", "'.join(lib_names))
  31. # This loads the GDAL/OGR C library
  32. lgdal = CDLL(lib_path)
  33. # On Windows, the GDAL binaries have some OSR routines exported with
  34. # STDCALL, while others are not. Thus, the library will also need to
  35. # be loaded up as WinDLL for said OSR functions that require the
  36. # different calling convention.
  37. if os.name == 'nt':
  38. from ctypes import WinDLL
  39. lwingdal = WinDLL(lib_path)
  40. def std_call(func):
  41. """
  42. Returns the correct STDCALL function for certain OSR routines on Win32
  43. platforms.
  44. """
  45. if os.name == 'nt':
  46. return lwingdal[func]
  47. else:
  48. return lgdal[func]
  49. #### Version-information functions. ####
  50. # Returns GDAL library version information with the given key.
  51. _version_info = std_call('GDALVersionInfo')
  52. _version_info.argtypes = [c_char_p]
  53. _version_info.restype = c_char_p
  54. def gdal_version():
  55. "Returns only the GDAL version number information."
  56. return _version_info('RELEASE_NAME')
  57. def gdal_full_version():
  58. "Returns the full GDAL version information."
  59. return _version_info('')
  60. def gdal_release_date(date=False):
  61. """
  62. Returns the release date in a string format, e.g, "2007/06/27".
  63. If the date keyword argument is set to True, a Python datetime object
  64. will be returned instead.
  65. """
  66. from datetime import date as date_type
  67. rel = _version_info('RELEASE_DATE')
  68. yy, mm, dd = map(int, (rel[0:4], rel[4:6], rel[6:8]))
  69. d = date_type(yy, mm, dd)
  70. if date: return d
  71. else: return d.strftime('%Y/%m/%d')
  72. version_regex = re.compile(r'^(?P<major>\d+)\.(?P<minor>\d+)(\.(?P<subminor>\d+))?')
  73. def gdal_version_info():
  74. ver = gdal_version()
  75. m = version_regex.match(ver)
  76. if not m: raise OGRException('Could not parse GDAL version string "%s"' % ver)
  77. return dict([(key, m.group(key)) for key in ('major', 'minor', 'subminor')])
  78. _verinfo = gdal_version_info()
  79. GDAL_MAJOR_VERSION = int(_verinfo['major'])
  80. GDAL_MINOR_VERSION = int(_verinfo['minor'])
  81. GDAL_SUBMINOR_VERSION = _verinfo['subminor'] and int(_verinfo['subminor'])
  82. GDAL_VERSION = (GDAL_MAJOR_VERSION, GDAL_MINOR_VERSION, GDAL_SUBMINOR_VERSION)
  83. del _verinfo
  84. # GeoJSON support is available only in GDAL 1.5+.
  85. if GDAL_VERSION >= (1, 5):
  86. GEOJSON = True
  87. else:
  88. GEOJSON = False