forked from zhouguiyun-uestc/FastFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastFlow.cpp
More file actions
454 lines (411 loc) · 12.7 KB
/
FastFlow.cpp
File metadata and controls
454 lines (411 loc) · 12.7 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include <iostream>
#include <string>
#include <fstream>
#include <queue>
#include <stack>
#include <algorithm>
#include "FlowAccu.h"
#include "DEM.h"
#include "Node.h"
#include "FlowDirection.h"
#include "utils.h"
#include <list>
#include <unordered_map>
#include "timeutils.h"
using namespace std;
using std::cout;
using std::endl;
using std::string;
using std::getline;
using std::fstream;
using std::ifstream;
using std::priority_queue;
using std::binary_function;
typedef std::vector<Node> NodeVector;
typedef std::priority_queue<Node, NodeVector, Node::Greater> PriorityQueue;
void accuMethodByBTI(FlowDirection& dirGrid, FlowAccu& accuGrid, long long* pTimeConsume);
void accuMethodByJiang(FlowDirection& dirGrid, FlowAccu& accuGrid, long long* consumeTime);
void accuMethodByRecursive(FlowDirection& dirGrid, FlowAccu& accuGrid, long long* pTimeConsume);
void accuMethodByWang(FlowDirection& dirGrid, FlowAccu& accuDem, long long* pTimeConsume);
void accuMethodByZhou(FlowDirection& dirGrid, FlowAccu& accuGrid, long long* consumeTime);
//Compute statistics for a DEM matrix
void calculateStatistics(const DEM& dem, double* min, double* max, double* mean, double* stdDev)
{
int width = dem.Get_NX();
int height = dem.Get_NY();
int validElements = 0;
double minValue, maxValue;
double sum = 0.0;
double sumSqurVal = 0.0;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
if (!dem.is_NoData(row, col))
{
double value = dem.asFloat(row, col);
//Initialize minValue and maxValue using the first valid value
if (validElements == 0)
{
minValue = maxValue = value;
}
validElements++;
if (minValue > value)
{
minValue = value;
}
if (maxValue < value)
{
maxValue = value;
}
sum += value;
sumSqurVal += (value * value);
}
}
}
double meanValue = sum / validElements;
double stdDevValue = sqrt((sumSqurVal / validElements) - (meanValue * meanValue));
*min = minValue;
*max = maxValue;
*mean = meanValue;
*stdDev = stdDevValue;
}
//Compute statistics for a FlowAccu matrix
void calculateStatistics(const FlowAccu& dem, double* min, double* max, double* mean, double* stdDev)
{
int width = dem.Get_NX();
int height = dem.Get_NY();
int validElements = 0;
double minValue, maxValue;
double sum = 0.0;
double sumSqurVal = 0.0;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
if (!dem.is_NoData(row, col))
{
double value = dem.asInt(row, col);
//Initialize minValue and maxValue using the first valid value
if (validElements == 0)
{
minValue = maxValue = value;
}
validElements++;
if (minValue > value)
{
minValue = value;
}
if (maxValue < value)
{
maxValue = value;
}
sum += value;
sumSqurVal += (value * value);
}
}
}
double meanValue = sum / validElements;
double stdDevValue = sqrt((sumSqurVal / validElements) - (meanValue * meanValue));
*min = minValue;
*max = maxValue;
*mean = meanValue;
*stdDev = stdDevValue;
}
//Compute statistics for a FlowDirection matrix
void calculateStatistics(const FlowDirection& dem, double* min, double* max, double* mean, double* stdDev)
{
int width = dem.Get_NX();
int height = dem.Get_NY();
int validElements = 0;
double minValue, maxValue;
double sum = 0.0;
double sumSqurVal = 0.0;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
if (!dem.is_NoData(row, col))
{
double value = dem.asByte(row, col);
//Initialize minValue and maxValue using the first valid value
if (validElements == 0)
{
minValue = maxValue = value;
}
validElements++;
if (minValue > value)
{
minValue = value;
}
if (maxValue < value)
{
maxValue = value;
}
sum += value;
sumSqurVal += (value * value);
}
}
}
double meanValue = sum / validElements;
double stdDevValue = sqrt((sumSqurVal / validElements) - (meanValue * meanValue));
*min = minValue;
*max = maxValue;
*mean = meanValue;
*stdDev = stdDevValue;
}
//Fill depresspions and calculate flow directions from input DEM using the algorithm in Wang and Liu (2006)
void FillDEMAndComputeFlowDirection(char* inputFile, char* outputDirPath)
{
DEM dem;
double geoTransformArgs[6];
if (!readTIFF(inputFile, dem, geoTransformArgs))
{
cout<<"Error occured when reading DEM!"<<endl;
return;
}
int width = dem.Get_NX();
int height = dem.Get_NY();
cout<<"DEM width:"<<width<<"\theight£º"<<height<<endl;
//Prepare a flag array of Boolean values
int nums = (width * height + 7) / 8;
unsigned char* flagArray = new unsigned char[nums]();
//Flow direction matrix
FlowDirection dirDEM;
dirDEM.SetHeight(height);
dirDEM.SetWidth(width);
if(dirDEM.Allocate() == false)
{
cout<<"Failed to allocate memory for the output FlowDirection matrix"<<endl;
return;
}
PriorityQueue queue;
int validElementsCount = 0;
cout<<"Using the method in Wang&Liu(2006) to fill depressions...\n";
//Push all border cells into the queue
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
Node tmpNode;
if (!dem.is_NoData(row, col))
{
validElementsCount++;
for (int i = 0; i < 8; i++)
{
int iRow, iCol;
iRow = Get_rowTo(i, row);
iCol = Get_colTo(i, col);
if (!dem.is_InGrid(iRow, iCol) || dem.is_NoData(iRow, iCol))
{
tmpNode.col = col;
tmpNode.row = row;
tmpNode.spill = dem.asFloat(row, col);
queue.push(tmpNode);
setFlag(row * width + col, flagArray);
break;
}
}
}
}
}
int percentFive = validElementsCount / 20;
cout<<"Start filling depressions and calculationg flow direciton matrix ..."<<endl;
struct timeval timeStart, timeEnd;
gettimeofday(&timeStart, NULL);
int count = 0;
while (!queue.empty())
{
count++;
if (count % percentFive == 0)
{
int percentNum = count / percentFive;
cout<<"Progress£º"<<percentNum * 5 <<"%\r";
}
Node tmpNode = queue.top();
queue.pop();
int row = tmpNode.row;
int col = tmpNode.col;
float spill = tmpNode.spill;
for (int i = 0; i < 8; i++)
{
int iRow, iCol;
iRow = Get_rowTo(i, row);
iCol = Get_colTo(i, col);
float iSpill;
if (dem.is_InGrid(iRow, iCol) && !dem.is_NoData(iRow, iCol))
{
if (!isProcessed(iRow * width + iCol, flagArray))
{
iSpill = dem.asFloat(iRow, iCol);
// a depression or a flat region is encountered
if (iSpill <= spill)
{
dem.Set_Value(iRow, iCol, spill);
setFlag(iRow * width + iCol, flagArray);
tmpNode.row = iRow;
tmpNode.col = iCol;
tmpNode.spill = spill;
//set the flow direction as the reverse of the search direction
dirDEM.Set_Value(iRow, iCol, inverse[i]);
}
else
{
dem.Set_Value(iRow, iCol, iSpill);
setFlag(iRow * width + iCol, flagArray);
tmpNode.row = iRow;
tmpNode.col = iCol;
tmpNode.spill = iSpill;
}
queue.push(tmpNode);
}
}
}
//if the flow direction of the cell is unspecified, use D8 rule to assign the direction
if (dirDEM.is_NoData(row, col))
{
unsigned char dir = dem.computeDirection(row, col, spill);
dirDEM.Set_Value(row, col, dir);
}
}
cout<<"Finish filling depressions and calculating flow direciton matrix."<<endl;
gettimeofday(&timeEnd, NULL);
long long consumeTime = timeval_diff(NULL, &timeEnd, &timeStart);
printf("Times used: %lld microsec\n", consumeTime);
cout<<"Writing flow direction matrix ..."<<endl;
double min, max, mean, stdDev;
delete[] flagArray;
calculateStatistics(dirDEM, &min, &max, &mean, &stdDev);
CreateGeoTIFF(outputDirPath, dirDEM.Get_NY(), dirDEM.Get_NX(),
(void *)dirDEM.getData(),GDALDataType::GDT_Byte, geoTransformArgs,
&min, &max, &mean, &stdDev, 0);
cout<<"Flow direction grid created!"<<endl;
}
int main(int argc, char* argv[])
{
FlowDirection dirGrid;
FlowAccu accuGrid;
if (argc < 4)
{
cout<<"FastFlow is a softwared tool that implements different algorithms for calculating flow accumulation matrices from flow direction matrices."<<endl;
cout<<"FastFlow also calculates flow direction matrices from unfilled raw DEMs."<<endl;
cout<<"FastFlow supports GeoTIFF file format through thd GDAL library."<<endl;
cout<<"\n Usage: FastFlow [method] inputFile outputFile."<<endl;
cout<<"\n [method] can be 'flowdirection', 'Wang','Jiang','BTI','Recursive','Zhou'"<<endl;
cout<<"\nExample Usage1: FastFlow flowdirection dem.tif flowdir.tif. \n\tCreate the flow direction GeoTIFF file from raw dem.tif file. The flow direction is coded in the same way as ArcGIS. \n\tPlease see http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z00000063000000.htm"<<endl;
cout<<"\nExample Usage2: FastFlow Zhou flowdir.tif flowaccu.tif. \n\tCreate the flow accumulation GeoTIFF file from flow direction file flowdir.tif using Zhou's method"<<endl;
return 1;
}
char* algName = argv[1];
char* inputPath = argv[2];
char* outputPath = argv[3];
long long consumeTime;
double geoTransformArgs[6];
double min, max, mean, stdDev;
double noDataValue = -9999.0;
if(strcmp(algName, "flowdirection") == 0)
{
FillDEMAndComputeFlowDirection(inputPath, outputPath);
}
else if(strcmp(algName, "Wang") == 0)
{
if(!readTIFF(inputPath, GDALDataType::GDT_Byte, dirGrid, geoTransformArgs))
{
printf("\nFailed to read flow direction GeoTIFF file! Make sure the input file is a flow direction file!\n");
return -1;
}
accuGrid.SetWidth(dirGrid.Get_NX());
accuGrid.SetHeight(dirGrid.Get_NY());
if(!accuGrid.Allocate())
{
fprintf(stderr, "Failed to allocate memory for accumulation matrix£¡\n");
return 0;
}
accuMethodByWang(dirGrid, accuGrid, &consumeTime);
calculateStatistics(accuGrid, &min, &max, &mean, &stdDev);
CreateGeoTIFF(outputPath, accuGrid.Get_NY(), accuGrid.Get_NX(), (void*)accuGrid.getData(), GDALDataType::GDT_Int32,
geoTransformArgs, &min, &max, &mean, &stdDev, noDataValue);
}
else if(strcmp(algName, "Jiang") == 0)
{
if(!readTIFF(inputPath, GDALDataType::GDT_Byte, dirGrid, geoTransformArgs))
{
fprintf(stderr, "read direction tif failed!\n");
return -1;
}
accuGrid.SetWidth(dirGrid.Get_NX());
accuGrid.SetHeight(dirGrid.Get_NY());
if(!accuGrid.Allocate())
{
fprintf(stderr, "Failed to allocate memory for accumulation matrix£¡\n");
return 0;
}
accuMethodByJiang(dirGrid, accuGrid, &consumeTime);
calculateStatistics(accuGrid, &min, &max, &mean, &stdDev);
CreateGeoTIFF(outputPath, accuGrid.Get_NY(), accuGrid.Get_NX(), (void*)accuGrid.getData(), GDALDataType::GDT_Int32,
geoTransformArgs, &min, &max, &mean, &stdDev, noDataValue);
}
else if(strcmp(algName, "BTI") == 0)
{
if(!readTIFF(inputPath, GDALDataType::GDT_Byte, dirGrid, geoTransformArgs))
{
fprintf(stderr, "read direction tif failed!\n");
return -1;
}
accuGrid.SetWidth(dirGrid.Get_NX());
accuGrid.SetHeight(dirGrid.Get_NY());
if(!accuGrid.Allocate())
{
fprintf(stderr, "Failed to allocate memory for accumulation matrix£¡\n");
return 0;
}
accuMethodByBTI(dirGrid, accuGrid, &consumeTime);
calculateStatistics(accuGrid, &min, &max, &mean, &stdDev);
CreateGeoTIFF(outputPath, accuGrid.Get_NY(), accuGrid.Get_NX(), (void*)accuGrid.getData(), GDALDataType::GDT_Int32,
geoTransformArgs, &min, &max, &mean, &stdDev, noDataValue);
}
else if(strcmp(algName, "Recursive") == 0)
{
if(!readTIFF(inputPath, GDALDataType::GDT_Byte, dirGrid, geoTransformArgs))
{
fprintf(stderr, "read direction tif failed!\n");
return -1;
}
accuGrid.SetWidth(dirGrid.Get_NX());
accuGrid.SetHeight(dirGrid.Get_NY());
if(!accuGrid.Allocate())
{
fprintf(stderr, "Failed to allocate memory for accumulation matrix£¡\n");
return 0;
}
accuMethodByRecursive(dirGrid, accuGrid, &consumeTime);
calculateStatistics(accuGrid, &min, &max, &mean, &stdDev);
CreateGeoTIFF(outputPath, accuGrid.Get_NY(), accuGrid.Get_NX(), (void*)accuGrid.getData(), GDALDataType::GDT_Int32,
geoTransformArgs, &min, &max, &mean, &stdDev, noDataValue);
}
else if(strcmp(algName, "Zhou") == 0)
{
if(!readTIFF(inputPath, GDALDataType::GDT_Byte, dirGrid, geoTransformArgs))
{
fprintf(stderr, "read direction tif failed!\n");
return -1;
}
accuGrid.SetWidth(dirGrid.Get_NX());
accuGrid.SetHeight(dirGrid.Get_NY());
if(!accuGrid.Allocate())
{
fprintf(stderr, "Failed to allocate memory for accumulation matrix£¡\n");
return 0;
}
accuMethodByZhou(dirGrid, accuGrid, &consumeTime);
calculateStatistics(accuGrid, &min, &max, &mean, &stdDev);
CreateGeoTIFF(outputPath, accuGrid.Get_NY(), accuGrid.Get_NX(), (void*)accuGrid.getData(), GDALDataType::GDT_Int32,
geoTransformArgs, &min, &max, &mean, &stdDev, noDataValue);
}
else
{
cout<<"Unknown command type."<<endl;
return -1;
}
return 0;
}