Exemple #1
0
    def __new__(self, geoms=None):
        """
        Parameters
        ----------
        geoms : list
            A list of shapely geometry instances, which may be heterogeneous.

        Example
        -------
        Create a GeometryCollection with a Point and a LineString

          >>> from shapely.geometry import LineString, Point
          >>> p = Point(51, -1)
          >>> l = LineString([(52, -1), (49, 2)])
          >>> gc = GeometryCollection([p, l])
        """
        if not geoms:
            # TODO better empty constructor
            return shapely.from_wkt("GEOMETRYCOLLECTION EMPTY")
        if isinstance(geoms, BaseGeometry):
            # TODO(shapely-2.0) do we actually want to split Multi-part geometries?
            # this is needed for the split() tests
            if hasattr(geoms, "geoms"):
                geoms = geoms.geoms
            else:
                geoms = [geoms]

        return shapely.geometrycollections(geoms)
Exemple #2
0
def test_geometrycollections_out(indices, expected):
    out = np.empty(4, dtype=object)
    out[3] = empty_point
    actual = shapely.geometrycollections([point, line_string],
                                         indices=indices,
                                         out=out)
    assert_geometries_equal(out, expected)
    assert actual is out
Exemple #3
0
    def __new__(self, geoms=None):
        if not geoms:
            # TODO better empty constructor
            return shapely.from_wkt("GEOMETRYCOLLECTION EMPTY")
        if isinstance(geoms, BaseGeometry):
            # TODO(shapely-2.0) do we actually want to split Multi-part geometries?
            # this is needed for the split() tests
            if hasattr(geoms, "geoms"):
                geoms = geoms.geoms
            else:
                geoms = [geoms]

        return shapely.geometrycollections(geoms)
Exemple #4
0
def test_get_parts_geometry_collection_multi():
    """On the first pass, the individual Multi* geometry objects are returned
    from the collection.  On the second pass, the individual singular geometry
    objects within those are returned.
    """
    geom = shapely.geometrycollections(
        [multi_point, multi_line_string, multi_polygon])
    expected_num_parts = shapely.get_num_geometries(geom)
    expected_parts = shapely.get_geometry(geom, range(0, expected_num_parts))

    parts = shapely.get_parts(geom)
    assert len(parts) == expected_num_parts
    assert_geometries_equal(parts, expected_parts)

    expected_subparts = []
    for g in np.asarray(expected_parts):
        for i in range(0, shapely.get_num_geometries(g)):
            expected_subparts.append(shapely.get_geometry(g, i))

    subparts = shapely.get_parts(parts)
    assert len(subparts) == len(expected_subparts)
    assert_geometries_equal(subparts, expected_subparts)
Exemple #5
0
def test_to_wkt_geometrycollection_with_point_empty():
    collection = shapely.geometrycollections([empty_point, point])
    # do not check the full value as some GEOS versions give
    # GEOMETRYCOLLECTION Z (...) and others give GEOMETRYCOLLECTION (...)
    assert shapely.to_wkt(collection).endswith("(POINT EMPTY, POINT (2 3))")
Exemple #6
0
                          byte_order=1) == ewkb

    point = shapely.points(1, 1)
    point_with_srid = shapely.set_srid(point, np.int32(4326))
    result = shapely.to_wkb(point_with_srid, include_srid=True, byte_order=1)
    assert np.frombuffer(result[5:9], "<u4").item() == 4326


@pytest.mark.parametrize(
    "geom,expected",
    [
        (empty_point, POINT_NAN_WKB),
        (empty_point_z, POINT_NAN_WKB),
        (shapely.multipoints([empty_point]), MULTIPOINT_NAN_WKB),
        (shapely.multipoints([empty_point_z]), MULTIPOINT_NAN_WKB),
        (shapely.geometrycollections([empty_point
                                      ]), GEOMETRYCOLLECTION_NAN_WKB),
        (shapely.geometrycollections([empty_point_z
                                      ]), GEOMETRYCOLLECTION_NAN_WKB),
        (
            shapely.geometrycollections([shapely.multipoints([empty_point])]),
            NESTED_COLLECTION_NAN_WKB,
        ),
        (
            shapely.geometrycollections([shapely.multipoints([empty_point_z])
                                         ]),
            NESTED_COLLECTION_NAN_WKB,
        ),
    ],
)
def test_to_wkb_point_empty_2d(geom, expected):
    actual = shapely.to_wkb(geom, output_dimension=2, byte_order=1)
