I have a flask app that first fetches a location from Google Maps API and am trying to find the location that is closer to it within 1500 meters that is in the database I have.
Here is my places model.
class Places(db.Model):
uuid = db.Column(UUID(as_uuid=True),
primary_key=True,
server_default=text("uuid_generate_v4()"))
name = db.Column(db.String(80), nullable=False)
# For Near Location Querying
geometric_point = db.Column(
Geometry(geometry_type='POINT', srid=4326), nullable=False)
I have this query to find the entry that has location nearby the submitted lng
and lat
within 1500meters.
def get_nearby_aqi_node(lat, lng):
distance_in_meters = 1500
geo_wkb = func.Geometry(func.ST_GeographyFromText(
'POINT({} {})'.format(lng, lat)))
point = db.session.query(Places.name,
Places.uuid).\
filter(func.ST_DFullyWithin(Places.geometric_point, geo_wkb, distance_in_meters)).\
order_by(
Comparator.distance_centroid(Places.geometric_point, geo_wkb)).limit(1).first()
return point
I am getting the single point that is closer to the place but it is not within the 1500 meter restriction.
I get the same result with the .filter()
removed.
Your points are of the geometry
type with an CRS 4326
, whose unit is degrees. The filer is a distance constraint of 1500 degrees (which is meaningless).
Even though you have called once func.ST_GeographyFromText()
, the output is still a geometry
and the cast between the two is automatic.
You can use the geography
type for both points in order to use a distance in meters.