from pathlib import Path
import pandas as pd
import os
os.environ['USE_PYGEOS'] = '0'
import geopandas as gpd
import requests
import io
import sys
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
# import colorcet as cc
import datashader as ds
# import datashader.transfer_functions as tf
import holoviews as hv
from holoviews.operation.datashader import rasterize
from holoviews import opts
import geoviews as gv
import cartopy.crs as ccrs
from spatialpandas import GeoDataFrame
from dask import dataframe as dd
gv.extension('bokeh')- Prepared plots showing extended wetland priority habitats.
- Tried Holoviews/Geoviews/Datashader for interactive plots, they works very well.
- Tried jupyterlite and pyodide but there are no wheels available for datashader and co. so micropip was not able to run with them
url = 'https://github.com/iacopoff/iacopoff.github.io/raw/main/data/n2k_extwet_priority_poly_onehot_s.fgb'dn = requests.get(url).contentdf = gpd.read_file(io.BytesIO(dn))url2 = 'https://github.com/iacopoff/iacopoff.github.io/raw/main/data/annex_I_extended_wetlands.csv'
dn2 = requests.get(url2).content
lookup = pd.read_csv(io.StringIO(dn2.decode('utf-8'))).set_index('habitatcode')hcodes = df.filter(regex='\d',axis=1).columns.tolist()df['n_pr_hb'] = df.filter(regex='\d',axis=1).sum(axis=1)df = df.to_crs(3857)df_sp = GeoDataFrame(df)ddf = dd.from_pandas(df_sp, npartitions=10)# cvs = ds.Canvas()
# tf.shade(cvs.polygons(ddf[ddf['3170'] == 1], geometry='geometry', agg=ds.mean('n_pr_hb')), cmap=cc.kg);bm = hv.element.tiles.EsriUSATopo()def get_canvas(hcodes, bm):
out = []
for h in hcodes:
out.append(bm * rasterize(hv.Polygons(ddf[ddf[h] == 1], vdims=['sitename','n_pr_hb']), aggregator=ds.mean('n_pr_hb')).opts(
colorbar=True, cmap='bgy',fontsize={'title':8}))
return outouts = get_canvas(hcodes, bm)# def compute_partitions(el):
# n = ddf[ddf['3170'] == 1].cx_partitions[slice(*el.range('x')), slice(*el.range('y'))].npartitions
# return el.opts(title=f'Population by country (npartitions: {n})')
# bm * out1.apply(compute_partitions).opts(width=700, height=500, tools=["hover"])# opts.Polygons(logz=True, tools=['hover'], xaxis=None, yaxis=None,
# show_grid=False, show_frame=False, width=500, height=500,
# color_index='das', colorbar=True, toolbar='above', line_color='white')hv.Layout([ts.opts(width=400, height=300).relabel(name + ": " + lookup.loc[name][0]) for name, ts in zip(hcodes,outs)]).opts(
opts.Tiles(xaxis=None, yaxis=None, width=100, height=100)).cols(3)?(caption)