Skip to content

Commit

Permalink
Changed default basemap opengeos#91
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Aug 4, 2021
1 parent 16709d4 commit 4dd3970
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 184 deletions.
160 changes: 80 additions & 80 deletions README.md

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions leafmap/basemaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@


leaf_basemaps = {
"OpenStreetMap": TileLayer(
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
attribution="OpenStreetMap",
name="OpenStreetMap",
),
"ROADMAP": TileLayer(
url="https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
attribution="Google",
Expand Down
53 changes: 7 additions & 46 deletions leafmap/foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class Map(folium.Map):
"""The Map class inherits folium.Map. By default, the Map will add Google Maps as the basemap.
"""The Map class inherits folium.Map. By default, the Map will add OpenStreetMap as the basemap.
Returns:
object: folium map object.
Expand Down Expand Up @@ -91,60 +91,21 @@ def __init__(self, **kwargs):
plugins.MiniMap().add_to(self)

if "google_map" not in kwargs:
layer = folium.TileLayer(
tiles="https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
attr="Google",
name="Google Maps",
overlay=True,
control=True,
)
layer.add_to(self)
elif kwargs["google_map"] is None:
pass
else:
elif kwargs["google_map"] is not None:
if kwargs["google_map"].upper() == "ROADMAP":
layer = folium.TileLayer(
tiles="https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
attr="Google",
name="Google Maps",
overlay=True,
control=True,
)
layer = folium_basemaps["ROADMAP"]
elif kwargs["google_map"].upper() == "HYBRID":
layer = folium.TileLayer(
tiles="https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",
attr="Google",
name="Google Satellite",
overlay=True,
control=True,
)
layer = folium_basemaps["HYBRID"]
elif kwargs["google_map"].upper() == "TERRAIN":
layer = folium.TileLayer(
tiles="https://mt1.google.com/vt/lyrs=p&x={x}&y={y}&z={z}",
attr="Google",
name="Google Terrain",
overlay=True,
control=True,
)
layer = folium_basemaps["TERRAIN"]
elif kwargs["google_map"].upper() == "SATELLITE":
layer = folium.TileLayer(
tiles="https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}",
attr="Google",
name="Google Satellite",
overlay=True,
control=True,
)
layer = folium_basemaps["SATELLITE"]
else:
print(
f'{kwargs["google_map"]} is invalid. google_map must be one of: ["ROADMAP", "HYBRID", "TERRAIN", "SATELLITE"]. Adding the default ROADMAP.'
)
layer = folium.TileLayer(
tiles="https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
attr="Google",
name="Google Maps",
overlay=True,
control=True,
)
layer = folium_basemaps["ROADMAP"]
layer.add_to(self)

if "layers_control" not in kwargs:
Expand Down
47 changes: 11 additions & 36 deletions leafmap/leafmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class Map(ipyleaflet.Map):
"""The Map class inherits ipyleaflet.Map. The arguments you can pass to the Map can be found at https://ipyleaflet.readthedocs.io/en/latest/api_reference/map.html. By default, the Map will add Google Maps as the basemap. Set add_google_map = False to use OpenStreetMap as the basemap.
"""The Map class inherits ipyleaflet.Map. The arguments you can pass to the Map can be found at https://ipyleaflet.readthedocs.io/en/latest/api_reference/map.html. By default, the Map will add OpenStreetMap as the basemap.
Returns:
object: ipyleaflet map object.
Expand Down Expand Up @@ -98,49 +98,25 @@ def handle_draw(target, action, geo_json):
if kwargs["scale_control"]:
self.add_control(ipyleaflet.ScaleControl(position="bottomleft"))

self.clear_layers()
self.add_layer(basemap_tiles["OpenStreetMap"])

if "google_map" not in kwargs:
layer = ipyleaflet.TileLayer(
url="https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
attribution="Google",
name="Google Maps",
)
self.add_layer(layer)
elif kwargs["google_map"] is None:
pass
else:
elif kwargs["google_map"] is not None:
if kwargs["google_map"].upper() == "ROADMAP":
layer = ipyleaflet.TileLayer(
url="https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
attribution="Google",
name="Google Maps",
)
layer = basemap_tiles["ROADMAP"]
elif kwargs["google_map"].upper() == "HYBRID":
layer = ipyleaflet.TileLayer(
url="https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",
attribution="Google",
name="Google Satellite",
)
layer = basemap_tiles["HYBRID"]
elif kwargs["google_map"].upper() == "TERRAIN":
layer = ipyleaflet.TileLayer(
url="https://mt1.google.com/vt/lyrs=p&x={x}&y={y}&z={z}",
attribution="Google",
name="Google Terrain",
)
layer = basemap_tiles["TERRAIN"]
elif kwargs["google_map"].upper() == "SATELLITE":
layer = ipyleaflet.TileLayer(
url="https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}",
attribution="Google",
name="Google Satellite",
)
layer = basemap_tiles["SATELLITE"]
else:
print(
f'{kwargs["google_map"]} is invalid. google_map must be one of: ["ROADMAP", "HYBRID", "TERRAIN", "SATELLITE"]. Adding the default ROADMAP.'
)
layer = ipyleaflet.TileLayer(
url="https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
attribution="Google",
name="Google Maps",
)
layer = basemap_tiles["ROADMAP"]
self.add_layer(layer)

if "toolbar_control" not in kwargs:
Expand Down Expand Up @@ -2433,7 +2409,6 @@ def linked_maps(

m = Map(
height=height,
google_map=None,
layout=widgets.Layout(margin="0px", padding="0px"),
**kwargs,
)
Expand Down Expand Up @@ -2592,7 +2567,7 @@ def ts_inspector(
left_layer = layers_dict[left_name]
right_layer = layers_dict[right_name]

m = Map(center=center, zoom=zoom, google_map=None, **kwargs)
m = Map(center=center, zoom=zoom, **kwargs)
control = ipyleaflet.SplitMapControl(left_layer=left_layer, right_layer=right_layer)
m.add_control(control)

Expand Down
12 changes: 8 additions & 4 deletions leafmap/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def all_layers_chk_changed(change):

layers = [
lyr
for lyr in m.layers[1:]
for lyr in m.layers
if (
isinstance(lyr, ipyleaflet.TileLayer)
or isinstance(lyr, ipyleaflet.WMSLayer)
Expand Down Expand Up @@ -843,11 +843,15 @@ def change_basemap(m):
"""
from .basemaps import leaf_basemaps

value = "OpenStreetMap.Mapnik"
if len(m.layers) > 0:
if m.layers[0].name in list(leaf_basemaps.keys()):
value = m.layers[0].name

dropdown = widgets.Dropdown(
options=list(leaf_basemaps.keys()),
value="ROADMAP",
layout=widgets.Layout(width="200px")
# description="Basemaps",
value=value,
layout=widgets.Layout(width="200px"),
)

close_btn = widgets.Button(
Expand Down
18 changes: 9 additions & 9 deletions tests/test_foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,19 @@ def test_find_layer(self):
with self.assertRaises(NotImplementedError):
m = leafmap.Map()
self.assertIsNone(m.find_layer("HYBRID"))
self.assertIsNotNone(m.find_layer("Google Maps"))
self.assertIsNotNone(m.find_layer("OpenStreetMap"))

def test_find_layer_index(self):
with self.assertRaises(NotImplementedError):
"""Check finding layer index"""
m = leafmap.Map()
self.assertEqual(m.find_layer_index("Google Maps"), 1)
self.assertEqual(m.find_layer_index("OpenStreetMap"), 0)

def test_get_layer_names(self):
"""Check getting layer names"""
with self.assertRaises(NotImplementedError):
m = leafmap.Map()
assert "Google Maps" in m.get_layer_names()
assert "OpenStreetMap" in m.get_layer_names()

def test_get_scale(self):
"""Check getting scale"""
Expand All @@ -407,8 +407,8 @@ def test_layer_opacity(self):
"""Check layer opacity"""
with self.assertRaises(NotImplementedError):
m = leafmap.Map()
m.layer_opacity("Google Maps", 0.5)
layer = m.find_layer("Google Maps")
m.layer_opacity("OpenStreetMap", 0.5)
layer = m.find_layer("OpenStreetMap")
self.assertEqual(layer.opacity, 0.5)

def test_set_center(self):
Expand All @@ -430,7 +430,7 @@ def test_to_html(self):
"""Check map to html"""
m = leafmap.Map()
out_str = m.to_html()
assert "Google Maps" in out_str
assert "OpenStreetMap" in out_str

def test_to_image(self):
"""Check map to image"""
Expand Down Expand Up @@ -465,7 +465,7 @@ def test_zoom_to_bounds(self):
bounds = [13, -130, 32, -100]
m.zoom_to_bounds(bounds)
out_str = m.to_html()
assert "Google Maps" in out_str
assert "OpenStreetMap" in out_str

def test_zoom_to_gdf(self):
"""Check zoom to GeoDataFrame"""
Expand All @@ -475,14 +475,14 @@ def test_zoom_to_gdf(self):
)
m.zoom_to_gdf(gdf)
out_str = m.to_html()
assert "Google Maps" in out_str
assert "OpenStreetMap" in out_str

def test_leafmap_split_map(self):
"""Check split-panel map"""
with self.assertRaises(NotImplementedError):
m = leafmap.split_map(left_layer="ROADMAP", right_layer="HYBRID")
out_str = m.to_html()
assert "Google Maps" in out_str
assert "OpenStreetMap" in out_str

def test_linked_maps(self):
"""Check linked maps"""
Expand Down
18 changes: 9 additions & 9 deletions tests/test_leafmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,17 @@ def test_find_layer(self):
"""Check finding layer"""
m = leafmap.Map()
self.assertIsNone(m.find_layer("HYBRID"))
self.assertIsNotNone(m.find_layer("Google Maps"))
self.assertIsNotNone(m.find_layer("OpenStreetMap"))

def test_find_layer_index(self):
"""Check finding layer index"""
m = leafmap.Map()
self.assertEqual(m.find_layer_index("Google Maps"), 1)
self.assertEqual(m.find_layer_index("OpenStreetMap"), 0)

def test_get_layer_names(self):
"""Check getting layer names"""
m = leafmap.Map()
assert "Google Maps" in m.get_layer_names()
assert "OpenStreetMap" in m.get_layer_names()

def test_get_scale(self):
"""Check getting scale"""
Expand All @@ -392,8 +392,8 @@ def test_image_overlay(self):
def test_layer_opacity(self):
"""Check layer opacity"""
m = leafmap.Map()
m.layer_opacity("Google Maps", 0.5)
layer = m.find_layer("Google Maps")
m.layer_opacity("OpenStreetMap", 0.5)
layer = m.find_layer("OpenStreetMap")
self.assertEqual(layer.opacity, 0.5)

def test_set_center(self):
Expand All @@ -413,7 +413,7 @@ def test_to_html(self):
"""Check map to html"""
m = leafmap.Map()
out_str = m.to_html()
assert "Google Maps" in out_str
assert "OpenStreetMap" in out_str

# def test_to_image(self):
# """Check map to image"""
Expand Down Expand Up @@ -445,7 +445,7 @@ def test_zoom_to_bounds(self):
bounds = [13, -130, 32, -100]
m.zoom_to_bounds(bounds)
out_str = m.to_html()
assert "Google Maps" in out_str
assert "OpenStreetMap" in out_str

def test_zoom_to_gdf(self):
"""Check zoom to GeoDataFrame"""
Expand All @@ -455,13 +455,13 @@ def test_zoom_to_gdf(self):
)
m.zoom_to_gdf(gdf)
out_str = m.to_html()
assert "Google Maps" in out_str
assert "OpenStreetMap" in out_str

def test_leafmap_split_map(self):
"""Check split-panel map"""
m = leafmap.split_map(left_layer="ROADMAP", right_layer="HYBRID")
out_str = m.to_html()
assert "Google Maps" in out_str
assert "OpenStreetMap" in out_str

def test_linked_maps(self):
"""Check linked maps"""
Expand Down

0 comments on commit 4dd3970

Please sign in to comment.