[PostGIS] #5895: Distance Calculation Issue with `ST_DistanceSphere`

PostGIS trac at osgeo.org
Thu May 8 00:50:37 PDT 2025


#5895: Distance Calculation Issue with `ST_DistanceSphere`
-----------------------------+---------------------
 Reporter:  giovannicimolin  |      Owner:  pramsey
     Type:  defect           |     Status:  new
 Priority:  medium           |  Milestone:
Component:  postgis          |    Version:  3.4.x
 Keywords:                   |
-----------------------------+---------------------
 I’ve recently came across a weird behavior while doing distance-based
 matching in PostGIS.
 My stack is Django (and GeoDjango), but I found the issue to be on the
 response provided by the DB.

 == Problem description

 When dealing with some shapes, the distance returned by the
 `ST_DistanceSphere`/`ST_Distance` function is zero.

 Here’s a minimal reproducible example:

 === Query
 {{{
 SELECT ST_DistanceSphere(
     ST_GeomFromGeoJSON('{
         "type": "Polygon",
         "coordinates": [
             [
                 [ -93.751932270476971, 32.089230804932349 ],
                 [ -93.753830066356031, 32.089181143046332 ],
                 [ -93.753864556452115, 32.090802706180867 ],
                 [ -93.751940314782857, 32.09084654939128 ],
                 [ -93.751932270476971, 32.089230804932349 ]
             ]
         ]
     }'),
     ST_GeomFromGeoJSON('{
         "type": "Point",
         "coordinates": [ 52.41828388240291, 28.68513149020453 ]
     }')
 ) AS distance;
 }}}

 === Results

 With `ST_DistanceSphere`:

 {{{
  distance
 ----------
         0
 (1 row)
 }}}


 Same operation with `ST_DistanceSpheroid`: (correct!)

 {{{
       distance
 --------------------
  12391246.377484955
 (1 row)
}}}

 === Notes

 Applying `ST_MakeValid` or `ST_Buffer(pol, 0)` does not fix the issue.
 However, if I change the polygon winding, the result comes out correct!

 {{{
 SELECT ST_DistanceSphere(
     ST_GeomFromGeoJSON('{
         "type": "Polygon",
         "coordinates": [
             [
                 [ -93.751932270476971, 32.089230804932349 ],
                 [ -93.751940314782857, 32.09084654939128 ],
                 [ -93.753864556452115, 32.090802706180867 ],
                 [ -93.753830066356031, 32.089181143046332 ],
                 [ -93.751932270476971, 32.089230804932349 ]
             ]
         ]
     }'),
     ST_GeomFromGeoJSON('{
         "type": "Point",
         "coordinates": [ 52.41828388240291, 28.68513149020453 ]
     }')
 ) AS distance;

 Result:
      distance
 -------------------
  12370146.65163511
 (1 row)
 }}}

 But the polygon is incorrectly wound in this case and applying `ST_Buffer`
 inverts the coordinates and yields the wrong result:
 {{{
 SELECT ST_DistanceSphere(
     ST_Buffer(ST_GeomFromGeoJSON('{
         "type": "Polygon",
         "coordinates": [
             [
                 [ -93.751932270476971, 32.089230804932349 ],
                 [ -93.751940314782857, 32.09084654939128 ],
                 [ -93.753864556452115, 32.090802706180867 ],
                 [ -93.753830066356031, 32.089181143046332 ],
                 [ -93.751932270476971, 32.089230804932349 ]
             ]
         ]
     }'), 0),
     ST_GeomFromGeoJSON('{
         "type": "Point",
         "coordinates": [ 52.41828388240291, 28.68513149020453 ]
     }')
 ) AS distance;
  distance

  distance
 ----------
         0
 (1 row)
 }}}

 Could you help me with this issue?
 I'm I doing something wrong with the data or is it really a bug?
-- 
Ticket URL: <https://trac.osgeo.org/postgis/ticket/5895>
PostGIS <http://trac.osgeo.org/postgis/>
The PostGIS Trac is used for bug, enhancement & task tracking, a user and developer wiki, and a view into the subversion code repository of PostGIS project.


More information about the postgis-tickets mailing list