Given latitude and longitide of two points I have to find if them are withing a given max distance in meter, I found this function: https://postgis.net/docs/ST_DWithin.html

and I am using it in this way:

ST_DWithin(st_point(:long1, :lat1)::geography, st_point(:long2, :lat2)::geography, :max_distance))

max_distance is an int.

Is this correct ?

This implies that I have use_spheroid = true, what does this parameter change between true/false ?

  • 1
    The principal reason for using ST_DWithin and not ST_Distance is to take advantage of the spatial index. If you are constructing both geometries on the fly, then no index is possible.
    – Vince
    Commented Oct 19, 2022 at 12:24
  • In the real case the first point as geometry is stored on a field in a table with a gist index applied on that field, the second one used for comparison is from input so build on the fly. I think the index will help in this case, right?
    – res1
    Commented Oct 19, 2022 at 15:12
  • Well, only if the field is geography or the index is on Geography(geom) gis.stackexchange.com/questions/247113/…
    – Vince
    Commented Oct 19, 2022 at 16:07

1 Answer 1


Yes that will work.

For use_spheroid = true:

The earth is round, but not perfectly round, it is an oblique spheroid. So using the spheroid takes this into account, and thus will be more accurate. The alternative is to use false, which would use a sphere for the calculation, which is faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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