Skip to content

toolbar module

A module for interactive toolbars with ipywidgets. Source: Dr. Qiusheng Wu -- https://github.com/giswqs/geodemo/blob/master/geodemo/toolbar.py

change_basemap(m)

Widget for change basemaps. Source: Dr. Qiusheng Wu -- https://github.com/giswqs/geemap/blob/master/geemap/toolbar.py

Parameters:

Name Type Description Default
m object

hagerstrand.Map()

required
Source code in hagerstrand/toolbar.py
def change_basemap(m):
    """Widget for change basemaps. Source: Dr. Qiusheng Wu -- https://github.com/giswqs/geemap/blob/master/geemap/toolbar.py
    Args:
        m (object): hagerstrand.Map()
    """
    from .basemaps import _ee_basemaps

    dropdown = widgets.Dropdown(
        options=list(_ee_basemaps.keys()),
        value="ROADMAP",
        layout=widgets.Layout(width="200px"),
        description="Basemaps",
    )

    close_btn = widgets.Button(
        icon="times",
        tooltip="Close the basemap widget",
        button_style="primary",
        layout=widgets.Layout(width="32px"),
    )

    basemap_widget = widgets.HBox([dropdown, close_btn])

    def on_click(change):
        basemap_name = change["new"]

        if len(m.layers) == 1:
            old_basemap = m.layers[0]
        else:
            old_basemap = m.layers[1]
        m.substitute_layer(old_basemap, _ee_basemaps[basemap_name])

    dropdown.observe(on_click, "value")

    def close_click(change):
        m.toolbar_reset()
        if m.basemap_ctrl is not None and m.basemap_ctrl in m.controls:
            m.remove_control(m.basemap_ctrl)
        basemap_widget.close()

    close_btn.on_click(close_click)

    basemap_control = WidgetControl(widget=basemap_widget, position="topright")
    m.add_control(basemap_control)
    m.basemap_ctrl = basemap_control

filter_df_widget(df, field)

Widget for filtering a DataFrame

Parameters:

Name Type Description Default
df pd.DataFrame

A DataFrame to filter

required
field str

A field within the DataFrame for the filter criterion

required
Source code in hagerstrand/toolbar.py
def filter_df_widget(df, field):
    """Widget for filtering a DataFrame

    Args:
        df (pd.DataFrame): A DataFrame to filter
        field (str): A field within the DataFrame for the filter criterion
    """

    dropdown_field = widgets.Dropdown(
        options = unique_sorted_values_plus_ALL(df[field]),
        value="ALL",
        layout=widgets.Layout(width="200px"),
        description=field
    )

    close_dropdown_field = widgets.Button(
        icon="times",
        tooltip="Close the filter widget",
        button_style="primary",
        layout=widgets.Layout(width="32px")
    )

    filter_widget = widgets.HBox([dropdown_field, close_dropdown_field])

    out_field = widgets.Output()

    def on_click_dropdown(change):
        with out_field:
            out_field.clear_output()
            if (change.new == 'ALL'):
                display(df)
            else:
                display(df[df[field] == change.new])

    dropdown_field.observe(on_click_dropdown, names="value")

    def close_click_dropdown(change):
        filter_widget.close()

    close_dropdown_field.on_click(close_click_dropdown)

    display(filter_widget)
    display(out_field)

Last update: 2021-05-03