1 |
# Copyright (C) 2003, 2004 by Intevation GmbH |
# Copyright (C) 2003, 2004, 2005 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Martin Mueller <[email protected]> |
# Martin Mueller <[email protected]> |
4 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
107 |
except psycopg.OperationalError, val: |
except psycopg.OperationalError, val: |
108 |
raise ConnectionError(str(val)) |
raise ConnectionError(str(val)) |
109 |
|
|
110 |
|
# Use autocommit mode. For simple reading of the database it's |
111 |
|
# sufficient and we don't have to care much about error |
112 |
|
# handling. Without autocommit, an errors during a cursor's |
113 |
|
# execute method requires a rollback on the connection, |
114 |
|
# otherwise later queries with the same or other cursors sharing |
115 |
|
# the same connection will lead to further errors ("ERROR: |
116 |
|
# current transaction is aborted, commands ignored until end of |
117 |
|
# transaction block") |
118 |
|
self.connection.autocommit() |
119 |
|
|
120 |
# determine the OID for the geometry type. This is PostGIS |
# determine the OID for the geometry type. This is PostGIS |
121 |
# specific. |
# specific. |
122 |
cursor = self.connection.cursor() |
cursor = self.connection.cursor() |
488 |
|
|
489 |
shapetype_map = {"POLYGON": SHAPETYPE_POLYGON, |
shapetype_map = {"POLYGON": SHAPETYPE_POLYGON, |
490 |
"MULTIPOLYGON": SHAPETYPE_POLYGON, |
"MULTIPOLYGON": SHAPETYPE_POLYGON, |
491 |
|
"LINESTRING": SHAPETYPE_ARC, |
492 |
"MULTILINESTRING": SHAPETYPE_ARC, |
"MULTILINESTRING": SHAPETYPE_ARC, |
493 |
"POINT": SHAPETYPE_POINT} |
"POINT": SHAPETYPE_POINT} |
494 |
|
|
525 |
self.quoted_geo_col = quote_identifier(self.geometry_column) |
self.quoted_geo_col = quote_identifier(self.geometry_column) |
526 |
|
|
527 |
def _fetch_table_information(self): |
def _fetch_table_information(self): |
528 |
"""Extend inherited method to retrieve the SRID""" |
"""Extend inherited method to retrieve the SRID and shape type""" |
529 |
PostGISTable._fetch_table_information(self) |
PostGISTable._fetch_table_information(self) |
530 |
|
|
531 |
|
# First, try to get it from the geometry_columns table. |
532 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
533 |
cursor.execute("SELECT srid FROM geometry_columns" |
cursor.execute("SELECT srid, type FROM geometry_columns" |
534 |
" WHERE f_table_name = %s AND f_geometry_column=%s", |
" WHERE f_table_name = %s AND f_geometry_column=%s", |
535 |
(self.tablename, self.geometry_column)) |
(self.tablename, self.geometry_column)) |
536 |
self.srid = cursor.fetchone()[0] |
row = cursor.fetchone() |
537 |
|
if row is not None: |
538 |
|
self.srid = row[0] |
539 |
|
self.shape_type = shapetype_map.get(row[1]) |
540 |
|
return |
541 |
|
|
542 |
|
# The table is probably really a view and thus not in |
543 |
|
# geometry_columns. Use a different approach |
544 |
|
cursor = self.db.cursor() |
545 |
|
cursor.execute("SELECT DISTINCT SRID(%s) FROM %s;" % |
546 |
|
(quote_identifier(self.geometry_column), |
547 |
|
self.tablename)) |
548 |
|
row = cursor.fetchone() |
549 |
|
if row is not None: |
550 |
|
self.srid = row[0] |
551 |
|
# Try to see whether there's another one |
552 |
|
row = cursor.fetchone() |
553 |
|
if row is not None: |
554 |
|
# There are at least two different srids. We don't |
555 |
|
# support that |
556 |
|
self.srid = None |
557 |
|
|
558 |
|
cursor = self.db.cursor() |
559 |
|
cursor.execute("SELECT DISTINCT GeometryType(%s) FROM %s;" |
560 |
|
% (quote_identifier(self.geometry_column), |
561 |
|
self.tablename)) |
562 |
|
row = cursor.fetchone() |
563 |
|
if row is not None: |
564 |
|
self.shape_type = shapetype_map.get(row[0]) |
565 |
|
# Try to see whether there's another one |
566 |
|
row = cursor.fetchone() |
567 |
|
if row is not None: |
568 |
|
# There are at least two different srids. We don't |
569 |
|
# support that |
570 |
|
self.shape_type = None |
571 |
|
|
572 |
def _create_col_from_description(self, index, description): |
def _create_col_from_description(self, index, description): |
573 |
"""Extend the inherited method to find geometry columns |
"""Extend the inherited method to find geometry columns |
621 |
|
|
622 |
def ShapeType(self): |
def ShapeType(self): |
623 |
"""Return the type of the shapes in the shapestore.""" |
"""Return the type of the shapes in the shapestore.""" |
624 |
cursor = self.db.cursor() |
return self.shape_type |
|
cursor.execute("SELECT type FROM geometry_columns WHERE" |
|
|
" f_table_name=%s AND f_geometry_column=%s", |
|
|
(self.tablename, self.geometry_column)) |
|
|
result = cursor.fetchone()[0] |
|
|
cursor.close() |
|
|
return shapetype_map[result] |
|
625 |
|
|
626 |
def RawShapeFormat(self): |
def RawShapeFormat(self): |
627 |
"""Return the raw data format of the shape data. |
"""Return the raw data format of the shape data. |