Skip to content

Instantly share code, notes, and snippets.

@bbelderbos
Last active January 3, 2023 20:06
Show Gist options
  • Save bbelderbos/37823bf81b85f2916f384afa27950a6d to your computer and use it in GitHub Desktop.
Save bbelderbos/37823bf81b85f2916f384afa27950a6d to your computer and use it in GitHub Desktop.
import csv
from urllib.request import urlretrieve
GEO_DATA = (
"https://raw.githubusercontent.com/pybites/"
"100DaysOfCode/master/050/simplemaps-worldcities-basic.csv"
)
CITIES = [
"Amsterdam",
"Paris",
"Madrid",
"Barcelona",
"København",
"Berlin",
"Seville",
"Marseille",
"Lyon",
"London",
"Nottingham",
"Brussels",
"Rome",
"Florence",
"Naples",
"Bucharest",
"Verona",
"Valencia",
"Palermo",
"Edinburgh",
"Biarritz",
]
def get_cities_lon_and_lats(csv_file=None, cities=CITIES):
if csv_file is None:
csv_file = "data.csv"
urlretrieve(GEO_DATA, csv_file)
city_coords = {}
with open(csv_file) as f:
rows = csv.DictReader(f)
for row in rows:
city = row["city"]
if city not in cities:
continue
city_coords[city] = [row["lat"], row["lng"]]
return city_coords
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment