Created
August 7, 2024 16:28
-
-
Save bbelderbos/f4764c461e6f277f8411e91d2dc42d4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import csv | |
# set MARVIN_OPENAI_API_KEY in your environment variables | |
import marvin | |
from pydantic import BaseModel | |
import folium | |
class Location(BaseModel): | |
name: str | |
latitude: float | |
longitude: float | |
cities = [ | |
"Den Haag", | |
"Berlin", | |
"Hamburg", | |
"Copenhagen", | |
"Hamburg", | |
"Cologne", | |
"Brussels", | |
"Heidelberg", | |
"Strasbourg", | |
"Paris", | |
"Lyon", | |
"Marseille", | |
"Arles", | |
"Tarascon", | |
"Avignon", | |
"Nîmes", | |
"Aix-en-Provence", | |
"Lille", | |
"Calais", | |
"Dover", | |
"York", | |
"Edinburgh", | |
"Manchester", | |
"Bath", | |
"Bristol", | |
"London", | |
"Harwich", | |
"Hoek van Holland", | |
] | |
cities_cache = "cities.csv" | |
def write_csv(data): | |
with open(cities_cache, mode="w", newline="") as file: | |
writer = csv.writer(file) | |
writer.writerow(["Name", "Latitude", "Longitude"]) # Write the header | |
for location in data: | |
writer.writerow([location.name, location.latitude, location.longitude]) | |
def get_locations(): | |
with open(cities_cache, mode="r") as file: | |
reader = csv.reader(file) | |
next(reader) | |
for row in reader: | |
name, latitude, longitude = row | |
yield Location( | |
name=name, latitude=float(latitude), longitude=float(longitude) | |
) | |
cities_cache = Path(cities_cache) | |
if not cities_cache.exists(): | |
print("Cache not found, fetching data") | |
data = marvin.cast(cities, target=list[Location]) | |
write_csv(data) | |
mymap = folium.Map(location=(52.0, 5.0), zoom_start=5) | |
for location in get_locations(): | |
folium.Marker( | |
location=[location.latitude, location.longitude], popup=location.name | |
).add_to(mymap) | |
mymap.save("cities_map.html") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment