diff --git a/backend/tests/test_osm_local.py b/backend/tests/test_osm_local.py index 1557f30..6181aad 100644 --- a/backend/tests/test_osm_local.py +++ b/backend/tests/test_osm_local.py @@ -45,7 +45,9 @@ class TestOsmLocal: assert set(f) == {'roads_km', 'road_density', 'water_distance_km', 'settlement_distance_km', 'forest_pct'} assert f['roads_km'] >= 0 - assert 0 <= f['forest_pct'] <= 100 + # Контракт: лес — ДОЛЯ 0..1 (scoring_service умножает weight × forest_pct). + # Регресс: раньше отдавались проценты 0..100 — лес доминировал со 100× весом. + assert 0 <= f['forest_pct'] <= 1 assert f['water_distance_km'] is None or f['water_distance_km'] > 0 def test_zones_differ_between_directions(self): diff --git a/services/geo_service.py b/services/geo_service.py index 431fa4a..816678c 100644 --- a/services/geo_service.py +++ b/services/geo_service.py @@ -344,9 +344,12 @@ def calculate_forest_coverage(elements: List[Dict], radius_m: int) -> float: search_area_km2 = math.pi * (radius_m / 1000) ** 2 estimated_forest_km2 = forest_ways * 0.1 - coverage_pct = min(100.0, (estimated_forest_km2 / search_area_km2) * 100) + # Контракт: ДОЛЯ 0..1 (scoring_service читает forest_pct как долю — + # weight × forest_pct; раньше отдавались проценты 0..100, что делало + # лес доминирующим фактором со 100× весом). + coverage = min(1.0, estimated_forest_km2 / search_area_km2) - return round(coverage_pct, 1) + return round(coverage, 3) async def get_zone_features(lat: float, lon: float, direction: str, radius_m: int) -> Dict: diff --git a/services/osm_local.py b/services/osm_local.py index e3bcaea..e6a90c9 100644 --- a/services/osm_local.py +++ b/services/osm_local.py @@ -145,9 +145,13 @@ def get_zone_features_postgis(lat: float, lon: float, direction: str, radius_m: search_area_km2 = math.pi * (radius_m / 1000.0) ** 2 road_density = roads_km / search_area_km2 if search_area_km2 > 0 else 0.0 + # Контракт Zone.forest_pct — ДОЛЯ леса 0..1 (так его читает + # scoring_service.score_zone: weight × forest_pct; тесты скоринга + # используют 0.3–0.8 как доли). Раньше отдались проценты 0..100 — + # лес в 100 раз перевешивал остальные факторы (зоны «вверх-вправо»). forest_pct = 0.0 if forest_m2 > 0 and search_area_km2 > 0: - forest_pct = round(min(100.0, (forest_m2 / 1e6) / search_area_km2 * 100), 1) + forest_pct = round(min(1.0, (forest_m2 / 1e6) / search_area_km2), 3) water_candidates = [float(m) for m in (water_poly_m, waterway_m) if m is not None] water_distance_km = round(min(water_candidates), 3) if water_candidates else None