问题描述
我在 BigQuery 表中存储了 40,000 条道路线串,我想为它们找到所有唯一的交叉点。我在此处 (https://gis.stackexchange.com/questions/20835/identifying-road-intersections-using-postgis/151277#151277) 的 postGIS 中找到了如何执行此操作,但我无法在 BigQuery 中使用此代码,因为我无法按地理分组。
SELECT
ST_Intersection(a.geom,b.geom),Count(distinct a.gid)
FROM
roads as a,roads as b
WHERE
ST_touches(a.geom,b.geom)
AND a.gid != b.gid
GROUP BY
ST_Intersection(a.geom,b.geom)
[1]: https://gis.stackexchange.com/questions/20835/identifying-road-intersections-using-postgis/151277#151277
解决方法
我不明白为什么您需要汇总结果:
SELECT ST_Intersection(r1.geom,r2.geom)
FROM roads r1 JOIN
roads r2
ON ST_Touches(r1.geom,r2.geom) AND
r1.gid < r2.gid
,
您可以使用大部分相同的答案,但只需要将地理解析为 CTE 中的文本,然后将其转换回地理。
with geomandcount as (
SELECT
st_astext(ST_Intersection(a.geom,b.geom)) as the_geom,Count(Distinct a.geoid) as count
FROM
`roadways_table` as a,`roadways_table` as b
WHERE
ST_intersects(a.geom,b.geom)
AND a.geoid != b.geoid
group by st_astext(ST_Intersection(a.geom,b.geom))
)
select st_geogfromtext(the_geom) as the_geom,count
from geomandcount