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
|
import json
from collections.abc import Callable
from pathlib import Path
from typing import Protocol, Union
import pytest
import shapely.geometry
from shapely.geometry import (
GeometryCollection,
LinearRing,
LineString,
MultiLineString,
MultiPoint,
MultiPolygon,
Point,
Polygon,
)
TEST_DATA_DIRECTORY = Path(__file__).parent / "data"
INPUT_DATA_DIRECTORY = TEST_DATA_DIRECTORY / "input"
OUTPUT_DATA_DIRECTORY = TEST_DATA_DIRECTORY / "output"
class Reader(Protocol):
def __call__(
self, name: str, subdirectory: str | None = None
) -> Union[
Point,
MultiPoint,
LineString,
MultiLineString,
Polygon,
MultiPolygon,
LinearRing,
GeometryCollection,
]: ...
@pytest.fixture
def read_input() -> Reader:
def read_input(
name: str,
subdirectory: str | None = None,
) -> Union[
Point,
MultiPoint,
LineString,
MultiLineString,
Polygon,
MultiPolygon,
LinearRing,
GeometryCollection,
]:
path = Path(subdirectory) / name if subdirectory is not None else Path(name)
return read_file((INPUT_DATA_DIRECTORY / path).with_suffix(".json"))
return read_input
@pytest.fixture
def input_path() -> Callable[[str], Path]:
def input_path(name: str) -> Path:
return (INPUT_DATA_DIRECTORY / name).with_suffix(".json")
return input_path
@pytest.fixture
def read_output() -> Reader:
def read_output(
name: str,
subdirectory: str | None = "flat",
) -> Union[
Point,
MultiPoint,
LineString,
MultiLineString,
Polygon,
MultiPolygon,
LinearRing,
GeometryCollection,
]:
path = Path(subdirectory) / name if subdirectory is not None else Path(name)
return read_file((OUTPUT_DATA_DIRECTORY / path).with_suffix(".json"))
return read_output
def read_file(
path: Path,
) -> Union[
Point,
MultiPoint,
LineString,
MultiLineString,
Polygon,
MultiPolygon,
LinearRing,
GeometryCollection,
]:
with open(path) as f:
data = json.load(f)
shape = shapely.geometry.shape(data)
return shape
|