我将如何编写这个空间查询?

发布于 2024-11-16 13:04:16 字数 558 浏览 2 评论 0原文

我有以下表结构:

column names : cell    longitude latitude bcch   bsic
data types   : varchar double    double   double double 
Keys         : x       

所有小区

  • 我想知道彼此 10 公里范围内且
  • 具有相同 bcc​​h+bsic 的

满足上述要求的空间sql查询是什么?

由于我对postgis的理解有限,请随意以“使用此数据库表结构代替”开始你的答案,以便它更面向GIS(我相信存在点的概念而不是纬度/经度列)。我以前没有写过空间查询,正在考虑购买“PostGIS In Action”一书,但需要知道我想做的事情是否可行以及如何实现。

另外我想提一下,我知道如何在标准 sql 中做到这一点。我需要一个空间查询,因为大约有 10000 条记录,并且使用标准 sql 方法,我需要生成 10000*10000(每个单元格的所有其他单元格)记录,然后查询它们,这将是非常低效的。

I have the following table structure:

column names : cell    longitude latitude bcch   bsic
data types   : varchar double    double   double double 
Keys         : x       

I want to know all the cells which are

  • in 10 km range of each other AND
  • have the same bcch+bsic.

What would be the spatial sql query for the above requirement?

Due to my limited understanding of postgis feel free to start your answer with "use this database table structure instead" so that it is more GIS oriented (I believe there is a concept of points rather than lat/long colums). I haven't written spatial queries before and am considering buying the "PostGIS In Action" book but need to know if what I am trying to do is possible and how.

Additionally I would like mention that I know how to do it in standard sql. I need a spatial query because there are round about 10000 records and using a standard sql method I would need to generate 10000*10000 (all the other cells for each cell) records and then query them which would be highly inefficient.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吖咩 2024-11-23 13:04:16

丹尼斯,

那不是真的。要点索引在这里会有所帮助。

Basarat 我不太清楚你期望什么输出。这是针对每个单元格的查询
将返回 10 公里范围内的那些。首先,您要添加一个地理列,然后为其创建一个要点索引。 《PostGIS 实践》第一章对此进行了介绍。

假设您有一个名为 geog 的新列,您已在其中添加了要点索引。

那么你的查询将是

SELECT c.cell, array_agg(n.cell) As cells_close
FROM cells As c INNER JOIN cells As n ON ST_DWithin(c.geog, n.geog, 10000)
WHERE c.bsic = n.bsic  --other criteria go here
GROUP BY c.cell;

如果你不希望输出为数组 - 你可以这样做

array_to_string(array_agg(n.cell),',') As cell_comma_sep

Denis,

that is not true. A gist index would help here.

Basarat I'm not quite clear what output you expect. Here is a query that for each cell
would return those at that are within 10km. First you want to add a geography column and then create a gist index of it. That's covered in first chapter of PostGIS in Action.

So lets say you have this new column called geog that you have put a gist index on.

Then your query would be

SELECT c.cell, array_agg(n.cell) As cells_close
FROM cells As c INNER JOIN cells As n ON ST_DWithin(c.geog, n.geog, 10000)
WHERE c.bsic = n.bsic  --other criteria go here
GROUP BY c.cell;

If you don't want the output as an array -- you can do

array_to_string(array_agg(n.cell),',') As cell_comma_sep
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文