1. Prepare input data of Kano River

Written by Men Vuthy, 2022


Import modules

[1]:
import os
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt

import rasterio
import geopandas as gpd

Label

[2]:
kano_class_img = rasterio.open('data/kano_river/out_img/class/kano_class.tiff')
[3]:
kano_class = kano_class_img.read(1).reshape(-1)
[4]:
kano_label = pd.DataFrame({'label':kano_class})

Dataframe of Season 1

[5]:
season1_rgbn = rasterio.open('data/kano_river/out_img/rgbn/kano_20180429_rgbn.tiff')
season1_ndvi = rasterio.open('data/kano_river/out_img/ndvi/kano_20180429_ndvi.tiff')
season1_ndwi = rasterio.open('data/kano_river/out_img/ndwi/kano_20180429_ndwi.tiff')
season1_bsi = rasterio.open('data/kano_river/out_img/bsi/kano_20180429_bsi.tiff')
[6]:
blue_1 = season1_rgbn.read(1).reshape(-1)
green_1 = season1_rgbn.read(2).reshape(-1)
red_1 = season1_rgbn.read(3).reshape(-1)
nir_1 = season1_rgbn.read(4).reshape(-1)

ndvi_1 = season1_ndvi.read(1).reshape(-1)
ndwi_1 = season1_ndwi.read(1).reshape(-1)
bsi_1 = season1_bsi.read(1).reshape(-1)
[7]:
Season_1 = pd.DataFrame({'B1':blue_1, 'G1':green_1, 'R1':red_1, 'NIR1':nir_1, 'NDVI1':ndvi_1, 'NDWI1':ndwi_1, 'BSI1':bsi_1})
[8]:
Season_1
[8]:
B1 G1 R1 NIR1 NDVI1 NDWI1 BSI1
0 0 0 0 0 NaN NaN NaN
1 0 0 0 0 NaN NaN NaN
2 0 0 0 0 NaN NaN NaN
3 0 0 0 0 NaN NaN NaN
4 0 0 0 0 NaN NaN NaN
... ... ... ... ... ... ... ...
23663796 0 0 0 0 NaN NaN NaN
23663797 0 0 0 0 NaN NaN NaN
23663798 0 0 0 0 NaN NaN NaN
23663799 0 0 0 0 NaN NaN NaN
23663800 0 0 0 0 NaN NaN NaN

23663801 rows × 7 columns

Dataframe of Season 2

[9]:
season2_rgbn = rasterio.open('data/kano_river/out_img/rgbn/kano_20180715_rgbn.tiff')
season2_ndvi = rasterio.open('data/kano_river/out_img/ndvi/kano_20180715_ndvi.tiff')
season2_ndwi = rasterio.open('data/kano_river/out_img/ndwi/kano_20180715_ndwi.tiff')
season2_bsi = rasterio.open('data/kano_river/out_img/bsi/kano_20180715_bsi.tiff')
[10]:
blue_2 = season2_rgbn.read(1).reshape(-1)
green_2 = season2_rgbn.read(2).reshape(-1)
red_2 = season2_rgbn.read(3).reshape(-1)
nir_2 = season2_rgbn.read(4).reshape(-1)

ndvi_2 = season2_ndvi.read(1).reshape(-1)
ndwi_2 = season2_ndwi.read(1).reshape(-1)
bsi_2 = season2_bsi.read(1).reshape(-1)
[11]:
Season_2 = pd.DataFrame({'B2':blue_2, 'G2':green_2, 'R2':red_2, 'NIR2':nir_2, 'NDVI2':ndvi_2, 'NDWI2':ndwi_2, 'BSI2':bsi_2})
[12]:
Season_2
[12]:
B2 G2 R2 NIR2 NDVI2 NDWI2 BSI2
0 0 0 0 0 NaN NaN NaN
1 0 0 0 0 NaN NaN NaN
2 0 0 0 0 NaN NaN NaN
3 0 0 0 0 NaN NaN NaN
4 0 0 0 0 NaN NaN NaN
... ... ... ... ... ... ... ...
23663796 0 0 0 0 NaN NaN NaN
23663797 0 0 0 0 NaN NaN NaN
23663798 0 0 0 0 NaN NaN NaN
23663799 0 0 0 0 NaN NaN NaN
23663800 0 0 0 0 NaN NaN NaN

23663801 rows × 7 columns

