Skip to content

Instantly share code, notes, and snippets.

@bbelderbos
Created August 7, 2024 16:28
Show Gist options
  • Save bbelderbos/f4764c461e6f277f8411e91d2dc42d4b to your computer and use it in GitHub Desktop.
Save bbelderbos/f4764c461e6f277f8411e91d2dc42d4b to your computer and use it in GitHub Desktop.
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