Php PostgreSQL选择查询(GPS)
问题强> 我有一个PostgreSQL数据库,我在上面保存汽车的GPS数据。现在我正在准备历史报告,我必须根据每辆车的速度显示两个不同的停车点,如果车速在5分钟内保持为0,如果超过5分钟,我必须显示停车。我每10秒从GPS上获取一次数据。有办法吗?感谢您的帮助 以下是表格结构:Php PostgreSQL选择查询(GPS),php,postgresql,gps,Php,Postgresql,Gps,问题 我有一个PostgreSQL数据库,我在上面保存汽车的GPS数据。现在我正在准备历史报告,我必须根据每辆车的速度显示两个不同的停车点,如果车速在5分钟内保持为0,如果超过5分钟,我必须显示停车。我每10秒从GPS上获取一次数据。有办法吗?感谢您的帮助 以下是表格结构: CREATE TABLE gps_data_abl_soft ( id bigint NOT NULL DEFAULT nextval('gps_data_id_seq'::regclass), device_id
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)
)
我通过创建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;
示例结果如下:
在我提供的样本数据中,没有速度为0的汽车。并且每10秒不接收一次数据。很抱歉:)您想要用PHP或PostgreSQL提供解决方案吗?如果可能的话,可以用PostgreSQL@ImClarky