postgresql 使用经纬度在给定半径内查找位置的密码查询

qv7cva1a  于 2023-08-04  发布在  PostgreSQL
关注(0)|答案(6)|浏览(214)

我正在尝试使用Apache AGE来存储地理信息。我有一个名为locations的表,其中包含以下列:
id:位置的唯一标识符name:位置latitude的名称:位置longitude的纬度:位置的经度我希望能够查询表,以查找给定位置的某个半径内的所有位置。我尝试使用以下Cypher查询,但它不起作用:

MATCH (location)
WHERE location.latitude > latitude - radius AND location.latitude < latitude + radius AND location.longitude > longitude - radius AND location.longitude < longitude + radius
RETURN location

字符串
查询未返回任何结果。我做错了什么?

mepcadol

mepcadol1#

如果radius小于或等于0,则查询将不返回任何东西,如果radius为正,则查询将返回所有节点,因为任何数量总是小于数量+正数并且大于数量-正数。
为了找到一个位置半径内的所有节点,首先需要找到或提供所需的位置,并找到在该节点半径内的其它节点。

nlejzf6q

nlejzf6q2#

我相信你忘了正确引用属性,你应该使用下面的查询:

MATCH (givenLocation {id: 1}), (location)
WHERE location.latitude > givenLocation.latitude - radius 
  AND location.latitude < givenLocation.latitude + radius 
  AND location.longitude > givenLocation.longitude - radius 
  AND location.longitude < givenLocation.longitude + radius
RETURN location

字符串
在更正后的查询中,我引入了一个新节点givenLocation,它的id属性设置为1,以表示某个给定的位置。您可以将1替换为所需的ID值或使用其他属性来获取所需的位置。
请注意,radius应该是一个特定的值,表示从给定位置到您要查找的其他位置的距离。
但是,我不认为它是这样工作的,我建议研究这个answer,它提供了一个计算来确定所需的距离。您可以使用该答案中提到的相同计算。

j8ag8udp

j8ag8udp3#

所有内容都没有正确引用。

MATCH (location)
WHERE location.latitude > latitude - radius AND location.latitude < latitude + 
radius AND location.longitude > longitude - radius AND location.longitude < 
longitude + radius
RETURN location

字符串
在您编写的查询中,没有对纬度或半径的引用,没有它编译器就无法解释。你必须明确地告诉它从哪里获取这些值,是从这种形式的位置表(location.radius)中获取,还是从其他表中获取这些值。
所以,检查这些东西,重写你的查询,克服这些弱点,它会工作得很好。

5f0d552i

5f0d552i4#

你给出的Cypher查询似乎缺乏对一些关键元素的引用,这些元素可能是问题的根源,纬度和半径在你写的查询中没有提到。没有它们,编译器无法读取问题。

js5cn81o

js5cn81o5#

根据我的理解,提供的查询缺少对变量latitudelongituderadius的正确引用。如果没有关于从何处获取这些值的显式说明,编译器就无法正确解释查询。要解决这个问题,您需要指定这些变量的来源,无论它们是来自location表(以location.radius的形式)还是从另一个表获得。
为了使查询正确工作,您应该重写它,清楚地指出这些变量的源并提供适当的引用。这样,编译器就可以正确地解释查询,并毫无问题地执行它。
我希望这将解决这个问题!

eaf3rand

eaf3rand6#

您可以使用半正矢公式,通过经度和纬度在给定半径内查找位置。下面是修改后的查询:

MATCH (location)
WHERE distance(point({latitude: location.latitude, longitude: location.longitude}), point({latitude: targetLatitude, longitude: targetLongitude})) <= radius
RETURN location

字符串
在这里,将 targetLatitudetargetLongituderadius 替换为您的值。

相关问题