Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
731 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...