基于R与Google Map API的城市研究路线拥堵数据获取问询
Hey there! For your urban research needs, you absolutely can get the congestion data for alternative routes over a specific time window using Google Maps' official APIs—and the googleway package you're already using can actually do this, you just need to target the right endpoint and parameters. Let me walk you through how:
1. Which Google API to Use?
The Google Maps Directions API is exactly what you need here. It returns detailed route information including:
- Multiple alternative routes (when enabled)
- Travel duration with traffic (real-time or historical, based on your specified departure time)
- Baseline travel duration (without traffic)
- Congestion-related delays calculated as the difference between these two durations
2. Using googleway to Fetch the Data
You were on the right track with googleway—it has built-in support for the Directions API. The key parameters you missed are:
alternatives = TRUE: Forces the API to return all viable alternative routes between your two pointsdeparture_time: A Unix timestamp (in seconds) specifying when you want to simulate departure (this works for both real-time traffic and historical traffic data—just use a timestamp from your target time window)traffic_model: Optional, but useful for historical data; usebest_guess(default) or specifypessimistic/optimisticif you want to model different congestion scenarios
Example Code
Here’s a quick snippet to get you started:
library(googleway) # Set your Google API key (make sure Directions API is enabled in Google Cloud Console) set_key("YOUR_GOOGLE_API_KEY") # Define your origin, destination, and target departure time (Unix timestamp) origin <- "Times Square, New York" destination <- "Brooklyn Bridge, New York" # Example: Departure time of 9 AM on October 1, 2024 (convert to Unix seconds) departure_time <- as.integer(as.POSIXct("2024-10-01 09:00:00", tz = "America/New_York")) # Call the Directions API with alternatives and traffic data route_data <- google_directions( origin = origin, destination = destination, departure_time = departure_time, alternatives = TRUE, traffic_model = "best_guess" ) # Extract congestion data for each alternative route if (route_data$status == "OK") { routes <- route_data$routes for (i in seq_along(routes)) { route <- routes[[i]] leg <- route$legs[[1]] # Baseline duration (without traffic) base_duration <- leg$duration$value / 60 # Convert seconds to minutes # Duration with traffic traffic_duration <- leg$duration_in_traffic$value / 60 # Congestion delay congestion_delay <- traffic_duration - base_duration cat(sprintf("Route %d:\n", i)) cat(sprintf(" Base duration: %.1f mins\n", base_duration)) cat(sprintf(" Duration with traffic: %.1f mins\n", traffic_duration)) cat(sprintf(" Congestion delay: %.1f mins\n\n", congestion_delay)) } } else { cat("Error fetching data:", route_data$status) }
3. Key Notes for Your Research
- Historical Traffic Data: Google Directions API provides historical traffic data for timestamps up to several months in the past—perfect for analyzing time-window congestion patterns.
- API Quotas & Costs: Make sure to check Google Cloud's pricing for Directions API calls; historical traffic requests may have the same cost as real-time ones, but verify your quota limits to avoid unexpected charges.
- Data Parsing: The response is a nested list, so you can use packages like
purrrorjsonliteto streamline extracting metrics across multiple routes or time points for your analysis.
4. Alternative Package (If You Want More Options)
If you want another R package to work with, gmapsdistance also supports calling the Directions API and can return traffic-aware duration data. But googleway is more flexible for map visualization and data extraction once you know the right parameters.
内容的提问来源于stack exchange,提问作者mjoudy

