Files
geo_tools/tests/conftest.py
2026-03-04 17:07:07 +08:00

81 lines
2.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
tests/conftest.py
~~~~~~~~~~~~~~~~~
共享测试夹具Fixture—— 提供测试数据,供所有测试文件复用。
"""
from __future__ import annotations
import json
import tempfile
from pathlib import Path
import geopandas as gpd
import pytest
from shapely.geometry import Point, Polygon
# ── 示例 GeoDataFrame ──────────────────────────────────────────────────────────
@pytest.fixture
def sample_points_gdf() -> gpd.GeoDataFrame:
"""3 个点的 GeoDataFrameWGS84"""
return gpd.GeoDataFrame(
{
"id": [1, 2, 3],
"name": ["点A", "点B", "点C"],
"value": [10.5, 20.0, 15.3],
},
geometry=[Point(116.4, 39.9), Point(121.5, 31.2), Point(113.3, 23.1)],
crs="EPSG:4326",
)
@pytest.fixture
def sample_polygon_gdf() -> gpd.GeoDataFrame:
"""1 个矩形多边形的 GeoDataFrameWGS84"""
poly = Polygon([(115.0, 38.0), (122.0, 38.0), (122.0, 41.0), (115.0, 41.0)])
return gpd.GeoDataFrame(
{"region": ["华北区"], "area_km2": [450000.0]},
geometry=[poly],
crs="EPSG:4326",
)
@pytest.fixture
def sample_multi_polygon_gdf() -> gpd.GeoDataFrame:
"""包含两个多边形的 GeoDataFrame用于融合/叠置测试WGS84"""
poly1 = Polygon([(100, 20), (110, 20), (110, 30), (100, 30)])
poly2 = Polygon([(105, 20), (115, 20), (115, 30), (105, 30)])
return gpd.GeoDataFrame(
{"zone": ["A", "B"], "value": [100, 200]},
geometry=[poly1, poly2],
crs="EPSG:4326",
)
# ── 临时文件路径 ───────────────────────────────────────────────────────────────
@pytest.fixture
def tmp_geojson_path(tmp_path: Path, sample_points_gdf: gpd.GeoDataFrame) -> Path:
"""将 sample_points_gdf 写出为临时 GeoJSON 并返回路径。"""
path = tmp_path / "sample.geojson"
sample_points_gdf.to_file(str(path), driver="GeoJSON")
return path
@pytest.fixture
def tmp_gpkg_path(tmp_path: Path, sample_points_gdf: gpd.GeoDataFrame) -> Path:
"""将 sample_points_gdf 写出为临时 GPKG 并返回路径。"""
path = tmp_path / "sample.gpkg"
sample_points_gdf.to_file(str(path), driver="GPKG", layer="points")
return path
@pytest.fixture
def tmp_output_dir(tmp_path: Path) -> Path:
"""空的临时输出目录。"""
out = tmp_path / "output"
out.mkdir()
return out