1

I have different folders containing Landsat 8 bands. I want to stack them into a single raster file. ArcPy can not be downloaded. Is there any other way to do that?

I have this snippet for the code

import os, numpy as np, rasterio as rs, matplotlib.pyplot as plt, earthpy.spatial as es, earthpy.plot as ep
from glob import glob


landsat_path = glob(
    "/Users/admin/Downloads/LC08_L2SP_028030_20220628_20220706_02_T1/LC08_L2SP_028030_20220628_20220706_02_T1_SR_B[1-7].TIF"
)
sorted_path = sorted(landsat_path)
# print(sorted_path)
    
arr_st, meta = es.stack(sorted_path, nodata=-9999)
# print(arr_st[1])
ndvi = es.normalized_diff(arr_st[4], arr_st[3])
titles = ["Landsat 8 - Normalized Difference Vegetation Index (NDVI)"]
ep.plot_bands(ndvi, cmap="RdYlGn", cols=1, title=titles, vmin=-1, vmax=1)
 

But I want to run it in every folder. Even if I get to stack them in a different folder I can try to run the NDVI generation.

2
  • In truth, ArcPy can be downloaded, as that's the only way for Esri customers to obtain ArcGIS media.
    – Vince
    Aug 14, 2022 at 22:51
  • I have tried. But it is asking me to restart the kernel in jupyter which seems not working. thank you @Vince
    – Arnab paul
    Aug 15, 2022 at 3:35

1 Answer 1

0

Well I figured it out how to stack the bands into a single image for a single path and row. Many thanks to this link

Here is the snippet:

import glob

# WRS-2 naming convention: path 28, row 30 = 02830)
wrs = ['028030']
#Local path for reference images 
refPath = '/Users/admin/Downloads/New_folder/landsat/'

for i in wrs:
    ''' Iteratively create datacubes by stacking bands within each acquistion scene folder''' 
    #check sub-directories (acquistions) under every WRS-scene
    subDirs = os.listdir(refPath)
    print(subDirs)
    for directory in subDirs:
        print(directory)
        #iterate subdirectories and compile datacubes within them 
        bandList = glob.glob(refPath +'/'+ directory + '/' + '*B[1-7].TIF')
        bandlist = sorted(bandList)
        print(bandlist)
        output = refPath + '/' + directory + '/'
        filename = directory + "_STACK.tif"
        array, raster = es.stack(bandList, out_path=output + filename, nodata = 0)

Your Answer

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

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