哪个SRID适用于在SpatiaLite数据库中添加和排序?

时间:2015-02-03 10:52:10

标签: django spatial geodjango spatialite

我正在尝试制作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]

如何让它们同时发挥作用?

1 个答案:

答案 0 :(得分:-1)

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

使用srid = 3857进行排序,但是当您添加位置时,请使用以下方法(来自this answer)将它们从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