-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot-maps-leaflet.py
More file actions
174 lines (142 loc) · 4.94 KB
/
plot-maps-leaflet.py
File metadata and controls
174 lines (142 loc) · 4.94 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import folium
import xarray as xr
import numpy as np
import matplotlib as mp
import os
import matplotlib.colors as cl
from pathlib import Path
from domains import domains
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
import branca
import base64
if not os.path.exists('build'):
os.mkdir('build')
cmap = mp.colormaps['hsv']
# Function to reconstruct the IFrame HTML from text and
# style
def build_html(d):
with open(d['popup'], 'r') as f:
text = f.read()
strout = ""
strout += """
<!DOCTYPE html>
<html>
<head>
<style>
"""
strout += style
strout += """
</style>
</head>
<body>
"""
f = d['map'].replace('.nc', '.png')
fileout = os.path.basename(f).replace('.nc', '.png')
fileout = os.path.join('created-maps', fileout)
print(fileout)
data_uri = base64.b64encode(open(fileout, 'rb').read()).decode('utf-8')
strout += '<div align="center">\n'
img_tag = '<img src="data:image/png;base64,{0}" height="200" align="center">'.format(data_uri)
strout += img_tag + '\n'
strout += '</div>'
strout += text
strout += """
</body>
</html>
"""
return strout
def colorize(array, r, g, b):
array = array.astype(int)
col1 = [r, g, b, 0]
col2 = [r, g, b, 1]
newcmp = ListedColormap([col1, col2])
normed_data = (array - array.min()) / (array.max() - array.min())
fig = plt.figure()
cs = plt.imshow(array, cmap=newcmp)
cs.set_clim(0, 1)
plt.colorbar(cs)
plt.savefig(str(cpt))
plt.close(fig)
return newcmp(normed_data)
# Recover the content of the HTML file
import pathlib
with open('style.css', 'r') as f:
style = f.read()
# Create a Map instance
m = folium.Map(zoom_start=3, control_scale=True, location=[0, 0], tiles=None)
tile1 = folium.TileLayer(
#tiles='https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
tiles='https://server.arcgisonline.com/ArcGIS/rest/services/Ocean/World_Ocean_Base/MapServer/tile/{z}/{y}/{x}',
attr='Esri',
name='Esri Satellite',
overlay=False,
control=True
)
tile2 = folium.TileLayer(
tiles='https://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}',
attr = 'Tiles © Esri — Esri, DeLorme, NAVTEQ, TomTom, Intermap, iPC, USGS, FAO, NPS, NRCAN, GeoBase, Kadaster NL, Ordnance Survey, Esri Japan, METI, Esri China (Hong Kong), and the GIS User Community',
overlay=False,
control=True
)
tile3 = folium.TileLayer(
tiles='https://server.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer/tile/{z}/{y}/{x}',
attr = 'Tiles © Esri — Esri, DeLorme, NAVTEQ, TomTom, Intermap, iPC, USGS, FAO, NPS, NRCAN, GeoBase, Kadaster NL, Ordnance Survey, Esri Japan, METI, Esri China (Hong Kong), and the GIS User Community',
overlay=False,
control=True
)
tile1.add_to(m)
BASE_COLORS = {'k': (0.0, 0.0, 0.0), 'g': (0, 0.5, 0), 'r': (1, 0, 0), 'c': (0, 0.75, 0.75), 'm': (0.75, 0, 0.75), 'y': (0.75, 0.75, 0)}
#BASE_COLORS = {}
#BASE_COLORS['blue'] = (12.2 / 100, 46.7 / 100, 70.6 / 100)
#BASE_COLORS['orange'] = (100 / 100, 49.8 / 100, 5.5 / 100)
colnames = list(BASE_COLORS.keys())
# +
cpt = 0
N = len(domains) - 1
for d in domains.values():
print('---------------------------------- ', d['title'])
r, g, b = BASE_COLORS[colnames[cpt % len(colnames)]]
iframe = branca.element.IFrame(build_html(d), width=500, height=800)
popup = folium.Popup(iframe,
min_width=500,
max_width=500, min_height=800)
data = xr.open_dataset(d['map'])
if(data['lon'].values.ndim == 1):
dlon = np.mean(np.diff(data['lon'].values))
dlat = np.mean(np.diff(data['lat'].values))
else:
dlon = np.mean(np.diff(data['lon'].values[0, :]))
dlat = np.mean(np.diff(data['lat'].values[:, 0]))
if 'lat_offset' in d.keys():
lat_offset = d['lat_offset']
else:
lat_offset = 0
factor = 0.5
lonmin = float(data['lon'].min())
lonmax = float(data['lon'].max())
latmin = float(data['lat'].min()) + lat_offset
latmax = float(data['lat'].max()) + lat_offset
image = data['mask'].values[::-1, :]
display = colorize(image, r, g, b)
folium.Marker(
location=[data['lat'].mean(), data['lon'].mean()],
popup=popup, icon=folium.Icon(prefix='fa', icon='fish', color='darkblue', icon_color='white')).add_to(m)
#folium.raster_layers.ImageOverlay(
# display,
# bounds=[[latmin, lonmin], [latmax, lonmax]],
#).add_to(m)
#folium.Rectangle(
# bounds=[[latmin, lonmin], [latmax, lonmax]],
# weight=0,
# color=(r, g, b),
# fill_color=(r, g,b),
# fill_opacity=0.0,
# fill=True,
# popup=popup,
# tooltip=d['title'],
# line_join="round",
#).add_to(m)
cpt += 1
m.save('build/index.html')
# -