def test_from_points(): # From Points line = LineString((Point(1.0, 2.0), Point(3.0, 4.0))) assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)] line = LineString([Point(1.0, 2.0), Point(3.0, 4.0)]) assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
def test_empty_subgeoms(): geom = GeometryCollection([Point(), LineString()]) assert geom.type == "GeometryCollection" assert geom.type == geom.geom_type assert geom.is_empty assert len(geom.geoms) == 2 assert list(geom.geoms) == [Point(), LineString()]
def test_from_coordinate_sequence(): # From coordinate tuples line = LineString(((1.0, 2.0), (3.0, 4.0))) assert len(line.coords) == 2 assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)] line = LineString([(1.0, 2.0), (3.0, 4.0)]) assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
def test_linestring_immutable(): line = LineString(((1.0, 2.0), (3.0, 4.0))) with pytest.raises(AttributeError): line.coords = [(-1.0, -1.0), (1.0, 1.0)] with pytest.raises(TypeError): line.coords[0] = (-1.0, -1.0)
def test_collection(): g = GeometryCollection([Point(1, 2), LineString([(1, 2), (3, 4)])]) assert hash(g) == hash( GeometryCollection([Point(1, 2), LineString([(1, 2), (3, 4)])])) assert hash(g) != hash( GeometryCollection([Point(1, 2), LineString([(1, 2), (3, 3)])]))
def test_polygon_from_linestring(): coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)] line = LineString(coords) polygon = Polygon(line) assert polygon.exterior.coords[:] == coords # from unclosed linestring line = LineString(coords[:-1]) polygon = Polygon(line) assert polygon.exterior.coords[:] == coords
def test_from_empty(): line = LineString() assert line.is_empty assert isinstance(line.coords, CoordinateSequence) assert line.coords[:] == [] line = LineString([]) assert line.is_empty assert isinstance(line.coords, CoordinateSequence) assert line.coords[:] == []
def test_child_with_deleted_parent(): # test that we can remove a collection while keeping # children around a = LineString([(0, 0), (1, 1), (1, 2), (2, 2)]) b = LineString([(0, 0), (1, 1), (2, 1), (2, 2)]) collection = a.intersection(b) child = collection.geoms[0] # delete parent of child del collection # access geometry, this should not seg fault as 1.2.15 did assert child.wkt is not None
def test_data_destriding(self): coords = np.array([[12, 34], [56, 78]], dtype=np.float32) # Easy way to introduce striding: reverse list order processed_coords = np.array(LineString(coords[::-1]).coords) assert coords[::-1].tolist() == processed_coords.tolist()
def test_multilinestring(self): # From coordinate tuples geom = MultiLineString((((1.0, 2.0), (3.0, 4.0)), )) assert isinstance(geom, MultiLineString) assert len(geom.geoms) == 1 assert dump_coords(geom) == [[(1.0, 2.0), (3.0, 4.0)]] # From lines a = LineString(((1.0, 2.0), (3.0, 4.0))) ml = MultiLineString([a]) assert len(ml.geoms) == 1 assert dump_coords(ml) == [[(1.0, 2.0), (3.0, 4.0)]] # From another multi-line ml2 = MultiLineString(ml) assert len(ml2.geoms) == 1 assert dump_coords(ml2) == [[(1.0, 2.0), (3.0, 4.0)]] # Sub-geometry Access geom = MultiLineString([(((0.0, 0.0), (1.0, 2.0)))]) assert isinstance(geom.geoms[0], LineString) assert dump_coords(geom.geoms[0]) == [(0.0, 0.0), (1.0, 2.0)] with pytest.raises(IndexError): # index out of range geom.geoms[1] # Geo interface assert geom.__geo_interface__ == { "type": "MultiLineString", "coordinates": (((0.0, 0.0), (1.0, 2.0)), ), }
def test_linearring_from_unclosed_linestring(): coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)] line = LineString(coords[:-1]) # Pass in unclosed line ring = LinearRing(line) assert len(ring.coords) == 4 assert ring.coords[:] == coords assert ring.geom_type == "LinearRing"
def test_linearring_from_too_short_linestring(): # Creation of LinearRing request at least 3 coordinates (unclosed) or # 4 coordinates (closed) coords = [(0.0, 0.0), (1.0, 1.0)] line = LineString(coords) with pytest.raises(ValueError, match="requires at least 4 coordinates"): LinearRing(line)
def test_linestring_array_coercion(): # don't convert to array of coordinates, keep objects line = LineString(((1.0, 2.0), (3.0, 4.0))) arr = np.array(line) assert arr.ndim == 0 assert arr.size == 1 assert arr.dtype == np.dtype("object") assert arr.item() == line
def test_index_3d_coords(self): c = [(float(x), float(-x), float(x * 2)) for x in range(4)] g = LineString(c) for i in range(-4, 4): assert g.coords[i] == c[i] with pytest.raises(IndexError): g.coords[4] with pytest.raises(IndexError): g.coords[-5]
def test_numpy_linestring_coords(self): from numpy.testing import assert_array_equal line = LineString(((1.0, 2.0), (3.0, 4.0))) expected = np.array([[1.0, 2.0], [3.0, 4.0]]) # Coordinate sequences can be adapted as well la = np.asarray(line.coords) assert_array_equal(la, expected)
def test_slice_3d_coords(self): c = [(float(x), float(-x), float(x * 2)) for x in range(4)] g = LineString(c) assert g.coords[1:] == c[1:] assert g.coords[:-1] == c[:-1] assert g.coords[::-1] == c[::-1] assert g.coords[::2] == c[::2] assert g.coords[:4] == c[:4] assert g.coords[4:] == c[4:] == []
def test_from_invalid_dim(): # TODO(shapely-2.0) better error message? # pytest.raises(ValueError, match="at least 2 coordinate tuples|at least 2 coordinates"): with pytest.raises(shapely.GEOSException): LineString([(1, 2)]) # exact error depends on numpy version with pytest.raises((ValueError, TypeError)): LineString([(1, 2, 3), (4, 5)]) with pytest.raises((ValueError, TypeError)): LineString([(1, 2), (3, 4, 5)]) msg = r"The ordinate \(last\) dimension should be 2 or 3, got {}" with pytest.raises(ValueError, match=msg.format(4)): LineString([(1, 2, 3, 4), (4, 5, 6, 7)]) with pytest.raises(ValueError, match=msg.format(1)): LineString([(1, ), (4, )])
def test_equals_argument_order(self): """ Test equals predicate functions correctly regardless of the order of the inputs. See issue #317. """ coords = ((0, 0), (1, 0), (1, 1), (0, 0)) ls = LineString(coords) lr = LinearRing(coords) assert ls.__eq__(lr) is False # previously incorrectly returned True assert lr.__eq__(ls) is False assert (ls == lr) is False assert (lr == ls) is False ls_clone = LineString(coords) lr_clone = LinearRing(coords) assert ls.__eq__(ls_clone) is True assert lr.__eq__(lr_clone) is True assert (ls == ls_clone) is True assert (lr == lr_clone) is True
def test_linestring(self): # From coordinate tuples line = LineString(((1.0, 2.0), (3.0, 4.0))) assert len(line.coords) == 2 assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)] # Bounds assert line.bounds == (1.0, 2.0, 3.0, 4.0) # Coordinate access assert tuple(line.coords) == ((1.0, 2.0), (3.0, 4.0)) assert line.coords[0] == (1.0, 2.0) assert line.coords[1] == (3.0, 4.0) with pytest.raises(IndexError): line.coords[2] # index out of range # Geo interface assert line.__geo_interface__ == { "type": "LineString", "coordinates": ((1.0, 2.0), (3.0, 4.0)), }
def test_from_linestring(): # From another linestring line = LineString(((1.0, 2.0), (3.0, 4.0))) copy = LineString(line) assert copy.coords[:] == [(1.0, 2.0), (3.0, 4.0)] assert copy.geom_type == "LineString"
def test_numpy_object_array(): geom = LineString([(0.0, 0.0), (0.0, 1.0)]) ar = np.empty(1, object) ar[:] = [geom] assert ar[0] == geom
def test_numpy_empty_linestring_coords(): # Check empty line = LineString([]) la = np.asarray(line.coords) assert la.shape == (0, 2)
def test_from_numpy(): # Construct from a numpy array line = LineString(np.array([[1.0, 2.0], [3.0, 4.0]])) assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
def test_from_generator(): gen = (coord for coord in [(1.0, 2.0), (3.0, 4.0)]) line = LineString(gen) assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
def test_linestring(): g = LineString([(1, 2), (3, 4)]) assert hash(g) == hash(LineString([(1, 2), (3, 4)])) assert hash(g) != hash(LineString([(1, 2), (3, 3)]))
def test_from_coordinate_sequence_3D(): line = LineString(((1.0, 2.0, 3.0), (3.0, 4.0, 5.0))) assert line.has_z assert line.coords[:] == [(1.0, 2.0, 3.0), (3.0, 4.0, 5.0)]
def test_numpy_object_array(): geom = GeometryCollection([LineString([(0, 0), (1, 1)])]) ar = np.empty(1, object) ar[:] = [geom] assert ar[0] == geom
def test_from_linearring(): coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)] ring = LinearRing(coords) copy = LineString(ring) assert copy.coords[:] == coords assert copy.geom_type == "LineString"
def test_from_mix(): # From mix of tuples and Points line = LineString((Point(1.0, 2.0), (2.0, 3.0), Point(3.0, 4.0))) assert line.coords[:] == [(1.0, 2.0), (2.0, 3.0), (3.0, 4.0)]
def test_from_linestring_z(): coords = [(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)] line = LineString(coords) copy = LineString(line) assert copy.coords[:] == coords assert copy.geom_type == "LineString"