how to start with geotiffs #732
-
|
Hi, I have a fairly simple task, but I don't know how to do it with your library even after reading examples. I want to read a geotiff with your library. Multiply every value by 0 and save it to the same file. Later on I want to change it to polygon, so I have boundaries of the raster. Can you help me with this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
xarray-spatial doesn't do file I/O, it's only the spatial analytics (slope, hillshade, etc.). For reading and writing GeoTIFFs you want import rioxarray
da = rioxarray.open_rasterio("your_file.tif")
da_zeroed = da * 0
da_zeroed.rio.to_raster("your_file.tif")To get the raster boundary as a polygon, use import numpy as np
from shapely.geometry import shape
import rasterio
from rasterio.features import shapes
with rasterio.open("your_file.tif") as src:
mask = src.read(1)
binary = (mask != src.nodata).astype(np.uint8) if src.nodata is not None else np.ones_like(mask, dtype=np.uint8)
results = list(shapes(binary, transform=src.transform))
polygons = [shape(geom) for geom, val in results if val == 1]Or if you just need the bounding box: from shapely.geometry import box
with rasterio.open("your_file.tif") as src:
bbox = box(*src.bounds) |
Beta Was this translation helpful? Give feedback.
Hi @LukaszKowalski2013,
xarray-spatial doesn't do file I/O, it's only the spatial analytics (slope, hillshade, etc.). For reading and writing GeoTIFFs you want
rioxarray:To get the raster boundary as a polygon, use
rasterio.features.shapes: