Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Goal of simplifying multi-color rasterize analysis into one line in hvplot? #1376

Open
ahuang11 opened this issue Jul 19, 2024 · 0 comments
Open
Labels
type: enhancement New feature or request

Comments

@ahuang11
Copy link
Collaborator

ahuang11 commented Jul 19, 2024

image

I want to simplify this workflow down to a single call in hvplot.

df.hvplot(
    "LON",
    "LAT",
    groupby="BaseDateTime",
    geo=True,
    tiles=True,
    datashade=True,
    dynspread=True,
    by="VesselType",
    aggregator=ds.count_cat,
    cmap="glasbey_bw_minc_20_minl_30",
)

I think it's doable, but hvplot is missing a few features, so this issue is kind of like an epic

  1. Automatic lon/lat to easting/northing Automatically or manually toggle to automatically use lon_lat_to_easting_northing #1375
  2. Map categorical strings to color given a cmap (or default cmap) and use as color_key in datashade
  3. Create legend based on the categories Util to create a custom legend holoviews#6320

For 2, I think rather than having a column be the actual color, color=["red", "green", "blue"], if it detects non-valid colors, it would automatically use a cmap to map categories to color (I always find that tedious) https://holoviews.org/user_guide/Style_Mapping.html#styling-mapping

To reproduce:
Data download: https://coast.noaa.gov/htdata/CMSP/AISDataHandler/2020/index.html

import pandas as pd
import hvplot.pandas
import geoviews as gv
import pandas as pd
import datashader as ds
import colorcet as cc
import holoviews as hv
import holoviews.operation.datashader as hd
from holoviews.util.transform import lon_lat_to_easting_northing

df = pd.read_csv("AIS_2020_01_01.csv", parse_dates=True, index_col="BaseDateTime")
df = df.assign(
    **pd.concat(lon_lat_to_easting_northing(df["LON"], df["LAT"]), axis=1).rename(
        {"LON": "EASTING", "LAT": "NORTHING"}, axis=1
    )
)

df.index = df.index.round("1Min")
df = df.sort_index()
df.index = df.index.astype(str)

vessel_types = pd.read_csv("AIS_categories.csv")

categories = {
    r.num: r.category if r.category in [0, 2, 3, 6, 7, 16, 14, 19, 12, 18] else 21
    for i, r in vessel_types.iterrows()
}


def category_desc(val):
    """Return description for the category with the indicated integer value"""
    return vessel_types[vessel_types.category == val].iloc[0].category_desc


groups = {categories[i]: category_desc(categories[i]) for i in categories.keys()}
colors = cc.glasbey_bw_minc_20_minl_30
color_key = {
    list(groups.keys())[i]: tuple(int(e * 255.0) for e in v)
    for i, v in enumerate(colors[: (len(groups))][::-1])
}
legend = hv.NdOverlay(
    {
        groups[k]: hv.Points([0, 0], label=str(groups[k])).opts(
            color=cc.rgb_to_hex(*v), size=0
        )
        for k, v in color_key.items()
    }
)

df = df.loc[df["VesselType"].isin(color_key)]

pts = hv.Dataset(df, kdims=["EASTING", "NORTHING"], vdims=["VesselType"]).to(
    hv.Points, groupby=["BaseDateTime"]
)
points = hd.dynspread(
    hd.datashade(pts, aggregator=ds.count_cat("VesselType"), color_key=color_key)
)

tiles = (
    hv.element.tiles.ESRI()
    .opts(alpha=0.4, bgcolor="black")
    .opts(responsive=True, min_height=600)
)
labels = hv.element.tiles.CartoDark().opts(alpha=0.7, level="glyph")
display(
    tiles
    * labels
    * points.opts(show_legend=False)
    * legend.opts(xaxis="bare", yaxis="bare", title="")
)
@ahuang11 ahuang11 added TRIAGE type: enhancement New feature or request and removed TRIAGE labels Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant