Skip to content

Instantly share code, notes, and snippets.

@ksindy
Created March 17, 2022 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksindy/31f8981fcb76fe76db825f709c7c9404 to your computer and use it in GitHub Desktop.
Save ksindy/31f8981fcb76fe76db825f709c7c9404 to your computer and use it in GitHub Desktop.
I used Python, geonames and folium to create a heatmap of the FARTers who have made orders around the country.
"""
I used Python, geonames and folium to create a heatmap of the FARTers who have made orders around the country.
I used Mike Cunha's excellent example to aid in creation on the map. https://github.com/mikecunha/pygeo_heatmap
https://python-visualization.github.io/folium/
https://www.geonames.org/
"""
import csv, os
from geopy.geocoders import Nominatim
import pandas as pd
import folium
from folium.plugins import HeatMap
geolocator = Nominatim(user_agent="fart")
loc_hr_dict = {}
loc_dict = {}
with open("FartEtsySoldOrders2022.csv") as orders:
next(orders)
read = csv.reader(orders, delimiter=',')
n_cities = 0
for row in read:
city = row[2].lower()
order_loc = f"{city}, {row[3]} {row[4]}"
print(order_loc)
geo_loc = geolocator.geocode(order_loc)
if city not in loc_hr_dict:
loc_dict[geo_loc.latitude, geo_loc.longitude] = 1
loc_hr_dict[city] = 1
n_cities += 1
else:
num = loc_hr_dict.get(city)
loc_hr_dict[city] = num + 1
loc_dict[geo_loc.latitude, geo_loc.longitude] = num +1
print(num + 1)
with open('lat-long.csv', 'w') as f:
f.write("lat, lon, Total \n")
for key in loc_dict.keys():
loc = ', '.join(str(lat_long) for lat_long in key)
f.write(f"{loc}, {loc_dict[key]}\n")
for_map = pd.read_csv('lat-long.csv', sep=', ', engine='python')
max_amount = for_map['Total'].max()
hmap = folium.Map(location=[35.539, -82.518], zoom_start=7, )
hm_wide = HeatMap( list(zip((for_map.lat.values).tolist(), (for_map.lon.values).tolist(), (for_map.Total.values).tolist())),
min_opacity=0.2,
radius=17, blur=15,
max_zoom=1,
)
hmap.add_child(hm_wide)
hmap.save(os.path.join('heatmap.html'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment