1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
from typing import cast
import pytest
import shapely.affinity
import shapely.geometry
from shapely.geometry import MultiPolygon, Point, Polygon
import antimeridian
from antimeridian import FixWindingWarning
from .conftest import Reader
@pytest.mark.parametrize(
("name"),
[
"almost-180",
"complex-split",
"crossing-latitude",
"extra-crossing",
"issues-171",
"issues-187",
"issues-81",
"latitude-band",
"north-pole",
"one-hole",
"over-180",
"overlap",
"point-on-antimeridian",
"simple",
"south-pole",
"split",
"two-holes",
],
)
@pytest.mark.parametrize(
"subdirectory,great_circle",
[("flat", False), ("spherical", True)],
)
def test_fix_polygon(
name: str,
read_input: Reader,
read_output: Reader,
subdirectory: str,
great_circle: bool,
) -> None:
input = read_input(name)
assert isinstance(input, Polygon)
output = read_output(name, subdirectory)
assert isinstance(input, Polygon) or isinstance(input, MultiPolygon)
fixed = antimeridian.fix_polygon(input, great_circle=great_circle).normalize()
assert fixed == output.normalize()
@pytest.mark.parametrize(
"subdirectory,great_circle",
[("flat", False), ("spherical", True)],
)
def test_both_poles(
read_input: Reader, read_output: Reader, subdirectory: str, great_circle: bool
) -> None:
input = read_input("both-poles")
assert isinstance(input, Polygon)
output = read_output("both-poles", subdirectory)
assert isinstance(input, Polygon)
fixed = antimeridian.fix_polygon(
input, fix_winding=False, great_circle=great_circle
).normalize()
assert fixed == output.normalize()
def test_fix_shape(read_input: Reader) -> None:
# Just a smoke test
input = shapely.geometry.mapping(read_input("simple"))
antimeridian.fix_shape(input)
@pytest.mark.parametrize(
"subdirectory,great_circle",
[("flat", False), ("spherical", True)],
)
def test_double_fix(
read_input: Reader,
read_output: Reader,
subdirectory: str,
great_circle: bool,
) -> None:
input = read_input("north-pole")
output = read_output("north-pole", subdirectory)
fixed = antimeridian.fix_polygon(input, great_circle=great_circle)
fixed = antimeridian.fix_polygon(fixed, great_circle=great_circle)
assert fixed.normalize() == output.normalize()
@pytest.mark.parametrize(
"subdirectory,great_circle",
[("flat", False), ("spherical", True)],
)
def test_force_north_pole(
read_input: Reader, read_output: Reader, subdirectory: str, great_circle: bool
) -> None:
input = read_input("force-north-pole")
output = read_output("force-north-pole", subdirectory)
fixed = antimeridian.fix_polygon(
input, force_north_pole=True, great_circle=great_circle
)
assert fixed.normalize() == output.normalize()
@pytest.mark.parametrize("minx,maxx", [(-180, -170), (170, 180)])
def test_dont_segment_antimeridian_overlap(minx: float, maxx: float) -> None:
shape = shapely.geometry.box(minx=minx, miny=-10, maxx=maxx, maxy=10)
fixed = antimeridian.fix_polygon(shape)
assert fixed.geom_type == "Polygon"
@pytest.mark.parametrize("name", ("cw-only", "cw-split", "issues-174", "issues-201"))
@pytest.mark.parametrize(
"subdirectory,great_circle",
[("flat", False), ("spherical", True)],
)
def test_fix_winding(
read_input: Reader,
read_output: Reader,
name: str,
subdirectory: str,
great_circle: bool,
) -> None:
input = read_input(name)
output = read_output(name, subdirectory)
with pytest.warns(FixWindingWarning):
fixed = antimeridian.fix_polygon(input, great_circle=great_circle)
assert fixed.normalize() == output.normalize()
@pytest.mark.parametrize("name", ("cw-only", "cw-split"))
@pytest.mark.parametrize(
"subdirectory,great_circle",
[("flat", False), ("spherical", True)],
)
def test_no_fix_winding(
read_input: Reader,
read_output: Reader,
name: str,
subdirectory: str,
great_circle: bool,
) -> None:
input = read_input(name)
output = read_output(f"{name}-no-fix", subdirectory)
fixed = antimeridian.fix_polygon(
input, fix_winding=False, great_circle=great_circle
)
assert fixed.normalize() == output.normalize()
@pytest.mark.parametrize("name", ("cw-only", "cw-split"))
@pytest.mark.parametrize(
"force_north_pole,force_south_pole", [(True, False), (False, True), (True, True)]
)
@pytest.mark.parametrize(
"subdirectory,great_circle",
[("flat", False), ("spherical", True)],
)
def test_no_fix_winding_when_forcing_poles(
read_input: Reader,
read_output: Reader,
name: str,
force_north_pole: bool,
force_south_pole: bool,
subdirectory: str,
great_circle: bool,
) -> None:
input = read_input(name)
output = read_output(f"{name}-no-fix", subdirectory)
fixed = antimeridian.fix_polygon(
input,
force_north_pole=force_north_pole,
force_south_pole=force_south_pole,
great_circle=great_circle,
)
assert fixed.normalize() == output.normalize()
def test_fix_winding_interior_no_segments(read_input: Reader) -> None:
input = read_input("simple-with-ccw-hole")
with pytest.warns(FixWindingWarning):
fixed = antimeridian.fix_polygon(input)
assert all(not shapely.is_ccw(interior) for interior in fixed.interiors)
def test_fix_winding_interior_segments(read_input: Reader, read_output: Reader) -> None:
input = read_input("one-ccw-hole")
output = read_output("one-hole", "spherical")
with pytest.warns(FixWindingWarning):
fixed = antimeridian.fix_polygon(input)
assert fixed.normalize() == output.normalize()
def test_centroid_simple(read_input: Reader) -> None:
input = read_input("simple")
centroid = cast(Point, antimeridian.centroid(input))
assert centroid.x == 95
assert centroid.y == 45
def test_centroid_split(read_output: Reader) -> None:
input = read_output("split")
centroid = cast(Point, antimeridian.centroid(input))
assert centroid.x == 180
assert centroid.y == 45
def test_centroid_split_with_shift(read_input: Reader) -> None:
input = read_input("split")
input = shapely.affinity.translate(input, xoff=+1)
input = antimeridian.fix_polygon(input, great_circle=False)
centroid = cast(Point, antimeridian.centroid(input))
assert centroid.x == -179
assert centroid.y == 45
def test_z_coordinates() -> None:
# https://github.com/gadomski/antimeridian/issues/115
polygon = Polygon([[0, 0, 1], [10, 0, 2], [10, 10, 3], [0, 10, 4]])
fixed = antimeridian.fix_polygon(polygon)
assert fixed.has_z
@pytest.mark.parametrize(
"subdirectory,great_circle",
[("flat", False), ("spherical", True)],
)
def test_force_south_pole(
read_input: Reader, read_output: Reader, subdirectory: str, great_circle: bool
) -> None:
# https://github.com/gadomski/antimeridian/issues/124
input = read_input("issues-124")
output = read_output("issues-124", subdirectory)
fixed = antimeridian.fix_polygon(
input, force_south_pole=True, great_circle=great_circle
)
assert fixed.normalize() == output.normalize()
@pytest.mark.parametrize(
"subdirectory,great_circle", (("flat", False), ("spherical", True))
)
def test_great_circle(
read_input: Reader, read_output: Reader, subdirectory: str, great_circle: bool
) -> None:
# https://github.com/gadomski/antimeridian/issues/153
input = read_input("great-circle")
output = read_output("great-circle", subdirectory)
fixed = antimeridian.fix_polygon(input, great_circle=great_circle)
assert fixed.normalize() == output.normalize()
def test_invalid_polygon(read_input: Reader) -> None:
input = read_input("issues-182")
with pytest.warns(FixWindingWarning):
with pytest.raises(ValueError):
antimeridian.fix_polygon(input)
|