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)
asked Aug 16, 2021 at 22:23

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

answered Aug 18, 2021 at 12:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.