Skip to content

Instantly share code, notes, and snippets.

@TravelTime-Frontend
Last active February 21, 2023 08:51
Show Gist options
  • Save TravelTime-Frontend/0af993d2fba49110e29afaeb4ede64ec to your computer and use it in GitHub Desktop.
Save TravelTime-Frontend/0af993d2fba49110e29afaeb4ede64ec to your computer and use it in GitHub Desktop.
Point in polygon R
library(traveltimeR)
library(rjson)
readRenviron(".env")
data <- read.csv("../data/customers.csv")
locations <- apply(data, 1, function(point) {
make_location(point["id"], list(
lat = as.numeric(point["lat"]),
lng = as.numeric(point["lng"])
))
})
locations <- unlist(locations, recursive = F)
arrival_ids <- as.list(data$id[-1])
departure_search <-
make_search(
id = "departure search example",
departure_location_id = data$id[1],
arrival_location_ids = arrival_ids,
departure_time = strftime(as.POSIXlt(Sys.time(), "UTC"), "%Y-%m-%dT%H:%M:%SZ"),
travel_time = 1800,
transportation = list(type = "driving"),
properties = list("travel_time", "distance")
)
result <-
time_filter(
departure_searches = departure_search,
locations = locations
)
unreachable_customers <- result$contentParsed$results[[1]]$unreachable
print(paste("Number of unreachable customers -", length(unreachable_customers)))
reachable_customers <- result$contentParsed$results[[1]]$locations
dir.create("results", showWarnings = F)
write(toJSON(reachable_customers), stringr::str_interp("results/results_time_filter_${Sys.time()}.json"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment