postgresql - how to use composite data types (e.g. geomval) in SQLAlchemy? -


i'm trying replicate nested raw postgresql/postgis raster query using sqlalchemy(0.8)/geoalchemy2(0.2.1) , can't figure how access components of geomval data type. it's compound data type returns 'geom' , 'val'.

here raw query works:

select (dap).val, (dap).geom (select st_dumpaspolygons(rast) dap my_raster_table) thing 

and sqlalchemy query i'm working with:

import geoalchemy2 ga2 sqlalchemy import * sqlalchemy.orm import sessionmaker  metadata = metadata() my_raster_table = table('my_raster_table', metadata,                   column('rid', integer),                   column('rast', ga2.raster))  engine = create_engine(my_conn_str) session = sessionmaker(engine)()  q = session.query(ga2.func.st_dumpaspolygons(my_raster_table.c.rast).label('dap')) 

and i'd access in subquery, this:

q2 = session.query(ga2.func.st_area(q.subquery().c.dap.geom)) 

but syntax doesn't work, or wouldn't posting question ;). have ideas? thanks!

the solution ended being simple:

first, define custom geomvaltype, inheriting geoalchemy2's compositetype , specifying typemap specific geomval:

class geomvaltype(ga2.types.compositetype):     typemap = {'geom':ga2.geometry('multipolygon'),'val':float} 

next, use type_coerce cast st_dumpaspolygons result geomvaltype in initial query:

q = session.query(type_coerce(ga2.func.st_dumpaspolygons(my_raster_table.c.rast), geomvaltype()).label('dap')) 

finally, access (successfully!) subquery trying before:

q2 = session.query(ga2.func.st_area(q.subquery().c.dap.geom)) 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -