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