analyze_commute
Analyze commute options between home and work locations using OpenStreetMap data. Compare transportation modes, travel times, distances, and turn-by-turn directions for informed planning on real estate, lifestyle, or workplace decisions.
Instructions
Perform a detailed commute analysis between home and work locations.
This advanced tool analyzes commute options between two locations (typically home and work), comparing multiple transportation modes and providing detailed metrics for each option. Includes estimated travel times, distances, turn-by-turn directions, and other commute-relevant data. Essential for real estate decisions, lifestyle planning, and workplace relocation analysis.
Args: home_latitude: Home location latitude (decimal degrees) home_longitude: Home location longitude (decimal degrees) work_latitude: Workplace location latitude (decimal degrees) work_longitude: Workplace location longitude (decimal degrees) modes: List of transportation modes to analyze (options: "car", "foot", "bike") depart_at: Optional departure time (format: "HH:MM") for time-sensitive routing
Returns: Comprehensive commute analysis with: - Summary comparing all transportation modes - Detailed route information for each mode - Total distance and duration for each option - Turn-by-turn directions
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| depart_at | No | ||
| home_latitude | Yes | ||
| home_longitude | Yes | ||
| modes | No | ||
| work_latitude | Yes | ||
| work_longitude | Yes |
Implementation Reference
- src/osm_mcp_server/server.py:950-1052 (handler)The handler function decorated with @mcp.tool() that implements the analyze_commute tool. It computes routes for different transportation modes between home and work locations using OSMClient's get_route method, reverse geocodes addresses, and returns a comparison of commute options including distances, durations, and directions.async def analyze_commute( home_latitude: float, home_longitude: float, work_latitude: float, work_longitude: float, ctx: Context, modes: List[str] = ["car", "foot", "bike"], depart_at: str = None # Time in HH:MM format, e.g. "08:30" ) -> Dict[str, Any]: """ Perform a detailed commute analysis between home and work locations. This advanced tool analyzes commute options between two locations (typically home and work), comparing multiple transportation modes and providing detailed metrics for each option. Includes estimated travel times, distances, turn-by-turn directions, and other commute-relevant data. Essential for real estate decisions, lifestyle planning, and workplace relocation analysis. Args: home_latitude: Home location latitude (decimal degrees) home_longitude: Home location longitude (decimal degrees) work_latitude: Workplace location latitude (decimal degrees) work_longitude: Workplace location longitude (decimal degrees) modes: List of transportation modes to analyze (options: "car", "foot", "bike") depart_at: Optional departure time (format: "HH:MM") for time-sensitive routing Returns: Comprehensive commute analysis with: - Summary comparing all transportation modes - Detailed route information for each mode - Total distance and duration for each option - Turn-by-turn directions """ osm_client = ctx.request_context.lifespan_context.osm_client # Get address information for both locations home_info = await osm_client.reverse_geocode(home_latitude, home_longitude) work_info = await osm_client.reverse_geocode(work_latitude, work_longitude) # Get commute information for each mode commute_options = [] for mode in modes: ctx.info(f"Calculating {mode} route for commute analysis") # Get route from OSRM try: route_data = await osm_client.get_route( home_latitude, home_longitude, work_latitude, work_longitude, mode ) if "routes" in route_data and len(route_data["routes"]) > 0: route = route_data["routes"][0] # Extract directions steps = [] if "legs" in route: for leg in route["legs"]: for step in leg.get("steps", []): steps.append({ "instruction": step.get("maneuver", {}).get("instruction", ""), "distance": step.get("distance"), "duration": step.get("duration"), "name": step.get("name", "") }) commute_options.append({ "mode": mode, "distance_km": round(route.get("distance", 0) / 1000, 2), "duration_minutes": round(route.get("duration", 0) / 60, 1), "directions": steps }) except Exception as e: ctx.warning(f"Error getting {mode} route: {str(e)}") commute_options.append({ "mode": mode, "error": str(e) }) # Sort by duration (fastest first) commute_options.sort(key=lambda x: x.get("duration_minutes", float("inf"))) return { "home": { "coordinates": { "latitude": home_latitude, "longitude": home_longitude }, "address": home_info.get("display_name", "Unknown location") }, "work": { "coordinates": { "latitude": work_latitude, "longitude": work_longitude }, "address": work_info.get("display_name", "Unknown location") }, "commute_options": commute_options, "fastest_option": commute_options[0]["mode"] if commute_options else None, "depart_at": depart_at }