0

I try to use postgis in rails and write my first request. I would like have : - the nearest points of another point (but without this point) - the name and the distance between the both

In my model I have write this scope :

 scope :close_to, -> (latitude, longitude, distance_in_m = 1) {
  where(%{
  ST_Distance(lonlat, 'POINT(%f %f)') < %d
 } % [longitude, latitude, distance_in_m * 1000])
}

How can I get the distance ?

0

Search for the Nearest Neighbor in postgis is a well know problem solved multiple times in stack overflow and the "internet", like:

In it's more basic form the query can be written like this:

SELECT name, gid, ST_Distance(geom, st_setsrid(st_makepoint(-90,40),4326))
FROM geonames
ORDER BY geom <-> st_setsrid(st_makepoint(-90,40),4326)
LIMIT 1;

but it can be improved depending on your use case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.