You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importipywidgetsaswidgetsfromipyleafletimportMap, GeomanDrawControl, basemaps# Radio buttons for class selectionclass_selector=widgets.RadioButtons(
options=["Water", "Crop", "Urban"], description="Class:", disabled=False
)
# Function to return different colors based on classdefget_class_color(polygon_class):
ifpolygon_class=="Water":
return"#0000FF"# Blueelifpolygon_class=="Crop":
return"#008800"# Greenelifpolygon_class=="Urban":
return"#FF0000"# Redreturn"#000000"# Default blackm=Map(center=(51.0, 0.0), zoom=12, basemap=basemaps.OpenStreetMap.Mapnik)
classLabelTool:
def__init__(self, m):
self.m=mself.current_polygon=None# Initial color is blue for "Water"initial_color=get_class_color(class_selector.value)
# Geoman Draw controlself.draw_control=GeomanDrawControl(
circle={},
polyline={},
polygon={"shapeOptions": {"color": initial_color}}, # Initial color
)
self.m.add_control(self.draw_control)
# Set up event listenersself.draw_control.on_draw(self.handle_draw)
class_selector.observe(self.update_draw_color, 'value')
# Draw event handlerdefhandle_draw(self, target, action, geo_json):
ifaction=='create':
self.current_polygon=geo_jsonpolygon_class=class_selector.valuecolor=get_class_color(polygon_class)
# Update drawn polygon's color by recreating the control with the new colorself.update_draw_color({'new': polygon_class})
# Update polygon color based on the selected classdefupdate_draw_color(self, change):
polygon_class=class_selector.valuecolor=get_class_color(polygon_class)
# Remove and recreate draw control with updated colorself.m.remove_control(self.draw_control)
self.draw_control=GeomanDrawControl(
circle={},
polyline={},
polygon={"shapeOptions": {"color": color}}, # Updated color
)
self.m.add_control(self.draw_control)
# Reattach the draw event handlerself.draw_control.on_draw(self.handle_draw)
# Display map and widgetsl=LabelTool(m)
widgets.VBox([m, class_selector])
The problem is that when the radio value is changed, the next create action draws several superimposed identical polygons (see the following screenshot, I slighly shifted the polygons for visualization)
Any idea how to fix that?
The text was updated successfully, but these errors were encountered:
I am trying to draw polygons where the polygon color is determined by
ipywidget.widgets.RadioButtons
This is what I got so far (inspired by #425)
The problem is that when the radio value is changed, the next
create
action draws several superimposed identical polygons (see the following screenshot, I slighly shifted the polygons for visualization)Any idea how to fix that?
The text was updated successfully, but these errors were encountered: