GeoDjango:基本地理查询的例外情况

时间:2012-10-27 16:37:38

标签: django geodjango geos

我在开发环境中使用SpatiaLite上运行的GeoDjango进行地理查询时出现问题。

from django.contrib.gis.db import models

class Test(models.Model):
    poly = models.PolygonField()
    point = models.PointField()
    geom = models.GeometryField()
    objects = models.GeoManager()

通过shell测试:

>>> from geotest.models import Test
>>> from django.contrib.gis.geos import GEOSGeometry
>>> 
>>> point = GEOSGeometry("POINT(0 0)")
>>> point
<Point object at 0x105743490>
>>> poly = GEOSGeometry("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))")
>>> poly
<Polygon object at 0x105743370>
>>> 

通过这些定义,我们可以尝试一些基本的地理查询。首先contains

>>> Test.objects.filter(point__within=poly)
Assertion failed: (0), function appendGeometryTaggedText, file WKTWriter.cpp, line 228.
Abort trap

django shell死了。并使用within

>>> Test.objects.filter(poly__contains=point)
GEOS_ERROR: Geometry must be a Point or LineString
Traceback (most recent call last):
...
GEOSException: Error encountered checking Coordinate Sequence returned from GEOS C function "GEOSGeom_getCoordSeq_r".
>>> 

其他组合也会导致各种异常。我必须遗漏一些明显的东西,因为这些是非常基本的。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的要求似乎是正确的。

您是否检查过您是否符合所有要求?

https://docs.djangoproject.com/en/1.4/ref/contrib/gis/install/#spatial-database

您是否正确初始化了spaceite数据库?

https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/spatialite/#creating-a-spatial-database-for-spatialite