Dataframe of Season 3

[13]:
season3_rgbn = rasterio.open('data/kano_river/out_img/rgbn/kano_20181029_rgbn.tiff')
season3_ndvi = rasterio.open('data/kano_river/out_img/ndvi/kano_20181029_ndvi.tiff')
season3_ndwi = rasterio.open('data/kano_river/out_img/ndwi/kano_20181029_ndwi.tiff')
season3_bsi = rasterio.open('data/kano_river/out_img/bsi/kano_20181029_bsi.tiff')
[14]:
blue_3 = season3_rgbn.read(1).reshape(-1)
green_3 = season3_rgbn.read(2).reshape(-1)
red_3 = season3_rgbn.read(3).reshape(-1)
nir_3 = season3_rgbn.read(4).reshape(-1)

ndvi_3 = season3_ndvi.read(1).reshape(-1)
ndwi_3 = season3_ndwi.read(1).reshape(-1)
bsi_3 = season3_bsi.read(1).reshape(-1)
[15]:
Season_3 = pd.DataFrame({'B3':blue_3, 'G3':green_3, 'R3':red_3, 'NIR3':nir_3, 'NDVI3':ndvi_3, 'NDWI3':ndwi_3, 'BSI3':bsi_3})
[16]:
Season_3
[16]:
B3 G3 R3 NIR3 NDVI3 NDWI3 BSI3
0 0 0 0 0 NaN NaN NaN
1 0 0 0 0 NaN NaN NaN
2 0 0 0 0 NaN NaN NaN
3 0 0 0 0 NaN NaN NaN
4 0 0 0 0 NaN NaN NaN
... ... ... ... ... ... ... ...
23663796 0 0 0 0 NaN NaN NaN
23663797 0 0 0 0 NaN NaN NaN
23663798 0 0 0 0 NaN NaN NaN
23663799 0 0 0 0 NaN NaN NaN
23663800 0 0 0 0 NaN NaN NaN

23663801 rows × 7 columns

Dataframe of Season 4

[17]:
season4_rgbn = rasterio.open('data/kano_river/out_img/rgbn/kano_20190129_rgbn.tiff')
season4_ndvi = rasterio.open('data/kano_river/out_img/ndvi/kano_20190129_ndvi.tiff')
season4_ndwi = rasterio.open('data/kano_river/out_img/ndwi/kano_20190129_ndwi.tiff')
season4_bsi = rasterio.open('data/kano_river/out_img/bsi/kano_20190129_bsi.tiff')
[18]:
blue_4 = season4_rgbn.read(1).reshape(-1)
green_4 = season4_rgbn.read(2).reshape(-1)
red_4 = season4_rgbn.read(3).reshape(-1)
nir_4 = season4_rgbn.read(4).reshape(-1)

ndvi_4 = season4_ndvi.read(1).reshape(-1)
ndwi_4 = season4_ndwi.read(1).reshape(-1)
bsi_4 = season4_bsi.read(1).reshape(-1)
[19]:
Season_4 = pd.DataFrame({'B4':blue_4, 'G4':green_4, 'R4':red_4, 'NIR4':nir_4, 'NDVI4':ndvi_4, 'NDWI4':ndwi_4, 'BSI4':bsi_4})
[20]:
Season_4
[20]:
B4 G4 R4 NIR4 NDVI4 NDWI4 BSI4
0 0 0 0 0 NaN NaN NaN
1 0 0 0 0 NaN NaN NaN
2 0 0 0 0 NaN NaN NaN
3 0 0 0 0 NaN NaN NaN
4 0 0 0 0 NaN NaN NaN
... ... ... ... ... ... ... ...
23663796 0 0 0 0 NaN NaN NaN
23663797 0 0 0 0 NaN NaN NaN
23663798 0 0 0 0 NaN NaN NaN
23663799 0 0 0 0 NaN NaN NaN
23663800 0 0 0 0 NaN NaN NaN

23663801 rows × 7 columns

Features and Label

