1

I am relatively new in using python for GIS, but I have the following code that I use to clip all created mosaics by a shapefile (aoi_shapefile). After the clipped rasters are placed in the rasterio xarray I would like to write each result to disk, but name each element in the xarray with the filenames clipped_mosaic_b2, clipped_mosaic_b4, clipped_mosaic_b5, clipped_mosaic_b6, clipped_mosaic_b8. I believe it can be done with a for loop, but haven't a clue on how to start it off.

# --- Clipping Task ---

# Directory of mosaics

mosaic_dir = r"C:\Users\Matthew\OneDrive\Land Suitability Model\Layers\Sentinel\Sentinel RST"

# Create list of all mosaics

# Search for all mosaic images in directory & glob images to a list

mosaic_query = "mosaic_*.tif"
mosaic_images = os.path.join(mosaic_dir, mosaic_query)
mosaic_list = glob.glob(mosaic_images)
mosaics_to_clip = []


# open all mosaics using rioxarray
for all_mosaics in mosaic_list:
    rxr_array = rxr.open_rasterio(all_mosaics)
    mosaics_to_clip.append(rxr_array)

# For loop to clip all open rasters
clipped_mosaics =[]
for i in mosaics_to_clip:
    clipped = i.rio.clip(aoi_shapefile.geometry.apply(mapping))
    clipped_mosaics.append(clipped)
    
for j in clipped_mosaics:
    show(j)

1 Answer 1

0

The rio.to_raster method should do what you want: https://corteva.github.io/rioxarray/stable/examples/convert_to_raster.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.