Skip to content

Commit

Permalink
fix: refactor osrmTrip() to handle points too close to each others
Browse files Browse the repository at this point in the history
fix #120 and better fix than the one used for #117
  • Loading branch information
rCarto committed Mar 29, 2023
1 parent c4e8d9f commit 6e2bd6c
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions R/osrmTrip.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ osrmTrip <- function(loc, exclude = NULL, overview = "simplified",
returnclass, osrm.server = getOption("osrm.server"),
osrm.profile = getOption("osrm.profile")){


opt <- options(error = NULL)
on.exit(options(opt), add=TRUE)

Expand All @@ -71,6 +70,7 @@ osrmTrip <- function(loc, exclude = NULL, overview = "simplified",
loc <- input_route(x = loc, id = "loc", single = FALSE, all.ids = TRUE)
oprj <- loc$oprj

# build url
url <- paste0(url,
paste(clean_coord(loc$lon) , clean_coord(loc$lat),
sep=",",collapse = ";"),
Expand Down Expand Up @@ -104,38 +104,45 @@ osrmTrip <- function(loc, exclude = NULL, overview = "simplified",
# In case of island, multiple trips
ntour <- dim(res$trips)[1]
trips <- vector("list", ntour)

for (nt in 1:ntour) {
# nt=1
# Coordinates of the line
geodf <- data.frame(res$trips[nt,]$geometry[[1]]$coordinates)
geodf$id <- 1:nrow(geodf)

# In case of unfinnish trip
if (geodf[nrow(geodf),1] != geodf[1,1]) {
geodf <- rbind(geodf,geodf[1,])
}
geodf$ind <- 1:nrow(geodf)

# Extract trip waypoints
waypoints <- waypointsg[waypointsg$trips_index == (nt - 1),]
geodf$id_wp <- NA
waypoints <- waypoints[order(waypoints$waypoint_index, decreasing = F),]
j <- 1
for(i in 1:(nrow(geodf)-1)){
if (any(geodf[i, c("X1", "X2")] == waypoints[j, c("X1", "X2")])){
geodf$id_wp[i] <- waypoints[j, "id"]
j <- j+1
if (j>nrow(waypoints)){j = 1}
}
}

# Get points order and indexes
geodf <- merge(geodf, waypoints,
by.x = c("X1", "X2"), by.y = c("X1","X2"),
all.x = T)
geodf <- geodf[order(geodf$ind, decreasing = F),]
base_ind <- geodf[!is.na(geodf$waypoint_index),]
indexes <- aggregate(base_ind$ind, by= list(base_ind$id), head, 1)$x
indexes <- c(sort(indexes), nrow(geodf))
if(is.na(geodf[nrow(geodf), "id_wp"])){
geodf[nrow(geodf), "id_wp"] <- geodf[1, "id_wp"]
}

l <- list()
j <- 1
for (i in 1:nrow(geodf)){
if(!is.na(geodf[i, "id_wp"])){
l[[j]] <- geodf[i, "id"]
j <- j + 1
}
}
# Build the polylines
wktl <- rep(NA,nrow(waypoints))
for (i in 1:(length(indexes) - 1)) {
ind0 <- indexes[i]
ind1 <- indexes[i+1]
if(ind1==ind0){
aind <- rep(ind0,2)
}else{
aind <- ind0:ind1
}
for (i in 1:length(wktl)) {
aind <- l[[i]]:l[[i+1]]
wktl[i] <- paste("LINESTRING(",
paste(geodf[aind,1]," ",
geodf[aind,2],
Expand All @@ -158,5 +165,4 @@ osrmTrip <- function(loc, exclude = NULL, overview = "simplified",
trips[[nt]] <- list(trip = sldf, summary = tripSummary)
}
return(trips)

}

0 comments on commit 6e2bd6c

Please sign in to comment.