projection_test.py 2.01 KB
Newer Older
1
import unittest
2

3 4
import geopandas as gpd
import numpy as np
5 6 7 8
from shapely.geometry import LineString, Point

from context import measprocess as mpc

9 10 11

class TestProjections(unittest.TestCase):
    def setUp(self):
12 13 14 15 16 17
        self._street_series = gpd.GeoSeries(
            [
                LineString([(0, 0), (10, 0), (10, 10)]),
                LineString([(100, 100), (200, 200)]),
            ]
        )
18

19 20 21 22 23 24 25
        self._point_series = gpd.GeoSeries(
            [
                Point(0, -10),  # Should be mapped to 0, 0
                Point(10, -1),  # Should be mapped to 10, 0
                Point(20, 20),  # Should be mapped to 10, 10
            ]
        )
26 27

    def test_other_projection(self):
28
        mpc.geospatial.process.project_onto_streets(
29 30
            self._point_series.set_crs("EPSG:4326"),
            self._street_series.set_crs("EPSG:4326"),
31
            epsg="EPSG:31287",
32 33 34 35
        )

    def test_exception_epsg(self):
        with self.assertRaises(ValueError):
36 37
            mpc.geospatial.process.project_onto_streets(
                self._point_series, self._street_series, epsg="EPSG:31287"
38 39
            )

40
        with self.assertRaises(ValueError):
41
            mpc.geospatial.process.project_onto_streets(
42 43
                self._point_series.set_crs("EPSG:4326"),
                self._street_series.set_crs("EPSG:4326"),
44
                epsg="EPSG:4326",
45 46 47
            )

    def test_basic_projection(self):
48
        projected, deviation = mpc.geospatial.process.project_onto_streets(
49
            self._point_series.set_crs("EPSG:31287"),
50
            self._street_series.set_crs("EPSG:31287"),
51 52 53
        )

        distances = projected.distance(
54 55 56
            gpd.GeoSeries(
                (Point(x, y) for x, y in [(0, 0), (10, 0), (10, 10)])
            ).set_crs("EPSG:31287")
57 58 59 60 61
        )

        self.assertTrue(distances.max() < 1e-1)

        self.assertTrue(
62 63
            np.max(deviation.values - np.array([10, 1, np.sqrt(10 ** 2 + 10 ** 2)]))
            < 1e-1
64
        )
65

66 67

if __name__ == "__main__":
68
    unittest.main()