PageRenderTime 119ms CodeModel.GetById 18ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://code.google.com/p/mango-py/
Python | 104 lines | 77 code | 11 blank | 16 comment | 13 complexity | eaf9e9436b4dd3654aa35225d0bcf754 MD5 | raw file
Possible License(s): BSD-3-Clause
  1import os, re, sys
  2from ctypes import c_char_p, CDLL
  3from ctypes.util import find_library
  4from django.contrib.gis.gdal.error import OGRException
  5
  6# Custom library path set?
  7try:
  8    from django.conf import settings
  9    lib_path = settings.GDAL_LIBRARY_PATH
 10except (AttributeError, EnvironmentError, ImportError):
 11    lib_path = None
 12
 13if lib_path:
 14    lib_names = None
 15elif os.name == 'nt':
 16    # Windows NT shared libraries
 17    lib_names = ['gdal18', 'gdal17', 'gdal16', 'gdal15']
 18elif os.name == 'posix':
 19    # *NIX library names.
 20    lib_names = ['gdal', 'GDAL', 'gdal1.7.0', 'gdal1.6.0', 'gdal1.5.0', 'gdal1.4.0']
 21else:
 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.
 26if 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        
 31if 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
 37lgdal = 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.
 43if os.name == 'nt':
 44    from ctypes import WinDLL
 45    lwingdal = WinDLL(lib_path)
 46
 47def 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
 64def gdal_version():
 65    "Returns only the GDAL version number information."
 66    return _version_info('RELEASE_NAME')
 67
 68def gdal_full_version(): 
 69    "Returns the full GDAL version information."
 70    return _version_info('')
 71
 72def 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
 85version_regex = re.compile(r'^(?P<major>\d+)\.(?P<minor>\d+)(\.(?P<subminor>\d+))?')
 86def 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()
 93GDAL_MAJOR_VERSION = int(_verinfo['major'])
 94GDAL_MINOR_VERSION = int(_verinfo['minor'])
 95GDAL_SUBMINOR_VERSION = _verinfo['subminor'] and int(_verinfo['subminor'])
 96GDAL_VERSION = (GDAL_MAJOR_VERSION, GDAL_MINOR_VERSION, GDAL_SUBMINOR_VERSION)
 97del _verinfo
 98
 99# GeoJSON support is available only in GDAL 1.5+.
100if GDAL_VERSION >= (1, 5):
101    GEOJSON = True
102else:
103    GEOJSON = False
104