Exemple #7
0
    shapely.box(2, 2, 4, 4),
)
point = shapely.points(2, 3)
line_string = shapely.linestrings([(0, 0), (1, 0), (1, 1)])
linear_ring = shapely.linearrings([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
polygon = shapely.polygons([(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)])
multi_point = shapely.multipoints([(0, 0), (1, 2)])
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)])
Exemple #8
0
    # 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)


@pytest.mark.parametrize("normalized", [False, True])
@pytest.mark.parametrize(
    "geom",
    [
        empty_point,
        point,
        polygon,
        multi_point,
        multi_polygon,
        shapely.geometrycollections([point]),
        shapely.geometrycollections([polygon]),
        shapely.geometrycollections([multi_line_string]),
        shapely.geometrycollections([multi_point]),
        shapely.geometrycollections([multi_polygon]),
    ],
)
def test_line_interpolate_point_invalid_type(geom, normalized):
    with pytest.raises(TypeError):
        assert shapely.line_interpolate_point(geom, 0.2, normalized=normalized)


def test_line_interpolate_point_none():
    assert shapely.line_interpolate_point(None, 0.2) is None

Exemple #9
0
@pytest.mark.parametrize("flags", [np.array([0, 1]), 4, "foo"])
def set_precision_illegal_flags(flags):
    # the preserve_topology kwarg is deprecated (ignored)
    with pytest.raises((ValueError, TypeError)):
        shapely.lib.set_precision(line_string, 1.0, flags)


def test_empty():
    """Compatibility with empty_like, see GH373"""
    g = np.empty_like(np.array([None, None]))
    assert shapely.is_missing(g).all()


# corresponding to geometry_collection_z:
geometry_collection_2 = shapely.geometrycollections([point, line_string])
empty_geom_mark = pytest.mark.skipif(
    shapely.geos_version < (3, 9, 0),
    reason="Empty points don't have a dimensionality before GEOS 3.9",
)


@pytest.mark.parametrize(
    "geom,expected",
    [
        (point, point),
        (point_z, point),
        pytest.param(empty_point, empty_point, marks=empty_geom_mark),
        pytest.param(empty_point_z, empty_point, marks=empty_geom_mark),
        (line_string, line_string),
        (line_string_z, line_string),
Exemple #10
0
def test_geometrycollections_no_index_raises():
    with pytest.raises(ValueError):
        shapely.geometrycollections(np.array([point, line_string],
                                             dtype=object),
                                    indices=[0, 2])
Exemple #11
0
def test_geometrycollections(geometries, indices, expected):
    actual = shapely.geometrycollections(np.array(geometries, dtype=object),
                                         indices=indices)
    assert_geometries_equal(actual, expected)
Exemple #12
0
    geometry_collection,
    geometry_collection_z,
    line_string,
    line_string_z,
    linear_ring,
    multi_line_string,
    multi_point,
    multi_polygon,
    point,
    point_z,
    polygon,
    polygon_with_hole,
    polygon_z,
)

nested_2 = shapely.geometrycollections([geometry_collection, point])
nested_3 = shapely.geometrycollections([nested_2, point])


@pytest.mark.parametrize(
    "geoms,count",
    [
        ([], 0),
        ([empty], 0),
        ([point, empty], 1),
        ([empty, point, empty], 1),
        ([point, None], 1),
        ([None, point, None], 1),
        ([point, point], 2),
        ([point, point_z], 2),
        ([line_string, linear_ring], 8),