1. Introduction (you may skip this if you like)
In order to implement a road snapping feature in R for recorded positioning data, I first searched the internet for possible solutions to the problem:
While it seems this can be solved with fuzzyMM (using fuzzy logic), the project using that approach is unfortunately not active any more and, thus, the documentation does not include the kind of information that would be required to get started with the package itself.
Another solution could be using online geospatial APIs, e.g. Google's. However, the current database already includes way more datasets than what's included in the free package.
At last, I found maptool
's function snapPointsToLines
(short introduction about how the function works can be found here) and attempted to snap a couple of coordinates to lines provided by a shapefile.
Note: Because the GPS-data was recorded on cars mostly driving on major roads in Germany, I downloaded the respective shapefile (of Bavaria, i.e. Bayern) here (.shp.zip
).
2. The specific problem
Road snapping with snapPointsToLines()
generates quite confusing values with this code. The following picture shows a list of recorded longitude / latitude values (see pointsCoord
).
roadsBavaria = readOGR("~/roads.shp", "roads")
pointsCoord = getValuesFromPicture()
pointsSpatial = SpatialPoints(pointsCoord)
pointsSpatialCorrected = snapPointsToLines(pointsSpatial, roadsBavaria)
Although this reduced test case sets the value pointsSpatialCorrected
(see result at the end), it fails (code after that is not executed) with
Warning messages:
1: In RGEOSDistanceFunc(spgeom1, spgeom2, byid, "rgeos_distance") :
Spatial object 1 is not projected; GEOS expects planar coordinates
2: In RGEOSDistanceFunc(spgeom1, spgeom2, byid, "rgeos_distance") :
Spatial object 2 is not projected; GEOS expects planar coordinates
Note that this error occurs even when I apply the shapefile's projection on the spatialPoints
manually via
pointsSpatial = SpatialPoints(pointsCoord, proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
What am I doing wrong here?
3. Additional information
The resulting value of spatialPointsCorrected
as displayed by View()
Best Answer
The warning message is quite clear here:
It requires planar (Cartesian) coordinates, i.e. in meters, miles, etc. You however use polar reference system (WGS-1984).
You should re-project your SpatialPointDataFrame to a Cartesian projection of-your-choice (UTM is one option) and run the function again - it should fix this error. See this thread for additional info on re-projection to UTM: converting-latitude-and-longitude-points-to-utm
EDIT: Reprojecting to UTM zone = 32N, code and results
See the following code and image. Re-projecting correctly fixes the problem. The code using the data provided by the OP.
Here are the original points coordinates in meters:
and the coordinates of the snapped points:
I also exported shp files of these two objects using:
writeOGR(pointsSpatialCorrected, dsn = ".", layer = "correctedPTS.shp", driver = "ESRI Shapefile")
and produced this image in QGIS (It can be plotted in R as well usingplot
, but the roads takes too much time).