[Python] Find locations with the same travel time to a destination: Heatmap/Contours based on...

Discussão em 'Python' iniciado por Stack, Agosto 11, 2025.

  1. Stack

    Stack Membro Participativo

    Note: Solutions in r or python are desired.

    I am trying to draw contours based on transportation time. To be more clear, I want to cluster the points which have similar travel time (let's say 10 minute interval) to a specific point (destination) and map them as contours or a heatmap.

    Right now, the only idea that I have is using R package gmapsdistance to find the travel time for different origins and then cluster them and draw them on a map. But, as you can tell, this is in no way a robust solution.

    This thread on GIS-community and this one for python illustrate a similar problem but for an origin to destinations within reach in specific time. I want to find origins which I can travel to the destination within certain time.

    Right now, the code below shows my rudimentary idea (using R):

    library(gmapsdistance)

    set.api.key("YOUR.API.KEY")

    mdestination <- "40.7+-73"
    morigin1 <- "40.6+-74.2"
    morigin2 <- "40+-74"

    gmapsdistance(origin = morigin1,
    destination = mdestination,
    mode = "transit")

    gmapsdistance(origin = morigin2,
    destination = mdestination,
    mode = "transit")


    This map also may help to understand the question:

    [​IMG]

    Using this answer I can get the points which I can go to from a point of origin but I need to reverse it and find the points which have travel time equal-less-than a certain time to my destination;

    library(httr)
    library(googleway)
    library(jsonlite)


    appId <- "TravelTime_APP_ID"
    apiKey <- "TravelTime_API_KEY"
    mapKey <- "GOOGLE_MAPS_API_KEY"

    location <- c(40, -73)
    CommuteTime <- (5 / 6) * 60 * 60

    url <- "http://api.traveltimeapp.com/v4/time-map"

    requestBody <- paste0('{
    "departure_searches" : [
    {"id" : "test",
    "coords": {"lat":', location[1], ', "lng":', location[2],' },
    "transportation" : {"type" : "driving"} ,
    "travel_time" : ', CommuteTime, ',
    "departure_time" : "2017-05-03T07:20:00z"
    }
    ]
    }')

    res <- httr::pOST(url = url,
    httr::add_headers('Content-Type' = 'application/json'),
    httr::add_headers('Accept' = 'application/json'),
    httr::add_headers('X-Application-Id' = appId),
    httr::add_headers('X-Api-Key' = apiKey),
    body = requestBody,
    encode = "json")

    res <- jsonlite::fromJSON(as.character(res))

    pl <- lapply(res$results$shapes[[1]]$shell, function(x){
    googleway::encode_pl(lat = x[['lat']], lon = x[['lng']])
    })


    df <- data.frame(polyline = unlist(pl))

    df_marker <- data.frame(lat = location[1], lon = location[2])

    google_map(key = mapKey) %>%
    add_markers(data = df_marker) %>%
    add_polylines(data = df, polyline = "polyline")


    [​IMG]


    Moreover, Documentation of Travel Time Map Platform talks about Multi Origins with Arrival time which is exactly the thing I want to do. But I need to do that for both public transportation and driving (for places with less than an hour commute time) and I think since public transport is tricky (based on what station you are close to) maybe heatmap is a better option than contours.

    Continue reading...

Compartilhe esta Página