コード例 #1
0
def test_polygonize_array():
    lines = [
        shapely.Geometry("LINESTRING (0 0, 1 1)"),
        shapely.Geometry("LINESTRING (0 0, 0 1)"),
        shapely.Geometry("LINESTRING (0 1, 1 1)"),
    ]
    expected = shapely.Geometry("GEOMETRYCOLLECTION (POLYGON ((1 1, 0 0, 0 1, 1 1)))")
    result = shapely.polygonize(np.array(lines))
    assert isinstance(result, shapely.Geometry)
    assert result == expected

    result = shapely.polygonize(np.array([lines]))
    assert isinstance(result, np.ndarray)
    assert result.shape == (1,)
    assert result[0] == expected

    arr = np.array([lines, lines])
    assert arr.shape == (2, 3)
    result = shapely.polygonize(arr)
    assert isinstance(result, np.ndarray)
    assert result.shape == (2,)
    assert result[0] == expected
    assert result[1] == expected

    arr = np.array([[lines, lines], [lines, lines], [lines, lines]])
    assert arr.shape == (3, 2, 3)
    result = shapely.polygonize(arr)
    assert isinstance(result, np.ndarray)
    assert result.shape == (3, 2)
    for res in result.flatten():
        assert res == expected
コード例 #2
0
def test_line_interpolate_point_geom_array():
    actual = shapely.line_interpolate_point(
        [line_string, linear_ring, multi_line_string], -1)
    assert_geometries_equal(actual[0], shapely.Geometry("POINT (1 0)"))
    assert_geometries_equal(actual[1], shapely.Geometry("POINT (0 1)"))
    assert_geometries_equal(actual[2],
                            shapely.Geometry("POINT (0.5528 1.1056)"),
                            tolerance=0.001)
コード例 #3
0
def test_reverse_none():
    assert shapely.reverse(None) is None
    assert shapely.reverse([None]).tolist() == [None]

    geometry = shapely.Geometry("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))")
    expected = shapely.Geometry("POLYGON ((0 0,  0 1, 1 1, 1 0, 0 0))")
    result = shapely.reverse([None, geometry])
    assert result[0] is None
    assert_geometries_equal(result[1], expected)
コード例 #4
0
ファイル: test_geometry.py プロジェクト: mwtoews/shapely
def test_set_precision_z(mode):
    with warnings.catch_warnings():
        warnings.simplefilter(
            "ignore")  # GEOS <= 3.9 emits warning for 'pointwise'
        geometry = shapely.set_precision(
            shapely.Geometry("POINT Z (0.9 0.9 0.9)"), 1, mode=mode)
        assert shapely.get_precision(geometry) == 1
        assert_geometries_equal(geometry,
                                shapely.Geometry("POINT Z (1 1 0.9)"))
コード例 #5
0
ファイル: test_creation.py プロジェクト: 92kns/Shapely
def test_linestrings_from_coords():
    actual = shapely.linestrings([[[0, 0], [1, 1]], [[0, 0], [2, 2]]])
    assert_geometries_equal(
        actual,
        [
            shapely.Geometry("LINESTRING (0 0, 1 1)"),
            shapely.Geometry("LINESTRING (0 0, 2 2)"),
        ],
    )
コード例 #6
0
ファイル: test_geometry.py プロジェクト: mwtoews/shapely
def set_precision_pointwise_pre_310():
    # using 'pointwise' emits a warning
    with pytest.warns(UserWarning):
        actual = shapely.set_precision(
            shapely.Geometry("LINESTRING (0 0, 0.1 0.1)"),
            1.0,
            mode="pointwise",
        )
    assert_geometries_equal(shapely.force_2d(actual),
                            shapely.Geometry("LINESTRING EMPTY"))
コード例 #7
0
ファイル: test_geometry.py プロジェクト: mwtoews/shapely
def set_precision_preserve_topology(preserve_topology):
    # the preserve_topology kwarg is deprecated (ignored)
    with pytest.warns(UserWarning):
        actual = shapely.set_precision(
            shapely.Geometry("LINESTRING (0 0, 0.1 0.1)"),
            1.0,
            preserve_topology=preserve_topology,
        )
    assert_geometries_equal(shapely.force_2d(actual),
                            shapely.Geometry("LINESTRING EMPTY"))
