Add DEM Switzerland.
[philipp/winterrodeln/wrpylib.git] / tests / test_wrdem.py
1 import unittest
2
3 import owslib.wms
4 import rasterio
5
6 from wrpylib.wrdem import get_ele_from_raster, get_ele_from_wms, MultiDem, transform_lon_lat, DemSwitzerland, DemBasemap
7 from wrpylib.wrvalidators import LonLat
8
9
10 class TestWrDemRaster(unittest.TestCase):
11     def setUp(self):
12         filename = '/home/philipp/daten/GeoData/dem/oesterreich_10m/dhm_lamb_10m.tif'
13         # https://www.data.gv.at/katalog/dataset/dgm
14         self.dataset = rasterio.open(filename)
15
16     def tearDown(self):
17         self.dataset.close()
18
19     def test_get_ele_from_raster_inside_valid(self):
20         x_y = transform_lon_lat(LonLat(13.682109, 47.934012), self.dataset.crs.to_proj4())
21         ele = get_ele_from_raster(self.dataset, x_y)
22         self.assertAlmostEqual(ele, 557, delta=0.5)
23
24     def test_get_ele_from_raster_inside_invalid(self):
25         x_y = transform_lon_lat(LonLat(14.67656, 48.16182), self.dataset.crs.to_proj4())
26         ele = get_ele_from_raster(self.dataset, x_y)
27         self.assertIsNone(ele)
28
29     def test_get_ele_from_raster_outside(self):
30         x_y = transform_lon_lat(LonLat(8.67656, 47.16182), self.dataset.crs.to_proj4())
31         ele = get_ele_from_raster(self.dataset, x_y)
32         self.assertIsNone(ele)
33
34
35 class TestWrDemWms(unittest.TestCase):
36     def setUp(self):
37         url = 'http://geoservices.buergernetz.bz.it/geoserver/p_bz-elevation/ows?' \
38               'SERVICE=WMS&request=getCapabilities'
39         self.wms = owslib.wms.WebMapService(url, '1.3.0')
40         self.layer = 'DTM-2p5m'
41
42     def test_get_ele_from_wms_inside_valid(self):
43         ele = get_ele_from_wms(LonLat(11.31786, 46.68018), self.wms, self.layer)
44         self.assertAlmostEqual(ele, 2140.460, delta=0.1)
45
46     def test_get_ele_from_wms_inside_invalid(self):
47         ele = get_ele_from_wms(LonLat(10.893336, 46.849965), self.wms, self.layer)
48         self.assertIsNone(ele)
49
50     def test_get_ele_from_wms_outside(self):
51         ele = get_ele_from_wms(LonLat(9.908062, 45.948257), self.wms, self.layer)
52         self.assertIsNone(ele)
53
54
55 class TestDemBasemap(unittest.TestCase):
56     def setUp(self):
57         self.dem = DemBasemap()
58
59     def tearDown(self):
60         self.dem.close()
61
62     def test_get_ele_inside_valid(self):
63         ele = self.dem.get_ele(LonLat(13.682109, 47.934012))
64         self.assertAlmostEqual(ele, 557, delta=0.5)
65
66     def test_get_ele_inside_invalid(self):
67         ele = self.dem.get_ele(LonLat(14.67656, 48.16182))
68         self.assertIsNone(ele)
69
70     def test_get_ele_outside(self):
71         ele = self.dem.get_ele(LonLat(8.67656, 47.16182))
72         self.assertIsNone(ele)
73
74
75 class TestDemSwitzerland(unittest.TestCase):
76     def setUp(self):
77         self.dem = DemSwitzerland()
78
79     def tearDown(self):
80         self.dem.close()
81
82     def test_get_ele_inside_valid(self):
83         ele = self.dem.get_ele(LonLat(8.452512, 47.064715))
84         self.assertAlmostEqual(ele, 818, delta=0.5)
85
86     def test_get_ele_inside_invalid(self):
87         ele = self.dem.get_ele(LonLat(8.319, 45.970))
88         self.assertIsNone(ele)
89
90     def test_get_ele_outside(self):
91         ele = self.dem.get_ele(LonLat(5.182008, 46.799233))
92         self.assertIsNone(ele)
93
94
95 class TestWrDemMultiDem(unittest.TestCase):
96     def setUp(self):
97         self.dem = MultiDem()
98
99     def tearDown(self):
100         self.dem.close()
101
102     def test_get_ele_valid(self):
103         ele, name = self.dem.get_ele(LonLat(13.682109, 47.934012))
104         self.assertAlmostEqual(ele, 557, delta=0.5)
105
106         ele, name = self.dem.get_ele(LonLat(11.31786, 46.68018))
107         self.assertAlmostEqual(ele, 2140.460, delta=0.1)
108
109     def test_get_ele_outside(self):
110         ele, name = self.dem.get_ele(LonLat(1., 2.))
111         self.assertIsNone(ele)