this object provides quoting for geos geometries into postgresqlpostgis initialize on the spatial object getting the wkb in string form to allow easy pickling of the adaptor and the srid from the geometry or raster does the given protocol conform to what psycopg2 expects from psycopg2 extensions import isqlquote if proto isqlquote return self else raise exception error implementing psycopg2 protocol is psycopg2 installed def eqself other return isinstanceother postgisadapter and self ewkb other ewkb def hashself return hashself ewkb def strself return self getquoted decode classmethod def fixpolygoncls poly return poly def getquotedself if self isgeometry psycopg will figure out whether to use e 000 or 000 return bss bstgeogfromwkb if self geography else bstgeomfromewkb sql quoteself ewkb encode else for rasters add explicit type cast to wkb string return b s raster self ewkb hex encode initialize on the spatial object getting the wkb in string form to allow easy pickling of the adaptor and the srid from the geometry or raster does the given protocol conform to what psycopg2 expects return a properly quoted string for use in postgresql postgis psycopg will figure out whether to use e 000 or 000 for rasters add explicit type cast to wkb string
from django.contrib.gis.db.backends.postgis.pgraster import to_pgraster
from django.contrib.gis.geos import GEOSGeometry
from django.db.backends.postgresql.psycopg_any import sql


class PostGISAdapter:
    def __init__(self, obj, geography=False):
        
        self.is_geometry = isinstance(obj, (GEOSGeometry, PostGISAdapter))

        
        
        if self.is_geometry:
            self.ewkb = bytes(obj.ewkb)
        else:
            self.ewkb = to_pgraster(obj)

        self.srid = obj.srid
        self.geography = geography

    def __conform__(self, proto):
        
        from psycopg2.extensions import ISQLQuote

        if proto == ISQLQuote:
            return self
        else:
            raise Exception(
                "Error implementing psycopg2 protocol. Is psycopg2 installed?"
            )

    def __eq__(self, other):
        return isinstance(other, PostGISAdapter) and self.ewkb == other.ewkb

    def __hash__(self):
        return hash(self.ewkb)

    def __str__(self):
        return self.getquoted().decode()

    @classmethod
    def _fix_polygon(cls, poly):
        return poly

    def getquoted(self):
        
        if self.is_geometry:
            
            return b"%s(%s)" % (
                b"ST_GeogFromWKB" if self.geography else b"ST_GeomFromEWKB",
                sql.quote(self.ewkb).encode(),
            )
        else:
            
            return b"'%s'::raster" % self.ewkb.hex().encode()