【发布时间】:2020-10-17 19:26:30
【问题描述】:
我是 UDF 函数的新手,我创建了一个 BigQuery UDF,它采用多边形几何并用它创建点。我正在尝试绘制点密度图(将多边形+人口数转换为点)。我改编了this blog post 的代码。因为 bigQuery 没有记录变量的方法,所以我一直在测试 in this codepen。
我正处于该功能似乎正常工作的地步。输出是点的几何集合。它在 bigquery 文档中说 st_geogfromgeojson 可以接受几何集合。
我的 UDF 返回一个字符串化的几何集合。
但我不知道为什么st_geogfromgeojson 不起作用。我不知道我是否只是没有取消嵌套或什么。
CREATE TEMP FUNCTION myFunc(feature string, ethnicity_column FLOAT64, year INT64)
RETURNS string
LANGUAGE js
OPTIONS (
library=["https://storage.googleapis.com.../d3.js","https://storage.googleapis.com/.../turf.min.js","https://storage.googleapis.com/.../wellknown.js"]
)
AS
"""
if (feature === undefined || feature === null) return;
var feature_parsed = wellknown.parse(feature)
const bounds = turf.bbox(feature_parsed);
const populationData = Math.round(ethnicity_column / 10);
if (!populationData) return;
const x_min = bounds[0];
const y_min = bounds[1];
const x_max = bounds[2];
const y_max = bounds[3];
let hits = 0;
let count = 0;
const limit = populationData * 10; // limit test to 10x the population.
let points = [];
while (hits < populationData - 1 && count < limit) {
const lat = y_min + Math.random() * (y_max - y_min);
const lng = x_min + Math.random() * (x_max - x_min);
const randomPoint = turf.point([lng, lat]);
if (turf.booleanPointInPolygon(randomPoint, feature_parsed)) {
points.push(randomPoint);
hits++;
}
count++;
}
return JSON.stringify((turf.geometryCollection(points)));
// return JSON.stringify(points)
""";
SELECT ST_GEOGFROMGEOJSON(JSON_EXTRACT((myFunc(st_astext(geom), white_pop, 2018)),'$')) FROM `myteam.kyle_data.blockgroups_with_acs`
我愿意接受所有建议。为简单起见,我返回一个字符串,但也许我需要使用 STRUCT。也许我应该从创建积分中删除草皮?我一定在这里遗漏了什么。
【问题讨论】:
-
你能发布 SELECT (myFunc(st_astext(geom), white_pop, 2018)) FROM
myteam.kyle_data.blockgroups_with_acs的输出吗? -
没问题:
{"type":"Feature","properties":{},"geometry":{"type":"GeometryCollection","geometries":[{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[-122.54238874622482,48.90183791377525]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[-122.53394002893639,48.869685080847184]}},{"type":"Feature","properties":{},"geometry": -
这有点奇怪。在顶部你有 GeometryCollection 的 Feature,这很好。但是 GeometryCollection 的几何字段包含特征,它应该包含根据 GeoJson RFC 的几何。我认为 BigQuery 错误来自 GeometryCollection 中的嵌套功能。
-
@MichaelEntin 是的,我很困惑。通过这样做,我学到了很多关于 bigQuery udfs 的知识,但是是的……不知道该尝试什么。我可以简单地避免所有这些并只返回所示的点数组吗? codepen.io/kpennell/pen/WNxwzxo?editors=0010 然后只是从特征中解析出几何图形?似乎草皮在这里做错了什么。也许我应该删除它。
标签: sql google-bigquery user-defined-functions