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
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,)
Exemple #3
0
    def polygonize(self, lines):
        """Creates polygons from a source of lines

        The source may be a MultiLineString, a sequence of LineString objects,
        or a sequence of objects than can be adapted to LineStrings.
        """
        source = getattr(lines, "geoms", None) or lines
        try:
            source = iter(source)
        except TypeError:
            source = [source]
        finally:
            obs = [self.shapeup(line) for line in source]
        collection = shapely.polygonize(obs)
        return collection.geoms
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
def test_polygonize_missing():
    # set of geometries that is all missing
    result = shapely.polygonize([None, None])
    assert result == shapely.Geometry("GEOMETRYCOLLECTION EMPTY")