davidmasp / data-visualization

This is a repository to host data visualizations
6 stars 0 forks source link

city travel time (grasshopper api) #3

Open davidmasp opened 1 year ago

davidmasp commented 1 year ago

The idea is to make a visualization of driving time/distance compared to the actual ditance measured in the map. Original ideas were:

davidmasp commented 1 year ago

For the grasshopper API I am creating an account on here in the free plan. According to the website it should give me access to the main Matrix API which is what I would need for this to work.

https://docs.graphhopper.com/#operation/postRoute

https://www.graphhopper.com/pricing/

I checked the API and it works!

I have a working implementation of the api in python, I need to decide how to select the coordinates.

I have the problem of how to calculate linear distance between coordinates. I got a chatgpt implementation in python. Hope that works?

davidmasp commented 1 year ago
import math

def haversine(lat1, lon1, lat2, lon2):
    R = 6371  # Radius of the Earth in kilometers
    lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2])

    dlat = lat2 - lat1
    dlon = lon2 - lon1

    a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))

    distance = R * c
    return distance

# Example usage:
lat1 = 52.5200
lon1 = 13.4050
lat2 = 48.8566
lon2 = 2.3522

distance = haversine(lat1, lon1, lat2, lon2)
print(f"The distance between the two points is {distance:.2f} kilometers.")

The HAVERSINE formula is described here
https://en.wikipedia.org/wiki/Haversine_formula

davidmasp commented 1 year ago

I did check using google maps and the answer is correct!

I had the problem of how to actually get the dimensions from the map into a csv. According to this reddit post I can draw polygons into the map and then export coordinates of the polygon using google maps (or rather my maps from gmaps)

https://www.reddit.com/r/GoogleDataStudio/comments/xogxpr/generate_coordinates_from_drawn_polygon_on_map/

https://www.google.com/maps/about/mymaps/

davidmasp commented 1 year ago

possible cities I have selected:

davidmasp commented 1 year ago

There is also another service, OpenRouteService

https://openrouteservice.org/

They have a iso-travel time (see tutorial here)

https://www.youtube.com/watch?v=UNGzJrx8VrE