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