Calculating travel times in R for several pair of addresses

85 views Asked by At

I am a newbie on this. I'm following tutorials and instructions as much as I can, and I get not results. The problem may not be in code (I think), but on what happens of the google maps API side.

I get 'no travel time' for each of my rows.

This is my code:

api_key <- 'IthinkIshouldNdtTellYOU'

desired_arrival_time <- as.POSIXct(paste(next_wednesday, "09:00:00"), tz = "America/New_York")
arrival_epoch_time <- as.numeric(desired_arrival_time)


 get_travel_time <- function(origin, destination, api_key, arrival_time) {
  base_url <- "https://maps.googleapis.com/maps/api/directions/json?"
  query <- paste0("origin=", URLencode(origin),
              "&destination=", URLencode(destination),
              "&mode=transit",
              "&arrival_time=", arrival_time,
              "&key=", api_key)
 response <- GET(paste0(base_url, query))

if (response$status_code == 200) {
data <- fromJSON(rawToChar(response$content))
if (length(data$routes) > 0) {
  travel_time <- data$routes[[1]]$legs[[1]]$duration$text
  return(travel_time)
} else {
  return(NA)
}
} else {
stop("API request failed with status code: ", response$status_code)
}
}

address_pairs$travel_time <- NA

for (i in 1:nrow(address_pairs)) {
 travel_time_result <- try(
   get_travel_time(
   origin = address_pairs$OriginAdd[i],
   destination = address_pairs$DestinAdd[i],
   api_key = api_key,
   arrival_time = arrival_epoch_time
),
silent = TRUE  # Set silent = TRUE to not print error messages directly
)

if (class(travel_time_result) == "try-error") {
    address_pairs$travel_time[i] <- NA
    print(paste("Error on row", i, ":", travel_time_result))
} else {
    # If no error, check if get_travel_time returns a value
    if (!is.na(travel_time_result) && length(travel_time_result) > 0) {
        address_pairs$travel_time[i] <- travel_time_result
    } else {
        # Assign NA if get_travel_time returns NA or length zero
        address_pairs$travel_time[i] <- NA
        print(paste("No travel time returned for row", i))
    }
}

Sys.sleep(0.2) # Be mindful of rate limiting by the API
}

This is what I get for each row. I cannot understand anything in my google cloud account. Is this costing me money? Did I overpass my quotas already?

No travel time returned for row 1208"
1 "No travel time returned for row 1209"
1 "No travel time returned for row 1210"

I tried creating a function of time of travel, and then running a loop to calculate time of travel using that function. I used a lot of ChatGPT for this.

I can see the API key being queried but I get not even one result.

My addresses are NYC addresses. Google Maps works pretty fine here. Rstudio seems to query google maps as it takes a long time, but showing no results.

Origin and destination addresses sample

OriginAdd                           DestinAdd
365 Fifth Ave, Manhattan, New York  120 Schermerhorn St, Brooklyn, NY 11201
3280 46th Avenue, Queens, Corona, NY    265 E 161 St, Bronx, NY 10451
106-05 37 Avenue, Queens, New York .    125-01 Queens Blvd, Queens, NY 11415
3280 46th Street, Queens, NY    265 E 161 St, Bronx, NY 10451
475 48th Ave, Queens, New York 11109    100 Centre St, New York, NY 10013
0

There are 0 answers