Work on parsing wikicode (currently Rodelbahnbox).
[philipp/winterrodeln/wrpylib.git] / tests / test_wrdem.py
index 8f166db49b4c2d0cec990ad045d600d2bc7885d6..fb6f0f69fa3dc70c2279e1feb6e562d07ec02388 100644 (file)
@@ -1,28 +1,36 @@
 import unittest
 
 import owslib.wms
+import rasterio  # pip install rasterio
 
-from wrpylib.wrdem import get_ele_from_raster, get_ele_from_wms
+from wrpylib.wrdem import get_ele_from_raster, get_ele_from_wms, MultiDem, transform_lon_lat, DemSwitzerland, \
+    DemBasemap, DemBavaria
 from wrpylib.wrvalidators import LonLat
 
 
 class TestWrDemRaster(unittest.TestCase):
     def setUp(self):
-        self.filename = '/home/philipp/daten/GeoData/dem/oesterreich_10m/dhm_lamb_10m.tif'
+        filename = '/home/philipp/daten/GeoData/dem/oesterreich_10m/dhm_lamb_10m.tif'
         # https://www.data.gv.at/katalog/dataset/dgm
+        self.dataset = rasterio.open(filename)
+
+    def tearDown(self):
+        self.dataset.close()
 
     def test_get_ele_from_raster_inside_valid(self):
-        value = get_ele_from_raster(LonLat(13.682109, 47.934012), self.filename)
-        ele = round(int(value))
-        self.assertEqual(ele, 557)
+        x_y = transform_lon_lat(LonLat(13.682109, 47.934012), self.dataset.crs.to_proj4())
+        ele = get_ele_from_raster(self.dataset, x_y)
+        self.assertAlmostEqual(ele, 557, delta=0.5)
 
     def test_get_ele_from_raster_inside_invalid(self):
-        value = get_ele_from_raster(LonLat(14.67656, 48.16182), self.filename)
-        self.assertIsNone(value)
+        x_y = transform_lon_lat(LonLat(14.67656, 48.16182), self.dataset.crs.to_proj4())
+        ele = get_ele_from_raster(self.dataset, x_y)
+        self.assertIsNone(ele)
 
     def test_get_ele_from_raster_outside(self):
-        value = get_ele_from_raster(LonLat(8.67656, 47.16182), self.filename)
-        self.assertIsNone(value)
+        x_y = transform_lon_lat(LonLat(8.67656, 47.16182), self.dataset.crs.to_proj4())
+        ele = get_ele_from_raster(self.dataset, x_y)
+        self.assertIsNone(ele)
 
 
 class TestWrDemWms(unittest.TestCase):
@@ -43,3 +51,86 @@ class TestWrDemWms(unittest.TestCase):
     def test_get_ele_from_wms_outside(self):
         ele = get_ele_from_wms(LonLat(9.908062, 45.948257), self.wms, self.layer)
         self.assertIsNone(ele)
+
+
+class TestDemBasemap(unittest.TestCase):
+    def setUp(self):
+        self.dem = DemBasemap()
+
+    def tearDown(self):
+        self.dem.close()
+
+    def test_get_ele_inside_valid(self):
+        ele = self.dem.get_ele(LonLat(13.682109, 47.934012))
+        self.assertAlmostEqual(ele, 557, delta=0.5)
+
+    def test_get_ele_inside_invalid(self):
+        ele = self.dem.get_ele(LonLat(14.67656, 48.16182))
+        self.assertIsNone(ele)
+
+    def test_get_ele_outside(self):
+        ele = self.dem.get_ele(LonLat(8.67656, 47.16182))
+        self.assertIsNone(ele)
+
+
+class TestDemSwitzerland(unittest.TestCase):
+    def setUp(self):
+        self.dem = DemSwitzerland()
+
+    def tearDown(self):
+        self.dem.close()
+
+    def test_get_ele_inside_valid(self):
+        ele = self.dem.get_ele(LonLat(8.452512, 47.064715))
+        self.assertAlmostEqual(ele, 818, delta=0.5)
+
+    def test_get_ele_inside_invalid(self):
+        ele = self.dem.get_ele(LonLat(8.319, 45.970))
+        self.assertIsNone(ele)
+
+    def test_get_ele_outside(self):
+        ele = self.dem.get_ele(LonLat(5.182008, 46.799233))
+        self.assertIsNone(ele)
+
+
+class TestDemBavaria(unittest.TestCase):
+    def setUp(self):
+        self.dem = DemBavaria()
+
+    def tearDown(self):
+        self.dem.close()
+
+    def test_get_ele_inside_valid(self):
+        ele = self.dem.get_ele(LonLat(11.5396, 47.6809))
+        self.assertAlmostEqual(ele, 863, delta=0.5)
+        # ele = self.dem.get_ele(LonLat(11.010041, 47.618947))
+        # self.assertAlmostEqual(ele, 998, delta=0.5)
+        # ele = self.dem.get_ele(LonLat(11.019577, 47.620695))
+        # self.assertAlmostEqual(ele, 833, delta=0.5)
+
+    def test_get_ele_inside_invalid(self):
+        ele = self.dem.get_ele(LonLat(9.91, 48.97))
+        self.assertIsNone(ele)
+
+    def test_get_ele_outside(self):
+        ele = self.dem.get_ele(LonLat(5.182008, 46.799233))
+        self.assertIsNone(ele)
+
+
+class TestWrDemMultiDem(unittest.TestCase):
+    def setUp(self):
+        self.dem = MultiDem()
+
+    def tearDown(self):
+        self.dem.close()
+
+    def test_get_ele_valid(self):
+        ele, name = self.dem.get_ele(LonLat(13.682109, 47.934012))
+        self.assertAlmostEqual(ele, 557, delta=0.5)
+
+        ele, name = self.dem.get_ele(LonLat(11.31786, 46.68018))
+        self.assertAlmostEqual(ele, 2140.460, delta=0.1)
+
+    def test_get_ele_outside(self):
+        ele, name = self.dem.get_ele(LonLat(1., 2.))
+        self.assertIsNone(ele)