PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms 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 | 3 complexity | f10591ed4595da6a3b6be23a7b12c20c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. This object provides quoting for GEOS geometries into PostgreSQL/PostGIS.
  3. """
  4. from psycopg2 import Binary
  5. from psycopg2.extensions import ISQLQuote
  6. class PostGISAdapter(object):
  7. def __init__(self, geom):
  8. "Initializes on the geometry."
  9. # Getting the WKB (in string form, to allow easy pickling of
  10. # the adaptor) and the SRID from the geometry.
  11. self.ewkb = str(geom.ewkb)
  12. self.srid = geom.srid
  13. def __conform__(self, proto):
  14. # Does the given protocol conform to what Psycopg2 expects?
  15. if proto == ISQLQuote:
  16. return self
  17. else:
  18. raise Exception('Error implementing psycopg2 protocol. Is psycopg2 installed?')
  19. def __eq__(self, other):
  20. return (self.ewkb == other.ewkb) and (self.srid == other.srid)
  21. def __str__(self):
  22. return self.getquoted()
  23. def getquoted(self):
  24. "Returns a properly quoted string for use in PostgreSQL/PostGIS."
  25. # Want to use WKB, so wrap with psycopg2 Binary() to quote properly.
  26. return 'ST_GeomFromEWKB(E%s)' % Binary(self.ewkb)
  27. def prepare_database_save(self, unused):
  28. return self