从Line中提取n个坐标-Bigquery

时间:2019-02-19 20:41:53

标签: sql google-bigquery

我想从大查询中获得的地理对象(一条线)中提取n个坐标(纬度,经度)。

是否可以指定我要获取多少st_geogpoint? (可以大于或小于用于创建带有st_geogpoint的行的st_makeline

示例:

LINESTRING(-115.2893119 36.218517, -115.2892195 36.2184946, -115.2879825 36.2184996, -115.2871506 36.2185021, -115.2870766 36.2185255)  

我希望能够从该行中提取 n (其中n> = 2)st_geogpoint。 有可能吗?

如果n = 2,则期望输出

[POINT(-115.2893119 36.218517), POINT(-115.2870766 36.2185255)]

如果n = 10

,则期望输出
[POINT(-115.2893119 36.218517),
POINT_2,
POINT_3,
POINT_4,
POINT_5,
POINT_6,
POINT_7,
POINT_8,
POINT_9,
POINT(-115.2870766 36.2185255) ]

我无法给出第一个点与最后一个点之间的点的示例,因为我希望它们是根据 n

的值从LINE中提取的

1 个答案:

答案 0 :(得分:3)

以下是用于BigQuery标准SQL

#standardSQL
SELECT 
  SAFE.ST_GEOGFROMTEXT(CONCAT('POINT(', point, ')')) geo_object
FROM `project.dataset.table`,
UNNEST(REGEXP_EXTRACT_ALL(ST_ASTEXT(geo_object), r'[^,\(\)]+')) point WITH OFFSET pos
WHERE pos BETWEEN 1 AND 3

您可以使用问题中的示例数据来进行测试,如上示例所示

#standardSQL
WITH `project.dataset.table` AS (
  SELECT ST_GEOGFROMTEXT('LINESTRING(-115.2893119 36.218517, -115.2892195 36.2184946, -115.2879825 36.2184996, -115.2871506 36.2185021, -115.2870766 36.2185255)') geo_object
)
SELECT 
  SAFE.ST_GEOGFROMTEXT(CONCAT('POINT(', point, ')')) geo_object
FROM `project.dataset.table`,
UNNEST(REGEXP_EXTRACT_ALL(ST_ASTEXT(geo_object), r'[^,\(\)]+')) point WITH OFFSET pos
WHERE pos BETWEEN 1 AND 3   

有结果

Row geo_object   
1   POINT(-115.2893119 36.218517)    
2   POINT(-115.2892195 36.2184946)   
3   POINT(-115.2879825 36.2184996)     

显然,通过在下面的行中调整1和3,您可以控制要输出的点数

WHERE pos BETWEEN 1 AND 3   

如果您期望输出与数组在同一行中-参见下文

#standardSQL
WITH `project.dataset.table` AS (
  SELECT ST_GEOGFROMTEXT('LINESTRING(-115.2893119 36.218517, -115.2892195 36.2184946, -115.2879825 36.2184996, -115.2871506 36.2185021, -115.2870766 36.2185255)') geo_object
)
SELECT 
  ARRAY(
    SELECT SAFE.ST_GEOGFROMTEXT(CONCAT('POINT(', point, ')'))
    FROM UNNEST(REGEXP_EXTRACT_ALL(ST_ASTEXT(geo_object), r'[^,\(\)]+')) point WITH OFFSET pos
    WHERE pos BETWEEN 1 AND 3  
    AND NOT point IS NULL
  ) geo_objects
FROM `project.dataset.table`

这个产生数组

Row geo_objects  
1   POINT(-115.2893119 36.218517)    
    POINT(-115.2892195 36.2184946)   
    POINT(-115.2879825 36.2184996)