I need to use the roads to compare GPS positions to verify that they are no errors in the data.

I downloaded the map.osm from a site that have information of City of Rome and had it transformed to Spatial Lines in R.

src <- osmsource_file(file = "rome.osm")
bb <- center_bbox(41.9, 12.3, 1000, 1000)
rome <- get_osm(bb, src)
ways <- find(rome, way(tags(k == "highway")))
ways <- find_down(rome, way(ways))
ways <- subset(rome, ids = ways)
hw_lines <- as_sp(ways, "lines")  

So with the Spatial Lines Object I tried to use distm function but It's accused that the spatial object can't be used (not is in a suitable format).

There is another way using OSM to get information of only roads that I can compare with GPS data in R?

  • 1
    What's the distm function? Can you show what you tried and the error message? Are you trying to compute distances from some points to the nearest point on the road network? – Spacedman Nov 16 '16 at 23:09
  • distm is a function from package geosphere in R, it calculates the distance betweeen point to point or point to Spatial Object, returning a matrix in the second case. ` a <- distm(c(taxi$lat[1], taxi$lon[1]), hw_lines, fun = distHaversine)` The error says "Error in .pointsToMatrix(y) : points should be vectors of length 2, matrices with 2 columns, or inheriting from a SpatialPoints* object". as_sp from osmar package convert the OSM object loaded to a Spatial Object. > class(hw_lines) [1] "SpatialLinesDataFrame" attr(,"package") [1] "sp" – Fabio Jojima Nov 17 '16 at 11:57

I downloaded a dataset from the City of Rome using the osmar package. After that, I followed your query to get the desired highways and then build a random SpatialPoints object inside Rome bounding box retrieved data. I measured distances between points and lines using the function dist2line from the geosphere package.

Please, try the code below:

# Load libraries

# Define the spatial extend of the OSM data we want to retrieve
rome.box <- center_bbox(center_lon = 12.5450, center_lat = 41.8992, width =  2000, height = 2000)

# Download all osm data inside this area
api <- osmsource_api()
rome <- get_osm(rome.box, source = api)

# General plot

# Find highways
ways <- find(rome, way(tags(k == "highway")))
ways <- find_down(rome, way(ways))
ways <- subset(rome, ids = ways)

# SpatialLinesDataFrame object
hw_lines <- as_sp(ways, "lines")  

# Plot
spplot(hw_lines, zcol = "uid")

# Interactive view

# Make a random points dataset (like GPS)
gpsPoints <- spsample(x = hw_lines, n = 100, type = "random")

# Plot points
plot(hw_lines, xlab = "Lon", ylab = "Lat")
plot(gpsPoints, add = TRUE, pch = 19, col = "red")

# Distances between Higways and random points
distances <- dist2Line(p = gpsPoints, line = hw_lines)


| improve this answer | |
  • I tried to use api <- osmsource_api() but they said that the bbox have too many points so I tried to download a .osm file. I've used rome.osm from http://metro.teczno.com/#rome. I don't understand, in https://en.wikipedia.org/wiki/Rome says that Rome coordinates are 41°54′N 12°30′E and in OSM Rome says it's 41°889'N 12°48'E, yet you used a different coordinates for your rome.box. get_osm() have a different coordinates sistem? – Fabio Jojima Nov 20 '16 at 17:10
  • @FabioJojima I'm sorry, It was an error! I found "Rome" as a city in USA! I updated the answer. Please check if it now answer your question! – Guzmán Nov 20 '16 at 18:38
  • 1
    I found that the optional call to mapview didn't work because our Rcpp routines didn't support 'POSIX*' objects. Anyway, the issue is solved now on the GitHub develop branch and will be available with the next official update on CRAN. – fdetsch Nov 21 '16 at 10:42
  • 1
    I get an error executing get_osm(rome.box, source = api) saying Space required after the Public Identifier. There is an answer here link on how to fix it. – Jesper Hybel Jan 16 '19 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.