https://python-visualization.github.io/folium/quickstart.html
m.add_child(folium.LatLngPopup())
m.add_child(folium.map.LayerControl(position='topleft', collapsed=False))
m.fit_bounds(m.get_bounds(),max_zoom=14)
folium.Rectangle(bounds=[(y1,x1), (y2,x2)],
fill=True,
color='orange',
tooltip='xxx',
name = 'xxx',
).add_to(m)
or using GeoPandas and folium.GeoJson, which allows for layer control:
from shapely.geometry import Polygon
import geopandas as gpd
import folium
crs_wgs84 = 'epsg:4326'
bbox = gpd.GeoSeries(Polygon(((x1, y1), (x2,y1), (x2,y2), (x1,y2))))
bbox.crs = crs_wgs84
print(bbox)
m = folium.Map(location=[y0,x0], zoom_start=8, tiles="Stamen Terrain")
folium.GeoJson(bbox,
name=layer_name,
tooltip=layer_name,
style_function=lambda feature: dict(color='yellow', weight=1),
).add_to(m)
m.add_child(folium.map.LayerControl(position='topleft', collapsed=False))
m.fit_bounds(m.get_bounds(),max_zoom=14)
m
Image Layover
yleaflet uses Web Mercator projection (epsg:3857) to display
crs_wgs84 = {'init': 'epsg:4326'}
crs_web = {'init': 'epsg:3857'}
From https://github.com/jupyter-widgets/ipyleaflet/blob/master/examples/Image_slider.ipynb
dataset = rasterio.open('temp.tif')
data = dataset.read()[0][300:1500]
data = np.where(data == 9999, np.nan, data) / 10 # in mm/h
from rasterio.warp import calculate_default_transform, reproject, Resampling
dst_transform, width, height = rasterio.warp.calculate_default_transform(
src_crs=dataset.crs.to_string(),
dst_crs=crs_web,
rows=data.shape[0],
cols=data.shape[1],
left=-180,
right=180,
bottom=-60,
top=60,
)
dst_shape = height, width
destination = np.zeros(dst_shape)
reproject(
source=data,
destination=destination,
src_transform=dataset.transform,
src_crs=datset.crs.to_string(),
dst_transform=dst_transform,
dst_crs=dst_crs,
resampling=Resampling.nearest,
)
return destination
def to_png(data, vmax, fname):
fig, ax = plt.subplots(1, figsize=(36, 12))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
ax.imshow(data, vmin=0, vmax=vmax, interpolation="nearest", cmap=plt.cm.jet)
ax.axis("tight")
ax.axis("off")
plt.savefig(fname)
plt.close()
def display_png(fname):
global io
with open(fname, "rb") as f:
data = b64encode(f.read())
if py3:
data = data.decode("ascii")
imgurl = "data:image/png;base64," + data
if io is None:
io = ImageOverlay(url=imgurl, bounds=bounds, opacity=0.4)
m.add(io)
else:
io.url = imgurl
No subset
import gdal
input_raster = gdal.Open('temp.tif')
gdal.Warp('temp_prj.tif', input_raster, dstSRS='EPSG:4326')
src = rasterio.open('temp_prj.tif')
plt.imshow(src.read(1), cmap='jet', aspect='auto', vmin=0.1, interpolation='nearest')
plt.axis('off')
plt.savefig('temp_prj.png', transparent=True, bbox_inches='tight', dpi=500)
bounds = ((miny, minx), (maxy, maxx))
image = ipyleaflet.ImageOverlay(
url=f'files/GIS_Training/Outputs/Raster_Outputs_Prj/temp_prj.png',
bounds=bounds, name='temp_prj')