我正在搞乱Geodjango,我只是想在数据库中添加一个简单的多边形字段,然后在其上运行一个多边形点,以确保一切正常。
这是我在views.py中的代码:
#adding a polygon
pe = PolygonExample.objects.create(name="uk_polygon", poly="POLYGON((58.768200159239576, -12.12890625, 58.49369382056807 1.1865234375, 49.18170338770662 -12.9638671875, 50.2612538275847 5.537109375))" )
#doing the point-in-polygon check
result = PolygonExample.objects.filter(poly__contains='POINT(52.696361078274485 -0.87890625)')
这是我在models.py中的内容:
class PolygonExample(models.Model):
name = models.CharField(max_length=16, db_index=True)
poly = models.PolygonField()
objects = models.GeoManager()
但是当我尝试添加多边形(PolygonExample.objects.create)时,我收到一个错误:“检查从GEOS C函数”GEOSWKTReader_read“返回的Geometry时遇到错误。
我添加多边形的代码是错误的吗?我不确定我是否理解如何直接插入纬度/经度坐标。
或者这是GEOS安装错误吗?
感谢。
答案 0 :(得分:5)
您的WKT有一些问题:
测试英国各地的多边形应如下所示:
>>> wkt = "POLYGON((-12.12890625 58.768200159239576, 1.1865234375 58.49369382056807, 5.537109375 50.2612538275847, -12.9638671875 49.18170338770662, -12.12890625 58.768200159239576))"
>>> pe = PolygonExample.objects.create(name="uk_polygon", poly=wkt)
>>> result = PolygonExample.objects.filter(poly__contains='POINT(-0.87890625 52.696361078274485)') # note x,y order here, too
>>> result[0].name
u'uk_polygon'