Esempio n. 1
0
def test_float_arg_array(geometry, func):
    if func is shapely.offset_curve and shapely.get_type_id(geometry) not in [1, 2]:
        with pytest.raises(GEOSException, match="only accept linestrings"):
            func([geometry, geometry], 0.0)
        return
    # voronoi_polygons emits an "invalid" warning when supplied with an empty
    # point (see https://github.com/libgeos/geos/issues/515)
    with ignore_invalid(
        func is shapely.voronoi_polygons and shapely.get_type_id(geometry) == 0
    ):
        actual = func([geometry, geometry], 0.0)
    assert actual.shape == (2,)
    assert isinstance(actual[0], Geometry)
Esempio n. 2
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")
Esempio n. 3
0
def test_from_wkb_point_empty(wkb, expected_type, expected_dim):
    geom = shapely.from_wkb(wkb)
    # POINT (nan nan) transforms to an empty point
    assert shapely.is_empty(geom)
    assert shapely.get_type_id(geom) == expected_type
    # The dimensionality (2D/3D) is only read correctly for GEOS >= 3.9.0
    if shapely.geos_version >= (3, 9, 0):
        assert shapely.get_coordinate_dimension(geom) == expected_dim
Esempio n. 4
0
def test_pickle(geom):
    if shapely.get_type_id(geom) == 2:
        # Linearrings get converted to linestrings
        expected = shapely.linestrings(shapely.get_coordinates(geom))
    else:
        expected = geom
    pickled = pickle.dumps(geom)
    assert_geometries_equal(pickle.loads(pickled), expected, tolerance=0)
Esempio n. 5
0
def test_get_rings(geom):
    if (shapely.get_type_id(geom) !=
            shapely.GeometryType.POLYGON) or shapely.is_empty(geom):
        rings = shapely.get_rings(geom)
        assert len(rings) == 0
    else:
        rings = shapely.get_rings(geom)
        assert len(rings) == 1
        assert rings[0] == shapely.get_exterior_ring(geom)
Esempio n. 6
0
 def geom_type(self):
     idx = shapely.get_type_id(self.g)
     return [
         "None",
         "Point",
         "LineString",
         "LinearRing",
         "Polygon",
         "MultiPoint",
         "MultiLineString",
         "MultiPolygon",
         "GeometryCollection",
     ][idx]
Esempio n. 7
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
Esempio n. 8
0
def test_geojson_all_types(geom):
    if shapely.get_type_id(geom) == shapely.GeometryType.LINEARRING:
        pytest.skip("Linearrings are not preserved in GeoJSON")
    geojson = shapely.to_geojson(geom)
    actual = shapely.from_geojson(geojson)
    assert_geometries_equal(actual, geom)
Esempio n. 9
0
def test_from_wkb_all_types(geom, use_hex, byte_order):
    if shapely.get_type_id(geom) == shapely.GeometryType.LINEARRING:
        pytest.skip("Linearrings are not preserved in WKB")
    wkb = shapely.to_wkb(geom, hex=use_hex, byte_order=byte_order)
    actual = shapely.from_wkb(wkb)
    assert_geometries_equal(actual, geom)
Esempio n. 10
0
def test_get_exterior_ring():
    actual = shapely.get_exterior_ring([polygon, polygon_with_hole])
    assert (shapely.get_type_id(actual) == 2).all()
Esempio n. 11
0
def test_get_type_id():
    actual = shapely.get_type_id(all_types).tolist()
    assert actual == [0, 1, 2, 3, 4, 5, 6, 7, 7]
Esempio n. 12
0
 def geom_type(self):
     """Name of the geometry's type, such as 'Point'"""
     return GEOMETRY_TYPES[shapely.get_type_id(self)]
Esempio n. 13
0
import numpy as np
import pytest

import shapely
from shapely import Geometry
from shapely.decorators import UnsupportedGEOSOperation
from shapely.testing import assert_geometries_equal

from .common import all_types, multi_polygon, point, polygon

# fixed-precision operations raise GEOS exceptions on mixed dimension geometry collections
all_single_types = [g for g in all_types if not shapely.get_type_id(g) == 7]

SET_OPERATIONS = (
    shapely.difference,
    shapely.intersection,
    shapely.symmetric_difference,
    shapely.union,
    # shapely.coverage_union is tested seperately
)

REDUCE_SET_OPERATIONS = (
    (shapely.intersection_all, shapely.intersection),
    (shapely.symmetric_difference_all, shapely.symmetric_difference),
    (shapely.union_all, shapely.union),
    #  shapely.coverage_union_all, shapely.coverage_union) is tested seperately
)

# operations that support fixed precision
REDUCE_SET_OPERATIONS_PREC = ((shapely.union_all, shapely.union), )
Esempio n. 14
0
def test_empty(geom_type):
    actual = shapely.empty((2, ), geom_type=geom_type)
    assert (~shapely.is_missing(actual)).all()
    assert shapely.is_empty(actual).all()
    assert (shapely.get_type_id(actual) == geom_type).all()
Esempio n. 15
0
def test_subclasses(with_point_in_registry):
    for _point in [Point("POINT (1 1)"), shapely.points(1, 1)]:
        assert isinstance(_point, Point)
        assert shapely.get_type_id(_point) == shapely.GeometryType.POINT
        assert _point.x == 1
Esempio n. 16
0
 def type_id(self):
     return shapely.get_type_id(self)