Sqlalchemy/Geoalchemy2 radius search returning empty list

54 views Asked by At

I want to implement a radius search on my location table (on mariadb) that has latitude and longitude stored. Now I would like to get all locations within a certain amount of km. This is my attempt but it always returns an empty list:

from geoalchemy2.elements import WKTElement    
from sqlalchemy import func
from sqlalchemy.orm import Session
    
def get_locations_in_radius(db: Session, radius: int, lat: float, lng: float):
    locations = db.query(models.Location).filter(
        func.ST_Distance_Sphere(
            func.ST_GeomFromText(f"POINT({models.Location.lng} {models.Location.lat})"),
            func.ST_GeomFromText(f"POINT({lng} {lat})")
        ) <= radius
    ).all()

    return locations
0

There are 0 answers