Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/tests/test_cartoee.py
2313 views
1
import pathlib
2
import tempfile
3
import unittest
4
5
import matplotlib.pyplot as plt
6
7
from geemap import cartoee
8
9
try:
10
import cartopy.crs as ccrs
11
12
CARTOPY_AVAILABLE = True
13
except ImportError:
14
CARTOPY_AVAILABLE = False
15
16
17
@unittest.skipIf(not CARTOPY_AVAILABLE, "cartopy is not installed")
18
class TestCartoee(unittest.TestCase):
19
20
def test_build_palette(self):
21
palette = cartoee.build_palette("viridis", 5)
22
self.assertEqual(len(palette), 5)
23
self.assertEqual(palette[0], "#440154")
24
self.assertEqual(palette[-1], "#fde725")
25
26
def test_add_colorbar(self):
27
fig = plt.figure()
28
ax = fig.add_subplot(111, projection=ccrs.PlateCarree())
29
vis_params = {"min": 0, "max": 10, "palette": ["#440154", "#fde725"]}
30
cartoee.add_colorbar(ax, vis_params, loc="right")
31
self.assertEqual(len(fig.axes), 2)
32
33
def test_buffer_box(self):
34
bbox = [1.1, 2.9, 3.1, 4.9]
35
interval = 1
36
# pylint: disable-next: protected-access
37
self.assertEqual(cartoee._buffer_box(bbox, interval), (1.0, 3.0, 3.0, 5.0))
38
bbox = [1.0, 3.0, 3.0, 5.0]
39
# pylint: disable-next: protected-access
40
self.assertEqual(cartoee._buffer_box(bbox, interval), (1.0, 3.0, 3.0, 5.0))
41
42
def test_bbox_to_extent(self):
43
self.assertEqual(cartoee.bbox_to_extent([1, 2, 3, 4]), (1, 3, 2, 4))
44
45
def test_add_gridlines(self):
46
fig = plt.figure()
47
ax = fig.add_subplot(111, projection=ccrs.PlateCarree())
48
ax.set_extent([-180, 180, -90, 90])
49
cartoee.add_gridlines(ax, interval=60)
50
self.assertEqual(len(ax.get_xticks()), 7)
51
52
def test_pad_view(self):
53
fig = plt.figure()
54
ax = fig.add_subplot(111, projection=ccrs.PlateCarree())
55
ax.set_extent([-10, 10, -10, 10])
56
cartoee.pad_view(ax, factor=0.1)
57
result = ax.get_xlim()
58
self.assertAlmostEqual(result[0], -12.0)
59
self.assertAlmostEqual(result[1], 12.0)
60
result = ax.get_ylim()
61
self.assertAlmostEqual(result[0], -12.0)
62
self.assertAlmostEqual(result[1], 12.0)
63
64
def test_add_north_arrow(self):
65
fig = plt.figure()
66
ax = fig.add_subplot(111, projection=ccrs.PlateCarree())
67
cartoee.add_north_arrow(ax)
68
self.assertEqual(len(ax.texts), 1)
69
70
def test_convert_si(self):
71
self.assertEqual(cartoee.convert_SI(1, "km", "m"), 1000.0)
72
73
def test_savefig(self):
74
fig = plt.figure()
75
fig.add_subplot(111, projection=ccrs.PlateCarree())
76
with tempfile.TemporaryDirectory() as tmpdir:
77
outfile = pathlib.Path(tmpdir) / "test.png"
78
cartoee.savefig(fig, str(outfile))
79
self.assertTrue(outfile.exists())
80
81
82
if __name__ == "__main__":
83
unittest.main()
84
85