Skip to content

Commit

Permalink
Only add one marker and add clearing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrahoang686 committed Jul 31, 2023
1 parent 223f26a commit 5a5781e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
3 changes: 2 additions & 1 deletion stac_ipyleaflet/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def __init__(self, **kwargs):
self.add_biomass_layers_options()
self.add_custom_tools()

# @TODO-SANDRA: Look at how these are being passed in again
self.inspect_control = InspectControlWidget.template(
self,
self.applied_layers,
Expand Down Expand Up @@ -238,7 +239,7 @@ def create_aoi_widget(self):
# NOTE-SANDRA: Create dynamic widget function
def create_inspect_widget(self):
inspect_widget_desc = HTML(
value="<h4><b>Marker</b></h4>",
value="<h4>Marker</h4>",
)
inspect_widget_html = HTML(
value="<code>Waiting for points of interest...</code>",
Expand Down
5 changes: 4 additions & 1 deletion stac_ipyleaflet/widgets/draw.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from ipyleaflet import DrawControl, GeoJSON
from ipywidgets import Box, Output

# @NOTE: This should be an extension of the IPYLEAFLET Class. Currently it is just being passed
# in instead due to import errors

# TODO: Fix linting errors caused by inferred inheritance and just pass in params instead

# @TODO: Fix linting errors caused by inferred inheritance and just pass in params instead
class DrawControlWidget:
def template(self, **kwargs) -> Box(style={"max_height: 200px"}):
main = self
Expand Down
34 changes: 24 additions & 10 deletions stac_ipyleaflet/widgets/inspect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ipyleaflet import DrawControl, MarkerCluster, Marker, DrawControl
from ipyleaflet import DrawControl, MarkerCluster, Marker, DrawControl, GeoJSON
from urllib.parse import urlparse, parse_qs
from typing import List

Expand All @@ -14,12 +14,13 @@ class LayerData:
data: COGRequestedData


# @NOTE: This should be an extension of the IPYLEAFLET Class. Currently it is just being passed
# in instead due to import errors
class InspectControlWidget:
def template(
self, applied_layers, interact_widget, make_get_request, titiler_endpoint
):
main = self
marker_cluster = list(MarkerCluster().markers)
draw_control = DrawControl(
edit=False,
remove=False,
Expand All @@ -41,7 +42,7 @@ def template(
interact_tab = (
interact_widget.children[0].children[0].children[0].children[0].children
)
point_coords = interact_tab[1]
point_data = interact_tab[1]
clear_button = interact_tab[2]

def get_visible_layers_data(coordinates) -> List[LayerData]:
Expand Down Expand Up @@ -77,6 +78,8 @@ def get_visible_layers_data(coordinates) -> List[LayerData]:
return visible_layers_data

def display_data(layers_data: LayerData):
point_data.value = ""

def create_html_template(layer_name, coordinates, values, band_names):
template = f"""
<p>
Expand All @@ -99,7 +102,7 @@ def create_html_template(layer_name, coordinates, values, band_names):
return template

for layer in layers_data:
point_coords.value += create_html_template(
point_data.value += create_html_template(
layer["layer_name"],
layer["data"]["coordinates"],
layer["data"]["values"],
Expand All @@ -109,25 +112,36 @@ def create_html_template(layer_name, coordinates, values, band_names):

def handle_interaction(self, action, geo_json, **kwargs):
def handle_clear(event):
self.clear() # NOTE: This will likely clear the aoi drawing as well...
point_coords.value = "<code>Waiting for points of interest...</code>"
draw_layer = main.find_layer("draw_layer")
main.remove_layer(draw_layer)
point_data.value = "<code>Waiting for points of interest...</code>"
clear_button.disabled = True
return

self.coordinates = []

if action == "created":
if geo_json["geometry"] and geo_json["geometry"]["type"] == "Point":
self.coordinates = geo_json["geometry"]["coordinates"]
marker_cluster.append(
Marker(location=(self.coordinates[0], self.coordinates[1]))
geojson_layer = GeoJSON(
name="draw_layer",
data=geo_json,
style={
"fillColor": "transparent",
"color": "#333",
"weight": 3,
},
)
main.add_layer(geojson_layer)
self.coordinates = geo_json["geometry"]["coordinates"]
print(f"applied_layers: {applied_layers}")
point_coords.value = f"<p><b>Coordinates:</b></p><code>{self.coordinates}</code><br/>"

if len(applied_layers):
layers_data = get_visible_layers_data(self.coordinates)
if layers_data:
display_data(layers_data)
elif not len(applied_layers):
point_data.value = f"<p><b>Coordinates:</b></p><code>{self.coordinates}</code><br/>"
self.clear()
clear_button.disabled = False
clear_button.on_click(handle_clear)
return
Expand Down

0 comments on commit 5a5781e

Please sign in to comment.