Create a layer widget for changing the visibility and opacity of a style layer. Args: id: The is of the layer. Returns: ipywidgets.Widget: The layer widget.
(self, id: str | None = None)
| 1389 | return hbox |
| 1390 | |
| 1391 | def style_layer_interact(self, id: str | None = None): |
| 1392 | """Create a layer widget for changing the visibility and opacity of a style layer. |
| 1393 | |
| 1394 | Args: |
| 1395 | id: The is of the layer. |
| 1396 | |
| 1397 | Returns: |
| 1398 | ipywidgets.Widget: The layer widget. |
| 1399 | """ |
| 1400 | layer_ids = list(self.style_dict.keys()) |
| 1401 | layer_ids.sort() |
| 1402 | if id is None: |
| 1403 | id = layer_ids[0] |
| 1404 | elif id not in layer_ids: |
| 1405 | raise ValueError(f"Layer {id} not found.") |
| 1406 | |
| 1407 | layer = self.style_dict[id] |
| 1408 | layer_type = layer.get("type") |
| 1409 | style = {"description_width": "initial"} |
| 1410 | dropdown = widgets.Dropdown( |
| 1411 | options=layer_ids, |
| 1412 | value=id, |
| 1413 | description="Layer", |
| 1414 | style=style, |
| 1415 | ) |
| 1416 | |
| 1417 | visibility = layer.get("layout", {}).get("visibility", "visible") |
| 1418 | if visibility == "visible": |
| 1419 | visibility = True |
| 1420 | else: |
| 1421 | visibility = False |
| 1422 | |
| 1423 | checkbox = widgets.Checkbox( |
| 1424 | description="Visible", |
| 1425 | value=visibility, |
| 1426 | style=style, |
| 1427 | layout=widgets.Layout(width="120px"), |
| 1428 | ) |
| 1429 | |
| 1430 | opacity = layer.get("paint", {}).get(f"{layer_type}-opacity", 1.0) |
| 1431 | opacity_slider = widgets.FloatSlider( |
| 1432 | description="Opacity", |
| 1433 | min=0, |
| 1434 | max=1, |
| 1435 | step=0.01, |
| 1436 | value=opacity, |
| 1437 | style=style, |
| 1438 | ) |
| 1439 | |
| 1440 | def extract_rgb(rgba_string): |
| 1441 | # Extracting the RGB values using regex |
| 1442 | rgb_tuple = tuple(map(int, re.findall(r"\d+", rgba_string)[:3])) |
| 1443 | return rgb_tuple |
| 1444 | |
| 1445 | color = layer.get("paint", {}).get(f"{layer_type}-color", "white") |
| 1446 | if color.startswith("rgba"): |
| 1447 | color = extract_rgb(color) |
| 1448 | color = coreutils.check_color(color) |