コード例 #8
0
ファイル: test_creation.py プロジェクト: 92kns/Shapely
def test_linestrings_from_xy_broadcast():
    x = [0, 1]  # the same X coordinates for both linestrings
    y = [2, 3], [4, 5]  # each linestring has a different set of Y coordinates
    actual = shapely.linestrings(x, y)
    assert_geometries_equal(
        actual,
        [
            shapely.Geometry("LINESTRING (0 2, 1 3)"),
            shapely.Geometry("LINESTRING (0 4, 1 5)"),
        ],
    )
コード例 #9
0
ファイル: test_geometry.py プロジェクト: mwtoews/shapely
def test_set_precision_drop_coords():
    # setting precision of 0 will not drop duplicated points in original
    geometry = shapely.set_precision(
        shapely.Geometry("LINESTRING (0 0, 0 0, 0 1, 1 1)"), 0)
    assert_geometries_equal(
        geometry, shapely.Geometry("LINESTRING (0 0, 0 0, 0 1, 1 1)"))

    # setting precision will remove duplicated points
    geometry = shapely.set_precision(geometry, 1)
    assert_geometries_equal(geometry,
                            shapely.Geometry("LINESTRING (0 0, 0 1, 1 1)"))
コード例 #10
0
def test_polygonize_array_axis():
    lines = [
        shapely.Geometry("LINESTRING (0 0, 1 1)"),
        shapely.Geometry("LINESTRING (0 0, 0 1)"),
        shapely.Geometry("LINESTRING (0 1, 1 1)"),
    ]
    arr = np.array([lines, lines])  # shape (2, 3)
    result = shapely.polygonize(arr, axis=1)
    assert result.shape == (2,)
    result = shapely.polygonize(arr, axis=0)
    assert result.shape == (3,)
コード例 #11
0
def test_polygonize_full_array_axis():
    lines = [
        shapely.Geometry("LINESTRING (0 0, 1 1)"),
        shapely.Geometry("LINESTRING (0 0, 0 1)"),
        shapely.Geometry("LINESTRING (0 1, 1 1)"),
    ]
    arr = np.array([lines, lines])  # shape (2, 3)
    result = shapely.polygonize_full(arr, axis=1)
    assert len(result) == 4
    assert all(arr.shape == (2,) for arr in result)
    result = shapely.polygonize_full(arr, axis=0)
    assert len(result) == 4
    assert all(arr.shape == (3,) for arr in result)
コード例 #12
0
ファイル: test_geometry.py プロジェクト: mwtoews/shapely
def test_set_precision(mode):
    initial_geometry = shapely.Geometry("POINT (0.9 0.9)")
    assert shapely.get_precision(initial_geometry) == 0

    geometry = shapely.set_precision(initial_geometry, 0, mode=mode)
    assert shapely.get_precision(geometry) == 0
    assert_geometries_equal(geometry, initial_geometry)

    geometry = shapely.set_precision(initial_geometry, 1, mode=mode)
    assert shapely.get_precision(geometry) == 1
    assert_geometries_equal(geometry, shapely.Geometry("POINT (1 1)"))
    # original should remain unchanged
    assert_geometries_equal(initial_geometry,
                            shapely.Geometry("POINT (0.9 0.9)"))
