Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
729 views
in Technique[技术] by (71.8m points)

gis - Equivalent of PostGIS ST_MakeValid in Django GEOS

Upon checking a polygon object's validity using Objects.polygon.valid, it thrown a GEOS_NOTICE: Self-intersection error.

I know this can be fixed by using the ST_MakeValid method of PostGIS.
I'm using Django 1.11 with GEOS support and can't find its equivalent in Django docs.

Is there any equivalent function for ST_MakeValid in Django?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Django version >= 1.10:

Exists the database method: MakeValid

Django version < 1.10:

You can create a custom database function by extending GeoFunc class which by itself extends the Func() class:

from django.contrib.gis.db.models.functions import GeoFunc

class MakeValid(GeoFunc):
    function='ST_MakeValid'

The MakeValid(field_name) applies the ST_MakeValid to the field with field_name.


Usage:

YourModel.objects.get(id=an_id).update(the_geom=MakeValid('the_geom'))

The following is an equivalent query using F() expression to execute the update:

YourModel.objects.get(id=an_id)
                 .update(the_geom=GeoFunc(
                     F('the_geom'), 
                     function='ST_MakeValid'
                 ))

Note: the_geom represents your geometry field (point, polygon, etc.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...