Find all users x miles away in flask + geoalchemy with ORM

383 Views Asked by At

I have created the following SQL query to find all users within a mile and it seems to work fine:

SELECT * FROM user 
WHERE ST_DWithin(
    user.location, 
    ST_MakePoint(-2.242631, 53.480759)::geography, 1609) 
);

However I want to convert this into a flask/sqlalchemy/geoalchemy query?

1

There are 1 best solutions below

0
On

Try something like this:

DISTANCE = 100 #100 meters
db.session.query(User).filter(func.ST_DWithin(User.location, cast(funct.ST_SetSRID(func.ST_MakePoint(-2.242631, 53.480759), 1609), Geography), DISTANCE)).all()