Opened 6 years ago

Closed 5 years ago

Last modified 4 years ago

#10594 closed (fixed)

GeoQuerySet methods should filter out NULL values by default

Reported by: whiteinge Owned by: jbronn
Component: GIS Version: master
Severity: Keywords: distance queryset
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Given the following GIS model:

    class SomeModel(models.Model):
        location = models.PointField(null=True)
        <snip>

When populated with instances that both have and do not have location information, doing a distance(geom) query yields unexpected results.

For example, SomeModel.objects.distance(somePoint) returns no results at all. SomeModel.objects.filter(location__isnull=False).distance(somePoint) works as expected.

IMO, distance(geom) results that cannot calculate distance should simply return None.

Attachments (2)

fix_null_geometry_querysets.diff (610 bytes) - added by yourcelf 6 years ago.
Prevent null geometry fields from resulting in empty querysets
fix_null_geometry_querysets_with_tests.diff (3.0 KB) - added by yourcelf 6 years ago.
Updated diff that includes tests

Download all attachments as: .zip

Change History (7)

Changed 6 years ago by yourcelf

Prevent null geometry fields from resulting in empty querysets

comment:1 Changed 6 years ago by yourcelf

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

The problem seems to arise from line 49 in django/contrib/gis/measure.py

            if not isinstance(value, float): value = float(value)

Where "value" is the value of the geometry field. If the field is null, this becomes None, and propagates a ValueError up the stack resulting in an empty queryset.

I'm attaching a patch which fixes this by altering the method which constructs geometry objects out of the measurement attributes to instead keep the attribute as "None" if the value is None.

Changed 6 years ago by yourcelf

Updated diff that includes tests

comment:2 Changed 6 years ago by jbronn

  • milestone set to 1.2
  • Owner changed from nobody to jbronn
  • Status changed from new to assigned
  • Summary changed from distance(geom) results ambiguous when the PointField can be null to GeoQuerySet methods should filter out NULL values by default
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 5 years ago by jbronn

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [12885]) Fixed #10594 -- GeoQuerySet measurment methods no longer crash on geometry fields with NULL values. Thanks, whiteinge for the bug report and yourcelf for the initial patch.

comment:4 Changed 5 years ago by jbronn

(In [12886]) [1.1.X] Fixed #10594 -- GeoQuerySet measurment methods no longer crash on geometry fields with NULL values. Thanks, whiteinge for the bug report and yourcelf for the initial patch.

Backport of r12885 from trunk.

comment:5 Changed 4 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

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