【问题标题】:Check for big changes in an array of lon , lat pair检查 lon , lat 对数组中的重大变化
【发布时间】:2021-08-10 18:02:51
【问题描述】:

我是 sql 新手,想看看是否有可能在 lon 和 lat 的数组中找到差异。如何检查从当前位置到下一个位置的值是否有很大变化 例子: [-79.88523, 30.2566][-65.5523,25.256] 使用 BigQuery 并且仅限于选择

select geo from ( 
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
cross join UNNEST(locations) as locs
where  locs.date between {{start_date}} and {{end_date}} 
group by routeID 
order by routeID
limit 1000
)where length(geo)-length(replace(geo,"]","")) > 1+4

【问题讨论】:

    标签: google-bigquery gis


    【解决方案1】:

    使用lag 访问以前的数据行。但首先必须将字符串解析为浮点数:

    select *,lon-lon_old as diff_lon, lat-lat_old as diff_lat
    #RouteID , count(1)
    from (
    select *, 
        lag(lon) over (partition by RouteID order by row) as lon_old,
        lag(lat) over (partition by RouteID order by row) as lat_old
    from (
    select 
        row_number() over () as row ,RouteID,
        cast(split(data,",")[offset(0)] as float64) as lon, 
        cast(replace(split(data,",")[offset(1)],"]","") as float64) as lat
    from(
    select RouteID, split(substring(geo,3),"],[") as data from ( 
    SELECT 
    routeID ,  json_extract(st_asgeojson(st_makeline( 
    array_agg(st_geogpoint(locs.lon, locs.lat) order by 
    locs.date))),'$.coordinates') as geo,
    FROM 
    
    (select 1 as RouteID,  [struct(5 as lon,5 as lat, current_date() as date), struct(1,2,"2020-01-01") ] as locations UNION ALL 
    select 2 as RouteID,  [struct(15 as lon,15 as lat, current_date() as date), struct(1,2,"2020-01-01") ]
    )
    
    
    cross join UNNEST(locations) as locs
    #where  locs.date between {{start_date}} and {{end_date}} 
    group by routeID 
    order by routeID
    limit 1000
    )where length(geo)-length(replace(geo,"]","")) > 1+4
    ), unnest(data) data  
    )) 
    #where lon-lon_old>0.5 or lat-lat_old>0.5
    #group by 1
    

    一个更简单的例子是

    With dat as  (select [struct(-79.88523 as A, 30.2566 as B), struct(-65.5523,25.256)] as data )
    
    select *,value.A-old.A as diff_A
    from (
    select *, lag(value) over (order by row) as old
    from (
    select row_number() over () as row, value
    from dat, unnest (dat.data) as value
    )
    )
    

    【讨论】:

    • 感谢您的回复,我不能使用 with 并且仅限于 SELECT 仅在数据集上,如何仅使用 select 重写 bigquery 的代码?
    • 感谢分配,成功了。然而,这仅显示来自一条路线的一组坐标,我如何制作它以便它对所有其他 routeID 执行相同的操作?我还需要集成一个 if 语句来过滤掉任何比 0.5 有更大变化的东西
    • where 需要过滤而不是 if。在第一行注释* 并删除其他 cmets 行。该脚本适用于多个 routeID。
    猜你喜欢
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2012-01-08
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多