PageRenderTime 114ms CodeModel.GetById 109ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/db/backends/postgis/adapter.py

https://code.google.com/p/mango-py/
Python | 35 lines | 21 code | 7 blank | 7 comment | 2 complexity | f10591ed4595da6a3b6be23a7b12c20c MD5 | raw file
 1"""
 2 This object provides quoting for GEOS geometries into PostgreSQL/PostGIS.
 3"""
 4
 5from psycopg2 import Binary
 6from psycopg2.extensions import ISQLQuote
 7
 8class 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