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