I want to do spatial join to an OSM line data and GPS point data both in geographic coordinate system, the result which I want is a road layer(has a unique ID) with the attributes of of road and the point file. I don't want to use buffer: sp::over
(I get NA values). I tried using gDistance()
it only gives me the distance but does not join the attribute tables. I want to replicate NNJoin of QGIS in R since I can't use batch processing for NNJoin.
I don't think I am satisfied by the answer here https://stackoverflow.com/questions/47675571/r-spatial-join-between-spatialpoints-gps-coordinates-and-spatiallinesdatafra.
longitude <- c(10.86361, 10.96062, 10.93032, 10.93103, 10.93212)
latitude <- c(44.53355, 44.63234, 44.63470, 44.63634, 44.64559)
longlat <- data.frame(longitude, latitude)
my_spdf<- SpatialPoints(longlat, proj4string=crs("+init=epsg:4326"))
shapefile_roads<- spLines(cbind(longitude+.1, latitude), cbind(longitude-.1, rev(latitude)), cbind(longitude-.1, latitude+1), crs=crs("+init=epsg:4326"))
shapefile_roads <- spTransform(shapefile_roads, CRS("+proj=utm +zone=43 ellps=WGS84"))
my_spdf <- spTransform(my_spdf, CRS("+proj=utm +zone=43 ellps=WGS84"))
result <- over(shapefile_roads, my_spdf)
Best Answer
Here's a solution using
library(sf)
:Please, when posting a reproducible example, make sure that it does indeed run and also include the packages that need to be loaded in order to execute the code. We cannot know where functions come from if the execution fails! Also, if you say you want IDs, please make sure that your example includes them!