问题描述
我正在将GeoDjango与PostGIS一起使用.然后,我在如何查询postgres db表以获取5米范围内的所有数据方面遇到麻烦.
I am using GeoDjango with PostGIS. Then I am into trouble on how to query my postgres db table to get all data within a distance of 5 meters.
UPDATES1 我正在使用GeoDjango 1.2.7
UPDATES1I am using GeoDjango 1.2.7
我从此URL中找到了一些内容 https ://docs.djangoproject.com/en/dev/ref/contrib/gis/geoquerysets/#std:fieldlookup-distance_lte
I found something from this url https://docs.djangoproject.com/en/dev/ref/contrib/gis/geoquerysets/#std:fieldlookup-distance_lte
Zipcode.objects.filter( poly__distance_lte =( geom , D (* m * = 5) ))
Zipcode.objects.filter(poly__distance_lte=(geom, D(*m*=5)))
但是不知道如何准备参数和变量.
But don't know on how to prepare the parameter and variables.
- 什么是 poly_distance_lte ?是功能吗?
- 什么是宝石?是一个变量?如何创建它?
- 什么是 D ?是功能吗?如果是, m 是 D 函数的参数名称吗?
- what is poly_distance_lte? is a function?
- what is geom? is a variable? how to create it?
- what is D? is a function? if yes, m is a parameter name of D function?
推荐答案
通常,此类查询的最佳PostGIS功能是 ST_DWithin() :
In general, the best PostGIS function for such a query is ST_DWithin():
例如所有居住在#1商店1000米以内的客户:
eg. all customers that live within 1000 meters of shop #1:
SELECT customers.*
FROM customers, shops
WHERE ST_DWithin(customers.the_geog, shops.the_geog, 1000)
AND shop.id = 1
ST_DWithin将使用您应该创建的空间索引,因此其性能优于ST_Distance.
ST_DWithin will use the spatial index which you should have created and therefore outperform ST_Distance.
在Django中,似乎有一个名为 在 之内:
In Django there seems to be a corresponding filter called dwithin:
Zipcode.objects.filter(poly__dwithin=(geom, D(m=5)))
Backend SQL Equivalent
PostGIS ST_DWithin(poly, geom, 5)
D(m = 5)返回长度为5米的距离对象
D(m=5) returns a distance object of length 5 meters
geom是要用来计算到Zipcode对象的距离的几何
geom is the geometry from which you want to calculate distances to Zipcode objects
dwithin()是使用的函数
dwithin() is the function used
poly是Zipcode对象的几何属性
poly is the geometry attribute of Zipcode objects
z = Zipcode(code=77096, poly='POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))')
这篇关于如何查询5米范围内的所有数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!