I am attempting to perform a union on two rasters I pick from my postgis database using GeoAlchemy2.

I have a Raster table defined:

from geoalchemy2 import Raster
from sqlalchemy.sql import func

Base = declarative_base()

class RasterData(Base):
Raster class
__tablename__ = 'images'
__table_args__ = {"schema": "public"}
raster = Column(Raster)

Then I attempt to use ORM to union two raster tiles using

engine = create_engine(db_str, echo=False)
Session = sessionmaker(bind=engine)
session = Session(expire_on_commit=False)

rasters = session.query(RasterData.raster).limit(2).all()
merged = session.query(func.ST_Union(rasters)).all()

This unfortunately errors out with:

psycopg2.ProgrammingError: can't adapt type 'RasterElement'

The above exception was the direct cause of the following exception:

It seems I might need to cast this as something else but I am unsure what. Any ideas what the code above is missing to complete the union?


I needed to pass the type_ kwarg to the function ST_Union.


merged = session.query(func.ST_Union(RasterData.raster, type_=Raster)).all()

Thanks to the developers of Geoalchemy2 who pointed this out.

Example shown in their gallery: Reproject A Raster using ST_Transform usage on Line 54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.