gpt4 book ai didi

php - PostgreSQL 选择查询 (GPS)

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:11 25 4
gpt4 key购买 nike

问题!

我有一个 PostgreSQL 数据库,用于保存汽车的 GPS 数据。现在我正在准备历史报告,我必须根据每辆车的速度向它们显示两种不同的状态,如果车速在 5 分钟内保持为 0,则首先停止,如果超过 5 分钟,则我必须显示 parking 位。我每 10 秒从 GPS 获取数据。有什么办法吗?任何帮助表示赞赏。

这是表结构:

CREATE TABLE gps_data_abl_soft
(
id bigint NOT NULL DEFAULT nextval('gps_data_id_seq'::regclass),
device_id bigint NOT NULL DEFAULT (-1),
keyword character varying(30),
coordinate geometry,
date_time timestamp without time zone NOT NULL DEFAULT now(),
message character varying(200) NOT NULL DEFAULT ''::character varying,
speed real NOT NULL DEFAULT 0.0,
angle real NOT NULL DEFAULT 0.0,
CONSTRAINT gps_data_pkey PRIMARY KEY (id)
)

Here is the sample data from my table:

最佳答案

我已经通过创建 PostgreSQL 函数解决了这个问题!

-- Function: stop_func(bigint)

-- DROP FUNCTION stop_func(bigint);

CREATE OR REPLACE FUNCTION stop_func(IN device_id_param bigint)
RETURNS TABLE(coordinate geometry, difference interval) AS
$BODY$
DECLARE
r record;
stop_records stop_record[];
speed_zero boolean;
prev_date_time timestamp;
coordinate geometry;
BEGIN

speed_zero:=false;

for r in SELECT g.coordinate, g.date_time, g.speed FROM gps_data_abl_soft g WHERE g.device_id=device_id_param ORDER BY g.date_time
LOOP
IF r.speed=0 THEN
IF NOT speed_zero THEN prev_date_time:=r.date_time; coordinate=r.coordinate; END IF;

speed_zero:=true;
ELSE
IF speed_zero THEN

stop_records=array_append(stop_records,(coordinate, r.date_time-prev_date_time)::stop_record);
END IF;

speed_zero:=false;

END IF;

END LOOP;
RETURN QUERY SELECT * FROM unnest(stop_records);



END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
ALTER FUNCTION stop_func(bigint)
OWNER TO dr;

这个函数的使用

 SELECT ST_AsGeoJSON(ST_Transform(ST_Multi(ST_Union(coordinate)), 4326)) AS coordinate,  
difference from stop_func(device_id, start_time, end_time) group by difference;

示例结果在这里:

enter image description here

关于php - PostgreSQL 选择查询 (GPS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45690185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com