[Django] #28738: Addition of PostGIS <-> operator

7 views
Skip to first unread message

Django

unread,
Oct 24, 2017, 2:59:25 PM10/24/17
to django-...@googlegroups.com
#28738: Addition of PostGIS <-> operator
----------------------------------------------+------------------------
Reporter: Matthew Somerville | Owner: nobody
Type: New feature | Status: new
Component: GIS | Version: 1.11
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
----------------------------------------------+------------------------
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.)

--
Ticket URL: <https://code.djangoproject.com/ticket/28738>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 24, 2017, 3:00:21 PM10/24/17
to django-...@googlegroups.com
#28738: Addition of PostGIS <-> operator
------------------------------------+--------------------------------------

Reporter: Matthew Somerville | Owner: nobody
Type: New feature | Status: new
Component: GIS | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Description changed by Matthew Somerville:

Old description:

> 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.)

New description:

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)

--

--
Ticket URL: <https://code.djangoproject.com/ticket/28738#comment:1>

Django

unread,
Oct 24, 2017, 9:54:10 PM10/24/17
to django-...@googlegroups.com
#28738: Add support for PostGIS <-> operator
------------------------------------+--------------------------------------

Reporter: Matthew Somerville | Owner: nobody
Type: New feature | Status: new
Component: GIS | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------

Comment (by Tim Graham):

Based on your description, I can't tell whether this is different from the
[https://github.com/django/django/blob/d1317edad0a2908574d2a5b07383bfe58884628c/django/contrib/postgres/search.py#L217-L219
TrigramDistance] function?

--
Ticket URL: <https://code.djangoproject.com/ticket/28738#comment:2>

Django

unread,
Oct 25, 2017, 3:28:16 AM10/25/17
to django-...@googlegroups.com
#28738: Add support for PostGIS <-> operator
------------------------------------+--------------------------------------
Reporter: Matthew Somerville | Owner: nobody
Type: New feature | Status: new
Component: GIS | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------

Comment (by Matthew Somerville):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/28738#comment:3>

Django

unread,
Oct 25, 2017, 10:24:14 AM10/25/17
to django-...@googlegroups.com
#28738: Add support for PostGIS <-> operator
------------------------------------+------------------------------------
Reporter: Matthew Somerville | Owner: nobody
Type: New feature | Status: new
Component: GIS | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/28738#comment:4>

Django

unread,
Mar 17, 2019, 5:22:41 PM3/17/19
to django-...@googlegroups.com
#28738: Add support for PostGIS <-> operator
-------------------------------------+-------------------------------------
Reporter: Matthew Somerville | Owner:
| franciscouzo
Type: New feature | Status: assigned
Component: GIS | Version: 1.11

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by franciscouzo):

* owner: nobody => franciscouzo
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/28738#comment:5>

Django

unread,
Mar 20, 2019, 11:46:46 AM3/20/19
to django-...@googlegroups.com
#28738: Add support for PostGIS <-> operator
-------------------------------------+-------------------------------------
Reporter: Matthew Somerville | Owner: Francisco
| Couzo

Type: New feature | Status: assigned
Component: GIS | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* has_patch: 0 => 1
* stage: Accepted => Ready for checkin


Comment:

[https://github.com/django/django/pull/11091 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/28738#comment:6>

Django

unread,
Mar 20, 2019, 12:55:26 PM3/20/19
to django-...@googlegroups.com
#28738: Add support for PostGIS <-> operator
-------------------------------------+-------------------------------------
Reporter: Matthew Somerville | Owner: Francisco
| Couzo
Type: New feature | Status: closed
Component: GIS | Version: 1.11
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"0193bf874f08f21cdec628b8d80d261471bfe5fc" 0193bf8]:
{{{
#!CommitTicketReference repository=""
revision="0193bf874f08f21cdec628b8d80d261471bfe5fc"
Fixed #28738 -- Added the GeometryDistance function.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28738#comment:7>

Reply all
Reply to author
Forward
0 new messages