コード例 #13
0
def test_polygonize_full_array():
    lines = [
        shapely.Geometry("LINESTRING (0 0, 1 1)"),
        shapely.Geometry("LINESTRING (0 0, 0 1)"),
        shapely.Geometry("LINESTRING (0 1, 1 1)"),
    ]
    expected = shapely.Geometry("GEOMETRYCOLLECTION (POLYGON ((1 1, 0 0, 0 1, 1 1)))")
    result = shapely.polygonize_full(np.array(lines))
    assert len(result) == 4
    assert all(isinstance(geom, shapely.Geometry) for geom in result)
    assert result[0] == expected
    assert all(
        geom == shapely.Geometry("GEOMETRYCOLLECTION EMPTY") for geom in result[1:]
    )

    result = shapely.polygonize_full(np.array([lines]))
    assert len(result) == 4
    assert all(isinstance(geom, np.ndarray) for geom in result)
    assert all(geom.shape == (1,) for geom in result)
    assert result[0][0] == expected
    assert all(
        geom[0] == shapely.Geometry("GEOMETRYCOLLECTION EMPTY") for geom in result[1:]
    )

    arr = np.array([lines, lines])
    assert arr.shape == (2, 3)
    result = shapely.polygonize_full(arr)
    assert len(result) == 4
    assert all(isinstance(arr, np.ndarray) for arr in result)
    assert all(arr.shape == (2,) for arr in result)
    assert result[0][0] == expected
    assert result[0][1] == expected
    assert all(
        g == shapely.Geometry("GEOMETRYCOLLECTION EMPTY")
        for geom in result[1:]
        for g in geom
    )

    arr = np.array([[lines, lines], [lines, lines], [lines, lines]])
    assert arr.shape == (3, 2, 3)
    result = shapely.polygonize_full(arr)
    assert len(result) == 4
    assert all(isinstance(arr, np.ndarray) for arr in result)
    assert all(arr.shape == (3, 2) for arr in result)
    for res in result[0].flatten():
        assert res == expected
    for arr in result[1:]:
        for res in arr.flatten():
            assert res == shapely.Geometry("GEOMETRYCOLLECTION EMPTY")
コード例 #14
0
ファイル: test_geometry.py プロジェクト: mwtoews/shapely
def test_set_precision_intersection():
    """Operations should use the most precise presision grid size of the inputs"""

    box1 = shapely.normalize(shapely.box(0, 0, 0.9, 0.9))
    box2 = shapely.normalize(shapely.box(0.75, 0, 1.75, 0.75))

    assert shapely.get_precision(shapely.intersection(box1, box2)) == 0

    # GEOS will use and keep the most precise precision grid size
    box1 = shapely.set_precision(box1, 0.5)
    box2 = shapely.set_precision(box2, 1)
    out = shapely.intersection(box1, box2)
    assert shapely.get_precision(out) == 0.5
    assert_geometries_equal(out, shapely.Geometry("LINESTRING (1 1, 1 0)"))
コード例 #15
0
def test_polygonize():
    lines = [
        shapely.Geometry("LINESTRING (0 0, 1 1)"),
        shapely.Geometry("LINESTRING (0 0, 0 1)"),
        shapely.Geometry("LINESTRING (0 1, 1 1)"),
        shapely.Geometry("LINESTRING (1 1, 1 0)"),
        shapely.Geometry("LINESTRING (1 0, 0 0)"),
        shapely.Geometry("LINESTRING (5 5, 6 6)"),
        shapely.Geometry("POINT (0 0)"),
        None,
    ]
    result = shapely.polygonize(lines)
    assert shapely.get_type_id(result) == 7  # GeometryCollection
    expected = shapely.Geometry(
        "GEOMETRYCOLLECTION (POLYGON ((0 0, 1 1, 1 0, 0 0)), POLYGON ((1 1, 0 0, 0 1, 1 1)))"
    )
    assert result == expected
コード例 #16
0
def test_line_interpolate_point_geom_array_normalized():
    actual = shapely.line_interpolate_point(
        [line_string, linear_ring, multi_line_string], 1, normalized=True)
    assert_geometries_equal(actual[0], shapely.Geometry("POINT (1 1)"))
    assert_geometries_equal(actual[1], shapely.Geometry("POINT (0 0)"))
    assert_geometries_equal(actual[2], shapely.Geometry("POINT (1 2)"))
コード例 #17
0
def test_polygonize_full():
    lines = [
        None,
        shapely.Geometry("LINESTRING (0 0, 1 1)"),
        shapely.Geometry("LINESTRING (0 0, 0 1)"),
        shapely.Geometry("LINESTRING (0 1, 1 1)"),
        shapely.Geometry("LINESTRING (1 1, 1 0)"),
        None,
        shapely.Geometry("LINESTRING (1 0, 0 0)"),
        shapely.Geometry("LINESTRING (5 5, 6 6)"),
        shapely.Geometry("LINESTRING (1 1, 100 100)"),
        shapely.Geometry("POINT (0 0)"),
        None,
    ]
    result = shapely.polygonize_full(lines)
    assert len(result) == 4
    assert all(shapely.get_type_id(geom) == 7 for geom in result)  # GeometryCollection
    polygons, cuts, dangles, invalid = result
    expected_polygons = shapely.Geometry(
        "GEOMETRYCOLLECTION (POLYGON ((0 0, 1 1, 1 0, 0 0)), POLYGON ((1 1, 0 0, 0 1, 1 1)))"
    )
    assert polygons == expected_polygons
    assert cuts == shapely.Geometry("GEOMETRYCOLLECTION EMPTY")
    expected_dangles = shapely.Geometry(
        "GEOMETRYCOLLECTION (LINESTRING (1 1, 100 100), LINESTRING (5 5, 6 6))"
    )
    assert dangles == expected_dangles
    assert invalid == shapely.Geometry("GEOMETRYCOLLECTION EMPTY")
