我想为特定范围内的项目过滤查询集。这就是我的模型的样子
class modelEmployee(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
location = models.PointField(srid=4326,max_length=40, blank=True,null=True)
objects = GeoManager()
现在这是我运行过滤器命令的方式。要退回90英里范围内的物品。
qset = modelEmployee.objects.filter(location__distance_lte=(someLocation, D(mi=90)))
结果返回一个距离实际上为223.732英里的项目,不应返回。
这是两个项目的位置
location A - lat: 47.628641 and long: -117.402997
location B - lat: 47.618337 and long: -122.205341
两者的距离b / w实际上是223.732英里。我一定过滤错了。关于我可能要去哪里的任何建议?
答案 0 :(得分:6)
在文档geo spatial query中,您应该使用dwithin
您的示例应像这样使用它:
qset = modelEmployee.objects.filter(location__dwithin=(someLocation, D(mi=90)))
答案 1 :(得分:0)
来自Django文档distance_lte用于返回模型,其中查找几何图形到几何图形字段的距离大于或等于给定的距离值。
qset = modelEmployee.objects.filter(location__distance_lte =(someLocation, D(m = 90)))
您可以使用dwithin进行查询。
qset = model.modelEmployee.objects.filter(poly__dwithin =(geom,D(m = 5)))
答案 2 :(得分:0)
您可以尝试使用此ref(gis/db-api),希望它能按您想要的方式工作。
示例:
from django.contrib.gis.db import models
class SouthTexasCity(models.Model):
name = models.CharField(max_length=30)
# A projected coordinate system (only valid for South Texas!)
# is used, units are in meters.
point = models.PointField(srid=32140)
然后可以如下执行距离查询:
from django.contrib.gis.geos import GEOSGeometry
from django.contrib.gis.measure import D # ``D`` is a shortcut for ``Distance``
from geoapp.models import SouthTexasCity
# Distances will be calculated from this point, which does not have to be projected.
pnt = GEOSGeometry('POINT(-96.876369 29.905320)', srid=4326)
# If numeric parameter, units of field (meters in this case) are assumed.
qs = SouthTexasCity.objects.filter(point__distance_lte=(pnt, 7000))
# Find all Cities within 7 km, > 20 miles away, and > 100 chains away (an obscure unit)
qs = SouthTexasCity.objects.filter(point__distance_lte=(pnt, D(km=7)))
qs = SouthTexasCity.objects.filter(point__distance_gte=(pnt, D(mi=20)))
qs = SouthTexasCity.objects.filter(point__distance_gte=(pnt, D(chain=100)))