Opened 9 years ago

Closed 8 years ago

#25605 closed Bug (fixed)

GIS functions don't work with left-hand-side expressions

Reported by: Sergey Fedoseev Owned by: Sergey Fedoseev
Component: GIS Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

geotest/models.py:

class Test(models.Model):
    point1 = models.PointField()
    point2 = models.PointField()

python manage.py shell

In [1]: %paste                                                                                                                                                               
from django.contrib.gis.db.models.functions import Distance
from django.contrib.gis.geos import Point

from geotest.models import Test

point1 = Point(0, 0, srid=4326)
point2 = Point(0, 1, srid=4326)
Test.objects.create(point1=point1, point2=point2)

## -- End pasted text --
Out[1]: <Test: Test object>

In [2]: print Test.objects.annotate(dist=Distance('point1', point2)).last().dist
111195.079735 m

In [3]: print Test.objects.annotate(dist=Distance(point1, 'point2')).last().dist
GEOS_ERROR: ParseException: Unknown type: 'POINT2'

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-aa91ca51a4df> in <module>()
----> 1 print Test.objects.annotate(dist=Distance(point1, 'point2')).last().dist

/home/sergey/dev/django/django/contrib/gis/db/models/functions.pyc in __init__(self, expr1, expr2, spheroid, **extra)
    239             self.spheroid = spheroid
    240             expressions += (self._handle_param(spheroid, 'spheroid', bool),)
--> 241         super(Distance, self).__init__(*expressions, **extra)
    242 
    243     def as_postgresql(self, compiler, connection):

/home/sergey/dev/django/django/contrib/gis/db/models/functions.pyc in __init__(self, expression, geom, *expressions, **extra)
     97                 geom = GEOSGeometry(geom)
     98             except Exception:
---> 99                 raise ValueError("This function requires a geometric parameter.")
    100         if not geom.srid:
    101             raise ValueError("Please provide a geometry attribute with a defined SRID.")

ValueError: This function requires a geometric parameter.

In [4]: print Test.objects.annotate(dist=Distance('point1', 'point2')).last().dist
GEOS_ERROR: ParseException: Unknown type: 'POINT2'

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-177990c7d19a> in <module>()
----> 1 print Test.objects.annotate(dist=Distance('point1', 'point2')).last().dist

/home/sergey/dev/django/django/contrib/gis/db/models/functions.pyc in __init__(self, expr1, expr2, spheroid, **extra)
    239             self.spheroid = spheroid
    240             expressions += (self._handle_param(spheroid, 'spheroid', bool),)
--> 241         super(Distance, self).__init__(*expressions, **extra)
    242 
    243     def as_postgresql(self, compiler, connection):

/home/sergey/dev/django/django/contrib/gis/db/models/functions.pyc in __init__(self, expression, geom, *expressions, **extra)
     97                 geom = GEOSGeometry(geom)
     98             except Exception:
---> 99                 raise ValueError("This function requires a geometric parameter.")
    100         if not geom.srid:
    101             raise ValueError("Please provide a geometry attribute with a defined SRID.")

ValueError: This function requires a geometric parameter.

Change History (8)

comment:2 by Claude Paroz, 9 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Tim Graham, 9 years ago

Patch needs improvement: set

comment:4 by Sergey Fedoseev, 9 years ago

Owner: changed from nobody to Sergey Fedoseev
Status: newassigned

comment:5 by Claude Paroz, 9 years ago

Summary: GeoDjango DB functions doesn't really work with expressionsGIS functions don't work with left-hand-side expressions

comment:6 by Sergey Fedoseev, 8 years ago

Patch needs improvement: unset

comment:7 by Sergey Fedoseev, 8 years ago

I closed #27529 as a duplicate.

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

Resolution: fixed
Status: assignedclosed

In bde86ce9:

Fixed #25605 -- Made GIS DB functions accept geometric expressions, not only values, in all positions.

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