How to set extent of spatial SQL query in python (geoalchemy) -


since days i've been hustling around trying figure out, how query data postgis database using geoalchemy2, extension python's sqlalchemy supporting spatial database operations.

i'm working python3.4 , openstreetmaps data brandenburg (admin area within germany) fed local postgres-db. data in lat/long. i've been following tutorials on how set things using orm part of geoalchemy package (https://geoalchemy-2.readthedocs.org/en/latest/orm_tutorial.html). in beginning, went fine

  1. define mapping

    base = declarative_base()  class queryschema(base):    __tablename__ = "brandenburg_polygon"    osm_id = column(integer, primary_key=true)    name = column(string)    amenity = column(string)    way = column(geometry('polygon')) 
  2. define db setup

    engine = create_engine( 'postgresql+psycopg2://postgres_andi:{pwd}@localhost/osm'.format(     pwd=keyring.get_password('osm', 'andi_postgres'))) session = sessionmaker(bind=engine) session = session() 
  3. do query

    buildings = session.query(queryschema) 

now, working fine until try reduce extent - don't want have buildings stored inside db maybe within given boundary or boundary polygon.

  1. reduce extent defining boundary box (wkt-format)

    bbox = 'polygon ((13.01881424267171 52.50091209200498, 13.01881424267171 52.57800809377812, 12.87181701302189 52.57800809377812, 12.87181701302189 52.50091209200498, 13.01881424267171 52.50091209200498))' 

i tried using .filter() various options didn't work out. far understood, filter() needs kind of bool input, 1 has define statement according that. wrong statement this?

    session.query(queryschema).filter(func.st_contains(bbox, queryschema.way)) 

checking result of func.st_contains(bbox, queryschema.way) brings <geoalchemy2.functions.st_contains @ 0x10a12a400; st_contains>so filter() won't work properly.

question: how have perform operation work properly, i.e. giving me db entries within given boundary?

try query:

session.query(queryschema).filter(queryschema.way.st_within(bbox)) 

Comments