我有一个名为 multipoint
的
geography列,其值如下所示:
{
"coordinates": [
[
-8.734592000000001e+01,
4.146811000000000e+01
],
[
-8.734592000000001e+01,
4.146811000000000e+01
],
[
-8.734592000000001e+01,
4.146811000000000e+01
],
[
-8.734592000000001e+01,
4.146811000000000e+01
],
[
-8.734592000000001e+01,
4.146811000000000e+01
]
],
"type": "MultiPoint"
}
如何将其转换为 LineString,以便我可以使用
[ST_LENGTH](https://docs.snowflake.com/en/sql-reference/functions/st_length)
获取长度?或者,有什么方法可以获取该点云表示的线的长度?
ST_MAKELINE
,但这需要两个地理参数。我每行只有一个地理对象。
这些是 2 个替代技巧:
ST_LENGTH()
。或者:
ST_MAKELINE()
,然后提取第一个元素,然后使用第一个元素与原始“MultiPoint”进行ST_MAKELINE()
。然后你就可以得到ST_LENGTH()
。不知何故,第二种选择在我的测试中运行得慢得多。与此同时,我要求 Snowflake 团队查看此案例,并希望支持数组作为
ST_MAKELINE()
的输入。
代码:
with data as (
select $$
{
"coordinates": [
[
1.734592000000001e+01,
1.146811000000000e+01
],
[
-8.734592000000001e+01,
4.146811000000000e+01
],
[
-8.734592000000001e+01,
4.146811000000000e+01
],
[
-8.734592000000001e+01,
4.146811000000000e+01
],
[
0,
0
]
],
"type": "MultiPoint"
}
$$ x)
select replace(x, 'MultiPoint', 'LineString') x2
, try_to_geography(x2) line
, st_length(line)
from data;
select try_to_geography(x) g, st_makeline(g, g) g2
, st_makeline(st_pointn(g2, 1), g) y, st_length(y)
from data;
如果多点是通过行聚合获得的,则可以使用 CTE 分别准备第一个点和轨道的其余部分并馈送到 ST_Makeline:
WITH numbered AS (
SELECT
ROW_NUMBER() OVER (ORDER BY timestamp) AS rownum,
ST_Point(longitude, latitude) AS point
FROM data
ORDER BY timestamp
),
first_point AS (
SELECT point
FROM numbered
LIMIT 1
),
no_first_point AS (
SELECT ST_Collect(point) track
FROM numbered
WHERE rownum <> 1
)
SELECT ST_Makeline(first_point.point, no_first_point.track)
FROM first_point, no_first_point
;