Opened 7 years ago

Closed 5 years ago

#28738 closed New feature (fixed)

Add support for PostGIS <-> operator

Reported by: Matthew Somerville Owned by: Francisco Couzo
Component: GIS Version: 1.11
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 Matthew Somerville)

https://postgis.net/docs/geometry_distance_knn.html – this function is so much quicker when looking up the nearest neighbour of an index, and it'd be great to be built into Django. For example, on a dataset of c. 1.8 million rows:

postcode = Postcode.objects
    .filter(location__distance_gte=(location, D(mi=0)))
    .distance(location)
    .order_by('distance')[0]

took 5 seconds, whereas:

postcode = Postcode.objects
    .annotate(centroid_distance=GeometryCentroidDistance('location', location))
    .filter(centroid_distance__gte=0)
    .distance(location)
    .order_by('centroid_distance')[0]

is pretty instantaneous.

(Where GeometryCentroidDistance is a Func subclass that sets function='', arg_joiner = '<-> ', output_field FloatField.)

(There is also <#> https://postgis.net/docs/geometry_distance_box.html)

Change History (7)

comment:1 by Matthew Somerville, 7 years ago

Description: modified (diff)

comment:2 by Tim Graham, 7 years ago

Summary: Addition of PostGIS <-> operatorAdd support for PostGIS <-> operator

Based on your description, I can't tell whether this is different from the TrigramDistance function?

comment:3 by Matthew Somerville, 7 years ago

The operator itself is the same, but the trigram code takes a string (which will be passed to Value() if not already a Value, so passed in strings aren't F() by default), whereas this operator would want to be a GeoFunc (a bit like Distance, I guess? Its return is the same as ST_Distance) taking geometries/expressions, transforming them to the same SRID and so on.

comment:4 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted

comment:5 by Francisco Couzo, 5 years ago

Owner: changed from nobody to Francisco Couzo
Status: newassigned

comment:6 by Tim Graham, 5 years ago

Has patch: set
Triage Stage: AcceptedReady for checkin

comment:7 by Tim Graham <timograham@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 0193bf8:

Fixed #28738 -- Added the GeometryDistance function.

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