【问题标题】:GeoDjango distance query with srid 4326 returns 'SpatiaLite does not support distance queries on geometry fields with a geodetic coordinate system.'带有 srid 4326 的 GeoDjango 距离查询返回“SpatiaLite 不支持对具有大地坐标系的几何字段的距离查询。”
【发布时间】:2016-09-02 07:27:31
【问题描述】:

我正在尝试从给定的纬度/经度半径 4 公里范围内获取附近的厨房。 我的空间后端是spatialite,设置是,

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.gis',
    'rest_framework',
    'oauth2_provider',
    'kitchen',
)

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.spatialite',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

这是我的模型

from django.contrib.gis.db import models
from django.contrib.gis.geos import Point

class Kitchen(models.Model):
    id = models.CharField(max_length=100,primary_key=True)
    #id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100,blank=False)
    address = models.CharField(max_length=1000, blank=True, default='')
    contact_no = models.CharField(max_length=100,blank=True, default='')
    location = models.PointField(srid=4326, geography=True, blank=True, null=True)
    objects = models.GeoManager()

我在 Django shell 中的查询是,

from kitchen.models import Kitchen
from django.contrib.gis import measure
from django.contrib.gis import geos

current_point = geos.fromstr('POINT(%s %s)' % (76.7698996, 17.338993), srid=4326)
Kitchen.objects.filter(location__distance_lte=(current_point, measure.D(km=4)))

返回以下值错误

SpatiaLite does not support distance queries on geometry fields with a geodetic coordinate system. Distance objects; use a numeric value of your distance in degrees instead.

在模型中设置不同的投影 srid(例如 3857、24381 等)会返回不正确的结果。 非常感谢这里的一些帮助。

【问题讨论】:

    标签: python django sqlite geodjango spatialite


    【解决方案1】:

    您的问题很可能是 SRID。似乎 spatialite 不支持对具有非投影坐标系的字段进行这种类型的距离查询。

    所以你走在正确的轨道上,但是只要你在模型定义中启用了geography=True,将 srid 设置为不同的值就不会产生任何影响。地理类型强制 srid 为 4326,如 django geography docs 中所述。

    因此请尝试将geography=False 和 srid 设置为您正在尝试的投影坐标系之一。

    【讨论】:

    • 使用srid=3857, geography=False,它返回的结果不正确(所有厨房)。
    猜你喜欢
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    相关资源
    最近更新 更多