""" 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 个点的 GeoDataFrame(WGS84)。""" 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 个矩形多边形的 GeoDataFrame(WGS84)。""" 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