1from . import unittest, numpy, shapely20_deprecated 2from .test_multi import MultiGeometryTestCase 3 4import pytest 5 6from shapely.errors import EmptyPartError, ShapelyDeprecationWarning 7from shapely.geometry import Point, MultiPoint, asMultiPoint 8from shapely.geometry.base import dump_coords 9 10 11class MultiPointTestCase(MultiGeometryTestCase): 12 13 def test_multipoint(self): 14 15 # From coordinate tuples 16 geom = MultiPoint(((1.0, 2.0), (3.0, 4.0))) 17 self.assertEqual(len(geom.geoms), 2) 18 self.assertEqual(dump_coords(geom), [[(1.0, 2.0)], [(3.0, 4.0)]]) 19 20 # From points 21 geom = MultiPoint((Point(1.0, 2.0), Point(3.0, 4.0))) 22 self.assertEqual(len(geom.geoms), 2) 23 self.assertEqual(dump_coords(geom), [[(1.0, 2.0)], [(3.0, 4.0)]]) 24 25 # From another multi-point 26 geom2 = MultiPoint(geom) 27 self.assertEqual(len(geom2.geoms), 2) 28 self.assertEqual(dump_coords(geom2), [[(1.0, 2.0)], [(3.0, 4.0)]]) 29 30 # Sub-geometry Access 31 self.assertIsInstance(geom.geoms[0], Point) 32 self.assertEqual(geom.geoms[0].x, 1.0) 33 self.assertEqual(geom.geoms[0].y, 2.0) 34 with self.assertRaises(IndexError): # index out of range 35 geom.geoms[2] 36 37 # Geo interface 38 self.assertEqual(geom.__geo_interface__, 39 {'type': 'MultiPoint', 40 'coordinates': ((1.0, 2.0), (3.0, 4.0))}) 41 42 @shapely20_deprecated 43 def test_multipoint_adapter(self): 44 # Adapt a coordinate list to a line string 45 coords = [[5.0, 6.0], [7.0, 8.0]] 46 geoma = asMultiPoint(coords) 47 self.assertEqual(dump_coords(geoma), [[(5.0, 6.0)], [(7.0, 8.0)]]) 48 49 @unittest.skipIf(not numpy, 'Numpy required') 50 def test_multipoint_from_numpy(self): 51 52 from numpy import array, asarray 53 from numpy.testing import assert_array_equal 54 55 # Construct from a numpy array 56 geom = MultiPoint(array([[0.0, 0.0], [1.0, 2.0]])) 57 self.assertIsInstance(geom, MultiPoint) 58 self.assertEqual(len(geom.geoms), 2) 59 self.assertEqual(dump_coords(geom), [[(0.0, 0.0)], [(1.0, 2.0)]]) 60 61 @shapely20_deprecated 62 @unittest.skipIf(not numpy, 'Numpy required') 63 def test_numpy(self): 64 65 from numpy import array, asarray 66 from numpy.testing import assert_array_equal 67 68 # Geo interface (cont.) 69 geom = MultiPoint((Point(1.0, 2.0), Point(3.0, 4.0))) 70 assert_array_equal(array(geom), array([[1., 2.], [3., 4.]])) 71 72 @shapely20_deprecated 73 @unittest.skipIf(not numpy, 'Numpy required') 74 def test_numpy_adapter(self): 75 76 from numpy import array, asarray 77 from numpy.testing import assert_array_equal 78 79 # Adapt a Numpy array to a multipoint 80 a = array([[1.0, 2.0], [3.0, 4.0]]) 81 geoma = asMultiPoint(a) 82 assert_array_equal(geoma.context, array([[1., 2.], [3., 4.]])) 83 self.assertEqual(dump_coords(geoma), [[(1.0, 2.0)], [(3.0, 4.0)]]) 84 85 # Now, the inverse 86 self.assertEqual(geoma.__array_interface__, 87 geoma.context.__array_interface__) 88 89 pas = asarray(geoma) 90 assert_array_equal(pas, array([[1., 2.], [3., 4.]])) 91 92 @shapely20_deprecated 93 def test_subgeom_access(self): 94 p0 = Point(1.0, 2.0) 95 p1 = Point(3.0, 4.0) 96 self.subgeom_access_test(MultiPoint, [p0, p1]) 97 98 def test_create_multi_with_empty_component(self): 99 with self.assertRaises(EmptyPartError) as exc: 100 wkt = MultiPoint([Point(0, 0), Point()]).wkt 101 102 self.assertEqual(str(exc.exception), "Can't create MultiPoint with empty component") 103 104 105def test_multipoint_adapter_deprecated(): 106 coords = [[5.0, 6.0], [7.0, 8.0]] 107 with pytest.warns(ShapelyDeprecationWarning, match="proxy geometries"): 108 asMultiPoint(coords) 109 110 111def test_multipoint_ctypes_deprecated(): 112 geom = MultiPoint(((1.0, 2.0), (3.0, 4.0))) 113 with pytest.warns(ShapelyDeprecationWarning, match="ctypes"): 114 geom.ctypes 115 116 117def test_multipoint_array_interface_deprecated(): 118 geom = MultiPoint(((1.0, 2.0), (3.0, 4.0))) 119 with pytest.warns(ShapelyDeprecationWarning, match="array_interface"): 120 geom.array_interface() 121 122 123@unittest.skipIf(not numpy, 'Numpy required') 124def test_multipoint_array_interface_numpy_deprecated(): 125 import numpy as np 126 127 geom = MultiPoint(((1.0, 2.0), (3.0, 4.0))) 128 with pytest.warns(ShapelyDeprecationWarning, match="array interface"): 129 np.array(geom) 130 131 132@shapely20_deprecated 133@pytest.mark.filterwarnings("error:An exception was ignored") # NumPy 1.21 134def test_numpy_object_array(): 135 np = pytest.importorskip("numpy") 136 137 geom = MultiPoint(((1.0, 2.0), (3.0, 4.0))) 138 ar = np.empty(1, object) 139 ar[:] = [geom] 140 assert ar[0] == geom 141 142 143def test_iteration_deprecated(): 144 geom = MultiPoint([[5.0, 6.0], [7.0, 8.0]]) 145 with pytest.warns(ShapelyDeprecationWarning, match="Iteration"): 146 for g in geom: 147 pass 148 149 150def test_getitem_deprecated(): 151 geom = MultiPoint([[5.0, 6.0], [7.0, 8.0]]) 152 with pytest.warns(ShapelyDeprecationWarning, match="__getitem__"): 153 part = geom[0] 154 155 156def test_len_deprecated(): 157 geom = MultiPoint([[5.0, 6.0], [7.0, 8.0]]) 158 with pytest.warns(ShapelyDeprecationWarning, match="__len__"): 159 assert len(geom) == 2 160