TransWikia.com

Remove points where user was stationary

Geographic Information Systems Asked by ArslanAnjum on February 1, 2021

I have a table where points are stored with recorded time. I am storing points with 1s interval. Now if the user stayed approximately at the same spot for 2 hours, a number of useless points are dumped in db. How to avoid this or how to convert delete all points except 2 points. 1, when user entered that location and 2, when he left that location ?

2 Answers

You should examine the track spatially, and many points on a straight line could be eliminated to reduce number of points. For example if you use PostgreSQL/PostGIS, the ST_Simplify/ST_SimplifyPreserveTopology functions can be used. If you store your track points by two longitude, latitude columns the point geometries should be created first, than concatenated to linestring and finally simplify it.

Let's suppose a table called gps_pos with track_id, lat, lon, gps_time columns in a PostGIS enabled database, then you should use the following SQL statement to create simplified track (using PostgreSQL 9.0+):

SELECT track_id,
     ST_Simplify(ST_MakeLine(ST_SetSRID(ST_MakePoint(longitude, latitude), 4326) ORDER BY gps_time), 0.0001) As newgeom
     FROM gps_pos
     GROUP BY track_id;

Be careful with the tolerance it should be in degrees (0.0001 is ~0.5 arc seconds ~10m on the surface of the Earth). It may be better to make the simplification in a projected coordinate system e.g. Web Mercator (EPSG:3857):

SELECT track_id,
     ST_Simplify(ST_MakeLine(ST_Transform(ST_SetSRID(ST_MakePoint(longitude, latitude), 4326) 3857) ORDER BY gps_time), 1) As newgeom
     FROM gps_pos
     GROUP BY track_id;

This case the tolerance (1) is given in meters (the unit depends on the used spatial reference system).

Answered by Zoltan on February 1, 2021

I imagine a simple lag & lead geometry proximity search that, when the tracks are at least slightly consistent along their paths, might work.

I didn't test and wrote this down out of my head, but this query ought to return all points that have near spatial duplicates as temporal neighbors. In theory, one point is obsolete (and should thus be in the return table) if both it's temporal lead and lag neighbors are very close ('duplicates').
I'm not sure, though, if this is not missing special (or even common) cases...or works as expected at all (have I overlooked something obvious?). If this works, it's easy to delete those then with a similar DELETE query.

WITH
  ll_geoms AS (
    SELECT <uid>,
           LAG(<geom>) OVER(PARTITION BY <track_id> ORDER BY <timestamp>) AS lag_geom,
           LEAD(<geom>) OVER(PARTITION BY <track_id> ORDER BY <timestamp>) AS lead_geom,
           <geom>
    FROM <your_table>
  )

SELECT <uid>,
       <geom>
FROM ll_geoms
WHERE ST_DWithin(lag_geom, <geom>, <tolerance>)
  AND ST_DWithin(lead_geom, <geom>, <tolerance>);

Note that <tolerance> will be treated as CRS units, i.e. degrees for e.g. EPSG:4326.

Try it maybe and report back?

Answered by geozelot on February 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP