示例#1
0
    def test_input_mutation_prevention(self):

        line_1 = line_string([[7, 50], [8, 50], [9, 50]])
        line_2 = line_string([[8, 49], [8, 50], [8, 51]])

        line_1_cpy = deepcopy(line_1)
        line_2_cpy = deepcopy(line_2)

        _ = line_intersect(line_1, line_2)

        assert line_1 == line_1_cpy
        assert line_2 == line_2_cpy
示例#2
0
def is_line_on_line(feature_1: Sequence, feature_2: Sequence) -> bool:
    """
    Checks if two linestring feature intersects each other

    :param feature_1: Coordinates of linestring feature 1
    :param feature_2: Coordinates of linestring feature 2
    :return: bool if there is an intersection
    """
    if isinstance(feature_1, list):
        feature_1 = line_string(feature_1)

    if isinstance(feature_2, list):
        feature_2 = line_string(feature_2)

    intersects = line_intersect(feature_1, feature_2)

    if len(intersects["features"]) >= 1:
        return True

    return False
示例#3
0
def prepare_response(destination_point, fixture_in):

    coords = get_coords_from_features(fixture_in)
    coords = [round(coord, 6) for coord in coords]

    dest_coords = get_coords_from_features(destination_point)
    dest_coords = [round(coord, 6) for coord in dest_coords]

    line = line_string([coords, dest_coords], {"stroke": "#F00", "stroke-width": 4})

    fixture_in["properties"]["marker-color"] = "#F00"

    result = feature_collection([line, fixture_in, destination_point])

    return result
示例#4
0
def coords_to_line(coords: Sequence, properties: Dict) -> LineFeature:
    """ Converts coordinates to a {LineString}} or {MultiLineString}.

    :param polygon: Feature to convert
    :param properties: Optional parameters

    :return:
        {feature} of a (Multi)LineString
    """

    if len(coords) > 1:

        feature = multi_line_string(coords, properties)

    else:

        feature = line_string(coords[0], properties)

    return feature
示例#5
0
class TestLineIntersectss:
    @pytest.mark.parametrize(
        "fixture",
        [
            pytest.param(fixture, id=fixture_name)
            for fixture_name, fixture in fixtures.items()
        ],
    )
    def test_line_intersect(self, fixture):

        line_1 = fixture["in"]["features"][0]
        line_2 = fixture["in"]["features"][1]

        result = line_intersect(line_1, line_2)

        assert result.keys() == fixture["out"].keys()
        assert len(result["features"]) == len(fixture["out"]["features"])
        assert (all([
            i in result["features"] for i in fixture["out"]["features"]
        ]) == True)
        assert (all([
            i in fixture["out"]["features"] for i in result["features"]
        ]) == True)

    def test_input_mutation_prevention(self):

        line_1 = line_string([[7, 50], [8, 50], [9, 50]])
        line_2 = line_string([[8, 49], [8, 50], [8, 51]])

        line_1_cpy = deepcopy(line_1)
        line_2_cpy = deepcopy(line_2)

        _ = line_intersect(line_1, line_2)

        assert line_1 == line_1_cpy
        assert line_2 == line_2_cpy

    @pytest.mark.parametrize(
        "input_value,expected_value",
        [
            pytest.param(
                (
                    line_string([[7, 50], [8, 50], [9, 50]])["geometry"],
                    line_string([[8, 49], [8, 50], [8, 51]])["geometry"],
                ),
                [8, 50],
                id="ListHandling",
            ),
            pytest.param(
                (
                    feature_collection(
                        [line_string([[7, 50], [8, 50], [9, 50], [7, 50]])]),
                    feature_collection(
                        [line_string([[8, 49], [8, 50], [8, 51], [8, 49]])]),
                ),
                [8, 50],
                id="FeatureCollectionHandling",
            ),
            pytest.param(
                (
                    polygon([[[7, 50], [8, 50], [9, 50], [7, 50]]]),
                    polygon([[[8, 49], [8, 50], [8, 51], [8, 49]]]),
                ),
                [8, 50],
                id="PolygonHandling",
            ),
        ],
    )
    def test_geometric_objects(self, input_value, expected_value):

        result = line_intersect(*input_value)

        assert result["features"][0]["geometry"][
            "coordinates"] == expected_value
示例#6
0
 def linestring(self) -> Union[Feature, Dict]:
     return line_string(self.arc_coordinates, self.properties)
