With PostgreSQL 13.6
have a table of multiple GPS points already group by track_id
(HEX number not sequential), and from where ST_Makeline
is made to build a line.
Some of these tracks have a time gap between them bigger than a given value (say 10 minutes).
Need to split the track_id
where these gaps exits, to a track_id
(before 1st gap) and track_id+1
(after 1st gap) ... track_id+n
(after n_st gap).
the track_id
is a non-sequential large intervals HEX number, so no issues on adding a few additional units (1 to 2 gaps per track) to it.
The actual location geom
is irrelevant in the analysis, since the points in the gap can be close (time spent on same position) or far apart (loose of reception).
This is only to clean and organize data, before any further analysis.
table example:
track_id | geom | time |
---|---|---|
123 | aaaa | 10:01 |
123 | aaab | 10:02 |
123 | aaac | 10:05 |
123 | aaad | 10:45 |
123 | aaae | 10:47 |
123 | aaaf | 10:49 |
234 | aaab | 10:52 |
234 | aaac | 10:55 |
234 | aaaa | 10:58 |
to build, new column 'new_track_id`:
track_id | geom | time | new_track_id |
---|---|---|---|
123 | aaaa | 10:01 | 123 |
123 | aaab | 10:02 | 123 |
123 | aaac | 10:05 | 123 |
123 | aaad | 10:45 | 124 |
123 | aaae | 10:47 | 124 |
123 | aaaf | 10:49 | 124 |
234 | aaab | 10:52 | 234 |
234 | aaac | 10:55 | 234 |
234 | aaaa | 10:58 | 234 |
create table public.testtime
(id SERIAL primary key,
track_id varchar,
time1 time);
--Test data:
insert into public.testtime (track_id, time1)
values (123, '10:01:00'),
(123, '10:02:00'),
(123, '10:05:00'),
(123, '10:45:00'),
(123, '10:47:00'),
(123, '10:49:00'),
(234, '10:52:00'),
(234, '10:55:00'),
(234, '10:58:00');
EDIT:
to verify that time_dif
is bigger than treshold:
UPDATE table SET
chk_dif=(CASE WHEN time_dif>'00:10' THEN TRUE END)
to populate the row after the true : not working
UPDATE table SET
chk_dif2=(CASE WHEN chk_dif=TRUE THEN TRUE
WHEN chk_dif IS NULL THEN (SELECT LAG(chk_dif2 OVER (PARTITION BY track_id ORDER BY time))
ELSE FALSE END)
it copies the TRUE from chk_dif to chk_dif2, but not repeat the chk_dif2 on the next rows, has applicable.
to change the track_id (simplify for now, change the last digit to 'x' : is not more an HEX, and only allows for a gap) looking for better
UPDATE table SET
new_track_id =
CASE WHEN chk_dif2=TRUE THEN CONCAT(LEFT(track_id,LENGTH(track_id)),'X') END)
EDIT2:
the problem is on the second step, to copy the TRUE for the next rows, as applicable to the second part of the segment.