我正在尝试创建位于较大多边形(蓝色多边形)内的方形随机多边形(红色框)的序列,给定较大多边形的几何形状。我怎样才能在大查询中实现这一点。我也
我尝试了
ST_MAKEENVELOP()
,但在BQ中没有找到,还有ST_XMIN和ST_XMAX
考虑以下方法 - 它应该是您了解确切用例的良好起点!
WITH input AS (
SELECT geo_code, city_geom, ST_BOUNDINGBOX(city_geom) box
FROM `bigquery-public-data.utility_us.us_cities_area`
WHERE geo_code = '51445'
), grid AS (
SELECT geo_code, city_geom,
box.xmin + x * (box.xmax - box.xmin) / num_cols AS x_min,
box.xmin + (x + 1) * (box.xmax - box.xmin) / num_cols AS x_max,
box.ymin + y * (box.ymax - box.ymin) / num_rows AS y_min,
box.ymin + (y + 1) * (box.ymax - box.ymin) / num_rows AS y_max
FROM input, UNNEST([STRUCT(20 AS num_cols, 20 AS num_rows)]),
UNNEST(GENERATE_ARRAY(0, num_cols - 1)) AS x, UNNEST(GENERATE_ARRAY(0, num_rows - 1)) AS y
)
SELECT ANY_VALUE(city_geom) city_geom, ST_UNION_AGG(smaller_box) boxes FROM (
SELECT geo_code, city_geom, smaller_box FROM (
SELECT geo_code, city_geom,
ST_GEOGFROM(FORMAT('POLYGON((%f %f, %f %f, %f %f, %f %f, %f %f))',
x_min, y_min, x_min, y_max, x_max, y_max, x_max, y_min, x_min, y_min)) AS smaller_box
FROM grid
)
WHERE ST_WITHIN(smaller_box, city_geom)
ORDER BY RAND()
LIMIT 10
)
GROUP BY geo_code
有输出
如下图所示