[21]:
# concat dataframe of all seasons and label into one dataframe
Kano_dataframe = pd.concat([Season_1, Season_2, Season_3, Season_4, kano_label], axis=1)
[22]:
Kano_dataframe
[22]:
B1 G1 R1 NIR1 NDVI1 NDWI1 BSI1 B2 G2 R2 ... NDWI3 BSI3 B4 G4 R4 NIR4 NDVI4 NDWI4 BSI4 label
0 0 0 0 0 NaN NaN NaN 0 0 0 ... NaN NaN 0 0 0 0 NaN NaN NaN 0
1 0 0 0 0 NaN NaN NaN 0 0 0 ... NaN NaN 0 0 0 0 NaN NaN NaN 0
2 0 0 0 0 NaN NaN NaN 0 0 0 ... NaN NaN 0 0 0 0 NaN NaN NaN 0
3 0 0 0 0 NaN NaN NaN 0 0 0 ... NaN NaN 0 0 0 0 NaN NaN NaN 0
4 0 0 0 0 NaN NaN NaN 0 0 0 ... NaN NaN 0 0 0 0 NaN NaN NaN 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
23663796 0 0 0 0 NaN NaN NaN 0 0 0 ... NaN NaN 0 0 0 0 NaN NaN NaN 0
23663797 0 0 0 0 NaN NaN NaN 0 0 0 ... NaN NaN 0 0 0 0 NaN NaN NaN 0
23663798 0 0 0 0 NaN NaN NaN 0 0 0 ... NaN NaN 0 0 0 0 NaN NaN NaN 0
23663799 0 0 0 0 NaN NaN NaN 0 0 0 ... NaN NaN 0 0 0 0 NaN NaN NaN 0
23663800 0 0 0 0 NaN NaN NaN 0 0 0 ... NaN NaN 0 0 0 0 NaN NaN NaN 0

23663801 rows × 29 columns

[23]:
# Remove all rows where NDVI1 is NaN
Kano_classified = Kano_dataframe.dropna(subset=['NDVI1'])
[24]:
Kano_classified
[24]:
B1 G1 R1 NIR1 NDVI1 NDWI1 BSI1 B2 G2 R2 ... NDWI3 BSI3 B4 G4 R4 NIR4 NDVI4 NDWI4 BSI4 label
584636 890 992 995 1965 0.242947 -0.214166 0.303773 860 912 819 ... -0.328072 0.325482 655 745 768 1148 0.207700 -0.172238 0.307462 6
584637 900 990 990 2008 0.255461 -0.225427 0.306076 882 936 865 ... -0.304147 0.332129 652 722 735 1072 0.195912 -0.154165 0.310903 6
584638 901 972 958 1862 0.235430 -0.198141 0.307286 877 915 854 ... -0.293547 0.329432 636 709 733 983 0.155239 -0.120563 0.312732 6
584639 801 889 863 1576 0.205786 -0.160642 0.297341 846 857 799 ... -0.290226 0.319362 619 683 705 874 0.116680 -0.080891 0.314762 6
584640 842 893 896 1301 0.093848 -0.063836 0.315164 821 811 749 ... -0.234630 0.313164 572 642 652 739 0.072273 -0.028123 0.307235 6
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
22000430 794 947 990 1867 0.221129 -0.211906 0.298590 835 877 864 ... -0.229348 0.326820 410 524 555 902 0.247357 -0.225399 0.287725 6
22000431 926 1057 1110 1911 0.177388 -0.170195 0.309133 1047 1082 1071 ... -0.200432 0.322006 447 539 592 921 0.226740 -0.221894 0.308992 6
22000432 926 1057 1110 1911 0.177388 -0.170195 0.309133 1047 1082 1071 ... -0.200432 0.322006 447 539 592 921 0.226740 -0.221894 0.308992 6
22000433 978 1124 1212 1919 0.136559 -0.142256 0.313926 1235 1269 1296 ... -0.166199 0.318265 490 593 650 835 0.134188 -0.128185 0.307732 6
22004751 892 1018 1070 1819 0.171261 -0.164487 0.309384 889 937 920 ... -0.195760 0.313958 383 516 545 793 0.194771 -0.170939 0.275729 6

692246 rows × 29 columns

[25]:
# Data dir
data_dir = "data/kano_river/out_img/classified"

# Output dataframe
out_df = os.path.join(data_dir, 'kano_classified.csv')

# export dataframe to csv
Kano_classified.to_csv(out_df, index=False)
[26]:
# Data dir
data_dir = "data/kano_river/out_img/classified"

# Output dataframe
out_df = os.path.join(data_dir, 'kano_classified_index.csv')

# export dataframe to csv
Kano_classified.to_csv(out_df)