0

I want to compute distances from a data frame source to a data frame destination in R. Both data frames have coordinates but my code returns the following error:

Error: "src" should contain coordinates.

Below is my code

randompoints_df <- randompts %>% 
  as.data.frame()

bus_stops_df <- bus_stops %>% 
  dplyr::select(id = stop_name, stop_lon, stop_lat) %>% 
  as.data.frame()

t0 <- Sys.time()

distancetable <- osrm::osrmTable(src = randompoints_df, dst = bus_stops_df)

I tried selecting only the id, lon and lat columns in my data to ensure it had coordinates but the src parameter still brought the same error. Any help in fixing this would be appreciated.

  • 3
    Hi, please see [how to make a great R reproducible example](https://stackoverflow.com/q/5963269/10264278) and edit your post accordingly. This will help us idendify your problem. As a first guess, the dataframe `randompoints_df` does not contains coordinates in the expected format. – Paul Jan 30 '23 at 07:58
  • [Error indcates](https://github.com/riatelab/osrm/blob/c4e8d9f096c3d292eb7463f25767a4fadff0468a/R/utils.R#L92) that your src does not comply with `ncol(x) == 2 && is.numeric(x[,1]) && is.numeric(x[,2])` test. Are there exactly 2 columns, both being numeric? – margusl Jan 30 '23 at 11:29
  • @margusl There are three columns: bus stop names in letters, longitude coordinates in numeric and latitude coordinates in numeric. – Jjnjoroge Jan 30 '23 at 11:53
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jan 30 '23 at 21:39

1 Answers1

0

osrmTable() accepts sf & sp objects or dataframes and matrices with exactly 2 columns, WGS84 lon & lat.

library(sf)
library(osrm)

# some sample data
stops <- read.csv(text = "lon,lat,name
                          24.7653725,59.4426444,Reisisadama D-terminal
                          24.7514720,59.4438342,Linnahall
                          24.7601144,59.4446286,Reisisadama A-terminal
                          24.7655886,59.4425595,Reisisadama D-terminal
                          24.7696412,59.4413391,Uus-Sadama
                          24.7593333,59.4401405,Siimeoni",header = T,  )

# 3 columns, numeric lon/lat 
str(stops)
#> 'data.frame':    6 obs. of  3 variables:
#>  $ lon : num  24.8 24.8 24.8 24.8 24.8 ...
#>  $ lat : num  59.4 59.4 59.4 59.4 59.4 ...
#>  $ name: chr  "Reisisadama D-terminal" "Linnahall" "Reisisadama A-terminal" "Reisisadama D-terminal" ...

# osrmTable() with 3-column tables - fails
osrmTable(stops[1:3,], stops[4:6,])
#> Error: "src" should contain coordinates.

# osrmTable() with 2-column tables - OK
osrmTable(stops[1:3,c("lon", "lat")], stops[4:6,c("lon", "lat")])
#> $durations
#>     4   5   6
#> 1 0.0 1.1 1.0
#> 2 3.1 3.0 2.3
#> 3 3.1 3.1 2.3
#> 
#> $sources
#>        lon      lat
#> 1 24.76541 59.44259
#> 2 24.75145 59.44378
#> 3 24.76006 59.44473
#> 
#> $destinations
#>        lon      lat
#> 4 24.76555 59.44261
#> 5 24.76971 59.44126
#> 6 24.75928 59.44008

# osrmTable() with sf objects - OK
stops_sf <- st_as_sf(stops, coords = c("lon", "lat"), crs = "WGS84")
osrmTable(stops_sf[1:3,], stops_sf[4:6,])
#> $durations
#>     4   5   6
#> 1 0.0 1.1 1.0
#> 2 3.1 3.0 2.3
#> 3 3.1 3.1 2.3
#> 
#> $sources
#>        lon      lat
#> 1 24.76541 59.44259
#> 2 24.75145 59.44378
#> 3 24.76006 59.44473
#> 
#> $destinations
#>        lon      lat
#> 4 24.76555 59.44261
#> 5 24.76971 59.44126
#> 6 24.75928 59.44008

Created on 2023-01-31 with reprex v2.0.2

margusl
  • 7,804
  • 2
  • 16
  • 20