Files
RadioPropagationApi/api/app/services/vegetation.py
T
2026-06-24 07:50:00 +03:00

43 lines
1.2 KiB
Python

from __future__ import annotations
from pathlib import Path
from app.config import get_settings
from app.core.geo import PathPoint
from app.core.landcover import LandcoverNotConfiguredError, landcover_path
from app.core.vegetation import p833_attenuation, p833_coefficients_for_class
def vegetation_loss_along(
points: list[PathPoint],
freq_hz: float,
include_vegetation: bool,
landcover_path_dir: str | Path | None = None,
canopy_path_dir: str | Path | None = None,
) -> float:
if not include_vegetation:
return 0.0
settings = get_settings()
landcover_dir = landcover_path_dir or settings.landcover_path
canopy_dir = canopy_path_dir or settings.canopy_path
try:
path = landcover_path(points, landcover_dir, canopy_dir)
except LandcoverNotConfiguredError:
return 0.0
dominant_class = "unknown"
if path.segments:
dominant_class = max(
path.segments,
key=lambda segment: segment.to_m - segment.from_m,
).class_name
return p833_attenuation(
depth_m=path.vegetation_depth_m,
freq_hz=freq_hz,
forest_type=dominant_class,
coefficients=p833_coefficients_for_class(dominant_class),
)