class TestGeometryTypeFromFeatures:
    @pytest.mark.parametrize(
        "fixture",
        [
            pytest.param(fixture, id=fixture_name)
            for fixture_name, fixture in fixtures.items()
        ],
    )
    def test_get_geometry_type_from_features_geojson(self, fixture):

        result = get_geometry_type(fixture["in"])

        if isinstance(result, tuple):
            assert result == tuple(fixture["out"])
        else:
            assert result == fixture["out"][0]

    @pytest.mark.parametrize(
        "input_value,output_value",
        [
            pytest.param(
                point([4.83, 45.75], as_geojson=False),
                "Point",
                id="point_feature_object",
            ),
            pytest.param(
                line_string([[4.86, 45.76], [4.85, 45.74]], as_geojson=False),
                "LineString",
                id="line_string_feature_object",
            ),
            pytest.param(
                polygon(
                    [[
                        [4.82, 45.79],
                        [4.88, 45.79],
                        [4.91, 45.76],
                        [4.89, 45.72],
                        [4.82, 45.71],
                        [4.77, 45.74],
                        [4.77, 45.77],
                        [4.82, 45.79],
                    ]],
                    as_geojson=False,
                ),
                "Polygon",
                id="polygon_feature_object",
            ),
            pytest.param(
                feature_collection(
                    [
                        polygon(
                            [[
                                [4.82, 45.79],
                                [4.88, 45.79],
                                [4.91, 45.76],
                                [4.89, 45.72],
                                [4.82, 45.71],
                                [4.77, 45.74],
                                [4.77, 45.77],
                                [4.82, 45.79],
                            ]],
                            as_geojson=False,
                        ),
                        line_string([[4.86, 45.76], [4.85, 45.74]],
                                    as_geojson=False),
                        point([4.83, 45.75]),
                    ],
                    as_geojson=False,
                ),
                ("Polygon", "LineString", "Point"),
                id="feature_collection_object",
            ),
            pytest.param(
                Point([4.83, 45.75]),
                "Point",
                id="Point_object",
            ),
            pytest.param(
                LineString([[4.86, 45.76], [4.85, 45.74]]),
                "LineString",
                id="LineString_object",
            ),
        ],
    )
    def test_get_geometry_from_features_objects(self, input_value,
                                                output_value):
        assert get_geometry_type(input_value) == output_value

    @pytest.mark.parametrize(
        "input_value,output_value",
        [
            pytest.param(
                ([4.83, 45.75], ["Point"]),
                "Point",
                id="point_feature_object",
            ),
            pytest.param(
                ([[4.86, 45.76], [4.85, 45.74]], ["LineString"]),
                "LineString",
                id="line_string_feature_object",
            ),
            pytest.param(
                (
                    [[
                        [4.82, 45.79],
                        [4.88, 45.79],
                        [4.91, 45.76],
                        [4.89, 45.72],
                        [4.82, 45.71],
                        [4.77, 45.74],
                        [4.77, 45.77],
                        [4.82, 45.79],
                    ]],
                    ["Polygon"],
                ),
                "Polygon",
                id="polygon_feature_object",
            ),
        ],
    )
    def test_get_geometry_from_lists(self, input_value, output_value):

        assert get_geometry_type(*input_value) == output_value

    @pytest.mark.parametrize(
        "input_value, exception_value",
        [
            pytest.param(
                (4.83),
                error_code_messages["InvalidGeometry"](allowed_types_default),
                id="InvalidFloat",
            ),
            pytest.param(
                (4),
                error_code_messages["InvalidGeometry"](allowed_types_default),
                id="InvalidInt",
            ),
            pytest.param(
                ("4.83"),
                error_code_messages["InvalidGeometry"](allowed_types_default),
                id="InvalidString",
            ),
            pytest.param(
                (None),
                error_code_messages["InvalidGeometry"](allowed_types_default),
                id="InvalidNone",
            ),
            pytest.param(
                ({}),
                error_code_messages["InvalidGeometry"](allowed_types_default),
                id="InvalidFeaturesInput",
            ),
        ],
    )
    def test_general_exception(self, input_value, exception_value):

        with pytest.raises(Exception) as excinfo:
            get_geometry_type(input_value)

        assert excinfo.type == InvalidInput
        assert str(excinfo.value) == exception_value

    @pytest.mark.parametrize(
        "input_value,exception_value",
        [
            pytest.param(
                (point([4.83, 45.75], as_geojson=False), ("LineString", )),
                error_code_messages["InvalidGeometry"](["LineString"]),
                id="point_vs_linestring_feature_object",
            ),
            pytest.param(
                (
                    line_string([[4.86, 45.76], [4.85, 45.74]],
                                as_geojson=False),
                    ["Point", "MultiPoint"],
                ),
                error_code_messages["InvalidGeometry"](["Point", "MultiPoint"
                                                        ]),
                id="point_vs_line_string_feature_object",
            ),
            pytest.param(
                (
                    polygon(
                        [[
                            [4.82, 45.79],
                            [4.88, 45.79],
                            [4.91, 45.76],
                            [4.89, 45.72],
                            [4.82, 45.71],
                            [4.77, 45.74],
                            [4.77, 45.77],
                            [4.82, 45.79],
                        ]],
                        as_geojson=False,
                    ),
                    ["Point"],
                ),
                error_code_messages["InvalidGeometry"](["Point"]),
                id="point_vs_polygon_feature_object",
            ),
            pytest.param(
                (
                    feature_collection(
                        [
                            polygon(
                                [[
                                    [4.82, 45.79],
                                    [4.88, 45.79],
                                    [4.91, 45.76],
                                    [4.89, 45.72],
                                    [4.82, 45.71],
                                    [4.77, 45.74],
                                    [4.77, 45.77],
                                    [4.82, 45.79],
                                ]],
                                as_geojson=False,
                            ),
                            line_string([[4.86, 45.76], [4.85, 45.74]],
                                        as_geojson=False),
                            point([4.83, 45.75]),
                        ],
                        as_geojson=False,
                    ),
                    ["Polygon", "Point"],
                ),
                error_code_messages["InvalidGeometry"](["Polygon", "Point"]),
                id="one_wrong_feature_collection_object",
            ),
            pytest.param(
                (Point([4.83, 45.75]), ["MultiPoint"]),
                error_code_messages["InvalidGeometry"](["MultiPoint"]),
                id="No_Point_object",
            ),
            pytest.param(
                (LineString([[4.86, 45.76], [4.85, 45.74]
                             ]), ["MultiLineString"]),
                error_code_messages["InvalidGeometry"](["MultiLineString"]),
                id="No_LineString_object",
            ),
        ],
    )
    def test_specifid_exception(self, input_value, exception_value):

        with pytest.raises(Exception) as excinfo:
            get_geometry_type(*input_value)

        assert excinfo.type == InvalidInput
        assert str(excinfo.value) == exception_value
