Geodjango:如何从点缓冲 [英] Geodjango: How to Buffer From Point
问题描述
我想进行基于半径的距离搜索。为此,我想在一个点对象周围创建一个缓冲区,以过滤其中的对象。
这是我要使用的位置:
>> lat = 37.7762179974
>> lon = -122.411562492
>>从django.contrib.gis.geos导入Point
>> pnt =点(纬度,经度)
>> buf = pnt.buffer(0.0001)
但是我在过滤时遇到问题事物
基于它们是否在缓冲区内:
>> z = Thing.objects.filter(pnt__intersects = buf)
(我知道上面的是不正确的,但是我用它来阐述我要做什么。)
如何在周围创建缓冲区指向
,然后过滤缓冲区
内部的事物
?
编辑: models.py
class Thing(models.Model):
lat = models.FloatField()
lon = models.FloatField()
如何根据由这两个模型字段组合而成的点进行过滤?
这显然不起作用,因为我的模型中没有 pnt
字段:
>> pnt =点(纬度,经度)
>> z = Thing.objects.filter(pnt__intersects = buf)
但是我该怎么做? / p>
PostGIS的方法称为 ST_MakePoint
可以创建2D,3D或4D点。
在一个上一个答案,我们看到的是可以从现有的PostGIS函数创建自定义数据库函数:
from django.contrib.gis.db.models.functions import GeoFunc
class MakePoint(GeoFunc):
function ='ST_MakePoint'
现在,我们可以通过 annotate()
ing 并在其上应用相交
:
z = Thing.objects。注释(pnt = MakePoint('lat','lon'))
.filter(pnt__intersects = buf)
<来自django.contrib.gis.db.models.functions的pre>
import GeoFunc
z = Thing.objects.annotate(pnt = GeoFunc(
F( 'lat'),F('lon'),
function ='ST_MakePoint'
).filter(pnt__intersects = buf)
注意:您可以考虑添加 pnt
在您的事物
模型中,请避免上述情况。
I want to have a radius based distance search. To do this, I want to create a buffer around a point object in order to filter objects that are inside it.
Here is where I am at with it:
>>> lat = 37.7762179974
>>> lon = -122.411562492
>>> from django.contrib.gis.geos import Point
>>> pnt = Point(lat, lon)
>>> buf = pnt.buffer(0.0001)
But I am having problems filtering the Thing
objects based on whether they are inside the buffer:
>>> z = Thing.objects.filter(pnt__intersects=buf)
(I know that the above is incorrect, but I use it to elaborate what I am trying to do.)
How can I create a buffer around the Point
and then filter Things
that are inside the buffer
?
EDIT: models.py
class Thing(models.Model):
lat = models.FloatField()
lon = models.FloatField()
How can I filter based on a point comprised of a combination of these two model fields?
This cannot work obviously, because I do not have a pnt
field in my model:
>>> pnt = Point(lat, lon)
>>> z = Thing.objects.filter(pnt__intersects=buf)
But how can I do something similar?
PostGIS has a method called ST_MakePoint
which can create a 2D, 3D or 4D point.
In a previous answer we saw that it is possible to create a custom database function from an existing PostGIS function:
from django.contrib.gis.db.models.functions import GeoFunc
class MakePoint(GeoFunc):
function='ST_MakePoint'
Now we can create a point by annotate()
ing and apply the intersects
on it:
z = Thing.objects.annotate(pnt=MakePoint('lat', 'lon'))
.filter(pnt__intersects=buf)
You can achieve the same effect by utilizing GeoFunc()
and F()
expressions:
from django.contrib.gis.db.models.functions import GeoFunc
z = Thing.objects.annotate(pnt=GeoFunc(
F('lat'), F('lon'),
function='ST_MakePoint'
).filter(pnt__intersects=buf)
Note: You could consider adding a pnt
field in your Thing
model and avoid the above.
这篇关于Geodjango:如何从点缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!