예제 #1
0
def get_line_segments(line: LinePolyFeature) -> Sequence:
    """
    Gets segments from a line feature

    :param line: any LineString or Polygon
    :return: sequence of segmetns
    """
    segments = []

    geometry_type = get_geometry_type(line)

    if isinstance(geometry_type, str):
        geometry_type = [geometry_type]

    for line_geo in geometry_type:
        if line_geo in ["MultiPolygon", "Polygon"]:
            line = polygon_to_line(line)
            line_geo = line["geometry"]["type"]

        line_coords = get_coords_from_features(
            line, ("LineString", "MultiLineString"))

        if line_geo in ["LineString"
                        ] and get_input_dimensions(line_coords) == 2:
            line_coords = [line_coords]

        for line_coord in line_coords:
            segments.extend(list(zip(line_coord, line_coord[1:])))

    return segments
예제 #2
0
def is_poly_in_poly(feature_1: Sequence, feature_2: Sequence) -> bool:
    """
    Checks if polygon feature_1 is inside polygon feature_2 and either way
    See http://stackoverflow.com/a/4833823/1979085

    :param feature: Coordinates of polygon feature 1
    :param feature: Coordinates of polygon feature 1
    :return: bool if there is an intersection
    """
    feature_1_line = polygon_to_line(polygon(feature_1))
    feature_2_line = polygon_to_line(polygon(feature_2))

    for coord1 in feature_1_line["geometry"]["coordinates"]:
        if boolean_point_in_polygon(coord1, feature_2):
            return True

    for coord2 in feature_2_line["geometry"]["coordinates"]:
        if boolean_point_in_polygon(coord2, feature_1):
            return True

    if is_line_on_line(feature_1_line, feature_2_line):
        return True

    return False
예제 #3
0
def is_line_in_poly(feature_1: Sequence, feature_2: Sequence) -> bool:
    """
    Checks if a linestring feature is inside or intersects a polygon feature

    :param feature_1: Coordinates of polygon feature
    :param feature_2: Coordinates of linestring feature
    :return: bool if there is an intersection
    """
    feature_1_line = polygon_to_line(polygon(feature_1))

    if is_line_on_line(feature_2, feature_1_line):
        return True

    for coord in feature_2:
        if boolean_point_in_polygon(coord, feature_1):
            return True

    return False
예제 #4
0
def is_poly_in_poly(feature_1: Sequence, feature_2: Sequence) -> bool:
    """
    Checks if feature_1 polygon feature is in feature_2 polygon

    :param feature_1: Coordinates of polygon feature 1
    :param feature_2: Coordinates of polygon feature 2

    :return: boolean True/False if feature 1 is within feature 2
    """
    poly_bbox_1 = bbox(feature_1)
    poly_bbox_2 = bbox(feature_2)

    if not bbox_overlap(poly_bbox_2, poly_bbox_1):
        return False

    feature_1 = polygon_to_line(polygon(feature_1))
    line_coords = get_coords_from_features(feature_1)

    for coords in line_coords:

        if not boolean_point_in_polygon(coords, feature_2):
            return False

    return True
예제 #5
0
    def test_properties_handling(self, input_value, expected_value):

        result = polygon_to_line(*input_value)

        assert result == expected_value
예제 #6
0
    def test_polygon_to_line(self, fixture):

        result = polygon_to_line(fixture["in"])

        assert result == fixture["out"]
예제 #7
0
    def test_exception(self, input_value, exception_value):
        with pytest.raises(Exception) as excinfo:
            polygon_to_line(*input_value)

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