1

I'm using Rasterio to read GeoTIFF from Sentinel 2 and calculating NDVI, but when I try to save as a new TIF file, the file turns to transparent and empty file. What am I missing?

That's the code:

class MapService:

    def __init__(self):

        dsRed = rasterio.open('data/bands/B04.TIF')
        bandRed = dsRed.read(1)

        dsNIR = rasterio.open('data/bands/B08.TIF')
        bandNIR = dsNIR.read(1)         

        ndvi = np.zeros(dsRed.shape, dtype=rasterio.float32)

        ndvi = (bandNIR.astype(float)-bandRed.astype(float))/(bandNIR+bandRed)

        kwargs = dsRed.meta
        kwargs.update(
            dtype=rasterio.float32,
            count=1,
            compress='lzw')

        self.ndvi = ndvi
        with rasterio.open('result/example-total.tif', 'w', **kwargs) as dst:
            dst.write(ndvi.astype(rasterio.float32),1)
  • When you say empty, what exactly do you mean? Is it 0 bytes? – bugmenot123 Sep 17 '18 at 9:45
  • Nope, but the tiff is transparent. I believe it should be a false color map – vicebas Sep 17 '18 at 16:37
  • Does the result file really contain only transparent/nodata pixels (check the histogram or some similar statistics)? What happens if you use your code to simply write one of the same files you read in, without any calculations? Can you share the input files? – bugmenot123 Sep 18 '18 at 8:29

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.