[GIS] Casting between PostGIS types in GeoAlchethe

geoalchemyopenstreetmaposm2pgsqlpostgispython

I'm working with OpenStreetMap data, and loading it into a PostGIS database using osm2pgsql.

The issue that I am currently dealing with is that osm2pgsql stores the shape as a geometry type.

I'm using GeoAlchemy2 to query the Postgres database, but I am having issues with the following query.

query = session.query(Route).filter(func.ST_Distance(Route.way,'SRID=4326;POINT(10.71223 47.69716)') < distance )

Because osm2pgsql stores the data as a Geometry type, it doesn't interpret my points as a geography type, and therefore doesn't filter based on distance in meters, but rather in degrees.

Would there be a way to cast the Geometry type objects (Route.way and the string representation of the point ) to a Geography form?

Best Answer

I realized that it is possible to cast from the Geometry object to the Geography object using the SQLAlchemy function, despite the two not being native SQLAlchemy objects.

query = session.query(Route).filter(func.ST_Distance(cast(Route.way,Geography),'SRID=4326;POINT(10.71223 47.69716)') < distance )