Skip to content

Commit

Permalink
restrict draw tool to 1 polygon
Browse files Browse the repository at this point in the history
  • Loading branch information
emmalu committed May 25, 2023
1 parent f6bc293 commit 9347c95
Showing 1 changed file with 51 additions and 27 deletions.
78 changes: 51 additions & 27 deletions stac_ipyleaflet/widgets/draw.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ipyleaflet import DrawControl, Polygon, Rectangle
from ipyleaflet import DrawControl, GeoJSON
from ipywidgets import Box, Output

class DrawControlWidget():
Expand All @@ -9,44 +9,68 @@ 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 = "<code>Waiting for area of interest...</code>"
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 = (",<br/>".join(map(str, coords_list)))
aoi_coords.value = f"<p><b>Coordinates:</b></p><code>{coords}</code><br/><p><b>BBox:</b></p><code>{bbox}</code>"
)

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 = "<code>Waiting for area of interest...</code>"
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 = (",<br/>".join(map(str, coords_list)))
aoi_coords.value = f"<p><b>Coordinates:</b></p><code>{coords}</code><br/><p><b>BBox:</b></p><code>{bbox}</code>"
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 = {
"shapeOptions": {
"fillColor": "transparent",
"color": "#333",
"fillOpacity": 1.0
}
},
"repeatMode": False
}
draw_control.output = bbox_out

Expand Down

0 comments on commit 9347c95

Please sign in to comment.