Django

Code

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

Revision 8012, 2.5 kB (checked in by jbronn, 4 months ago)

gis: Now use ctypes.util.find_library to get the C library names. This is preferrable to manually specifying the different library naming schemes used by different platforms/distributions.

Line 
1 import os, 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 = ['gdal15']
18 elif os.name == 'posix':
19     # *NIX library names.
20     lib_names = ['gdal', 'gdal1.5.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_name)
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')
Note: See TracBrowser for help on using the browser.