1

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 ?

Vince
  • 20,017
  • 15
  • 45
  • 64
res1
  • 113
  • 3
  • 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 Oct 19 '22 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 Oct 19 '22 at 15:12
  • Well, only if the field is geography or the index is on Geography(geom) https://gis.stackexchange.com/questions/247113/setting-up-indexes-for-postgis-distance-queries/247131#247131 – Vince Oct 19 '22 at 16:07

1 Answers1

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.

HeikkiVesanto
  • 16,433
  • 2
  • 46
  • 68