【问题标题】:PostgreSQL select query (GPS)PostgreSQL 选择查询 (GPS)
【发布时间】:2017-08-17 16:21:42
【问题描述】:

问题!

我有一个 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 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:

【问题讨论】:

  • 在我提供的样本数据中,没有速度为 0 的汽车。并且每 10 秒都没有收到数据。对此感到抱歉:)
  • 您想要使用 PHP 还是 PostgreSQL 的解决方案?
  • 如果可能在 PostgreSQL 中@ImClarky

标签: php postgresql gps


【解决方案1】:

我自己通过创建 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;

示例结果在这里:

【讨论】:

    猜你喜欢
    • 2012-03-05
    • 2018-12-13
    • 1970-01-01
    • 2021-12-17
    • 2012-11-22
    • 1970-01-01
    • 2014-07-28
    • 2018-01-11
    • 2017-04-30
    相关资源
    最近更新 更多