/
index.py
206 lines (160 loc) · 5.93 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import rasterio as rio
from rasterio.plot import show
#import rasterio.plot as plt #import show
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.style.use('seaborn-deep')
def index(b2, b1):
logor = lambda x, y: np.logical_or(x>0, y>0)
calc = lambda x, y, ch: np.where(ch, (x-y)/(x+y), 0)
ind = calc(b1, b2, logor(b2, b1))
return ind
def plot_hist(plot_percent, dates, d):
d = list(d)
for i in range(len(plot_percent)):
plot_percent[i] = plot_percent[i][:-1]
plot_percent = list(np.array(plot_percent).transpose())
ind = np.arange(len(dates))
#dates = list(map(lambda x: round(x, 2), dates))
ax = plt.subplot()
bottom = [0]*len(dates)
for i in range(len(d)):
p = list(plot_percent[i])
ax.bar(ind, p, bottom = bottom, width = 0.35, label = d[i])
bottom = [bottom[i]+p[i] for i in range(len(p))]
ax.set_xticks(ind, minor=False)
ax.set_xticklabels(dates)
ax.set_title('Percent Area Cover for years {} \n'.format(', '.join(dates[:-1])+' and ' + dates[-1]))
for p in ax.patches:
width, height = p.get_width(), p.get_height()
if(height!=0.0):
x, y = p.get_xy()
ax.annotate(str(round(height,1))+'%', (p.get_x()+.05*width, p.get_y()+.4*height))
plt.show()
def plot_diff_util(ind_arr, bands, dates, ch):
normalize = lambda array: ((array - array.min())/(array.max() - array.min()))
bandsn = []
for band in bands:
band = band[:3]
t = list(map(lambda x: normalize(x), band))
t[0], t[2] = t[2], t[0]
t = tuple(t)
bandsn.append(np.dstack(t))
n = len(ind_arr)
d = list(zip(ind_arr, bandsn))
date_dict = dict(zip(dates, d))
return date_dict
def calc_table(ind, x, y, d):
k, v = np.unique(ind[:]<=1.0, return_counts = True)
tot_area = dict(zip(k, v))
x = 23.5
y = 23.5
tot_area = tot_area[True]*x*y
area, perc = [], []
for k, v in d.items():
a, b = np.unique(ind[:]<=v, return_counts = True)
ar = dict(zip(a, b))
ar = ar.get(True, 0)*x*y
if v==0:
area.append(ar)
perc.append((ar*100)/tot_area)
else:
p = abs(sum(area)-ar)
area.append(p)
perc.append((p*100)/tot_area)
area, perc = map(lambda t: [round(i, 3) for i in t], [area, perc])
area.append(round(tot_area, 3))
perc.append(100)
return (perc, area)
def diff_table(ind_arr, bands, dates, d):
aggr = list(zip(ind_arr, bands))
percent = []
ar = []
df_list = []
l = []
y = list(d.values())
for v in range(len(d)):
if v==0:
l.append('<= '+str(y[v]))
else:
l.append(str(y[v-1])+' - '+str(y[v]))
for ind, band in aggr:
x, y = band[4], band[5]
a, b = calc_table(ind, x, y, d)
percent.append(a)
ar.append(b)
#df_list.append(b)
print(ar)
ar = list(np.array(ar).transpose())
pr = list(np.array(percent).transpose())
date = []
for i in range(len(dates)):
date.append(str(dates[i]))
date.append('')
df_dict = {'Year': date, '#': ['Area (in m2)' if i%2==0 else 'Percentage Cover' for i in range(len(date))]}
i = 0
for k, v in d.items():
print(i)
a, b = ar[i], pr[i]
app = []
for x in range(len(a)):
app.append(str(a[x]))
app.append(str(b[x])+"%")
df_dict[k] = app
i = i + 1
last = ar[-1]
date = []
for i in range(len(last)):
date.append(str(last[i]))
date.append('100%')
df_dict['Total Area'] = date
dfres = pd.DataFrame(df_dict)
dfres.to_excel('result.xlsx')
return percent
def calc_diff(l, ch):
bands = []
years = []
for i in range(len(l)):
trans = l[i][0].transform
years.append(l[i][4])
bands.append([l[i][0].read(1), l[i][1].read(1),
l[i][2].read(1), l[i][3].read(1), trans[0], -trans[4]])
if ch==1:
ind_arr = [index(band[1], band[2]) for band in bands]
d = {'No Vegetation': 0.0, 'Lowest Vegetation': 0.15, 'Low Vegetation': 0.3, 'Dense Vegetation': 0.6, 'Highest Vegetation': 1.0}
percent = diff_table(ind_arr, bands, years, d)
plot_hist(percent, years, d.keys())
elif ch==2:
ind_arr = [index(band[3], band[0]) for band in bands]
d = {'No Water': 0.0, 'Lowest Water': 0.15, 'Less Water': 0.3, 'Dense Water': 0.6, 'Highest Water': 1.0}
percent = diff_table(ind_arr, bands, years, d)
plot_hist(percent, years, d.keys())
else:
ind_arr = [index(band[2], band[3]) for band in bands]
d = {'No Built-Up': 0.0, 'Lowest Built-up': 0.15, 'Less Built-Up': 0.3, 'Dense Built-up': 0.6, 'Highest Built-up': 1.0}
percent = diff_table(ind_arr, bands, years, d)
plot_hist(percent, years, d.keys())
date_dict = plot_diff_util(ind_arr, bands, years, ch)
while 1:
date = input('enter year to plot')
ind, band = date_dict[date]
mid = ind.mean()
minx=np.nanmin(ind)
maxx=np.nanmax(ind)
ax = plt.subplot(121)
ax.imshow(ind, cmap=colormap, vmin=minx, vmax=maxx)
ax.set_title('YEAR: {}'.format(date))
ax1 = plt.subplot(122)
ax1.imshow(band)
ax1.set_title('FCC YEAR: {}'.format(date))
plt.show()
if __name__ == '__main__':
ch = int(input('Choose Index:\n1. NDVI\n2. NDWI\n3. NDBI'))
k = int(input('No. of Images: '))
l=[]
print('Enter green, red, nir and swir image and year for {} images'.format(k))
l = [tuple(input().strip().split()) for i in range(k)]
l = list(map(lambda x: (rio.open(x[0]), rio.open(x[1]), rio.open(x[2]), rio.open(x[3]), x[4]), l))
l = sorted(l, key = lambda x:x[4])
calc_diff(l, ch)