[英]ST_EXTENT or ST_ENVELOPE in BigQuery?
I want the equivalent of ST_EXTENT
or ST_ENVELOPE
in BigQuery, but I can't find a way to make this query run:我想要 BigQuery 中的
ST_EXTENT
或ST_ENVELOPE
等价物,但我找不到运行此查询的方法:
SELECT REGEXP_EXTRACT(name, ', (..)') state
, ST_EXTENT(ARRAY_AGG(urban_area_geom)) corners
, COUNT(*) cities
FROM `bigquery-public-data.geo_us_boundaries.urban_areas`
GROUP BY state
The desired result of this query is a list of bounding boxes to cover all urban areas around the US, grouped by state.此查询的预期结果是一个边界框列表,以覆盖美国所有城市地区,按 state 分组。
I created a feature request to get a native implementation of ST_EXTENT().我创建了一个功能请求来获得 ST_EXTENT() 的本机实现。 Please add your votes and evidence of why you need this function so the team can prioritize and keep you informed of any developments:
请添加您的投票和您为什么需要此功能的证据,以便团队可以优先考虑并随时通知您任何进展:
In the meantime, the best solution I can offer:同时,我可以提供的最佳解决方案:
fhoffa.x.st_bounding_box()
: a naive bounding box UDF. fhoffa.x.st_bounding_box()
:一个简单的边界框 UDF。 Use it like this:像这样使用它:
SELECT REGEXP_EXTRACT(name, ', (..)') state
, fhoffa.x.st_bounding_box(ARRAY_AGG(urban_area_geom)).polygon
, COUNT(*) urban_areas
FROM `bigquery-public-data.geo_us_boundaries.urban_areas`
GROUP BY state
CREATE OR REPLACE FUNCTION fhoffa.x.st_bounding_box(arr ANY TYPE) AS ((
SELECT AS STRUCT *
, ST_MakePolygon(ST_GeogFromText(FORMAT('LINESTRING(%f %f,%f %f,%f %f,%f %f)',minlon,minlat,maxlon,minlat,maxlon,maxlat,minlon, maxlat))) polygon
FROM (
SELECT MIN(m.min_x) minlon, MAX(m.max_x) maxlon , MIN(m.min_y) minlat, MAX(m.max_y) maxlat
FROM (
SELECT
(SELECT AS STRUCT MIN(x) min_x, MAX(x) max_x, MIN(y) min_y, MAX(y) max_y FROM UNNEST(coords)) m
FROM (
SELECT ARRAY(
SELECT STRUCT(
CAST(SPLIT(c, ', ')[OFFSET(0)] AS FLOAT64) AS x,
CAST(SPLIT(c, ', ')[OFFSET(1)] AS FLOAT64) AS y
)
FROM UNNEST(REGEXP_EXTRACT_ALL(ST_ASGEOJSON(geog), r'\[([^[\]]*)\]')) c
) coords
FROM UNNEST(arr) geog
)
)
)
))
Notes:笔记:
Additional effort is needed to make it work with geometries that cross the -180 line.需要额外的努力才能使其适用于跨越 -180 线的几何图形。
Due to geodesic edges, the function result is not a true bounding box, ie ST_Covers(box, geom) might return FALSE.由于测地线边缘,函数结果不是真正的边界框,即 ST_Covers(box, geom) 可能返回 FALSE。
In the picture above I'm not expecting each state to be fully covered, just its urban areas.在上面的图片中,我不希望每个州都被完全覆盖,只是它的城市地区。 So the bounding box is correct if there's no urban area in those uncovered corners.
因此,如果那些未覆盖的角落中没有市区,则边界框是正确的。
The following polygon construction will give you exact "rectangles", but they become much more complex structures to work with.以下多边形构造将为您提供精确的“矩形”,但它们会变得更加复杂。
ST_GEOGFROMGEOJSON(
FORMAT('{"type": "Polygon", "coordinates": [[[%f,%f],[%f,%f],[%f,%f],[%f,%f],[%f, %f]]]}'
, minlon,minlat,maxlon,minlat,maxlon,maxlat,minlon,maxlat,minlon,minlat)
)
I'll be looking forward to your comments and suggestions.我将期待您的意见和建议。
Since September 27, 2021 BigQuery support ST_BOUNDINGBOX and ST_EXTENT自 2021 年 9 月 27 日起,BigQuery 支持ST_BOUNDINGBOX和ST_EXTENT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.