-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_folium.py
78 lines (56 loc) · 2.51 KB
/
plot_folium.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
75
76
77
78
def get_folium_map(df, latitude_col,longitude_col, colour_by = None, popup_name_col = None):
'''
Plots an interactive map of longitude latitude coordinates in Folium.
df: A dataframe with a longitude and latitude coordinates within.
latitude_col The column name containing latitude figures
longitude_col The column name containing longitude figures
colour_by Any column name containing the categories you wish to colour by
pop_name_col Enter the column name which contains any popup information you wish to display
when you click point
'''
import pandas as pd
import folium
import numpy as np
global colourdict
# Set up a list of points
locationlist = df[[latitude_col, longitude_col]].values.tolist()
# Find the mean long/lat coordinates so we an use these as the maps centre point
mean_lat = df[latitude_col].mean()
mean_long = df[longitude_col].mean()
m = folium.Map(location=[mean_lat, mean_long],
#zoom_start=zoom,
prefer_canvas=True,
zoom_control= True)
# Check if a colour column has been defined
# if so, set up a colour dictionary to define some colours
if colour_by:
colour_options = ['blue', 'red', 'green', 'brown','yellow','purple','orange',
'darkgreen', 'darkpurple', 'darkred', 'gray', 'darkblue', 'lightblue',
'lightgray', 'lightgreen', 'lightred', 'orange', 'pink', 'purple','cadetblue',
'black']
categories = np.unique(df[colour_by])
colours = np.linspace(0, 1, len(categories))
colourdict = dict(zip(categories, colours))
colour_no = 0 # counter for keep track of max colours
for i in colourdict.keys():
colourdict[i] = colour_options[colour_no]
colour_no += 1
if colour_no == len(colour_options): colour_no = 0 #reset counter if we reach the max
for point in range(0, len(locationlist)):
# Check if a popup name column has been defined
if popup_name_col:
popup = folium.Popup(df.iloc[point][popup_name_col], parse_html=True)
else:
popup = None
# Check if a colour column has been defined
if colour_by:
colour = colourdict[df.iloc[point][colour_by]]
else:
colour = 'red'
folium.CircleMarker(locationlist[point],
radius=1,
color=colour,
popup=popup
).add_to(m)
m
return m