0

I want to translate following SQL-query into the Python code using SQLAlchemy.

SELECT (ST_Dump(ST_Linemerge(ST_Union(geog::geometry)))).geom
FROM my_table
GROUP BY some_foreign_key

I had a partial success with

from geoalchemy2 import Geometry
from sqlalchemy import func, cast

data = session.query(
    func.ST_Dump(
        func.ST_Linemerge(
            func.ST_Union(
                cast(MyTable.geog, Geometry)
            )
        )
    ).label('dumped_geom')
).group_by(
    MyTable.some_foreign_key
)

The issue is that ST_Dump returns set instead of single value and this query in Python returns string with a path and geometry and I need only geometry part. Documentation for ST_Dump in geoalchemy2 doesn't help at all.

SS_Rebelious
  • 1,274
  • 2
  • 19
  • 35

1 Answers1

0

geoalchemy2 provides a ST_Dump function (wich return an object with a geom property)

Ex:

from geoalchemy2 import Geometry
from geoalchemy2.functions import ST_Dump
from sqlalchemy import cast, func

data = session.query(
    ST_Dump(
        func.ST_Linemerge(
            func.ST_Union(
                cast(MyTable.geog, Geometry)
            )
        )
    ).geom
).group_by(
    MyTable.some_foreign_key
)
ulric260
  • 364
  • 4
  • 15