0

我正在尝试制作 geodjango 应用程序。我正在使用 SQLite 和 SpatialLite。我想添加商店,并能够从离我的位置最近到最远对它们进行排序。

在我的模型中,我有:

location = gis_models.PointField(srid=4326, blank=True, null=True)

然后添加作品,但按距离排序不起作用,我得到:

SQLite does not support linear distance calculations on geodetic coordinate systems.

当我有:

location = gis_models.PointField(srid=3857, blank=True, null=True)

添加不起作用,排序起作用,我得到:

geo_shop.location violates Geometry constraint [geom-type or SRID not allowed]

我该怎么做才能让它们同时工作?

4

1 回答 1

-1

添加位置时出错与 srid 不匹配有关。

使用 srid=3857 进行排序,但是当您添加位置时,请使用以下方法(来自此答案)将它们从 4326 转换为 3857:

>>> from django.contrib.gis.gdal import SpatialReference, CoordTransform
>>> from django.contrib.gis.geos import Point
>>> gcoord = SpatialReference(4326)
>>> mycoord = SpatialReference(22186)
>>> trans = CoordTransform(gcoord, mycoord)

>>> pnt = Point(30, 50, srid=4326)
>>> print 'x: %s; y: %s; srid: %s' % (pnt.x, pnt.y, pnt.srid)
x: 30.0; y: 50.0; srid: 4326
>>> pnt.transform(trans)
>>> print 'x: %s; y: %s; srid: %s' % (pnt.x, pnt.y, pnt.srid)
x: 11160773.5712; y: 19724623.9117; srid: 22186
于 2015-10-20T19:08:37.013 回答