Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/tests/test_plotlymap.py
2313 views
1
"""Tests for the plotlymap module."""
2
3
import unittest
4
5
import pandas as pd
6
7
try:
8
from plotly import graph_objects as go
9
from geemap import plotlymap
10
11
PLOTLY_AVAILABLE = True
12
except ImportError:
13
PLOTLY_AVAILABLE = False
14
15
16
@unittest.skipUnless(PLOTLY_AVAILABLE, "plotly not available")
17
class PlotlymapTest(unittest.TestCase):
18
19
def test_map_init_default_params(self):
20
m = plotlymap.Map(ee_initialize=False)
21
self.assertEqual(m.layout.mapbox.center.lat, 20)
22
self.assertEqual(m.layout.mapbox.center.lon, 0)
23
self.assertEqual(m.layout.mapbox.zoom, 1)
24
self.assertEqual(m.layout.mapbox.style, "open-street-map")
25
self.assertEqual(m.layout.height, 600)
26
self.assertEqual(len(m.data), 1)
27
self.assertEqual(m.get_layers(), {})
28
self.assertEqual(m.get_tile_layers(), {})
29
self.assertEqual(m.get_data_layers(), {})
30
self.assertIsNone(m.find_layer_index("nonexistent"))
31
32
def test_map_init_custom_center(self):
33
m = plotlymap.Map(center=(40, -100), ee_initialize=False)
34
self.assertEqual(m.layout.mapbox.center.lat, 40)
35
self.assertEqual(m.layout.mapbox.center.lon, -100)
36
37
def test_map_init_custom_zoom(self):
38
m = plotlymap.Map(zoom=10, ee_initialize=False)
39
self.assertEqual(m.layout.mapbox.zoom, 10)
40
41
def test_map_init_custom_height(self):
42
m = plotlymap.Map(height=800, ee_initialize=False)
43
self.assertEqual(m.layout.height, 800)
44
45
def test_map_init_custom_basemap(self):
46
m = plotlymap.Map(basemap="carto-positron", ee_initialize=False)
47
self.assertEqual(m.layout.mapbox.style, "carto-positron")
48
49
def test_set_center_with_zoom(self):
50
m = plotlymap.Map(ee_initialize=False)
51
m.set_center(lat=37.8, lon=-122.4, zoom=12)
52
self.assertEqual(m.layout.mapbox.center.lat, 37.8)
53
self.assertEqual(m.layout.mapbox.center.lon, -122.4)
54
self.assertEqual(m.layout.mapbox.zoom, 12)
55
56
def test_set_center_without_zoom(self):
57
m = plotlymap.Map(ee_initialize=False)
58
m.set_center(lat=37.8, lon=-122.4)
59
self.assertEqual(m.layout.mapbox.center.lat, 37.8)
60
self.assertEqual(m.layout.mapbox.center.lon, -122.4)
61
self.assertEqual(m.layout.mapbox.zoom, 1)
62
63
def test_add_controls_string_and_list(self):
64
m = plotlymap.Map(ee_initialize=False)
65
m.add_controls("drawline")
66
self.assertIn("drawline", m.layout.modebar.add)
67
m.add_controls(["drawline", "drawopenpath"])
68
self.assertIn("drawopenpath", m.layout.modebar.add)
69
70
def test_add_controls_invalid_type_raises(self):
71
m = plotlymap.Map(ee_initialize=False)
72
with self.assertRaises(ValueError):
73
m.add_controls(12345)
74
75
def test_remove_controls_string_and_list(self):
76
m = plotlymap.Map(ee_initialize=False)
77
m.remove_controls("zoomin")
78
self.assertIn("zoomin", m.layout.modebar.remove)
79
m.remove_controls(["zoomin", "zoomout"])
80
self.assertIn("zoomout", m.layout.modebar.remove)
81
82
def test_remove_controls_invalid_type_raises(self):
83
m = plotlymap.Map(ee_initialize=False)
84
with self.assertRaises(ValueError):
85
m.remove_controls(12345)
86
87
def test_add_tile_layer(self):
88
m = plotlymap.Map(ee_initialize=False)
89
m.add_tile_layer(
90
url="https://tile.example.com/{z}/{x}/{y}.png",
91
name="Test Layer",
92
)
93
self.assertIn("Test Layer", m.get_tile_layers())
94
95
def test_add_layer(self):
96
m = plotlymap.Map(ee_initialize=False)
97
layer = go.Scattermapbox(lat=[37.8], lon=[-122.4])
98
m.add_layer(layer, name="My Layer")
99
self.assertIn("My Layer", m.get_data_layers())
100
101
def test_find_layer_index_data_layer(self):
102
m = plotlymap.Map(ee_initialize=False)
103
layer = go.Scattermapbox(lat=[37.8], lon=[-122.4], name="pts")
104
m.add_layer(layer)
105
self.assertEqual(m.find_layer_index("pts"), 1)
106
107
def test_clear_layers_keeps_basemap(self):
108
m = plotlymap.Map(ee_initialize=False)
109
layer = go.Scattermapbox(lat=[37.8], lon=[-122.4], name="extra")
110
m.add_layer(layer)
111
m.clear_layers()
112
self.assertEqual(len(m.data), 1)
113
114
def test_clear_layers_with_basemap(self):
115
m = plotlymap.Map(ee_initialize=False)
116
m.clear_layers(clear_basemap=True)
117
self.assertEqual(len(m.data), 0)
118
119
def test_add_heatmap_dataframe(self):
120
m = plotlymap.Map(ee_initialize=False)
121
df = pd.DataFrame(
122
{
123
"latitude": [37.8, 37.7],
124
"longitude": [-122.4, -122.3],
125
"value": [1.0, 0.5],
126
}
127
)
128
m.add_heatmap(df)
129
self.assertEqual(len(m.data), 2)
130
131
def test_add_heatmap_invalid_data_raises(self):
132
m = plotlymap.Map(ee_initialize=False)
133
with self.assertRaises(ValueError):
134
m.add_heatmap({"invalid": "data"})
135
136
def test_add_basemap_invalid_raises(self):
137
m = plotlymap.Map(ee_initialize=False)
138
with self.assertRaises(ValueError):
139
m.add_basemap("INVALID_BASEMAP_XYZ")
140
141
def test_add_ee_layer_invalid_type_raises(self):
142
m = plotlymap.Map(ee_initialize=False)
143
with self.assertRaises(AttributeError):
144
m.add_ee_layer("not_an_ee_object")
145
146
def test_addlayer_alias(self):
147
m = plotlymap.Map(ee_initialize=False)
148
self.assertEqual(m.addLayer, m.add_ee_layer)
149
150
def test_remove_layer(self):
151
m = plotlymap.Map(ee_initialize=False)
152
m.add_tile_layer(
153
url="https://tile.example.com/{z}/{x}/{y}.png",
154
name="removable",
155
)
156
self.assertIn("removable", m.get_tile_layers())
157
m.remove_layer("removable")
158
self.assertNotIn("removable", m.get_tile_layers())
159
160
def test_set_layer_visibility(self):
161
m = plotlymap.Map(ee_initialize=False)
162
layer = go.Scattermapbox(lat=[0], lon=[0], name="vis_test")
163
m.add_layer(layer)
164
m.set_layer_visibility("vis_test", show=False)
165
index = m.find_layer_index("vis_test")
166
self.assertFalse(m.data[index].visible)
167
168
def test_set_layer_opacity(self):
169
m = plotlymap.Map(ee_initialize=False)
170
m.add_tile_layer(
171
url="https://tile.example.com/{z}/{x}/{y}.png",
172
name="opacity_test",
173
)
174
m.set_layer_opacity("opacity_test", opacity=0.5)
175
index = m.find_layer_index("opacity_test")
176
self.assertEqual(m.layout.mapbox.layers[index].opacity, 0.5)
177
178
179
if __name__ == "__main__":
180
unittest.main()
181
182