コード例 #18
0
def test_polygonize_missing():
    # set of geometries that is all missing
    result = shapely.polygonize([None, None])
    assert result == shapely.Geometry("GEOMETRYCOLLECTION EMPTY")
コード例 #19
0
ファイル: test_geometry.py プロジェクト: mwtoews/shapely
def test_adapt_ptr_raises():
    point = shapely.Geometry("POINT (2 2)")
    with pytest.raises(AttributeError):
        point._geom += 1
コード例 #20
0
ファイル: test_geometry.py プロジェクト: mwtoews/shapely
def test_set_precision_grid_size_nan():
    assert shapely.set_precision(shapely.Geometry("POINT (0.9 0.9)"),
                                 np.nan) is None
コード例 #21
0
def test_clip_by_rect_polygon(geom, rect, expected):
    geom, expected = shapely.Geometry(geom), shapely.Geometry(expected)
    actual = shapely.clip_by_rect(geom, *rect)
    assert_geometries_equal(actual, expected)
コード例 #22
0
def test_clip_by_rect(geom, expected):
    geom, expected = shapely.Geometry(geom), shapely.Geometry(expected)
    actual = shapely.clip_by_rect(geom, 10, 10, 20, 20)
    assert_geometries_equal(actual, expected)
コード例 #23
0
    with pytest.raises(TypeError, match=msg):
        shapely.offset_curve([line_string, line_string], 1, mitre_limit=[5.0, 6.0])


def test_offset_curve_join_style_invalid():
    with pytest.raises(ValueError, match="'invalid' is not a valid option"):
        shapely.offset_curve(line_string, 1.0, join_style="invalid")


