2017-08-15 78 views
2

的問題!PostgreSQL的選擇查詢(GPS)

我對我現在的儲蓄汽車的GPS數據的PostgreSQL數據庫。現在,我在準備歷史報告,我必須根據自己的速度,以顯示兩個不同的stutus到每一輛汽車,杉杉採空如果汽車速度保持0 5分鐘以內,如果超過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

我在其中設置有一個與速度0沒有汽車的樣本數據。並且數據不會每10秒收到一次。對不起,這個:) – Mukhammadsher

+0

你在PHP想一個解決方案,或在PostgreSQL的? – ImClarky

+0

如果可能的話在PostgreSQL的@ImClarky – Mukhammadsher

回答

0

我已經通過創建PostgreSQL的功能解決了這個問題嘍!此功能

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; 

樣品結果的

-- 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; 

用法是在這裏:

enter image description here