import os


def ensure_dir(path):
    os.makedirs(path, exist_ok=True)
    return path


def safe_area(geom):
    if geom is None or geom.is_empty:
        return 0.0
    return geom.area


def sqm_to_sqkm(area_m2):
    return area_m2 / 1e6


def make_output_paths(base_dir, prefix):
    return {
        'shp': os.path.join(base_dir, f'{prefix}.shp'),
        'csv': os.path.join(base_dir, f'{prefix}.csv'),
    }


def describe_threshold_method(method):
    methods = {
        'otsu': "Otsu's method (automatic global threshold)",
        'fixed': "User-defined fixed threshold",
        'percentile': "Percentile-based adaptive threshold",
    }
    return methods.get(method, method)
