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.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…