Snapping points to lines is a common task when dealing with points that have to be located over lines for some specific reason.
One of the most common applications is to locate trajectory data on a road network. As such, it is a georeferencing problem that aims to get enhanced locations of trajectories getting rid of their deviations by using road networks as reference.
This problem, also called point-to-curve matching, is one of the simplest approaches for map-matching algorithms, which can also include topology, probability and fuzzy logic, among others. A complete review of map-matching algorithms is given by (Quddus et al., 2007).
In terms of free and open source software for Geomatics (FOSS4G), libraries such as Java Topology Suite (JTS) and its port to C called GEOS provide classes and functions that can be used to snap points to lines. Some examples on how to use them are provided by P. Ramsey (using SQL and PostGIS functions), Marcello Benigno (using GRASS) and GeoTools User Guide.
However, the R package rgeos, which is the R interface to GEOS, still doesn’t provide those functions and one has to build his own ones. In the rest of this post I will show you my solution, which snaps the trajectory points to the nearest points lying on the road network.
Import trajectory data from http://ifgi.uni-muenster.de/~epebe_01/pm10/All4_1a.txt and ensure that both (road network and trajectory) have the same projected reference system (for distance calculation):
Define the functions nearestPointOnSegment, nearestPointOnLine, this way:
Finally, define the main function called snapPointsToLines:
Now you can use the snapPointsToLines function with the whole datasets:
Or you can establish a maximum distance (in this case in meters) to avoid snapping points that are farther apart:
You can also get rid of the original attributes and just get the coordinates of the snapped points and the id of their nearest line:
This is what I get executing the line 69 and calling the function plot (Red crosses are original trajectory points and green ones are snapped points):
A more detailed view (Red crosses are original trajectory points and blue ones are snapped points):
A basic way to improve the result is to pass a filtered road network, e.g., only main roads. Of course, it depends on the trajectory. This is the result of snapping the points to a filtered road network (using 100 meters as maximum distance):
As I’m still unfamiliar with R data structures, this solution could be improved in terms of performance. With these test datasets this code is almost twice slower than a PostGIS implementation.
- M. Quddus, W. Ochieng and R. Noland. Current map-matching algorithms for transport applications : State-of-the art and future research directions. In Elsevier, Kidlington, ROYAUME-UNI, editor, Transportation research. Part C, Emerging technologies, vol. 15, no5, pages 312-328, 2007.
I’m pleased to announce the functions snapPointsToLines, nearestPointOnSegment and nearestPointOnLine have been included into the R package called maptools (v. 0.8-16).
The functions can be called directly after importing maptools into the R session, this way: