Skip to content

Commit

Permalink
grass.jupyter: Add LayerControl to InteractiveMap by default (#3880)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaedri committed Jun 19, 2024
1 parent 318251d commit 5be030e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
1 change: 0 additions & 1 deletion doc/notebooks/jupyter_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"outputs": [],
"source": [
"# Import Python standard library and IPython packages we need.\n",
"import os\n",
"import subprocess\n",
"import sys\n",
"\n",
Expand Down
6 changes: 3 additions & 3 deletions doc/notebooks/jupyter_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"# Import Python standard library and IPython packages we need.\n",
"import subprocess\n",
"import sys\n",
"\n",
Expand Down Expand Up @@ -231,8 +231,7 @@
"source": [
"# Add raster, vector and layer control to map\n",
"raleigh_map.add_raster(\"elevation\")\n",
"raleigh_map.add_vector(\"roadsmajor\")\n",
"raleigh_map.add_layer_control(position=\"bottomright\")"
"raleigh_map.add_vector(\"roadsmajor\")"
]
},
{
Expand All @@ -241,6 +240,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Display Interactive Map\n",
"raleigh_map.show()"
]
},
Expand Down
21 changes: 12 additions & 9 deletions python/grass/jupyter/interactivemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ class InteractiveMap:
>>> m = InteractiveMap()
>>> m.add_vector("streams")
>>> m.add_raster("elevation")
>>> m.add_layer_control()
>>> m.show()
"""

Expand Down Expand Up @@ -300,7 +299,6 @@ def _import_ipyleaflet(error):
API_key=API_key, # pylint: disable=invalid-name
)
# Set LayerControl default
self.layer_control = False
self.layer_control_object = None

self._renderer = ReprojectionRenderer(
Expand Down Expand Up @@ -337,11 +335,12 @@ def add_raster(self, name, title=None, **kwargs):
Raster(name, title=title, renderer=self._renderer, **kwargs).add_to(self.map)

def add_layer_control(self, **kwargs):
"""Add layer control to display"
"""Add layer control to display.
Accepts keyword arguments to be passed to layer control object"""
A Layer Control is added by default. Call this function to customize
layer control object. Accepts keyword arguments to be passed to leaflet
layer control object"""

self.layer_control = True
if self._folium:
self.layer_control_object = self._folium.LayerControl(**kwargs)
else:
Expand All @@ -355,16 +354,20 @@ def show(self):
added after calling show()."""

self.map.fit_bounds(self._renderer.get_bbox())

if not self.layer_control_object:
self.add_layer_control()

# folium
if self._folium:
if self.layer_control:
self.map.add_child(self.layer_control_object)
self.map.add_child(self.layer_control_object)
fig = self._folium.Figure(width=self.width, height=self.height)
fig.add_child(self.map)

return fig

# ipyleaflet
if self.layer_control:
self.map.add(self.layer_control_object)
self.map.add(self.layer_control_object)
return self.map

def save(self, filename):
Expand Down

0 comments on commit 5be030e

Please sign in to comment.