Spaces:
Paused
Paused
Download python/spatial_toolkit_py/io_conversions.py from abhirsc/spatial-toolkit-py: direct link, hf CLI and curl.
- Browser
- Download file 3.13 kB
-
https://huggingface.co/spaces/abhirsc/spatial-toolkit-py/resolve/main/python/spatial_toolkit_py/io_conversions.py
- Command line
-
hf download hf://spaces/abhirsc/spatial-toolkit-py/python/spatial_toolkit_py/io_conversions.py
-
curl -L -o io_conversions.py https://huggingface.co/spaces/abhirsc/spatial-toolkit-py/resolve/main/python/spatial_toolkit_py/io_conversions.py
3.13 kB
| 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 | |