ColorPicker¶
Use lonboard with an ipywidgets ColorPicker object.
Example gif:
Dependencies¶
lonboard
-
Run
pip install geodatasets
if needed.
Imports¶
import geodatasets
import geopandas as gpd
import ipywidgets
from lonboard import SolidPolygonLayer
geodatasets.get_path
will download the dataset if it hasn't been downloaded and cached before.
gdf = gpd.read_file(geodatasets.get_path("nybb"))
Create our basic map:
map_ = SolidPolygonLayer.from_geopandas(gdf)
map_
/Users/kyle/github/developmentseed/lonboard/lonboard/layer.py:571: UserWarning: GeoDataFrame being reprojected to EPSG:4326 warnings.warn("GeoDataFrame being reprojected to EPSG:4326")
SolidPolygonLayer(table=pyarrow.Table BoroCode: int64 BoroName: string Shape_Leng: double Shape_Area: double g…
Create the ColorPicker
widget:
color_picker = ipywidgets.ColorPicker()
color_picker
ColorPicker(value='black')
Note that the "data" contained by the color_picker
instance is held by the value
attribute:
color_picker.value
'black'
We'll now link the two widgets together, so that whenever the color picker is updated, ipywidgets
will update the value of get_fill_color
on the map.
The format of dlink
is:
- tuple: (source widget, attribute of source widget)
- tuple: (target widget, attribute of target widget)
So we use the below syntax to link color_picker.value
to map_.get_fill_color
.
We need to use dlink
, not link
, which creates a one-directional link (from the color picker to the map) and not a bi-directional link. This is because the color picker and the map have a different color representation under the hood (ColorPicker
stores a hex string; the SolidPolygonLayer
works in terms of RGBA color integers).
ipywidgets.dlink(
(color_picker, 'value'),
(map_, 'get_fill_color')
)
<traitlets.traitlets.directional_link at 0x127dd8a90>
Now click on the color widget and change the color! You need to press Enter to select a specific color.