|
0
|
1 |
""" |
|
|
2 |
This object provides quoting for GEOS geometries into PostgreSQL/PostGIS. |
|
|
3 |
""" |
|
|
4 |
|
|
|
5 |
from django.contrib.gis.db.backend.postgis.query import GEOM_FROM_WKB |
|
|
6 |
from psycopg2 import Binary |
|
|
7 |
from psycopg2.extensions import ISQLQuote |
|
|
8 |
|
|
|
9 |
class PostGISAdaptor(object): |
|
|
10 |
def __init__(self, geom): |
|
|
11 |
"Initializes on the geometry." |
|
|
12 |
# Getting the WKB (in string form, to allow easy pickling of |
|
|
13 |
# the adaptor) and the SRID from the geometry. |
|
|
14 |
self.wkb = str(geom.wkb) |
|
|
15 |
self.srid = geom.srid |
|
|
16 |
|
|
|
17 |
def __conform__(self, proto): |
|
|
18 |
# Does the given protocol conform to what Psycopg2 expects? |
|
|
19 |
if proto == ISQLQuote: |
|
|
20 |
return self |
|
|
21 |
else: |
|
|
22 |
raise Exception('Error implementing psycopg2 protocol. Is psycopg2 installed?') |
|
|
23 |
|
|
|
24 |
def __eq__(self, other): |
|
|
25 |
return (self.wkb == other.wkb) and (self.srid == other.srid) |
|
|
26 |
|
|
|
27 |
def __str__(self): |
|
|
28 |
return self.getquoted() |
|
|
29 |
|
|
|
30 |
def getquoted(self): |
|
|
31 |
"Returns a properly quoted string for use in PostgreSQL/PostGIS." |
|
|
32 |
# Want to use WKB, so wrap with psycopg2 Binary() to quote properly. |
|
|
33 |
return "%s(%s, %s)" % (GEOM_FROM_WKB, Binary(self.wkb), self.srid or -1) |
|
|
34 |
|
|
|
35 |
def prepare_database_save(self, unused): |
|
|
36 |
return self |