projection_test.py 2.06 KB
Newer Older
1 2 3 4 5 6 7 8 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
import unittest
import geopandas as gpd
from shapely.geometry import Point, LineString
from context import measprocess as mpc
import numpy as np

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(
            self._point_series.set_crs("EPSG:3416"),
            self._street_series.set_crs("EPSG:3416"),
            epsg="EPSG:3416"
        )

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

            mpc.geospatial.project_onto_streets(
                self._point_series.set_crs("EPSG:31287"),
                self._street_series.set_crs("EPSG:31287"),
                epsg="EPSG:3416"
            )

    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
        )