示例#8
0
class TestGeometryFromFeatures:
    @pytest.mark.parametrize(
        "fixture",
        [
            pytest.param(fixture, id=fixture_name)
            for fixture_name, fixture in fixtures.items()
        ],
    )
    def test_get_geometry_from_features_geojson(self, fixture):

        try:
            allowed_types = fixture["in"]["properties"]["allowed_types"]
        except TypeError:

            allowed_types = fixture["in"][0]
            fixture["in"] = fixture["in"][1]

        assert (
            get_geometry_from_features(fixture["in"], allowed_types) == fixture["out"]
        )

    @pytest.mark.parametrize(
        "input_value,output_value",
        [
            pytest.param(
                (point([4.83, 45.75], as_geojson=False), ["Point"]),
                Point([4.83, 45.75]),
                id="point_feature_object",
            ),
            pytest.param(
                (
                    line_string([[4.86, 45.76], [4.85, 45.74]], as_geojson=False),
                    ["LineString"],
                ),
                LineString([[4.86, 45.76], [4.85, 45.74]]),
                id="line_string_feature_object",
            ),
            pytest.param(
                (
                    polygon(
                        [
                            [
                                [4.82, 45.79],
                                [4.88, 45.79],
                                [4.91, 45.76],
                                [4.89, 45.72],
                                [4.82, 45.71],
                                [4.77, 45.74],
                                [4.77, 45.77],
                                [4.82, 45.79],
                            ]
                        ],
                        as_geojson=False,
                    ),
                    ["Polygon"],
                ),
                Polygon(
                    [
                        [
                            [4.82, 45.79],
                            [4.88, 45.79],
                            [4.91, 45.76],
                            [4.89, 45.72],
                            [4.82, 45.71],
                            [4.77, 45.74],
                            [4.77, 45.77],
                            [4.82, 45.79],
                        ]
                    ]
                ),
                id="polygon_feature_object",
            ),
            pytest.param(
                (
                    feature_collection(
                        [
                            polygon(
                                [
                                    [
                                        [4.82, 45.79],
                                        [4.88, 45.79],
                                        [4.91, 45.76],
                                        [4.89, 45.72],
                                        [4.82, 45.71],
                                        [4.77, 45.74],
                                        [4.77, 45.77],
                                        [4.82, 45.79],
                                    ]
                                ],
                                as_geojson=False,
                            ),
                            line_string(
                                [[4.86, 45.76], [4.85, 45.74]], as_geojson=False
                            ),
                            point([4.83, 45.75]),
                        ],
                        as_geojson=False,
                    ),
                    ["LineString", "Polygon", "Point"],
                ),
                [
                    Polygon(
                        [
                            [
                                [4.82, 45.79],
                                [4.88, 45.79],
                                [4.91, 45.76],
                                [4.89, 45.72],
                                [4.82, 45.71],
                                [4.77, 45.74],
                                [4.77, 45.77],
                                [4.82, 45.79],
                            ]
                        ]
                    ),
                    LineString([[4.86, 45.76], [4.85, 45.74]]),
                    Point([4.83, 45.75]),
                ],
                id="feature_collection_object",
            ),
            pytest.param(
                (Point([4.83, 45.75]), ["Point"]),
                Point([4.83, 45.75]),
                id="Point_object",
            ),
            pytest.param(
                (LineString([[4.86, 45.76], [4.85, 45.74]]), ["LineString"],),
                LineString([[4.86, 45.76], [4.85, 45.74]]),
                id="LineString_object",
            ),
        ],
    )
    def test_get_geometry_from_features_objects(self, input_value, output_value):

        assert get_geometry_from_features(*input_value) == output_value

    @pytest.mark.parametrize(
        "input_value, exception_value",
        [
            pytest.param(
                (point([4.83, 45.75], as_geojson=False), ["LineString"]),
                error_code_messages["InvalidGeometry"](["LineString"]),
                id="InvalidGeometry-geojson",
            ),
            pytest.param(
                ([4.83, 45.75], ["LineString"]),
                error_code_messages["InvalidGeometry"](["LineString"]),
                id="InvalidGeometry-list",
            ),
            pytest.param(
                ([4.83], ["Point"]),
                error_code_messages["InvalidPointInput"],
                id="InvalidPoint",
            ),
            pytest.param(
                ({}, ["Point"]),
                error_code_messages["InvalidGeometry"](["Point"]),
                id="InvalidFeaturesInput",
            ),
        ],
    )
    def test_exception(self, input_value, exception_value):

        with pytest.raises(Exception) as excinfo:
            get_geometry_from_features(*input_value)

        assert excinfo.type == InvalidInput
        assert str(excinfo.value) == exception_value
示例#9
0
class TestPointLineDistance:
    @pytest.mark.parametrize(
        "fixture",
        [
            pytest.param(fixture, id=fixture_name)
            for fixture_name, fixture in fixtures.items()
        ],
    )
    def test_point_to_line_distance(self, fixture):

        point = fixture["in"]["features"][0]
        line = fixture["in"]["features"][1]

        properties = fixture["in"].get("properties", {})
        options = {"units": properties.get("units", "kilometers")}

        result = {}

        for method in ["geodesic", "planar"]:

            options["method"] = method
            result[method] = round(
                point_to_line_distance(point, line, options), 8)

        test_result = {k: round(v, 8) for k, v in fixture["out"].items()}

        assert result == test_result

    @pytest.mark.parametrize(
        "input_value,exception_value",
        [
            pytest.param(
                (point([0, 0]), line_string([[1, 1], [-1, 1]]), {
                    "units": "foo"
                }),
                error_code_messages["InvalidUnits"]("foo"),
                id="InvalidUnits",
            ),
            pytest.param(
                (None, line_string([[1, 1], [-1, 1]])),
                error_code_messages["InvalidGeometry"](["Point"]),
                id="InvalidPoint",
            ),
            pytest.param(
                (point([10, 10]), None),
                error_code_messages["InvalidGeometry"](["LineString"]),
                id="InvalidLineStringInput",
            ),
            pytest.param(
                (point([10, 10]), point([10, 10])),
                error_code_messages["InvalidGeometry"](["LineString"]),
                id="InvalidLineStringInput",
            ),
            pytest.param(
                (line_string([[1, 1], [-1, 1]]), line_string([[1, 1], [-1, 1]
                                                              ])),
                error_code_messages["InvalidGeometry"](["Point"]),
                id="InvalidPoint",
            ),
        ],
    )
    def test_exception(self, input_value, exception_value):
        with pytest.raises(Exception) as excinfo:
            point_to_line_distance(*input_value)

        assert excinfo.type == InvalidInput
        assert str(excinfo.value) == exception_value