From 9347c95ccbfea2657e9cd608cc4ea709a56b0480 Mon Sep 17 00:00:00 2001 From: Emma Paz Date: Thu, 25 May 2023 20:19:15 +0000 Subject: [PATCH] restrict draw tool to 1 polygon --- stac_ipyleaflet/widgets/draw.py | 78 +++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/stac_ipyleaflet/widgets/draw.py b/stac_ipyleaflet/widgets/draw.py index 5e4f65f..f90f148 100644 --- a/stac_ipyleaflet/widgets/draw.py +++ b/stac_ipyleaflet/widgets/draw.py @@ -1,4 +1,4 @@ -from ipyleaflet import DrawControl, Polygon, Rectangle +from ipyleaflet import DrawControl, GeoJSON from ipywidgets import Box, Output class DrawControlWidget(): @@ -9,36 +9,59 @@ def template(self, **kwargs) -> Box( style={"max_height: 200px"} ): # Set unwanted draw controls to False or empty objects draw_control = DrawControl( edit=False, - # remove=False, + remove=False, circlemarker = {}, polygon = {}, polyline = {}, - ) - - - def handle_draw(self, action, geo_json): - aoi_coords = main.aoi_widget.children[1] - main.aoi_coordinates = [] - main.aoi_bbox = () - - if action == "deleted": - aoi_coords.value = "Waiting for area of interest..." - main.aoi_coordinates = [] - main.aoi_bbox = () - if action == "created": - if geo_json["geometry"]: - raw_coordinates = geo_json["geometry"]["coordinates"][0] - def bounding_box(points): - x_coordinates, y_coordinates = zip(*points) - return (min(x_coordinates), min(y_coordinates), max(x_coordinates), max(y_coordinates)) - bbox = bounding_box(raw_coordinates) - main.aoi_coordinates = raw_coordinates - main.aoi_bbox = bbox - coords_list = [coord for coord in raw_coordinates] - coords = (",
".join(map(str, coords_list))) - aoi_coords.value = f"

Coordinates:

{coords}

BBox:

{bbox}" + ) + + aoi_coords = main.aoi_widget.children[1] + aoi_clear_button = main.aoi_widget.children[2] + + def handle_clear(self): + draw_layer = main.find_layer("draw_layer") + main.remove_layer(draw_layer) + aoi_coords.value = "Waiting for area of interest..." + aoi_clear_button.disabled = True + + def handle_draw(self, action, geo_json, **kwargs): + main.aoi_coordinates = [] + main.aoi_bbox = () + + + if action == "created": + if geo_json["geometry"]: + geojson_layer = GeoJSON( + name="draw_layer", + data=geo_json, + style={ + "fillColor": "transparent", + "color": "#333", + "weight": 3 + } + ) + main.add_layer(geojson_layer) + raw_coordinates = geo_json["geometry"]["coordinates"][0] + def bounding_box(points): + x_coordinates, y_coordinates = zip(*points) + return (min(x_coordinates), min(y_coordinates), max(x_coordinates), max(y_coordinates)) + bbox = bounding_box(raw_coordinates) + main.aoi_coordinates = raw_coordinates + main.aoi_bbox = bbox + coords_list = [coord for coord in raw_coordinates] + coords = (",
".join(map(str, coords_list))) + aoi_coords.value = f"

Coordinates:

{coords}

BBox:

{bbox}" + self.clear() + aoi_clear_button.disabled = False + aoi_clear_button.on_click(handle_clear) + + return + def value_changed(change): + print("CHANGE", change) + draw_control.on_draw(callback=handle_draw) + draw_control.observe(value_changed, names=["value"]) # Add rectangle draw control for bounding box draw_control.rectangle = { @@ -46,7 +69,8 @@ def bounding_box(points): "fillColor": "transparent", "color": "#333", "fillOpacity": 1.0 - } + }, + "repeatMode": False } draw_control.output = bbox_out