I have a number of pedestrian trajectories (green) that I would like to snap to the nearest road (brown). The roads are downloaded from OpenStreetMap and have been dissolved by name with
CREATE TABLE dissolved AS
SELECT osm_roads.name, ST_Union(osm_roads.geom) FROM osm_roads GROUP BY osm_roads.name;
Now I am left with two tables – dissolved
and trajectories
My goal is first, to snap each trajectory to the nearest road and then, find the "mileage" walked along each road by
ST_LENGTH(ST_Intersection(dissolved.geom, snapped.geom))
How would I perform the first part of the query – snapping each individual trajectory to the nearest road – with ST_Snap
– and then group ST_LENGTH(ST_Intersection())
by road name?
Best Answer
In this case ST_SNAP will give you bad results because your pedestrian trajectories are too far from the OSM lines.
One solution is to create a buffer around your OSM line and check wich pedestrian trajectories are fully contains by those buffers. If it's not already done, your OSM lines need to be splitted on each node of the network.
The buffer size need to be large enough in order to include all the trajectories.
For example:
It's simple and work well BUT there is 2 disadvantages:
Some small roads, adjacent to your main trajectories could be fully contains by the buffer and will appear in the result.
If your guy make 2 time the same circle, you lose the path of the second circle.