【问题标题】:how to remove coordinates from geojson with less then 4 values如何从geojson中删除少于4个值的坐标
【发布时间】:2021-08-02 19:35:01
【问题描述】:

正如标题所说,我正在查询存储在 bigquery 中的共享单车数据 我能够提取数据并以正确的顺序排列以显示在路径图中。在数据中,只有开始和结束的经纬度协调,或者有时只开始经纬度,我如何删除少于 4 点的任何东西? 这是代码,我也仅限于选择

SELECT 
 routeID , 
 json_extract(st_asgeojson(st_makeline( array_agg(st_geogpoint(locs.lon, locs.lat) order by locs.date))),'$.coordinates') as geo

FROM 
 howardcounty.routebatches
where  unlockedAt between {{start_date}} and {{end_date}} 
cross join UNNEST(locations) as locs
GROUP BY routeID
order by routeID
limit 10

为了清晰起见,还包括了一个屏幕截图

【问题讨论】:

    标签: sql google-bigquery gis


    【解决方案1】:

    要在group by 之后应用条件,请使用having。对于一个简单的条件——路线是否至少有两个数据集? -- 可以使用这个查询:

    With dummy as (
        Select 1 as routeID,  [struct(current_timestamp() as date, 1 as lon, 2 as lat),struct(current_timestamp() as date, 3 as lon, 4 as lat)] as locations 
        Union all select 2 as routeID,  [struct(current_timestamp() as date, 10 as lon, 20 as lat)]
           )
    
    
    SELECT 
     routeID , count(locs.date) as amountcoord,
     json_extract(st_asgeojson(st_makeline( array_agg(st_geogpoint(locs.lon, locs.lat) order by locs.date))),'$.coordinates') as geo
    
    FROM 
     #howardcounty.routebatches
    dummy
    
    #where  unlockedAt between {{start_date}} and {{end_date}} 
    cross join UNNEST(locations) as locs
    GROUP BY routeID
    having count(locs.date)>1
    order by routeID
    limit 10
    

    对于更复杂的,嵌套的select 可以完成这项工作:

    Select *
    from (
     --- your code ---
    ) where length(geo)-length(replace(geo,"]","")) > 1+4
    

    JSON 在您的代码中转换为字符串。如果计算 ] 并在 JSON 末尾减去 1,则计算内部数组。

    【讨论】:

    • 对不起,我应该更清楚,我只能在这个数据集上选择
    • 感谢最后一部分完成了这项工作 --> where length(geo)-length(replace(geo,"]","")) > 1+4
    猜你喜欢
    • 2021-12-26
    • 2020-07-09
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    相关资源
    最近更新 更多