20 |
|
|
21 |
from data import SHAPETYPE_POLYGON, SHAPETYPE_ARC, SHAPETYPE_POINT, RAW_WKT |
from data import SHAPETYPE_POLYGON, SHAPETYPE_ARC, SHAPETYPE_POINT, RAW_WKT |
22 |
|
|
23 |
|
def has_postgis_support(): |
24 |
|
"""Return whether this Thuban instance supports PostGIS connections |
25 |
|
|
26 |
|
Having PostGIS support means that the psycopg module can be |
27 |
|
imported. |
28 |
|
""" |
29 |
|
return psycopg is not None |
30 |
|
|
31 |
|
def psycopg_version(): |
32 |
|
return psycopg.__version__ |
33 |
|
|
34 |
if psycopg is not None: |
if psycopg is not None: |
35 |
type_map = [(psycopg.STRING, table.FIELDTYPE_STRING), |
type_map = [(psycopg.STRING, table.FIELDTYPE_STRING), |
36 |
(psycopg.INTEGER, table.FIELDTYPE_INT), |
(psycopg.INTEGER, table.FIELDTYPE_INT), |
37 |
(psycopg.FLOAT, table.FIELDTYPE_DOUBLE)] |
(psycopg.FLOAT, table.FIELDTYPE_DOUBLE)] |
38 |
|
|
39 |
|
|
40 |
|
class ConnectionError(Exception): |
41 |
|
|
42 |
|
"""Class for exceptions occurring when establishing a Databse connection""" |
43 |
|
|
44 |
|
|
45 |
class PostGISConnection: |
class PostGISConnection: |
46 |
|
|
47 |
"""Represent a PostGIS database |
"""Represent a PostGIS database |
64 |
self.user = user |
self.user = user |
65 |
self.password = password |
self.password = password |
66 |
self.dbtype = dbtype |
self.dbtype = dbtype |
67 |
|
self.connect() |
68 |
|
|
69 |
|
def connect(self): |
70 |
|
"""Internal: Establish the database connection""" |
71 |
params = [] |
params = [] |
72 |
for name in ("host", "port", "dbname", "user", "password"): |
for name in ("host", "port", "dbname", "user", "password"): |
73 |
val = getattr(self, name) |
val = getattr(self, name) |
74 |
if val: |
if val: |
75 |
params.append("%s=%s" % (name, val)) |
params.append("%s=%s" % (name, val)) |
76 |
self.connection = psycopg.connect(" ".join(params)) |
try: |
77 |
|
self.connection = psycopg.connect(" ".join(params)) |
78 |
|
except psycopg.OperationalError, val: |
79 |
|
raise ConnectionError(str(val)) |
80 |
|
|
81 |
# determine the OID for the geometry type. This is PostGIS |
# determine the OID for the geometry type. This is PostGIS |
82 |
# specific. |
# specific. |
90 |
else: |
else: |
91 |
raise ValueError("Can't determine postgres type of geometries") |
raise ValueError("Can't determine postgres type of geometries") |
92 |
|
|
93 |
|
def BriefDescription(self): |
94 |
|
"""Return a brief, one-line description of the connection |
95 |
|
|
96 |
|
The return value is suitable for a list box of all database |
97 |
|
connections. |
98 |
|
""" |
99 |
|
return ("postgis://%(user)s@%(host)s:%(port)s/%(dbname)s" |
100 |
|
% self.__dict__) |
101 |
|
|
102 |
def Close(self): |
def Close(self): |
103 |
"""Close the database connection""" |
"""Close the database connection""" |
104 |
self.connection.close() |
self.connection.close() |
172 |
% (", ".join([col.name for col in self.columns]), |
% (", ".join([col.name for col in self.columns]), |
173 |
self.tablename)) |
self.tablename)) |
174 |
|
|
175 |
|
def DBConnection(self): |
176 |
|
"""Return the dbconnection used by the table""" |
177 |
|
return self.db |
178 |
|
|
179 |
|
def TableName(self): |
180 |
|
"""Return the name of the table in the database""" |
181 |
|
return self.tablename |
182 |
|
|
183 |
def Dependencies(self): |
def Dependencies(self): |
184 |
"""Return an empty tuple because a PostGISTable depends on nothing else |
"""Return an empty tuple because a PostGISTable depends on nothing else |
185 |
""" |
""" |
287 |
|
|
288 |
|
|
289 |
shapetype_map = {"POLYGON": SHAPETYPE_POLYGON, |
shapetype_map = {"POLYGON": SHAPETYPE_POLYGON, |
290 |
|
"MULTIPOLYGON": SHAPETYPE_POLYGON, |
291 |
"MULTILINESTRING": SHAPETYPE_ARC, |
"MULTILINESTRING": SHAPETYPE_ARC, |
292 |
"POINT": SHAPETYPE_POINT} |
"POINT": SHAPETYPE_POINT} |
293 |
|
|