|
0
|
1 |
""" |
|
|
2 |
This utility module is for obtaining information about the PostGIS |
|
|
3 |
installation. |
|
|
4 |
|
|
|
5 |
See PostGIS docs at Ch. 6.2.1 for more information on these functions. |
|
|
6 |
""" |
|
|
7 |
import re |
|
|
8 |
|
|
|
9 |
def _get_postgis_func(func): |
|
|
10 |
"Helper routine for calling PostGIS functions and returning their result." |
|
|
11 |
from django.db import connection |
|
|
12 |
cursor = connection.cursor() |
|
|
13 |
cursor.execute('SELECT %s()' % func) |
|
|
14 |
row = cursor.fetchone() |
|
|
15 |
cursor.close() |
|
|
16 |
return row[0] |
|
|
17 |
|
|
|
18 |
### PostGIS management functions ### |
|
|
19 |
def postgis_geos_version(): |
|
|
20 |
"Returns the version of the GEOS library used with PostGIS." |
|
|
21 |
return _get_postgis_func('postgis_geos_version') |
|
|
22 |
|
|
|
23 |
def postgis_lib_version(): |
|
|
24 |
"Returns the version number of the PostGIS library used with PostgreSQL." |
|
|
25 |
return _get_postgis_func('postgis_lib_version') |
|
|
26 |
|
|
|
27 |
def postgis_proj_version(): |
|
|
28 |
"Returns the version of the PROJ.4 library used with PostGIS." |
|
|
29 |
return _get_postgis_func('postgis_proj_version') |
|
|
30 |
|
|
|
31 |
def postgis_version(): |
|
|
32 |
"Returns PostGIS version number and compile-time options." |
|
|
33 |
return _get_postgis_func('postgis_version') |
|
|
34 |
|
|
|
35 |
def postgis_full_version(): |
|
|
36 |
"Returns PostGIS version number and compile-time options." |
|
|
37 |
return _get_postgis_func('postgis_full_version') |
|
|
38 |
|
|
|
39 |
### Routines for parsing output of management functions. ### |
|
|
40 |
version_regex = re.compile('^(?P<major>\d)\.(?P<minor1>\d)\.(?P<minor2>\d+)') |
|
|
41 |
def postgis_version_tuple(): |
|
|
42 |
"Returns the PostGIS version as a tuple." |
|
|
43 |
|
|
|
44 |
# Getting the PostGIS version |
|
|
45 |
version = postgis_lib_version() |
|
|
46 |
m = version_regex.match(version) |
|
|
47 |
if m: |
|
|
48 |
major = int(m.group('major')) |
|
|
49 |
minor1 = int(m.group('minor1')) |
|
|
50 |
minor2 = int(m.group('minor2')) |
|
|
51 |
else: |
|
|
52 |
raise Exception('Could not parse PostGIS version string: %s' % version) |
|
|
53 |
|
|
|
54 |
return (version, major, minor1, minor2) |