|
16 | 16 | Polygon, |
17 | 17 | Ring, |
18 | 18 | ) |
19 | | -from .......core.geometry.cartesian.algorithms import for_each_point_distance |
| 19 | +from .......core.geometry.cartesian.algorithms import ( |
| 20 | + for_each_point_distance, |
| 21 | + for_each_point_pairwise_distance, |
| 22 | +) |
20 | 23 |
|
21 | 24 |
|
22 | 25 | class TestForEachPointDistance: |
@@ -141,3 +144,65 @@ def test_for_each_point_distance_all_inside(self) -> None: |
141 | 144 | assert len(result) == 4 |
142 | 145 | # All distances should be 0 |
143 | 146 | assert np.allclose(result, 0.0) |
| 147 | + |
| 148 | + |
| 149 | +class TestForEachPointPairwiseDistance: |
| 150 | + """Tests for for_each_point_pairwise_distance algorithm.""" |
| 151 | + |
| 152 | + def test_for_each_point_pairwise_distance_multipoint(self) -> None: |
| 153 | + """Test pairwise distances for multipoints.""" |
| 154 | + geometry1 = MultiPoint( |
| 155 | + np.array([0.0, 1.0, 4.0]), np.array([0.0, 1.0, 4.0]) |
| 156 | + ) |
| 157 | + geometry2 = MultiPoint( |
| 158 | + np.array([0.0, 2.0, 4.0]), np.array([0.0, 2.0, 7.0]) |
| 159 | + ) |
| 160 | + |
| 161 | + result = for_each_point_pairwise_distance(geometry1, geometry2) |
| 162 | + |
| 163 | + assert isinstance(result, np.ndarray) |
| 164 | + assert result.dtype == np.float64 |
| 165 | + assert len(result) == 3 |
| 166 | + assert np.allclose(result, np.array([0.0, np.sqrt(2.0), 3.0])) |
| 167 | + |
| 168 | + def test_for_each_point_pairwise_distance_linestring(self) -> None: |
| 169 | + """Test pairwise distances for linestrings.""" |
| 170 | + geometry1 = LineString(np.array([0.0, 2.0]), np.array([0.0, 2.0])) |
| 171 | + geometry2 = LineString(np.array([0.0, 2.0]), np.array([0.0, 5.0])) |
| 172 | + |
| 173 | + result = for_each_point_pairwise_distance(geometry1, geometry2) |
| 174 | + |
| 175 | + assert isinstance(result, np.ndarray) |
| 176 | + assert result.dtype == np.float64 |
| 177 | + assert len(result) == 2 |
| 178 | + assert np.allclose(result, np.array([0.0, 3.0])) |
| 179 | + |
| 180 | + def test_for_each_point_pairwise_distance_ring(self) -> None: |
| 181 | + """Test pairwise distances for rings.""" |
| 182 | + geometry1 = Ring( |
| 183 | + np.array([0.0, 0.0, 1.0, 1.0, 0.0]), |
| 184 | + np.array([0.0, 1.0, 1.0, 0.0, 0.0]), |
| 185 | + ) |
| 186 | + geometry2 = Ring( |
| 187 | + np.array([0.0, 0.0, 2.0, 2.0, 0.0]), |
| 188 | + np.array([0.0, 1.0, 1.0, 0.0, 0.0]), |
| 189 | + ) |
| 190 | + |
| 191 | + result = for_each_point_pairwise_distance(geometry1, geometry2) |
| 192 | + |
| 193 | + assert isinstance(result, np.ndarray) |
| 194 | + assert result.dtype == np.float64 |
| 195 | + assert len(result) == 5 |
| 196 | + assert np.allclose(result, np.array([0.0, 0.0, 1.0, 1.0, 0.0])) |
| 197 | + |
| 198 | + def test_for_each_point_pairwise_distance_size_mismatch(self) -> None: |
| 199 | + """Test pairwise distances with different number of points.""" |
| 200 | + geometry1 = MultiPoint(np.array([0.0, 1.0]), np.array([0.0, 1.0])) |
| 201 | + geometry2 = MultiPoint(np.array([0.0]), np.array([0.0])) |
| 202 | + |
| 203 | + with pytest.raises( |
| 204 | + ValueError, |
| 205 | + match="Source and target geometries must have the same " |
| 206 | + "number of points", |
| 207 | + ): |
| 208 | + for_each_point_pairwise_distance(geometry1, geometry2) |
0 commit comments