0

I have a folder (with subfolders) containing different raster files (Sentinel-2 images).

I want to import all the bands to python and creater a raster stack to allow further processing (classification, temporal analysis, etc.). So far, I have managed to create a list containing the full path to each raster. I am missing how to create a loop to import each raster and after that create a stack.

Here is the code I am using so far:

import os

# Define path to unziped sentinel-2 image
path='/shared/Training/TAT0618_CropMapping_Seville/Original/S2/S2A_MSIL2A_20170621T110651_N0205_R137_T30STG_20170621T111222.SAFE'

# Create a list with all the files in the path (and subfolders) which have the pattern _10m.jp2
rasterFiles = [os.path.join(root, name)
             for root, dirs, files in os.walk(path)
             for name in files
             if name.endswith("_10m.jp2")]
print(rasterFiles)

['/shared/Training/TAT0618_CropMapping_Seville/Original/S2/S2A_MSIL2A_20170621T110651_N0205_R137_T30STG_20170621T111222.SAFE/GRANULE/L2A_T30STG_A010430_20170621T111222/IMG_DATA/R10m/L2A_T30STG_20170621T110651_B03_10m.jp2', 

I am trying something like:

for i in rasterFiles():
    gdal.Open(rasterFiles[i], GA_ReadOnly)

TypeError: list indices must be integers, not str

Your Answer

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

Browse other questions tagged or ask your own question.