This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_schatzmeister.py
74 lines (58 loc) · 2.38 KB
/
map_schatzmeister.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
"""map_schatzmeister.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ztBOPXSu-SK5Ykp1TRo4iuzsM5SoW5mA
"""
# Quelle: https://colab.research.google.com/drive/19TbFZCaOBcCJ8aNoRM0CAfhAZqDnPaVm#scrollTo=bMnYdhHoTDcU
# Quelle2: https://python-graph-gallery.com/313-bubble-map-with-folium/
# import and install packages
!pip install geopy # geocoding library
!pip install folium
import folium
import pandas as pd
from geopy.geocoders import Nominatim
# create a geocoder that uses OpenStreetMap Nominatim API
geolocator = Nominatim(timeout=10, user_agent = "dlab.berkeley.edu-workshop")
# read data
df = pd.read_csv('ergebnis_28082020_loesch_ohne_duplikate.csv', sep = ";")
df
# add the column "Anzahl"
df['anzahl'] = 1
df
!ls
# number of gifts by postcode
df_pivot = df.pivot_table(values='anzahl', index='postleitzahl', aggfunc='sum')
df_pivot.reset_index(inplace=True)
df_pivot
# geocode the addresses in the pandas df
# Iterates over all rows in the dataframe df.
# Joins the values in the columns Landmark, City, and State into one string (the full address)
# Submits the string we just created as the address to be geocoded
# Saves results to the geocodes list object.
geocodes = [geolocator.geocode({'country': 'de', 'postalcode': df_pivot.loc[i, 'postleitzahl']}) for i in range(len(df_pivot))]
geocodes
# add parts of the output latitude and longitude values in the geocodes list to the df dataframe
df_pivot['lat'] = [g.latitude for g in geocodes]
df_pivot['lon'] = [g.longitude for g in geocodes]
df_pivot
# first make an empty map centered on Münster
map1 = folium.Map(location=(51.9606649, 7.6261347), zoom_start=12)
for index,row in df_pivot.iterrows():
# Add the geocoded locations to the map
folium.Marker([row['lat'],row['lon']], popup=str(int(row['anzahl']))).add_to(map1)
display(map1)
# save the map
map1.save("muenster_marker.html")
# first make an empty map centered on Münster
map2 = folium.Map(location=(51.9606649, 7.6261347), zoom_start=11.5)
for index,row in df_pivot.iterrows():
# Add the geocoded locations to the map
folium.Circle([row['lat'],row['lon']],
popup=str(int(row['anzahl'])),
radius=row['anzahl']*4,
fill=True,
fill_color='blue').add_to(map2)
display(map2)
# save the map
map2.save("muenster_bubble.html")