@@ -19,152 +19,155 @@ def wsa(res_name, res_directory):
1919 slno = 0
2020 no_zones = 50
2121 for filename in filtered_files :
22- if filename .startswith ("Clipped_" ):
23- slno = slno + 1
24- print (slno )
25- print (filename )
26- ndwi = gdal_array .LoadFile (filename ).astype (np .float32 )
27- ndwi = np .nan_to_num (ndwi , nan = - 0.5 )
28-
29- # Clip NDWI rasters by the expanded mask
30- exp_mask = gdal_array .LoadFile (drtr + "\Expanded_Mask.tif" ).astype (np .float32 )
31- clip_ndwi = ndwi
32- clip_ndwi [np .where (exp_mask == 0 )] = - 0.5
33-
34- # K-means clustering clipped NDWI raters to 3 clusters
35- # (water, wet non-water, and dry non-water) (1 more cluster for the value of -0.5)
36- rows = len (clip_ndwi )
37- columns = len (clip_ndwi [0 ])
38- x = clip_ndwi .ravel ()
39- km = KMeans (n_clusters = 4 )
40- km .fit (x .reshape (- 1 ,1 ))
41- z = km .cluster_centers_
42- z1 = max (z )
43- z2 = - 1
44- z3 = - 1
45- for i in range (0 , 4 ):
46- if z [i ] < z1 and z [i ] > z2 :
47- z2 = z [i ]
48- for i in range (0 , 4 ):
49- if z [i ] < z2 and z [i ] > z3 :
50- z3 = z [i ]
51- threshold = round (float ((z1 + z2 )/ 2 ),3 )
52- print (" K-Means clustering threshold = " + str (threshold ))
53- # plt.figure(figsize=[30,15])
54- # plt.hist(x, bins=200, range=[-0.49, 0.5], color='c')
55- # plt.axvline(z[0], color='navy', linestyle='dashed', linewidth=2)
56- # plt.axvline(z[1], color='navy', linestyle='dashed', linewidth=2)
57- # plt.axvline(z[2], color='navy', linestyle='dashed', linewidth=2)
58- # plt.axvline(z[3], color='navy', linestyle='dashed', linewidth=2)
59- # plt.axvline((z1+z2)/2, color='red', linestyle='dashed', linewidth=2)
60- # plt.axvline((z2+z3)/2, color='red', linestyle='dashed', linewidth=2)
61- # plt.title(filename, fontsize=30)
62- # plt.xlabel('NDWI', fontsize=30)
63- # plt.xticks(fontsize=30)
64- # plt.yticks(fontsize=30)
65- # plt.show()
66- labels = np .reshape (km .labels_ ,(- 1 ,columns ))
67- water_label = 0
68- for i in range (0 , 4 ):
69- if z [i ] == max (z ):
70- water_label = i
71- water_cluster = labels - labels
72- water_cluster [np .where (labels == water_label )] = 1
73-
74- # Assess image quality
75- zone_mask = gdal_array .LoadFile (drtr + "\Zone_Mask.tif" ).astype (np .float32 )
76- count_zm = np .zeros (no_zones )
77- for i in range (0 , no_zones ):
78- count_zm [i ] = np .count_nonzero (zone_mask == i + 1 )
79- cluster_zone = zone_mask
80- cluster_zone [np .where (water_cluster == 0 )] = 0
81- count_cl = np .zeros (no_zones )
82- ratio = np .zeros (no_zones )
83- N_10 = 0
84- for i in range (0 , no_zones ):
85- count_cl [i ] = np .count_nonzero (cluster_zone == i + 1 )
86- ratio [i ] = count_cl [i ]/ (count_zm [i ] + 1.0e-20 )
87- if ratio [i ] >= 0.1 :
88- N_10 += 1
89- print (" Ratio of zone 50 = " + str (round (ratio [no_zones - 1 ],3 )))
90- print (" No. of zones having >=10% water pixels = " + str (int (N_10 )))
91-
92- # Improve image classification
93- ratio_nm = ratio * 100 / (max (ratio ) + 1.0e-20 )
94- x_axis = np .zeros (no_zones )
95- for i in range (0 , no_zones ):
96- x_axis [i ] = i + 1
97- xx = np .vstack ((x_axis , ratio_nm )).T
98- kkm = KMeans (n_clusters = 2 ).fit (xx )
99- llb = kkm .labels_
100- minx0 = no_zones
101- minx1 = no_zones
102- for i in range (0 , no_zones ):
103- if llb [i ] == 0 :
104- if x_axis [i ] < minx0 :
105- minx0 = x_axis [i ]
106- elif llb [i ] == 1 :
107- if x_axis [i ] < minx1 :
108- minx1 = x_axis [i ]
109- s_index = max (minx0 , minx1 )
110- if minx0 == s_index :
111- water_id = 0
112- elif minx1 == s_index :
113- water_id = 1
114- # print(" Additional water pixels start from zone "+str(int(s_index)))
115- # colors = ['navy' if x==water_id else 'lightblue' for x in llb]
116- # plt.figure(figsize=[30,15])
117- # plt.bar(x_axis, ratio, color=colors)
118- # plt.ylim(top=1)
119- # plt.axvline(x=s_index,color='red',linestyle='--')
120- # plt.title(filename, fontsize=30)
121- # plt.xlabel('Zone', fontsize=30)
122- # plt.ylabel('Ratio', fontsize=30)
123- # plt.xticks(fontsize=30)
124- # plt.yticks(fontsize=30)
125- # plt.show()
126-
127- recall_zm = gdal_array .LoadFile (drtr + "\Zone_Mask.tif" ).astype (np .float32 )
128- add = recall_zm
129- add [np .where (recall_zm < s_index )] = 0
130- improved = added_cluster = water_cluster + add
131- improved [np .where (added_cluster > 1 )] = 1
132- bf_area = np .count_nonzero (water_cluster == 1 )* 0.0009
133- af_area = np .count_nonzero (improved == 1 )* 0.0009
134- print (" Water surface area:" )
135- print (" Before improvement: " + str (round (bf_area ,3 ))+ " km2" )
136- print (" After improvement: " + str (round (af_area ,3 ))+ " km2" )
137- if bf_area == 0 :
138- fn_area = bf_area
139- qual = 0
140- print (" Image cannot be improved" )
141- else :
142- if threshold < - 0.5 :
22+ try :
23+ if filename .startswith ("Clipped_" ):
24+ slno = slno + 1
25+ print (slno )
26+ print (filename )
27+ ndwi = gdal_array .LoadFile (filename ).astype (np .float32 )
28+ ndwi = np .nan_to_num (ndwi , nan = - 0.5 )
29+
30+ # Clip NDWI rasters by the expanded mask
31+ exp_mask = gdal_array .LoadFile (drtr + "\Expanded_Mask.tif" ).astype (np .float32 )
32+ clip_ndwi = ndwi
33+ clip_ndwi [np .where (exp_mask == 0 )] = - 0.5
34+
35+ # K-means clustering clipped NDWI raters to 3 clusters
36+ # (water, wet non-water, and dry non-water) (1 more cluster for the value of -0.5)
37+ rows = len (clip_ndwi )
38+ columns = len (clip_ndwi [0 ])
39+ x = clip_ndwi .ravel ()
40+ km = KMeans (n_clusters = 4 , n_init = 10 )
41+ km .fit (x .reshape (- 1 ,1 ))
42+ z = km .cluster_centers_
43+ z1 = max (z )
44+ z2 = - 1
45+ z3 = - 1
46+ for i in range (0 , 4 ):
47+ if z [i ] < z1 and z [i ] > z2 :
48+ z2 = z [i ]
49+ for i in range (0 , 4 ):
50+ if z [i ] < z2 and z [i ] > z3 :
51+ z3 = z [i ]
52+ threshold = round (float ((z1 + z2 )/ 2 ),3 )
53+ print (" K-Means clustering threshold = " + str (threshold ))
54+ # plt.figure(figsize=[30,15])
55+ # plt.hist(x, bins=200, range=[-0.49, 0.5], color='c')
56+ # plt.axvline(z[0], color='navy', linestyle='dashed', linewidth=2)
57+ # plt.axvline(z[1], color='navy', linestyle='dashed', linewidth=2)
58+ # plt.axvline(z[2], color='navy', linestyle='dashed', linewidth=2)
59+ # plt.axvline(z[3], color='navy', linestyle='dashed', linewidth=2)
60+ # plt.axvline((z1+z2)/2, color='red', linestyle='dashed', linewidth=2)
61+ # plt.axvline((z2+z3)/2, color='red', linestyle='dashed', linewidth=2)
62+ # plt.title(filename, fontsize=30)
63+ # plt.xlabel('NDWI', fontsize=30)
64+ # plt.xticks(fontsize=30)
65+ # plt.yticks(fontsize=30)
66+ # plt.show()
67+ labels = np .reshape (km .labels_ ,(- 1 ,columns ))
68+ water_label = 0
69+ for i in range (0 , 4 ):
70+ if z [i ] == max (z ):
71+ water_label = i
72+ water_cluster = labels - labels
73+ water_cluster [np .where (labels == water_label )] = 1
74+
75+ # Assess image quality
76+ zone_mask = gdal_array .LoadFile (drtr + "\Zone_Mask.tif" ).astype (np .float32 )
77+ count_zm = np .zeros (no_zones )
78+ for i in range (0 , no_zones ):
79+ count_zm [i ] = np .count_nonzero (zone_mask == i + 1 )
80+ cluster_zone = zone_mask
81+ cluster_zone [np .where (water_cluster == 0 )] = 0
82+ count_cl = np .zeros (no_zones )
83+ ratio = np .zeros (no_zones )
84+ N_10 = 0
85+ for i in range (0 , no_zones ):
86+ count_cl [i ] = np .count_nonzero (cluster_zone == i + 1 )
87+ ratio [i ] = count_cl [i ]/ (count_zm [i ] + 1.0e-20 )
88+ if ratio [i ] >= 0.1 :
89+ N_10 += 1
90+ print (" Ratio of zone 50 = " + str (round (ratio [no_zones - 1 ],3 )))
91+ print (" No. of zones having >=10% water pixels = " + str (int (N_10 )))
92+
93+ # Improve image classification
94+ ratio_nm = ratio * 100 / (max (ratio ) + 1.0e-20 )
95+ x_axis = np .zeros (no_zones )
96+ for i in range (0 , no_zones ):
97+ x_axis [i ] = i + 1
98+ xx = np .vstack ((x_axis , ratio_nm )).T
99+ kkm = KMeans (n_clusters = 2 ).fit (xx )
100+ llb = kkm .labels_
101+ minx0 = no_zones
102+ minx1 = no_zones
103+ for i in range (0 , no_zones ):
104+ if llb [i ] == 0 :
105+ if x_axis [i ] < minx0 :
106+ minx0 = x_axis [i ]
107+ elif llb [i ] == 1 :
108+ if x_axis [i ] < minx1 :
109+ minx1 = x_axis [i ]
110+ s_index = max (minx0 , minx1 )
111+ if minx0 == s_index :
112+ water_id = 0
113+ elif minx1 == s_index :
114+ water_id = 1
115+ # print(" Additional water pixels start from zone "+str(int(s_index)))
116+ # colors = ['navy' if x==water_id else 'lightblue' for x in llb]
117+ # plt.figure(figsize=[30,15])
118+ # plt.bar(x_axis, ratio, color=colors)
119+ # plt.ylim(top=1)
120+ # plt.axvline(x=s_index,color='red',linestyle='--')
121+ # plt.title(filename, fontsize=30)
122+ # plt.xlabel('Zone', fontsize=30)
123+ # plt.ylabel('Ratio', fontsize=30)
124+ # plt.xticks(fontsize=30)
125+ # plt.yticks(fontsize=30)
126+ # plt.show()
127+
128+ recall_zm = gdal_array .LoadFile (drtr + "\Zone_Mask.tif" ).astype (np .float32 )
129+ add = recall_zm
130+ add [np .where (recall_zm < s_index )] = 0
131+ improved = added_cluster = water_cluster + add
132+ improved [np .where (added_cluster > 1 )] = 1
133+ bf_area = np .count_nonzero (water_cluster == 1 )* 0.0009
134+ af_area = np .count_nonzero (improved == 1 )* 0.0009
135+ print (" Water surface area:" )
136+ print (" Before improvement: " + str (round (bf_area ,3 ))+ " km2" )
137+ print (" After improvement: " + str (round (af_area ,3 ))+ " km2" )
138+ if bf_area == 0 :
143139 fn_area = bf_area
144140 qual = 0
145141 print (" Image cannot be improved" )
146142 else :
147- if ratio [ 49 ] == 0 :
143+ if threshold < - 0.5 :
148144 fn_area = bf_area
149145 qual = 0
150146 print (" Image cannot be improved" )
151147 else :
152- if N_10 == 0 :
148+ if ratio [ 49 ] == 0 :
153149 fn_area = bf_area
154150 qual = 0
155151 print (" Image cannot be improved" )
156152 else :
157- fn_area = af_area
158- qual = 1
159- print (" Final area: " + str (round (fn_area ,3 ))+ " km2" )
160- print (" " )
161- results = np .append (results , [[str (filename [8 ]), str (filename [11 ]),filename [18 :28 ],
162- round (threshold ,3 ), round (ratio [49 ],3 ),
163- int (N_10 ), int (s_index ), int (qual ),
164- round (bf_area ,3 ), round (af_area ,3 ),
165- round (fn_area ,3 )]], axis = 0 )
166- continue
167- else :
153+ if N_10 == 0 :
154+ fn_area = bf_area
155+ qual = 0
156+ print (" Image cannot be improved" )
157+ else :
158+ fn_area = af_area
159+ qual = 1
160+ print (" Final area: " + str (round (fn_area ,3 ))+ " km2" )
161+ print (" " )
162+ results = np .append (results , [[str (filename [8 ]), str (filename [11 ]),filename [18 :28 ],
163+ round (threshold ,3 ), round (ratio [49 ],3 ),
164+ int (N_10 ), int (s_index ), int (qual ),
165+ round (bf_area ,3 ), round (af_area ,3 ),
166+ round (fn_area ,3 )]], axis = 0 )
167+ continue
168+ else :
169+ continue
170+ except :
168171 continue
169172
170173
0 commit comments