Opened 5 years ago

Closed 5 years ago

#30649 closed New feature (wontfix)

Implement MakeValid() for non-column values.

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

Description (last modified by milosu)

PostGIS provides function ST_MakeValid, that can be used in Django as a PostGISOperator.

There is however no way how to use ST_MakeValid to simply repair GEOS Geometry.

Note that there is also no comparable function in the GEOS library itself (not counting something simple like buffer(0)).

This missing feature is also subject to Stackoverflow questions, like:
https://stackoverflow.com/questions/45631855/equivalent-of-postgis-st-makevalid-in-django-geos/45633779

Attached patch implements new PostGISOperations method called "make_valid", that can be run from implementation code simply by:

from django.db import connection

repaired_geos_geometry = connection.ops.make_valid(some_goes_geometry)

Attachments (1)

make_valid_patch.diff (3.2 KB ) - added by milosu 5 years ago.

Download all attachments as: .zip

Change History (7)

by milosu, 5 years ago

Attachment: make_valid_patch.diff added

comment:1 by milosu, 5 years ago

Description: modified (diff)

comment:2 by milosu, 5 years ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 5 years ago

Resolution: invalid
Status: newclosed
Summary: Implement geometry validation in GIS database backendImplement geometry validation in GIS database backend.

MakeValid is already implemented.

comment:4 by milosu, 5 years ago

Resolution: invalid
Status: closednew

St_MakeValid is implemented, but there is no way to call it on GEOS geometry.
See comments in:
https://stackoverflow.com/questions/45631855/equivalent-of-postgis-st-makevalid-in-django-geos/45633779

Last edited 5 years ago by milosu (previous) (diff)

comment:5 by Simon Charette, 5 years ago

The MakeValid function also supports Geometry objects so you could theoretically do

valid_geometry = AModel.objects.annotate(
    valid_geometry=MakeValid(invalid_geometry),
).values_list('valid_geometry', flat=True).first()

But that assumes AModel has at least one row.

The ORM doesn't support SELECT from no tables by it's table <-> model class mapping nature.

If we wanted to add a way to do that it we should do it for all non-column referencing expressions and not only for MakeValid.

e.g.

  • Expression.from_db() -> SELECT expression -> what's returned from the database.
  • MakeValid(invalid_geometry).from_db('other') -> SELECT ST_MakeValid(<Geometry>) -> <Valid Geometry>
  • Unaccent(Lower('ÉtÉ')).from_db() -> SELECT unaccent(lower('ÉtÉ')) -> 'ete'

This would have to be discussed on the developer mailing list and I wouldn't be surprised if there was existing tickets tracking this feature request.

Version 1, edited 5 years ago by Simon Charette (previous) (next) (diff)

comment:6 by Mariusz Felisiak, 5 years ago

Resolution: wontfix
Status: newclosed
Summary: Implement geometry validation in GIS database backend.Implement MakeValid() for non-column values.

If you want to propose to add a new API for all non-column expressions (as described by Simon) then please fill a new ticket (I couldn't find an existing ticket) that would have to be discussed on the developer mailing list.

IMO, adding a special behavior only for MakeValid() is not something that we would like to add into Django.

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