projection_test.py 2.16 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 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

class TestProjections(unittest.TestCase):
    def setUp(self):
        self._street_series = gpd.GeoSeries([
                LineString([
                    (0, 0),
                    (10, 0),
                    (10, 10)
                ]),
                LineString([
                    (100, 100),
                    (200, 200)
                ])
        ])

        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
        ])



    def test_other_projection(self):
        mpc.geospatial.project_onto_streets(
34 35 36
            self._point_series.set_crs("EPSG:4326"),
            self._street_series.set_crs("EPSG:4326"),
            epsg="EPSG:31287"
37 38
        )

39

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

48
        with self.assertRaises(ValueError):
49
            mpc.geospatial.project_onto_streets(
50 51 52
                self._point_series.set_crs("EPSG:4326"),
                self._street_series.set_crs("EPSG:4326"),
                epsg="EPSG:4326"
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
            )

    def test_basic_projection(self):
        projected, deviation = mpc.geospatial.project_onto_streets(
            self._point_series.set_crs("EPSG:31287"),
            self._street_series.set_crs("EPSG:31287")
        )

        distances = projected.distance(
            gpd.GeoSeries((
                Point(x, y) for x, y in [
                    (0, 0),
                    (10, 0),
                    (10, 10)
                ]
            )).set_crs("EPSG:31287")
        )

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

        self.assertTrue(
            np.max(
                deviation.values - np.array([10, 1, np.sqrt(10**2+10**2)])
            ) < 1e-1
        )
78 79 80

if __name__ == '__main__':
    unittest.main()