import rastereasy

Computing NDVI and identifying baresoils

1) Read and visualize image

# Read image
im1=rastereasy.open('./data/demo/sentinel_crops.tif')
im1.info()
im1.colorcomp(['4','3','2'])
- Size of the image:
   - Rows (height): 903
   - Cols (width): 867
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (48.30106114, -3.78200251)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32630
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}
../_images/5f1285d04f5e1cea79766a9bf8c0ce22c321e755bbc2d911f6edb2e359a59130.png

2) Get red and nir bands

r=im1.select_bands('4')
nir=im1.select_bands('8')

3) Compute ndvi

# Add an epsilon to prevent from dividing by zero
epsilon=1e-7
ndvi=(nir-r)/(nir+r+epsilon)

4) Visualize

# Change the name of the band for consistency
ndvi.change_names({'ndvi':1})
ndvi.info()
ndvi.visu(cmap='Greens',colorbar=True,title='ndvi',fig_size=(10,10))
ndvi.hist(title='histogram of NDVI')

# save ndvi
ndvi.save('./data/results/NDVI/ndvi.tif')
- Size of the image:
   - Rows (height): 903
   - Cols (width): 867
   - Bands: 1
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (48.30106114, -3.78200251)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32630
- Nodata: nan

- Given names for spectral bands: 
   {'ndvi': 1}
../_images/f339fafae448fbae642972d6a2f8c90c6093b02baf96807e66d97ff55519dd7b.png ../_images/60435a5f29a8b5c557004793250851b0013282dabb0f0d23b3bd287c01d10b82.png

2) Identifying bare soils

1) With a threshold on NDVI

threshold=0.4
baresoil=ndvi<=threshold
baresoil.info()
baresoil.visu()
- Size of the image:
   - Rows (height): 903
   - Cols (width): 867
   - Bands: 1
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (48.30106114, -3.78200251)
- Driver: GTiff
- Data type: bool
- Projection system: EPSG:32630
- Nodata: 0

- Given names for spectral bands: 
   {'ndvi': 1}
../_images/43781907b15208468961a2e6e7bb38fc416e89a87a5560c0c9705f2484c7e934.png

2) With ndvi.where() (similar than with numpy)

ndvi.info()
baresoil2=ndvi.where(ndvi<=threshold,1,0)
baresoil2.info()
baresoil2.visu()
# check that this is identical
print(' diff of the two baresoils', (baresoil2==baresoil).sum()-baresoil.shape[0]*baresoil.shape[1])
- Size of the image:
   - Rows (height): 903
   - Cols (width): 867
   - Bands: 1
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (48.30106114, -3.78200251)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32630
- Nodata: nan

- Given names for spectral bands: 
   {'ndvi': 1}


- Size of the image:
   - Rows (height): 903
   - Cols (width): 867
   - Bands: 1
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (48.30106114, -3.78200251)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32630
- Nodata: nan

- Given names for spectral bands: 
   {'ndvi': 1}
../_images/43781907b15208468961a2e6e7bb38fc416e89a87a5560c0c9705f2484c7e934.png
 diff of the two baresoils 0

3) With indexing

baresoil3=ndvi*0
baresoil3[ndvi<=threshold]=1
baresoil3[ndvi>threshold]=0
baresoil3.info()
baresoil3.visu()
print(' diff of the two baresoils',(baresoil3==baresoil2).sum()-baresoil3.shape[0]*baresoil3.shape[1])
- Size of the image:
   - Rows (height): 903
   - Cols (width): 867
   - Bands: 1
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (48.30106114, -3.78200251)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32630
- Nodata: nan

- Given names for spectral bands: 
   {'ndvi': 1}
../_images/43781907b15208468961a2e6e7bb38fc416e89a87a5560c0c9705f2484c7e934.png
 diff of the two baresoils 0