@pytest.mark.skipif(shapely.geos_version < (3, 7, 0), reason="GEOS < 3.7")
@pytest.mark.parametrize(
    "geom,expected",
    [
        (
            shapely.Geometry("LINESTRING (0 0, 1 2)"),
            shapely.Geometry("LINESTRING (1 2, 0 0)"),
        ),
        (
            shapely.Geometry("LINEARRING (0 0, 1 2, 1 3, 0 0)"),
            shapely.Geometry("LINEARRING (0 0, 1 3, 1 2, 0 0)"),
        ),
        (
            shapely.Geometry("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))"),
            shapely.Geometry("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))"),
        ),
        (
            shapely.Geometry(
                "POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (2 2, 2 4, 4 4, 4 2, 2 2))"
            ),
            shapely.Geometry(
コード例 #24
0
    assert_geometries_equal(actual[1], shapely.Geometry("POINT (0 0)"))
    assert_geometries_equal(actual[2], shapely.Geometry("POINT (1 2)"))


def test_line_interpolate_point_float_array():
    actual = shapely.line_interpolate_point(line_string, [0.2, 1.5, -0.2])
    assert_geometries_equal(actual[0], shapely.Geometry("POINT (0.2 0)"))
    assert_geometries_equal(actual[1], shapely.Geometry("POINT (1 0.5)"))
    assert_geometries_equal(actual[2], shapely.Geometry("POINT (1 0.8)"))


@pytest.mark.parametrize("normalized", [False, True])
@pytest.mark.parametrize(
    "geom",
    [
        shapely.Geometry("LINESTRING EMPTY"),
        shapely.Geometry("LINEARRING EMPTY"),
        shapely.Geometry("MULTILINESTRING EMPTY"),
        shapely.Geometry("MULTILINESTRING (EMPTY, (0 0, 1 1))"),
        shapely.Geometry("GEOMETRYCOLLECTION EMPTY"),
        shapely.Geometry("GEOMETRYCOLLECTION (LINESTRING EMPTY, POINT (1 1))"),
    ],
)
def test_line_interpolate_point_empty(geom, normalized):
    # These geometries segfault in some versions of GEOS (in 3.8.0, still
    # some of them segfault). Instead, we patched this to return POINT EMPTY.
    # This matches GEOS 3.8.0 behavior on simple empty geometries.
    assert_geometries_equal(
        shapely.line_interpolate_point(geom, 0.2, normalized=normalized),
        empty_point)
コード例 #25
0
def test_from_wkb_empty(wkt):
    wkb = shapely.to_wkb(shapely.Geometry(wkt))
    geom = shapely.from_wkb(wkb)
    assert shapely.is_geometry(geom).all()
    assert shapely.is_empty(geom).all()
    assert shapely.to_wkb(geom) == wkb
コード例 #26
0
def test_line_merge_geom_array():
    actual = shapely.line_merge([line_string, multi_line_string])
    assert_geometries_equal(actual[0], line_string)
    assert_geometries_equal(actual[1],
                            shapely.Geometry("LINESTRING (0 0, 1 2)"))
コード例 #27
0
def test_polygonize_full_missing():
    # set of geometries that is all missing
    result = shapely.polygonize_full([None, None])
    assert len(result) == 4
    assert all(geom == shapely.Geometry("GEOMETRYCOLLECTION EMPTY") for geom in result)
コード例 #28
0
def test_line_interpolate_point_float_array():
    actual = shapely.line_interpolate_point(line_string, [0.2, 1.5, -0.2])
    assert_geometries_equal(actual[0], shapely.Geometry("POINT (0.2 0)"))
    assert_geometries_equal(actual[1], shapely.Geometry("POINT (1 0.5)"))
    assert_geometries_equal(actual[2], shapely.Geometry("POINT (1 0.8)"))
コード例 #29
0
def test_to_wkt_empty_z(wkt):
    assert shapely.to_wkt(shapely.Geometry(wkt)) == wkt
コード例 #30
0
multi_line_string = shapely.multilinestrings([[(0, 0), (1, 2)]])
multi_polygon = shapely.multipolygons(
    [
        [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)],
        [(2.1, 2.1), (2.2, 2.1), (2.2, 2.2), (2.1, 2.2), (2.1, 2.1)],
    ]
)
geometry_collection = shapely.geometrycollections(
    [shapely.points(51, -1), shapely.linestrings([(52, -1), (49, 2)])]
)
point_z = shapely.points(2, 3, 4)
line_string_z = shapely.linestrings([(0, 0, 4), (1, 0, 4), (1, 1, 4)])
polygon_z = shapely.polygons([(0, 0, 4), (2, 0, 4), (2, 2, 4), (0, 2, 4), (0, 0, 4)])
geometry_collection_z = shapely.geometrycollections([point_z, line_string_z])
polygon_with_hole = shapely.Geometry(
    "POLYGON((0 0, 0 10, 10 10, 10 0, 0 0), (2 2, 2 4, 4 4, 4 2, 2 2))"
)
empty_point = shapely.Geometry("POINT EMPTY")
empty_point_z = shapely.Geometry("POINT Z EMPTY")
empty_line_string = shapely.Geometry("LINESTRING EMPTY")
empty_line_string_z = shapely.Geometry("LINESTRING Z EMPTY")
empty_polygon = shapely.Geometry("POLYGON EMPTY")
empty = shapely.Geometry("GEOMETRYCOLLECTION EMPTY")
line_string_nan = shapely.linestrings([(np.nan, np.nan), (np.nan, np.nan)])
multi_point_z = shapely.multipoints([(0, 0, 4), (1, 2, 4)])
multi_line_string_z = shapely.multilinestrings([[(0, 0, 4), (1, 2, 4)]])
multi_polygon_z = shapely.multipolygons(
    [
        [(0, 0, 4), (1, 0, 4), (1, 1, 4), (0, 1, 4), (0, 0, 4)],
        [(2.1, 2.1, 4), (2.2, 2.1, 4), (2.2, 2.2, 4), (2.1, 2.2, 4), (2.1, 2.1, 4)],
    ]