2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
This object provides quoting for GEOS geometries into PostgreSQL/PostGIS.
|
|
|
|
"""
|
|
|
|
from psycopg2 import Binary
|
|
|
|
from psycopg2.extensions import ISQLQuote
|
|
|
|
|
2016-04-22 00:03:14 +08:00
|
|
|
from django.contrib.gis.db.backends.postgis.pgraster import to_pgraster
|
2017-09-11 11:00:18 +08:00
|
|
|
from django.contrib.gis.geos import GEOSGeometry
|
2016-04-22 00:03:14 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class PostGISAdapter:
|
2016-04-22 00:03:14 +08:00
|
|
|
def __init__(self, obj, geography=False):
|
|
|
|
"""
|
|
|
|
Initialize on the spatial object.
|
|
|
|
"""
|
2017-09-11 11:00:18 +08:00
|
|
|
self.is_geometry = isinstance(obj, (GEOSGeometry, PostGISAdapter))
|
2016-04-22 00:03:14 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Getting the WKB (in string form, to allow easy pickling of
|
2016-04-22 00:03:14 +08:00
|
|
|
# the adaptor) and the SRID from the geometry or raster.
|
|
|
|
if self.is_geometry:
|
|
|
|
self.ewkb = bytes(obj.ewkb)
|
|
|
|
self._adapter = Binary(self.ewkb)
|
|
|
|
else:
|
|
|
|
self.ewkb = to_pgraster(obj)
|
|
|
|
|
|
|
|
self.srid = obj.srid
|
2015-01-19 23:09:41 +08:00
|
|
|
self.geography = geography
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def __conform__(self, proto):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""Does the given protocol conform to what Psycopg2 expects?"""
|
2008-08-06 02:13:06 +08:00
|
|
|
if proto == ISQLQuote:
|
|
|
|
return self
|
|
|
|
else:
|
|
|
|
raise Exception('Error implementing psycopg2 protocol. Is psycopg2 installed?')
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2017-09-28 17:39:12 +08:00
|
|
|
return isinstance(other, PostGISAdapter) and self.ewkb == other.ewkb
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-04-15 13:46:19 +08:00
|
|
|
def __hash__(self):
|
2017-07-19 22:04:15 +08:00
|
|
|
return hash(self.ewkb)
|
2015-04-15 13:46:19 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.getquoted()
|
|
|
|
|
2011-09-14 04:43:33 +08:00
|
|
|
def prepare(self, conn):
|
|
|
|
"""
|
|
|
|
This method allows escaping the binary in the style required by the
|
|
|
|
server's `standard_conforming_string` setting.
|
|
|
|
"""
|
2016-04-22 00:03:14 +08:00
|
|
|
if self.is_geometry:
|
|
|
|
self._adapter.prepare(conn)
|
2011-09-14 04:43:33 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def getquoted(self):
|
2016-04-22 00:03:14 +08:00
|
|
|
"""
|
|
|
|
Return a properly quoted string for use in PostgreSQL/PostGIS.
|
|
|
|
"""
|
|
|
|
if self.is_geometry:
|
|
|
|
# Psycopg will figure out whether to use E'\\000' or '\000'.
|
2017-01-20 17:20:53 +08:00
|
|
|
return '%s(%s)' % (
|
2016-04-22 00:03:14 +08:00
|
|
|
'ST_GeogFromWKB' if self.geography else 'ST_GeomFromEWKB',
|
2017-01-20 17:20:53 +08:00
|
|
|
self._adapter.getquoted().decode()
|
2016-04-22 00:03:14 +08:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
# For rasters, add explicit type cast to WKB string.
|
|
|
|
return "'%s'::raster" % self.ewkb
|