| 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 |
|
| 7 |
from django.contrib.gis.gdal.error import GDALException |
| 8 |
from django.core.exceptions import ImproperlyConfigured |
| 9 |
|
| 10 |
logger = logging.getLogger("django.contrib.gis") |
| 11 |
|
| 12 |
# Custom library path set? |
| 13 |
try: |
| 14 |
from django.conf import settings |
| 15 |
|
| 16 |
lib_path = settings.GDAL_LIBRARY_PATH |
| 17 |
except (AttributeError, ImportError, ImproperlyConfigured, OSError): |
| 18 |
lib_path = None |
| 19 |
|
| 20 |
if lib_path: |
| 21 |
lib_names = None |
| 22 |
elif os.name == "nt": |
| 23 |
# Windows NT shared libraries |
| 24 |
lib_names = [ |
| 25 |
"gdal310", |
| 26 |
"gdal309", |
| 27 |
"gdal308", |
| 28 |
"gdal307", |
| 29 |
"gdal306", |
| 30 |
"gdal305", |
| 31 |
"gdal304", |
| 32 |
"gdal303", |
| 33 |
"gdal302", |
| 34 |
"gdal301", |
| 35 |
] |
| 36 |
elif os.name == "posix": |
| 37 |
# *NIX library names. |
| 38 |
lib_names = [ |
| 39 |
"gdal", |
| 40 |
"GDAL", |
| 41 |
"gdal3.10.0", |
| 42 |
"gdal3.9.0", |
| 43 |
"gdal3.8.0", |
| 44 |
"gdal3.7.0", |
| 45 |
"gdal3.6.0", |
| 46 |
"gdal3.5.0", |
| 47 |
"gdal3.4.0", |
| 48 |
"gdal3.3.0", |
| 49 |
"gdal3.2.0", |
| 50 |
"gdal3.1.0", |
| 51 |
] |
| 52 |
else: |
| 53 |
raise ImproperlyConfigured('GDAL is unsupported on OS "%s".' % os.name) |
| 54 |
|
| 55 |
# Using the ctypes `find_library` utility to find the |
| 56 |
# path to the GDAL library from the list of library names. |
| 57 |
if lib_names: |
| 58 |
for lib_name in lib_names: |
| 59 |
lib_path = find_library(lib_name) |
| 60 |
if lib_path is not None: |
| 61 |
break |
| 62 |
|
| 63 |
if lib_path is None: |
| 64 |
raise ImproperlyConfigured( |
| 65 |
'Could not find the GDAL library (tried "%s"). Is GDAL installed? ' |
| 66 |
"If it is, try setting GDAL_LIBRARY_PATH in your settings." |
| 67 |
% '", "'.join(lib_names) |
| 68 |
) |
| 69 |
|
| 70 |
# This loads the GDAL/OGR C library |
| 71 |
lgdal = CDLL(lib_path) |
| 72 |
|
| 73 |
# On Windows, the GDAL binaries have some OSR routines exported with |
| 74 |
# STDCALL, while others are not. Thus, the library will also need to |
| 75 |
# be loaded up as WinDLL for said OSR functions that require the |
| 76 |
# different calling convention. |
| 77 |
if os.name == "nt": |
| 78 |
from ctypes import WinDLL |
| 79 |
|
| 80 |
lwingdal = WinDLL(lib_path) |
| 81 |
|
| 82 |
|
| 83 |
def std_call(func): |
| 84 |
""" |
| 85 |
Return the correct STDCALL function for certain OSR routines on Win32 |
| 86 |
platforms. |
| 87 |
""" |
| 88 |
if os.name == "nt": |
| 89 |
return lwingdal[func] |
| 90 |
else: |
| 91 |
return lgdal[func] |
| 92 |
|
| 93 |
|
| 94 |
# #### Version-information functions. #### |
| 95 |
|
| 96 |
# Return GDAL library version information with the given key. |
| 97 |
_version_info = std_call("GDALVersionInfo") |
| 98 |
_version_info.argtypes = [c_char_p] |
| 99 |
_version_info.restype = c_char_p |
| 100 |
|
| 101 |
|
| 102 |
def gdal_version(): |
| 103 |
"Return only the GDAL version number information." |
| 104 |
return _version_info(b"RELEASE_NAME") |
| 105 |
|
| 106 |
|
| 107 |
def gdal_full_version(): |
| 108 |
"Return the full GDAL version information." |
| 109 |
return _version_info(b"") |
| 110 |
|
| 111 |
|
| 112 |
def gdal_version_info(): |
| 113 |
ver = gdal_version() |
| 114 |
m = re.match(rb"^(?P<major>\d+)\.(?P<minor>\d+)(?:\.(?P<subminor>\d+))?", ver) |
| 115 |
if not m: |
| 116 |
raise GDALException('Could not parse GDAL version string "%s"' % ver) |
| 117 |
major, minor, subminor = m.groups() |
| 118 |
return (int(major), int(minor), subminor and int(subminor)) |
| 119 |
|
| 120 |
|
| 121 |
GDAL_VERSION = gdal_version_info() |
| 122 |
|
| 123 |
# Set library error handling so as errors are logged |
| 124 |
CPLErrorHandler = CFUNCTYPE(None, c_int, c_int, c_char_p) |
| 125 |
|
| 126 |
|
| 127 |
def err_handler(error_class, error_number, message): |
| 128 |
logger.error("GDAL_ERROR %d: %s", error_number, message) |
| 129 |
|
| 130 |
|
| 131 |
err_handler = CPLErrorHandler(err_handler) |
| 132 |
|
| 133 |
|
| 134 |
def function(name, args, restype): |
| 135 |
func = std_call(name) |
| 136 |
func.argtypes = args |
| 137 |
func.restype = restype |
| 138 |
return func |
| 139 |
|
| 140 |
|
| 141 |
set_error_handler = function("CPLSetErrorHandler", [CPLErrorHandler], CPLErrorHandler) |
| 142 |
set_error_handler(err_handler) |