Django GEOS中的PostGIS ST_MakeValid等效项 [英] Equivalent of PostGIS ST_MakeValid in Django GEOS
问题描述
使用Objects.polygon.valid
检查多边形对象的有效性时,抛出了GEOS_NOTICE: Self-intersection
错误.
Upon checking a polygon object's validity using Objects.polygon.valid
, it thrown a GEOS_NOTICE: Self-intersection
error.
我知道可以使用PostGIS的ST_MakeValid
方法解决此问题.
我正在使用具有GEOS支持的Django 1.11,但在Django文档中找不到与之等效的版本.
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.
Django中的ST_MakeValid
是否有等效的功能?
Is there any equivalent function for ST_MakeValid
in Django?
推荐答案
Django版本> = 1.10:
存在数据库方法: MakeValid
Exists the database method: MakeValid
Django版本< 1.10:
您可以通过扩展 GeoFunc
类,该类本身扩展了 Func()
类:
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'
MakeValid(field_name)
将ST_MakeValid
应用于具有field_name
的字段.
The MakeValid(field_name)
applies the ST_MakeValid
to the field with field_name
.
用法:
YourModel.objects.get(id=an_id).update(the_geom=MakeValid('the_geom'))
以下是使用 F()
的等效查询表达式以执行更新:
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'
))
注意: the_geom
代表您的几何字段(点,多边形等)
Note: the_geom
represents your geometry field (point, polygon, etc.)
这篇关于Django GEOS中的PostGIS ST_MakeValid等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!