-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathautoShowClassMap.m
249 lines (219 loc) · 7.36 KB
/
autoShowClassMap.m
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
function autoShowClassMap(varargin)
% This function is used to provde all land classification maps for each year
% Results for LCMAP
% Version 1.2 Acquire map from annual classfication map (04/09/2015)
% Version 1.1 Add disturbance in the annual map (04/01/2015);
% Version 1.0 No disturbance class in the cover map (11/06/2015)
% Tools
%
%
% Specific parameters
% ------------------------
% 'CCDCDir' Directory of input data. Default is the path to
% the current folder.
% 'StartYear' Start year.
% 'EndYear' End year.
% 'Year' Year. Only a certain date's map will be outputted
% when inputing this.
% 'Month' 7
% 'Day' 1
%
%
% Examples:
% ------------------------
% autoShowClassMap('StartYear',2010,'EndYear',2015)
% will give the classifiaction maps at July 1 between 2010 and 2015.
%
% autoShowClassMap('StartYear',2000,'EndYear',2015,'Month',12,'Day',1)
% will give the classifiaction maps at December 1 between 2010 and 2015.
%
% autoShowClassMap('Year',2000,'Month',7,'Day',1)
% will give the classifiaction map at July 1, 2000.
%
%% get parameters from inputs
% where the all Landsat zipped files are
dir_cur = pwd;
p = inputParser;
p.FunctionName = 'paras';
addParameter(p,'CCDCDir',dir_cur);
% default date is 1th, July.
addParameter(p,'StartYear',0);
addParameter(p,'EndYear',0);
addParameter(p,'Year',0);
addParameter(p,'Month',7);
addParameter(p,'Day',1);
% request user's input
parse(p,varargin{:});
dir_cur=p.Results.CCDCDir;
start_year=p.Results.StartYear;
end_year=p.Results.EndYear;
oneyear=p.Results.Year;
mm=p.Results.Month;
dd=p.Results.Day;
% give certain year
if oneyear>0
start_year = oneyear;
end_year = oneyear;
end
% optional
% addpath('~/ccdc');
cd(dir_cur);
% INPUTS:
all_yrs = start_year:end_year; % all of years for producing maps
v_input = ccdc_Inputs;
% dimension and projection of the image
nrows = v_input.ijdim(1);
ncols = v_input.ijdim(2);
jiDim = [ncols,nrows];
jiUL = v_input.jiul;
res = v_input.resolu;
zc = v_input.zc;
% number of coefficients
num_c = v_input.num_c;
% number of bands
nbands = v_input.nbands;
% max number of maps
max_n = length(all_yrs);
% all julidan dates
% mm = 7;
% dd = 1;
jul_d = datenummx(all_yrs,mm,dd);
jul_start = datenummx(all_yrs,1,1);
jul_end = datenummx(all_yrs,12,31);
% produce land cover map
CoverMap = 255*ones(nrows,ncols,max_n,'uint8'); % Trends categories (0~11)
% produce land cover map QA (unsupervised emsemble margin)
CoverQAMap = 255*ones(nrows,ncols,max_n,'uint8'); % Trends categories (0~100)
% make Predict folder for storing predict images
n_map=v_input.name_map;% 'CCDCMap';
if isempty(dir(n_map))
mkdir(n_map);
end
% cd to the folder for storing recored structure
cd(v_input.name_rst);
imf = dir(fullfile(pwd,'record_change*')); % folder names
num_line = size(imf,1);
line_pt = 0;
%%
for line = 1:num_line
if 100*(line/num_line) - line_pt > 1
fprintf('Processing %.0f percent\n',ceil(100*(line/num_line)));
line_pt = 100*(line/num_line);
end
% load one line of time series models
load(fullfile(pwd,imf(line).name));
% postions
pos = [rec_cg.pos];
% continue if there is no model available
l_pos = length(pos);
if l_pos == 0
continue
end
% matrix of each component
% start time
t_start = [rec_cg.t_start];
% end time
t_end = [rec_cg.t_end];
% break time
t_break = [rec_cg.t_break];
% class
class = [rec_cg.class];
% classification QA
class_qa = [rec_cg.classQA];
% % % put disturbed class to grass/shrub
% % class(class == 10) = 7;
% For Condition & Cover Map
for i = 1:l_pos
% get row and col
[I,J] = ind2sub(jiDim,pos(i));
% initialize pixels have at least one model
if sum(CoverMap(J,I,:) == 255) == max_n
% write land cover to CoverMap
CoverMap(J,I,:) = 0;
% write land cover to CoverQAMap
CoverQAMap(J,I,:) = 0;
end
% give disturbed class for each year
% year (band) the curve belongs to
n_band = jul_d >= t_start(i) & (jul_d <= t_end(i) | jul_d < t_break(i));
% write land cover to CoverMap
CoverMap(J,I,n_band) = class(i);
% write land cover to CoverQAMap
CoverQAMap(J,I,n_band) = class_qa(i);
% give next land cover category for gaps
if i > 1
if pos(i) == pos(i-1) % same location
n_dist = jul_d < t_start(i) & jul_d >= t_break(i-1);
% find the first nonzero value from class(:,i);
idn = find(class(:,i)>0);
if ~isempty(idn)
% write land cover (distubed) to CoverMap
CoverMap(J,I,n_dist) = class(idn(1),i);
end
end
end
end
end
cd ..
enviwrite_bands(fullfile(v_input.l_dir,n_map,'CoverMap'),CoverMap,'uint8',res,jiUL,'bsq',zc,all_yrs);
clear CoverMap;
enviwrite_bands(fullfile(v_input.l_dir,n_map,'CoverQAMap'),CoverQAMap,'uint8',res,jiUL,'bsq',zc,all_yrs);
clear CoverQAMap;
% % % write ENVI files for ARD
% % % Cover Map
% % ARD_enviwrite_bands([v_input.l_dir,'/',n_map,'/CoverMap'],CoverMap,'uint8','bsq',all_yrs);
% % clear CoverMap
% % ARD_enviwrite_bands([v_input.l_dir,'/',n_map,'/CoverQAMap'],CoverQAMap,'uint8','bsq',all_yrs);
% % clear CoverQAMap
end
% %% Add change event
% change_map = double(enviread('CCDCMap/ChangeMap'));
% % minimum mapping unit
% num_obj = 5;
%
% % spatial filtering
% for i = 1:size(change_map,3)
% i
% tmp = change_map(:,:,i);
% tmp(tmp > 0) = 1;
% segm_tmp=bwlabeln(tmp,8);
% L = segm_tmp;
% s = regionprops(L,'area');
% area = [s.Area];
%
% % filter out cloud object < than num_cldoj pixels
% idx = find(area >= num_obj);
% tmp(ismember(L,idx)==0) = 0;
% copy_map = change_map(:,:,i);
% copy_map(tmp==0) = 0;
% change_map(:,:,i) = copy_map;
% end
% % write filtered change map
% ARD_enviwrite_bands([v_input.l_dir,'/',n_map,'/FChangeMap'],change_map,'uint16','bsq',all_yrs,'example_img');
%
% %% change analysis
% max_n = 30;
% map = double(enviread('CCDCMap/CoverMap1_4'));
% change_stat = [];
%
% for i = 1:max_n-1
% i
% change_map = map(:,:,i)*100 + map(:,:,i+1);
% idstb = map(:,:,i+1)-map(:,:,i);
% change_stat = [change_stat;change_map(idstb~=0)];
% end
%
% % update class number
% all_class = unique(change_stat);
% % update number of class
% n_class = length(all_class);
% % calculate proportion based # for each class
% number = hist(change_stat,all_class);
% prct = number/sum(number);
% %% add disturbance (3) to the classification map
% for i = 1:max_n
% change_tmp = change_map(:,:,i);
% class_tmp = cover_map(:,:,i);
% class_tmp(change_tmp>0) = 3;
% cover_map(:,:,i) = class_tmp;
% end