Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#25498 closed Cleanup/optimization (fixed)

PostGIS Distance lookups use ST_Distance_Sphere() and not ST_Distance()

Reported by: Bibhas C Debnath Owned by: nobody
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Bibhas C Debnath)

Currently the doc says that PostGIS uses ST_Distance() for distance lookups like distance_gte etc. But actually ST_Distance_Sphere() is used. We need to fix that on the doc.

The doc also says in the beginning -

... an optional third element, 'spheroid', may be included to tell GeoDjango to use the more accurate spheroid distance calculation functions on fields with a geodetic coordinate system (e.g., ST_Distance_Spheroid would be used instead of ST_Distance_Sphere).

But then ST_Distance_Sphere is not mentioned anywhere else.

Here is what currently is happening -

current_localities = Locality.objects.filter(centroid__distance_lte=(pnt, 1000))

results in -

(0.005) SELECT "map_locality"."id", "map_locality"."name", "map_locality"."slug", "map_locality"."centroid", "map_locality"."radius" FROM "map_locality" WHERE ST_Distance_Sphere("map_locality"."centroid", ST_GeomFromEWKB('\x0101000020e6100000865ad3bce3685340e9b7af03e7ec2940'::bytea)) <= 1000 LIMIT 21; args=(<django.contrib.gis.db.backends.postgis.adapter.PostGISAdapter object at 0x7fdcc9d320d0>, 1000)

Attaching a patch with the issue. I can send a PR.

Attachments (1)

doc_gis_distance.patch (1.9 KB ) - added by Bibhas C Debnath 9 years ago.
patch to rename st_distance to st_distance_sphere

Download all attachments as: .zip

Change History (11)

by Bibhas C Debnath, 9 years ago

Attachment: doc_gis_distance.patch added

patch to rename st_distance to st_distance_sphere

comment:1 by Bibhas C Debnath, 9 years ago

Has patch: set

comment:2 by Bibhas C Debnath, 9 years ago

Description: modified (diff)

comment:3 by Claude Paroz, 9 years ago

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

You are right in that the documentation is not up-to-date. However, it's a bit more complicated than that. The real function used depends on the type of the geometry field in the database. You can see the logic here: https://github.com/django/django/blob/master/django/contrib/gis/db/backends/postgis/operations.py#L39

If the field is geodetic (latitude/longitude), ST_Distance_Sphere is used by default (with spheroid as an option). If the field has projected coordinates, ST_Distance is used by default.

in reply to:  3 comment:4 by Bibhas C Debnath, 9 years ago

Replying to claudep:

If the field is geodetic (latitude/longitude), ST_Distance_Sphere is used by default (with spheroid as an option). If the field has projected coordinates, ST_Distance is used by default.

By geodetic do you mean a Point type geometry field using SRID 4326? In my example above, centroid is of type geometry(Point,4326).

Just trying to get a clear idea about it.

comment:5 by Claude Paroz, 9 years ago

Yes, 4326 is geodetic in the sense that coordinates (latitude/longitude) are measured in angles based on an ellispoid representation of the earth. On the other hand, projected systems are measured in meters or miles based on a projected plane of the earth (generally only accurate on limited areas).

comment:6 by Bibhas C Debnath, 9 years ago

So maybe we can say something like -

PostGIS: ST_Distance_Sphere(poly, geom) <= 5 when SRID 4326 is used
PostGIS: ST_Distance(poly, geom) <= 5 when any other projected projected coordinates are used

in 2 different rows. Or use a single row to say both of them.

Version 0, edited 9 years ago by Bibhas C Debnath (next)

comment:8 by Tim Graham, 9 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin
Type: BugCleanup/optimization

comment:9 by Claude Paroz <claude@…>, 9 years ago

Resolution: fixed
Status: newclosed

In 617b1a2:

Fixed #25498 -- Documented ST_Distance/ST_Distance_Sphere difference

Thanks Bibhas Debnath for the report and Tim Graham for the review.

comment:10 by Claude Paroz <claude@…>, 9 years ago

In 66319cc5:

[1.9.x] Fixed #25498 -- Documented ST_Distance/ST_Distance_Sphere difference

Thanks Bibhas Debnath for the report and Tim Graham for the review.
Backport of 617b1a21f from master.

Note: See TracTickets for help on using tickets.
Back to Top