简体   繁体   English

BigQuery ST_DWITHIN 多点与多点

[英]BigQuery ST_DWITHIN multiple points with multiple points

So I've got N points to consider and M points for reference.所以我有N分要考虑,M分可供参考。 I'm trying to filter all the Points in N, that are within some radius R of any of the M points.我正在尝试过滤 N 中的所有点,它们在任何 M 点的某个半径 R 内。

For the following example, my-table-1 is a table with ids and single coordinate GEOGRAPHY entries对于以下示例,my-table-1 是具有 id 和单坐标 GEOGRAPHY 条目的表

Currently im only able to get this working for N to 1 Point using something like this:目前我只能使用这样的东西来获得 N 到 1 点的工作:

SELECT *
FROM 'my-table-1' as one
WHERE ST_DWITHIN(points, (SELECT points FROM 'my-table-1' WHERE id = '1234'), 10000)

So basically what im trying to accomplish is that same statement but using multiple points to compare.所以基本上我试图完成的是相同的陈述,但使用多个点进行比较。 What I want is basically this:我想要的基本上是这样的:

SELECT *
FROM 'my-table-1' as one
WHERE ST_DWITHIN(points, (SELECT points FROM 'my-table-1' WHERE id IN('1234', '2345', ...)), 10000)

But this results in a error because ST_DWITHIN can only compare 1 point to N points.但这会导致错误,因为 ST_DWITHIN 只能将 1 个点与 N 个点进行比较。 I tried looking into BQ LOOPS and ARRAYS but couldnt figure out a solution so far.我尝试研究 BQ LOOPS 和 ARRAYS 但到目前为止还没有找到解决方案。 The exact error im getting in the second statement is Scalar subquery produced more than one element我在第二个语句中得到的确切错误是Scalar subquery produced more than one element

Basically comparing all points with a set of specific points in the same table and check if they are within some radius around those基本上将所有点与同一张表中的一组特定点进行比较,并检查它们是否在这些点周围的某个半径范围内

I'm trying to filter all the Points in N, that are within some radius R of any of the M points.我正在尝试过滤 N 中的所有点,它们在任何 M 点的某个半径 R 内。

The way to work with two tables is via JOIN.使用两个表的方法是通过 JOIN。 Your query您的查询

SELECT *
FROM 'my-table-1' as one
WHERE ST_DWITHIN(points, (SELECT points FROM 'my-table-1' WHERE id IN ...), 10000)

Should become应该成为

SELECT one.id, one.points
FROM 'my-table-1' one JOIN 'my-table-1' two
ON ST_DWITHIN(one.points, two.points, 10000)
WHERE two.id IN ('1234', '2345', ...)

Note this will produce duplicate points, if some point from one is within 10km of several points in two .请注意,如果一个点的one点在two点的几个点的 10 公里范围内,这将产生重复的点。 So you'll probably want to remove duplicates, I would add GROUP BY for this (this assumes we have some unique column id ):所以你可能想要删除重复项,我会为此添加GROUP BY (假设我们有一些唯一的列id ):

SELECT id, ANY_VALUE(points) AS points
FROM (
  SELECT one.id, one.points
  FROM 'my-table-1' one JOIN 'my-table-1' two
  ON ST_DWITHIN(one.points, two.points, 10000)
  WHERE two.id IN ('1234', '2345', ...)
)
GROUP BY id

If I understand the case correctly I would probably:如果我正确理解这个案例,我可能会:

  • Create a buffer with radius R around the M points (ST_Buffer)在 M 点 (ST_Buffer) 周围创建一个半径为 R 的缓冲区
  • Union those geometries (ST_Union)联合这些几何图形(ST_Union)
  • Select from the N points using ST_DWithin on the N points and the union/buffer geometry Select 从 N 点使用 ST_DWithin 在 N 点和联合/缓冲区几何

This is from the top of my head, I can't construct the query in my mind though, but I think a single query should do it or you could throw a WITH statement there.这是我的想法,虽然我无法在脑海中构建查询,但我认为单个查询应该完成,或者您可以在那里抛出 WITH 语句。

Let me know if that works for you.让我知道这是否适合你。 I saw that for BigQuery you have no ST_Buffer, but you can use jslibs.turf.ST_BUFFER instead.我看到对于 BigQuery,您没有 ST_Buffer,但您可以改用 jslibs.turf.ST_BUFFER。

Edit: Damn, I just realized this is not a PG question, but a BQ question... Shame on me.编辑:该死的,我刚刚意识到这不是一个 PG 问题,而是一个 BQ 问题......我真丢脸。 Are those functions available there?这些功能在那里可用吗?

Here's an example PG query (tested):这是一个示例 PG 查询(已测试):

select n.id from "N_Points" as n, (select st_buffer(m.geom, 20000) as geom 
from "M_Points" m where m.id < 10) as p
where st_dwithin(n.geom, p.geom, 0);

where 20000 is the radius (R) in projection units and geom is the geometry/geography column.其中 20000 是投影单位的半径 (R),geom 是几何/地理列。 You can replace it with a join or use a "with" clause instead of that long select st_buffer.您可以将其替换为连接或使用“with”子句代替长 select st_buffer。 Looks like you don't even need to Union those geometries.看起来您甚至不需要合并这些几何图形。 :) You can use ST_Within too: :) 您也可以使用 ST_Within:

ST_Within(n.geom, p.geom)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM