简体   繁体   English

BigQuery 中的 ST_EXTENT 或 ST_ENVELOPE?

[英]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_EXTENTST_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

在此处输入图片说明

在此处输入图片说明 The code behind it:它背后的代码:

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_BOUNDINGBOXST_EXTENT

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

相关问题 如何在 BigQuery 中使用 ST_UNION - How to use ST_UNION in BigQuery 如何提高 BigQuery 中 ST_INTERSECT 的性能? - How to improve performance of ST_INTERSECT in BigQuery? ST_GeogFromGeoJSON 在 bigquery 中失败而在 postgres 中成功 - ST_GeogFromGeoJSON fails in bigquery while successful in postgres Presto SQL left joining using ST_intersects, ST_crosses 产生意想不到的结果 - Presto SQL left joining using ST_intersects, ST_crosses yield unexpected results ST_MAKEPOLYGON 逆 function - ST_MAKEPOLYGON inverse function 仅将 sim 卡插入第一个插槽时如何获取 otp? - how to get otp when sim is inserted in 1st slot only? ST_Intersects 上的 Full Outer Join 会出错,但有时会起作用 - Full Outer Join on ST_Intersects gives error but worked sometimes Aws Glue Crawler 在第一次爬网后没有更新表 - Aws Glue Crawler is not updating the table after 1st crawl 如何从 Streamlit 应用程序中保存在云存储中的文件的下拉菜单中使用 st.selectbox() 到 select 文件? - How to use st.selectbox() to select file from drop down menu from files saved in cloud storage in Streamlit app? 如何通过 AWS S3 删除 CSV 文件的第一行 - How can I remove the 1st line of a CSV file which is in AWS S3 through
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM