Skip to content

Commit

Permalink
Revert "Clean up layer management ivars."
Browse files Browse the repository at this point in the history
This reverts commit 3fa6599.
  • Loading branch information
naschmitz committed Aug 29, 2023
1 parent 3fa6599 commit 899501a
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 146 deletions.
131 changes: 56 additions & 75 deletions geemap/geemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,13 @@ def __init__(self, **kwargs):

# Map attributes for layers
self.geojson_layers = []
self.ee_layers = {}
self.ee_layers = []
self.ee_layer_names = []
self.ee_raster_layers = []
self.ee_raster_layer_names = []
self.ee_vector_layers = []
self.ee_vector_layer_names = []
self.ee_layer_dict = {}

# ipyleaflet built-in layer control
self.layer_control = None
Expand All @@ -288,53 +294,6 @@ def __init__(self, **kwargs):
self.roi_reducer = ee.Reducer.mean()
self.roi_reducer_scale = None

@property
def ee_layer_names(self):
warnings.warn(
"ee_layer_names is deprecated. Use ee_layers.keys() instead.",
DeprecationWarning,
)
return self.ee_layers.keys()

@property
def ee_layer_dict(self):
warnings.warn(
"ee_layer_dict is deprecated. Use ee_layers instead.", DeprecationWarning
)
return self.ee_layers

@property
def ee_raster_layer_names(self):
warnings.warn(
"ee_raster_layer_names is deprecated. Use self.ee_raster_layers.keys() instead.",
DeprecationWarning,
)
return self.ee_raster_layers.keys()

@property
def ee_vector_layer_names(self):
warnings.warn(
"ee_vector_layer_names is deprecated. Use self.ee_vector_layers.keys() instead.",
DeprecationWarning,
)
return self.ee_vector_layers.keys()

@property
def ee_raster_layers(self):
return dict(filter(self._raster_filter, self.ee_layers.items()))

@property
def ee_vector_layers(self):
return dict(filter(self._vector_filter, self.ee_layers.items()))

def _raster_filter(self, pair):
return isinstance(pair[1]["ee_object"], (ee.Image, ee.ImageCollection))

def _vector_filter(self, pair):
return isinstance(
pair[1]["ee_object"], (ee.Geometry, ee.Feature, ee.FeatureCollection)
)

def add(self, object):
"""Adds a layer or control to the map.
Expand Down Expand Up @@ -447,21 +406,30 @@ def add_ee_layer(

layer = self.find_layer(name=name)
if layer is not None:
existing_object = self.ee_layers[name]["ee_object"]
existing_object = self.ee_layer_dict[name]["ee_object"]

if isinstance(existing_object, (ee.Image, ee.ImageCollection)):
self.ee_raster_layers.remove(existing_object)
self.ee_raster_layer_names.remove(name)
if (
hasattr(self, "_plot_dropdown_widget")
and self._plot_dropdown_widget is not None
):
self._plot_dropdown_widget.options = list(
self.ee_raster_layers.keys()
self.ee_raster_layer_names
)
elif isinstance(ee_object, (ee.Geometry, ee.Feature, ee.FeatureCollection)):
self.ee_vector_layers.remove(existing_object)
self.ee_vector_layer_names.remove(name)

self.ee_layers.pop(name, None)
self.ee_layers.remove(existing_object)
self.ee_layer_names.remove(name)
self.remove_layer(layer)

self.ee_layers[name] = {
self.ee_layers.append(ee_object)
if name not in self.ee_layer_names:
self.ee_layer_names.append(name)
self.ee_layer_dict[name] = {
"ee_object": ee_object,
"ee_layer": tile_layer,
"vis_params": vis_params,
Expand All @@ -470,11 +438,16 @@ def add_ee_layer(
self.add(tile_layer)

if isinstance(ee_object, (ee.Image, ee.ImageCollection)):
self.ee_raster_layers.append(ee_object)
self.ee_raster_layer_names.append(name)
if (
hasattr(self, "_plot_dropdown_widget")
and self._plot_dropdown_widget is not None
):
self._plot_dropdown_widget.options = list(self.ee_raster_layers.keys())
self._plot_dropdown_widget.options = list(self.ee_raster_layer_names)
elif isinstance(ee_object, (ee.Geometry, ee.Feature, ee.FeatureCollection)):
self.ee_vector_layers.append(ee_object)
self.ee_vector_layer_names.append(name)

arc_add_layer(tile_layer.url_format, name, shown, opacity)

Expand All @@ -486,9 +459,17 @@ def remove_ee_layer(self, name):
Args:
name (str): The name of the Earth Engine layer to remove.
"""
if name in self.ee_layers:
ee_layer = self.ee_layers[name]["ee_layer"]
self.ee_layers.pop(name, None)
if name in self.ee_layer_dict:
ee_object = self.ee_layer_dict[name]["ee_object"]
ee_layer = self.ee_layer_dict[name]["ee_layer"]
if name in self.ee_raster_layer_names:
self.ee_raster_layer_names.remove(name)
self.ee_raster_layers.remove(ee_object)
elif name in self.ee_vector_layer_names:
self.ee_vector_layer_names.remove(name)
self.ee_vector_layers.remove(ee_object)
self.ee_layers.remove(ee_object)
self.ee_layer_names.remove(name)
if ee_layer in self.layers:
self.remove_layer(ee_layer)

Expand Down Expand Up @@ -1117,8 +1098,8 @@ def add_legend(
else:
self.legends.append(legend_control)

if layer_name in self.ee_layers:
self.ee_layers[layer_name]["legend"] = legend_control
if layer_name in self.ee_layer_names:
self.ee_layer_dict[layer_name]["legend"] = legend_control

except Exception as e:
raise Exception(e)
Expand Down Expand Up @@ -1179,10 +1160,10 @@ def add_colorbar(
)

self._colorbar = colormap_ctrl
if layer_name in self.ee_layers:
if "colorbar" in self.ee_layers[layer_name]:
self.remove_control(self.ee_layers[layer_name]["colorbar"])
self.ee_layers[layer_name]["colorbar"] = colormap_ctrl
if layer_name in self.ee_layer_names:
if "colorbar" in self.ee_layer_dict[layer_name]:
self.remove_control(self.ee_layer_dict[layer_name]["colorbar"])
self.ee_layer_dict[layer_name]["colorbar"] = colormap_ctrl
if not hasattr(self, "colorbars"):
self.colorbars = [colormap_ctrl]
else:
Expand Down Expand Up @@ -1219,7 +1200,7 @@ def create_vis_widget(self, layer_dict):
"""Create a GUI for changing layer visualization parameters interactively.
Args:
layer_dict (dict): A dict containning information about the layer. It is an element from Map.ee_layers.
layer_dict (dict): A dict containning information about the layer. It is an element from Map.ee_layer_dict.
Returns:
object: An ipywidget.
Expand Down Expand Up @@ -2446,16 +2427,16 @@ def import_btn_clicked(b):
def apply_btn_clicked(b):
compute_label.value = "Computing ..."

if new_layer_name.value in self.ee_layers:
if new_layer_name.value in self.ee_layer_names:
old_layer = new_layer_name.value

if "legend" in self.ee_layers[old_layer].keys():
legend = self.ee_layers[old_layer]["legend"]
if "legend" in self.ee_layer_dict[old_layer].keys():
legend = self.ee_layer_dict[old_layer]["legend"]
if legend in self.controls:
self.remove_control(legend)
legend.close()
if "colorbar" in self.ee_layers[old_layer].keys():
colorbar = self.ee_layers[old_layer]["colorbar"]
if "colorbar" in self.ee_layer_dict[old_layer].keys():
colorbar = self.ee_layer_dict[old_layer]["colorbar"]
if colorbar in self.controls:
self.remove_control(colorbar)
colorbar.close()
Expand Down Expand Up @@ -2638,7 +2619,7 @@ def _on_close():
self.layer_manager_control = None

def _on_open_vis(layer_name):
self.create_vis_widget(self.ee_layers.get(layer_name, None))
self.create_vis_widget(self.ee_layer_dict.get(layer_name, None))

self.layer_manager_widget = map_widgets.LayerManager(self)
self.layer_manager_widget.collapsed = not opened
Expand Down Expand Up @@ -3156,7 +3137,7 @@ def plot_raster(
self.default_style = {"cursor": "crosshair"}
msg = "The plot function can only be used on ee.Image or ee.ImageCollection with more than one band."
if (ee_object is None) and len(self.ee_raster_layers) > 0:
ee_object = self.ee_raster_layers.values()[-1]["ee_object"]
ee_object = self.ee_raster_layers[-1]
if isinstance(ee_object, ee.ImageCollection):
ee_object = ee_object.mosaic()
elif isinstance(ee_object, ee.ImageCollection):
Expand Down Expand Up @@ -3728,8 +3709,8 @@ def add_colorbar_branca(
else:
self.colorbars.append(colormap_ctrl)

if layer_name in self.ee_layers:
self.ee_layers[layer_name]["colorbar"] = colormap_ctrl
if layer_name in self.ee_layer_names:
self.ee_layer_dict[layer_name]["colorbar"] = colormap_ctrl

def image_overlay(self, url, bounds, name):
"""Overlays an image from the Internet or locally on the map.
Expand Down Expand Up @@ -5153,7 +5134,7 @@ def add_time_slider(
ee_object = ee_object.clip(region)
elif isinstance(region, ee.FeatureCollection):
ee_object = ee_object.clipToCollection(region)
if layer_name not in self.ee_layers:
if layer_name not in self.ee_raster_layer_names:
self.addLayer(ee_object, {}, layer_name, False, opacity)
band_names = ee_object.bandNames()
ee_object = ee.ImageCollection(
Expand Down Expand Up @@ -5197,7 +5178,7 @@ def add_time_slider(

first = ee.Image(ee_object.first())

if layer_name not in self.ee_layers:
if layer_name not in self.ee_raster_layer_names:
self.addLayer(ee_object.toBands(), {}, layer_name, False, opacity)
self.addLayer(first, vis_params, "Image X", True, opacity)

Expand Down Expand Up @@ -5264,7 +5245,7 @@ def slider_changed(change):
index = slider.value - 1
label.value = labels[index]
image = ee.Image(ee_object.toList(ee_object.size()).get(index))
if layer_name not in self.ee_layers:
if layer_name not in self.ee_raster_layer_names:
self.addLayer(ee_object.toBands(), {}, layer_name, False, opacity)
self.addLayer(image, vis_params, "Image X", True, opacity)
self.default_style = {"cursor": "default"}
Expand Down
10 changes: 5 additions & 5 deletions geemap/map_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ def _get_visible_map_layers(self):
if self._names is not None:
names = [names] if isinstance(names, str) else self._names
for name in names:
if name in self._host_map.ee_layers:
layers[name] = self._host_map.ee_layers[name]
if name in self._host_map.ee_layer_names:
layers[name] = self._host_map.ee_layer_dict[name]
else:
layers = self._host_map.ee_layers
layers = self._host_map.ee_layer_dict
return {k: v for k, v in layers.items() if v["ee_layer"].visible}

def _root_node(self, title, nodes, **kwargs):
Expand Down Expand Up @@ -774,10 +774,10 @@ def _on_layer_visibility_changed(self, change, layer):
layer.visible = change["new"]

layer_name = change["owner"].description
if layer_name not in self._host_map.ee_layers:
if layer_name not in self._host_map.ee_layer_names:
return

layer_dict = self._host_map.ee_layers[layer_name]
layer_dict = self._host_map.ee_layer_dict[layer_name]
for attachment_name in ["legend", "colorbar"]:
attachment = layer_dict.get(attachment_name, None)
attachment_on_map = attachment in self._host_map.controls
Expand Down
Loading

0 comments on commit 899501a

Please sign in to comment.