如何查询5米范围内的所有数据? [英] How can I query all my data within a distance of 5 meters?

查看:106
本文介绍了如何查询5米范围内的所有数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将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

UPDATES1 I 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.

  1. 什么是 poly_distance_lte ?是功能吗?
  2. 什么是宝石?是一个变量?如何创建它?
  3. 什么是 D ?是功能吗?如果是, m D 函数的参数名称吗?
  1. what is poly_distance_lte? is a function?
  2. what is geom? is a variable? how to create it?
  3. what is D? is a function? if yes, m is a parameter name of D function?

推荐答案

通常,此类查询的最佳PostGIS功能是

In general, the best PostGIS function for such a query is ST_DWithin():

如果几何之间的指定距离内,则返回true.

Returns true if the geometries are within the specified distance of one another.

例如所有居住在#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:

返回其中距查找几何图形到几何图形字段的距离在给定距离内的模型.

Returns models where the distance to the geometry field from the lookup geometry are within the given distance from one another.

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米范围内的所有数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