import os
import folium
print(folium.__version__)
0.6.0
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.feature as cfeature
LAND = cfeature.NaturalEarthFeature(
'physical', 'land', '50m',
edgecolor='face',
facecolor=cfeature.COLORS['land']
)
def make_map(bbox, projection=ccrs.Mercator()):
fig, ax = plt.subplots(
figsize=(8, 6),
subplot_kw={'projection': projection}
)
ax.set_extent(bbox)
ax.add_feature(LAND, facecolor='0.25')
ax.coastlines(resolution='50m')
return fig, ax
import numpy.ma as ma
from scipy.io import loadmat
fname = os.path.join('data', 'mercator_temperature.mat')
merc = loadmat(fname, squeeze_me=True)
x, y = merc['x'], merc['y']
bbox = [x.min(), x.max(), y.min(), y.max()]
level = 10 # 10 meters temperature.
data = ma.masked_invalid(merc['temp'][level, ...])
fig, ax = make_map(bbox=bbox)
cs = ax.pcolormesh(x, y, data, transform=ccrs.PlateCarree())
fig.savefig(os.path.join('results', 'GS.png'), transparent=True)
import numpy as np
from PIL import Image, ImageChops
def trim(img):
border = Image.new(img.mode, img.size, img.getpixel((0, 0)))
diff = ImageChops.difference(img, border)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
if bbox:
img = img.crop(bbox)
return np.array(img)
with Image.open(os.path.join('results', 'GS.png')) as img:
img = trim(img)
image = np.zeros((61, 61))
image[0, :] = 1.0
image[60, :] = 1.0
image[:, 0] = 1.0
image[:, 60] = 1.0
from folium.raster_layers import ImageOverlay
zoom_start = 5
m = folium.Map(location=[y.mean(), x.mean()], zoom_start=zoom_start)
ImageOverlay(
image=image,
bounds=[[34.5, -71.7], [36.6, -69]],
colormap=lambda x: (1, 0, 0, x),
zindex=1,
).add_to(m)
ImageOverlay(
image=img,
bounds=[[y.min(), x.min()], [y.max(), x.max()]],
opacity=0.95,
pixelated=False,
).add_to(m)
m
Bypass the figure creation entirely:
from matplotlib import colors
carto_temps = [
'#009392',
'#39b185',
'#9ccb86',
'#e9e29c',
'#eeb479',
'#e88471',
'#cf597e',
]
cmap = colors.ListedColormap(carto_temps, name='carto_temps')
def colorize(array, cmap):
normed_data = (array - array.min()) / (array.max() - array.min())
cm = plt.cm.get_cmap(cmap)
return cm(normed_data)
colored_data = colorize(data, cmap)
m = folium.Map(location=[y.mean(), x.mean()], zoom_start=zoom_start)
ImageOverlay(
image=image,
bounds=[[34.5, -71.7], [36.6, -69]],
colormap=lambda x: (1, 0, 0, x),
zindex=2,
).add_to(m)
ImageOverlay(
image=np.flipud(colored_data),
bounds=[[y.min(), x.min()], [y.max(), x.max()]],
opacity=1,
pixelated=True,
zindex=1,
).add_to(m)
m
import matplotlib.pyplot as plt
import mplleaflet
fig, ax = plt.subplots()
ax.contour(x, y, data)
gj = mplleaflet.fig_to_geojson(fig=fig)
How does a single feature looks like?
gj['features'][0]
{'type': 'Feature', 'geometry': {'type': 'LineString', 'coordinates': [[-69.97959325412467, 41.978797912597656], [-69.83333587646484, 41.88143903101549], [-69.80411639170462, 41.85477828979492], [-69.78796634397719, 41.820953627356644]]}, 'properties': {'color': '#440154', 'weight': 1.5, 'opacity': None, 'dashArray': '10,0'}}
import folium
m = folium.Map(location=[y.mean(), x.mean()], zoom_start=zoom_start)
# We need to parse the feature `properties` correctly.
folium.features.GeoJson(gj).add_to(m)
m
mplleaflet.show(fig=fig, tiles='mapbox bright')