File size: 3,134 Bytes
843b944
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import geopandas as gpd
import numpy as np
import rasterio
import rioxarray
from rasterio import features
from rasterio.transform import from_origin


def create_raster_stack(input_paths: list[str], output_path: str) -> str:
    if len(input_paths) < 2:
        raise ValueError("At least two raster files are required to build a raster stack.")

    with rasterio.open(input_paths[0]) as first:
        profile = first.profile.copy()
        bands = [first.read(1)]

    for path in input_paths[1:]:
        with rasterio.open(path) as src:
            bands.append(src.read(1))

    profile.update(driver="GTiff", count=len(bands))

    with rasterio.open(output_path, "w", **profile) as dst:
        for index, band in enumerate(bands, start=1):
            dst.write(band, index)

    return output_path


def netcdf_to_tiff(input_path: str, output_path: str) -> str:
    data = rioxarray.open_rasterio(input_path)
    data.rio.to_raster(output_path)
    return output_path


def ascii_to_tiff(input_path: str, output_path: str) -> str:
    with rasterio.open(input_path) as src:
        profile = src.profile.copy()
        profile["driver"] = "GTiff"
        with rasterio.open(output_path, "w", **profile) as dst:
            dst.write(src.read())
    return output_path


def tiff_to_ascii(input_path: str, output_path: str) -> str:
    with rasterio.open(input_path) as src:
        profile = src.profile.copy()
        profile["driver"] = "AAIGrid"
        with rasterio.open(output_path, "w", **profile) as dst:
            dst.write(src.read(1), 1)
    return output_path


def tiff_to_netcdf(input_path: str, output_path: str) -> str:
    data = rioxarray.open_rasterio(input_path)
    dataset = data.to_dataset(name="band_data")
    dataset.to_netcdf(output_path)
    return output_path


def shp_to_tiff(input_path: str, output_path: str, resolution: float = 0.01) -> str:
    vector = gpd.read_file(input_path)
    xmin, ymin, xmax, ymax = vector.total_bounds
    width = max(1, int(np.ceil((xmax - xmin) / resolution)))
    height = max(1, int(np.ceil((ymax - ymin) / resolution)))
    transform = from_origin(xmin, ymax, resolution, resolution)
    shapes = ((geom, 1) for geom in vector.geometry if geom is not None)

    raster = features.rasterize(
        shapes=shapes,
        out_shape=(height, width),
        transform=transform,
        fill=0,
        dtype="uint8",
    )

    with rasterio.open(
        output_path,
        "w",
        driver="GTiff",
        height=height,
        width=width,
        count=1,
        dtype=raster.dtype,
        crs=vector.crs,
        transform=transform,
    ) as dst:
        dst.write(raster, 1)

    return output_path


def tiff_to_shp(input_path: str, output_path: str) -> str:
    with rasterio.open(input_path) as src:
        band = src.read(1)
        shape_iter = features.shapes(band, transform=src.transform)
        records = [{"geometry": geom, "properties": {"value": value}} for geom, value in shape_iter]
        crs = src.crs

    vector = gpd.GeoDataFrame.from_features(records, crs=crs)
    vector.to_file(output_path)
    return output_path