I'm using below piece of code in Rstudio to use here.com to obtain in routes in Canada. API results in my df show a combined polygon, distance and duration for a row, but second is all NA. Could you please help me understand, and if possible, fix this issue?
# Packages
library("sf")
library("tidyverse")
library( "hereR")
library("stringi")
library("dplyr")
# Load data
distancePairs <- structure(list(Origin = c("Saskatoon", "St. John's"), Lat1 = c(52.1583630506965,
47.5535718086621), Lon1 = c(-106.671978365698, -52.7486803304426
), Destination = c("St. John's", "Calgary"), Lat2 = c(47.5535718086621,
51.0452926068494), Lon2 = c(-52.7486803304426, -114.05905562721
), id = c(17, 18), straight.distance.meters = structure(c(3808910.50512732,
4332741.9186734), units = structure(list(numerator = "m", denominator = character(0)), class = "symbolic_units"), class = "units")), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"))
# Set API key for free through throug https://developer.here.com/self-serve
set_key(api_key = "YOUR_FREEMIUN_KEY")
# Create SFDFs (points with lat-lon pairs):
dist.pairs.input.1 <- dist.pairs.query %>%
st_as_sf(coords = c("Lon1","Lat1"),
crs = "WGS84",
remove = F)
dist.pairs.input.2 <- dist.pairs.query %>%
st_as_sf(coords = c("Lon2", "Lat2"),
crs = "WGS84",
remove = F)
#Calculate drive time&distance:
query_result <- route(origin = dist.pairs.input.1,
destination = dist.pairs.input.2,
traffic = T)
# Can't dput my answer to share it's structure because it's a huge line that's not fully visible in my console,
# but result has 3 objets with 13 variables, all id 1, rank 1, section 1-2-3 and it's giving me a summary of 1
query_result2 <- structure(list(id = c(1, 2), departure = structure(c(1700159564,
NA), class = c("POSIXct", "POSIXt"), tzone = ""), arrival = structure(c(1700373938,
NA), class = c("POSIXct", "POSIXt"), tzone = ""), type = c("vehicle",
NA), mode = c("car", NA), distance_km = c(5605.622, NA), duration_min = c(3542.9,
NA), Origin = c("Saskatoon", "St. John's"), Lat1 = c(52.1583630506965,
47.5535718086621), Lon1 = c(-106.671978365698, -52.7486803304426
), Destination = c("St. John's", "Calgary"), Lat2 = c(47.5535718086621,
51.0452926068494), Lon2 = c(-52.7486803304426, -114.05905562721
), straight.distance.meters = structure(c(3808910.50512732, 4332741.9186734
), units = structure(list(numerator = "m", denominator = character(0)), class = "symbolic_units"), class = "units")), row.names = 1:2, class = "data.frame") # without shp files
result_summary <- query_result %>%
group_by(id) %>%
summarise(new_shp = st_combine(.),
departure = first(departure),
arrival = last(arrival),
type = mode(type),
mode = mode(mode),
distance_km = sum(distance)/1000,
duration_min = sum(duration)/60)
# Below snapped result is what I get
distancePairs$id <- 1:2
result_output<- merge(result_summary,
dist.pairs.query,
by = "id",
all = TRUE)
# Looks like below without geometry, but first one's a MULTILINESTRINGZ and the one that's NA results is a GEOMETRYCOLLECTION Z EMPTY
result_output2 <- structure(list(id = c(1, 2), departure = structure(c(1700159564,
NA), class = c("POSIXct", "POSIXt"), tzone = ""), arrival = structure(c(1700373938,
NA), class = c("POSIXct", "POSIXt"), tzone = ""), type = c("vehicle",
NA), mode = c("car", NA), distance_km = c(5605.622, NA), duration_min = c(3542.9,
NA), Origin = c("Saskatoon", "St. John's"), Lat1 = c(52.1583630506965,
47.5535718086621), Lon1 = c(-106.671978365698, -52.7486803304426
), Destination = c("St. John's", "Calgary"), Lat2 = c(47.5535718086621,
51.0452926068494), Lon2 = c(-52.7486803304426, -114.05905562721
), straight.distance.meters = structure(c(3808910.50512732, 4332741.9186734
), units = structure(list(numerator = "m", denominator = character(0)), class = "symbolic_units"), class = "units")), row.names = 1:2, class = "data.frame")