| 1 |
/**************************************************************************** |
| 2 |
* |
| 3 |
* MODULE: r.li.padcv |
| 4 |
* AUTHOR(S): Serena Pallecchi (original contributor) |
| 5 |
* student of Computer Science University of Pisa (Italy) |
| 6 |
* Commission from Faunalia Pontedera (PI) www.faunalia.it |
| 7 |
* Fixes: Markus Neteler <neteler itc.it> |
| 8 |
* Rewrite: Markus Metz |
| 9 |
* Patch identification: Michael Shapiro - CERL |
| 10 |
* |
| 11 |
* PURPOSE: calculates coefficient of variation of patch areas |
| 12 |
* COPYRIGHT: (C) 2007-2014 by the GRASS Development Team |
| 13 |
* |
| 14 |
* This program is free software under the GNU General Public |
| 15 |
* License (>=v2). Read the file COPYING that comes with GRASS |
| 16 |
* for details. |
| 17 |
* |
| 18 |
*****************************************************************************/ |
| 19 |
|
| 20 |
#include <stdlib.h> |
| 21 |
#include <fcntl.h> |
| 22 |
#include <math.h> |
| 23 |
|
| 24 |
#include <grass/gis.h> |
| 25 |
#include <grass/raster.h> |
| 26 |
#include <grass/glocale.h> |
| 27 |
|
| 28 |
#include "../r.li.daemon/daemon.h" |
| 29 |
#include "../r.li.daemon/GenericCell.h" |
| 30 |
|
| 31 |
/* template is patchnum */ |
| 32 |
|
| 33 |
/* cell count and type of each patch */ |
| 34 |
struct pst { |
| 35 |
long count; |
| 36 |
generic_cell type; |
| 37 |
}; |
| 38 |
|
| 39 |
rli_func patchAreaDistributionCV; |
| 40 |
int calculate(int fd, struct area_entry *ad, double *result); |
| 41 |
int calculateD(int fd, struct area_entry *ad, double *result); |
| 42 |
int calculateF(int fd, struct area_entry *ad, double *result); |
| 43 |
|
| 44 |
int main(int argc, char *argv[]) |
| 45 |
{ |
| 46 |
struct Option *raster, *conf, *output; |
| 47 |
struct GModule *module; |
| 48 |
|
| 49 |
G_gisinit(argv[0]); |
| 50 |
module = G_define_module(); |
| 51 |
module->description = |
| 52 |
_("Calculates coefficient of variation of patch area on a raster map"); |
| 53 |
G_add_keyword(_("raster")); |
| 54 |
G_add_keyword(_("landscape structure analysis")); |
| 55 |
G_add_keyword(_("patch index")); |
| 56 |
|
| 57 |
/* define options */ |
| 58 |
raster = G_define_standard_option(G_OPT_R_INPUT); |
| 59 |
|
| 60 |
conf = G_define_standard_option(G_OPT_F_INPUT); |
| 61 |
conf->key = "config"; |
| 62 |
conf->description = _("Configuration file"); |
| 63 |
conf->required = YES; |
| 64 |
|
| 65 |
output = G_define_standard_option(G_OPT_R_OUTPUT); |
| 66 |
|
| 67 |
if (G_parser(argc, argv)) |
| 68 |
exit(EXIT_FAILURE); |
| 69 |
return calculateIndex(conf->answer, patchAreaDistributionCV, NULL, |
| 70 |
raster->answer, output->answer); |
| 71 |
} |
| 72 |
|
| 73 |
int patchAreaDistributionCV(int fd, char **par G_UNUSED, struct area_entry *ad, |
| 74 |
double *result) |
| 75 |
{ |
| 76 |
double indice = 0; |
| 77 |
int ris = RLI_OK; |
| 78 |
|
| 79 |
switch (ad->data_type) { |
| 80 |
case CELL_TYPE: { |
| 81 |
ris = calculate(fd, ad, &indice); |
| 82 |
break; |
| 83 |
} |
| 84 |
case DCELL_TYPE: { |
| 85 |
ris = calculateD(fd, ad, &indice); |
| 86 |
break; |
| 87 |
} |
| 88 |
case FCELL_TYPE: { |
| 89 |
ris = calculateF(fd, ad, &indice); |
| 90 |
break; |
| 91 |
} |
| 92 |
default: { |
| 93 |
G_fatal_error("data type unknown"); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if (ris != RLI_OK) { |
| 98 |
*result = -1; |
| 99 |
return RLI_ERRORE; |
| 100 |
} |
| 101 |
|
| 102 |
*result = indice; |
| 103 |
|
| 104 |
return RLI_OK; |
| 105 |
} |
| 106 |
|
| 107 |
int calculate(int fd, struct area_entry *ad, double *result) |
| 108 |
{ |
| 109 |
CELL *buf, *buf_sup, *buf_null; |
| 110 |
CELL corrCell, precCell, supCell; |
| 111 |
long npatch, area; |
| 112 |
long pid, old_pid, new_pid, *pid_corr, *pid_sup, *ltmp; |
| 113 |
struct pst *pst; |
| 114 |
long nalloc, incr; |
| 115 |
int i, j, k; |
| 116 |
int connected; |
| 117 |
int res = RLI_OK; |
| 118 |
int mask_fd, *mask_buf, *mask_sup, *mask_tmp, masked; |
| 119 |
struct Cell_head hd; |
| 120 |
|
| 121 |
Rast_get_window(&hd); |
| 122 |
|
| 123 |
buf_null = Rast_allocate_c_buf(); |
| 124 |
Rast_set_c_null_value(buf_null, Rast_window_cols()); |
| 125 |
buf_sup = buf_null; |
| 126 |
|
| 127 |
/* initialize patch ids */ |
| 128 |
pid_corr = G_malloc(Rast_window_cols() * sizeof(long)); |
| 129 |
pid_sup = G_malloc(Rast_window_cols() * sizeof(long)); |
| 130 |
|
| 131 |
for (j = 0; j < Rast_window_cols(); j++) { |
| 132 |
pid_corr[j] = 0; |
| 133 |
pid_sup[j] = 0; |
| 134 |
} |
| 135 |
|
| 136 |
/* open mask if needed */ |
| 137 |
mask_fd = -1; |
| 138 |
mask_buf = mask_sup = NULL; |
| 139 |
masked = FALSE; |
| 140 |
if (ad->mask == 1) { |
| 141 |
if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0) { |
| 142 |
G_free(buf_null); |
| 143 |
G_free(pid_corr); |
| 144 |
G_free(pid_sup); |
| 145 |
return RLI_ERRORE; |
| 146 |
} |
| 147 |
mask_buf = G_malloc(ad->cl * sizeof(int)); |
| 148 |
if (mask_buf == NULL) { |
| 149 |
close(mask_fd); |
| 150 |
G_fatal_error("malloc mask_buf failed"); |
| 151 |
} |
| 152 |
mask_sup = G_malloc(ad->cl * sizeof(int)); |
| 153 |
if (mask_sup == NULL) { |
| 154 |
close(mask_fd); |
| 155 |
G_fatal_error("malloc mask_buf failed"); |
| 156 |
} |
| 157 |
for (j = 0; j < ad->cl; j++) |
| 158 |
mask_buf[j] = 0; |
| 159 |
|
| 160 |
masked = TRUE; |
| 161 |
} |
| 162 |
|
| 163 |
/* calculate number of patches */ |
| 164 |
npatch = 0; |
| 165 |
area = 0; |
| 166 |
pid = 0; |
| 167 |
|
| 168 |
/* patch size and type */ |
| 169 |
incr = 1024; |
| 170 |
if (incr > ad->rl) |
| 171 |
incr = ad->rl; |
| 172 |
if (incr > ad->cl) |
| 173 |
incr = ad->cl; |
| 174 |
if (incr < 2) |
| 175 |
incr = 2; |
| 176 |
nalloc = incr; |
| 177 |
pst = G_malloc(nalloc * sizeof(struct pst)); |
| 178 |
for (k = 0; k < nalloc; k++) { |
| 179 |
pst[k].count = 0; |
| 180 |
} |
| 181 |
|
| 182 |
for (i = 0; i < ad->rl; i++) { |
| 183 |
buf = RLI_get_cell_raster_row(fd, i + ad->y, ad); |
| 184 |
if (i > 0) { |
| 185 |
buf_sup = RLI_get_cell_raster_row(fd, i - 1 + ad->y, ad); |
| 186 |
} |
| 187 |
|
| 188 |
if (masked) { |
| 189 |
mask_tmp = mask_sup; |
| 190 |
mask_sup = mask_buf; |
| 191 |
mask_buf = mask_tmp; |
| 192 |
if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) { |
| 193 |
res = 0; |
| 194 |
goto free_exit; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
ltmp = pid_sup; |
| 199 |
pid_sup = pid_corr; |
| 200 |
pid_corr = ltmp; |
| 201 |
|
| 202 |
Rast_set_c_null_value(&precCell, 1); |
| 203 |
|
| 204 |
connected = 0; |
| 205 |
for (j = 0; j < ad->cl; j++) { |
| 206 |
pid_corr[j + ad->x] = 0; |
| 207 |
|
| 208 |
corrCell = buf[j + ad->x]; |
| 209 |
if (masked && (mask_buf[j] == 0)) { |
| 210 |
Rast_set_c_null_value(&corrCell, 1); |
| 211 |
} |
| 212 |
|
| 213 |
if (Rast_is_c_null_value(&corrCell)) { |
| 214 |
connected = 0; |
| 215 |
precCell = corrCell; |
| 216 |
continue; |
| 217 |
} |
| 218 |
|
| 219 |
area++; |
| 220 |
|
| 221 |
supCell = buf_sup[j + ad->x]; |
| 222 |
if (masked && (mask_sup[j] == 0)) { |
| 223 |
Rast_set_c_null_value(&supCell, 1); |
| 224 |
} |
| 225 |
|
| 226 |
if (!Rast_is_c_null_value(&precCell) && corrCell == precCell) { |
| 227 |
pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x]; |
| 228 |
connected = 1; |
| 229 |
pst[pid_corr[j + ad->x]].count++; |
| 230 |
} |
| 231 |
else { |
| 232 |
connected = 0; |
| 233 |
} |
| 234 |
|
| 235 |
if (!Rast_is_c_null_value(&supCell) && corrCell == supCell) { |
| 236 |
|
| 237 |
if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) { |
| 238 |
/* connect or merge */ |
| 239 |
/* after r.clump */ |
| 240 |
if (connected) { |
| 241 |
npatch--; |
| 242 |
|
| 243 |
if (npatch == 0) { |
| 244 |
if (masked) |
| 245 |
close(mask_fd); |
| 246 |
G_fatal_error("npatch == 0 at row %d, col %d", i, |
| 247 |
j); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
old_pid = pid_corr[j + ad->x]; |
| 252 |
new_pid = pid_sup[j + ad->x]; |
| 253 |
pid_corr[j + ad->x] = new_pid; |
| 254 |
if (old_pid > 0) { |
| 255 |
/* merge */ |
| 256 |
/* update left side of the current row */ |
| 257 |
for (k = 0; k < j; k++) { |
| 258 |
if (pid_corr[k + ad->x] == old_pid) |
| 259 |
pid_corr[k + ad->x] = new_pid; |
| 260 |
} |
| 261 |
/* update right side of the previous row */ |
| 262 |
for (k = j + 1; k < ad->cl; k++) { |
| 263 |
if (pid_sup[k + ad->x] == old_pid) |
| 264 |
pid_sup[k + ad->x] = new_pid; |
| 265 |
} |
| 266 |
pst[new_pid].count += pst[old_pid].count; |
| 267 |
pst[old_pid].count = 0; |
| 268 |
|
| 269 |
if (old_pid == pid) |
| 270 |
pid--; |
| 271 |
} |
| 272 |
else { |
| 273 |
pst[new_pid].count++; |
| 274 |
} |
| 275 |
} |
| 276 |
connected = 1; |
| 277 |
} |
| 278 |
|
| 279 |
if (!connected) { |
| 280 |
/* start new patch */ |
| 281 |
npatch++; |
| 282 |
pid++; |
| 283 |
pid_corr[j + ad->x] = pid; |
| 284 |
|
| 285 |
if (pid >= nalloc) { |
| 286 |
pst = (struct pst *)G_realloc(pst, (pid + incr) * |
| 287 |
sizeof(struct pst)); |
| 288 |
|
| 289 |
for (k = nalloc; k < pid + incr; k++) |
| 290 |
pst[k].count = 0; |
| 291 |
|
| 292 |
nalloc = pid + incr; |
| 293 |
} |
| 294 |
|
| 295 |
pst[pid].count = 1; |
| 296 |
pst[pid].type.t = CELL_TYPE; |
| 297 |
pst[pid].type.val.c = corrCell; |
| 298 |
} |
| 299 |
precCell = corrCell; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
if (npatch > 0) { |
| 304 |
double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2; |
| 305 |
double area_units; |
| 306 |
double area_p; |
| 307 |
double cell_size_m; |
| 308 |
double mps; |
| 309 |
double psd; |
| 310 |
double pcv; |
| 311 |
double diff; |
| 312 |
|
| 313 |
/* calculate distance */ |
| 314 |
G_begin_distance_calculations(); |
| 315 |
/* EW Dist at North edge */ |
| 316 |
EW_DIST1 = G_distance(hd.east, hd.north, hd.west, hd.north); |
| 317 |
/* EW Dist at South Edge */ |
| 318 |
EW_DIST2 = G_distance(hd.east, hd.south, hd.west, hd.south); |
| 319 |
/* NS Dist at East edge */ |
| 320 |
NS_DIST1 = G_distance(hd.east, hd.north, hd.east, hd.south); |
| 321 |
/* NS Dist at West edge */ |
| 322 |
NS_DIST2 = G_distance(hd.west, hd.north, hd.west, hd.south); |
| 323 |
|
| 324 |
cell_size_m = (((EW_DIST1 + EW_DIST2) / 2) / hd.cols) * |
| 325 |
(((NS_DIST1 + NS_DIST2) / 2) / hd.rows); |
| 326 |
area_units = cell_size_m * area; |
| 327 |
|
| 328 |
/* mean patch size in hectares */ |
| 329 |
mps = area_units / (npatch * 10000); |
| 330 |
|
| 331 |
/* calculate patch size standard deviation */ |
| 332 |
psd = 0; |
| 333 |
for (old_pid = 1; old_pid <= pid; old_pid++) { |
| 334 |
if (pst[old_pid].count > 0) { |
| 335 |
area_p = cell_size_m * pst[old_pid].count / 10000; |
| 336 |
diff = area_p - mps; |
| 337 |
psd += diff * diff; |
| 338 |
} |
| 339 |
} |
| 340 |
psd = sqrt(psd / npatch); |
| 341 |
pcv = psd * 100 / mps; |
| 342 |
*result = pcv; |
| 343 |
} |
| 344 |
else |
| 345 |
Rast_set_d_null_value(result, 1); |
| 346 |
|
| 347 |
free_exit: |
| 348 |
if (masked) { |
| 349 |
close(mask_fd); |
| 350 |
G_free(mask_buf); |
| 351 |
G_free(mask_sup); |
| 352 |
} |
| 353 |
G_free(buf_null); |
| 354 |
G_free(pid_corr); |
| 355 |
G_free(pid_sup); |
| 356 |
G_free(pst); |
| 357 |
|
| 358 |
return res; |
| 359 |
} |
| 360 |
|
| 361 |
int calculateD(int fd, struct area_entry *ad, double *result) |
| 362 |
{ |
| 363 |
DCELL *buf, *buf_sup, *buf_null; |
| 364 |
DCELL corrCell, precCell, supCell; |
| 365 |
long npatch, area; |
| 366 |
long pid, old_pid, new_pid, *pid_corr, *pid_sup, *ltmp; |
| 367 |
struct pst *pst; |
| 368 |
long nalloc, incr; |
| 369 |
int i, j, k; |
| 370 |
int connected; |
| 371 |
int res = RLI_OK; |
| 372 |
int mask_fd, *mask_buf, *mask_sup, *mask_tmp, masked; |
| 373 |
struct Cell_head hd; |
| 374 |
|
| 375 |
Rast_get_window(&hd); |
| 376 |
|
| 377 |
buf_null = Rast_allocate_d_buf(); |
| 378 |
Rast_set_d_null_value(buf_null, Rast_window_cols()); |
| 379 |
buf_sup = buf_null; |
| 380 |
|
| 381 |
/* initialize patch ids */ |
| 382 |
pid_corr = G_malloc(Rast_window_cols() * sizeof(long)); |
| 383 |
pid_sup = G_malloc(Rast_window_cols() * sizeof(long)); |
| 384 |
|
| 385 |
for (j = 0; j < Rast_window_cols(); j++) { |
| 386 |
pid_corr[j] = 0; |
| 387 |
pid_sup[j] = 0; |
| 388 |
} |
| 389 |
|
| 390 |
/* open mask if needed */ |
| 391 |
mask_fd = -1; |
| 392 |
mask_buf = mask_sup = NULL; |
| 393 |
masked = FALSE; |
| 394 |
if (ad->mask == 1) { |
| 395 |
if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0) { |
| 396 |
G_free(buf_null); |
| 397 |
G_free(pid_corr); |
| 398 |
G_free(pid_sup); |
| 399 |
return RLI_ERRORE; |
| 400 |
} |
| 401 |
mask_buf = G_malloc(ad->cl * sizeof(int)); |
| 402 |
if (mask_buf == NULL) { |
| 403 |
close(mask_fd); |
| 404 |
G_fatal_error("malloc mask_buf failed"); |
| 405 |
} |
| 406 |
mask_sup = G_malloc(ad->cl * sizeof(int)); |
| 407 |
if (mask_sup == NULL) { |
| 408 |
close(mask_fd); |
| 409 |
G_fatal_error("malloc mask_buf failed"); |
| 410 |
} |
| 411 |
for (j = 0; j < ad->cl; j++) |
| 412 |
mask_buf[j] = 0; |
| 413 |
|
| 414 |
masked = TRUE; |
| 415 |
} |
| 416 |
|
| 417 |
/* calculate number of patches */ |
| 418 |
npatch = 0; |
| 419 |
area = 0; |
| 420 |
pid = 0; |
| 421 |
|
| 422 |
/* patch size and type */ |
| 423 |
incr = 1024; |
| 424 |
if (incr > ad->rl) |
| 425 |
incr = ad->rl; |
| 426 |
if (incr > ad->cl) |
| 427 |
incr = ad->cl; |
| 428 |
if (incr < 2) |
| 429 |
incr = 2; |
| 430 |
nalloc = incr; |
| 431 |
pst = G_malloc(nalloc * sizeof(struct pst)); |
| 432 |
for (k = 0; k < nalloc; k++) { |
| 433 |
pst[k].count = 0; |
| 434 |
} |
| 435 |
|
| 436 |
for (i = 0; i < ad->rl; i++) { |
| 437 |
buf = RLI_get_dcell_raster_row(fd, i + ad->y, ad); |
| 438 |
if (i > 0) { |
| 439 |
buf_sup = RLI_get_dcell_raster_row(fd, i - 1 + ad->y, ad); |
| 440 |
} |
| 441 |
|
| 442 |
if (masked) { |
| 443 |
mask_tmp = mask_sup; |
| 444 |
mask_sup = mask_buf; |
| 445 |
mask_buf = mask_tmp; |
| 446 |
if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) { |
| 447 |
res = 0; |
| 448 |
goto free_exit; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
ltmp = pid_sup; |
| 453 |
pid_sup = pid_corr; |
| 454 |
pid_corr = ltmp; |
| 455 |
|
| 456 |
Rast_set_d_null_value(&precCell, 1); |
| 457 |
|
| 458 |
connected = 0; |
| 459 |
for (j = 0; j < ad->cl; j++) { |
| 460 |
pid_corr[j + ad->x] = 0; |
| 461 |
|
| 462 |
corrCell = buf[j + ad->x]; |
| 463 |
if (masked && (mask_buf[j] == 0)) { |
| 464 |
Rast_set_d_null_value(&corrCell, 1); |
| 465 |
} |
| 466 |
|
| 467 |
if (Rast_is_d_null_value(&corrCell)) { |
| 468 |
connected = 0; |
| 469 |
precCell = corrCell; |
| 470 |
continue; |
| 471 |
} |
| 472 |
|
| 473 |
area++; |
| 474 |
|
| 475 |
supCell = buf_sup[j + ad->x]; |
| 476 |
if (masked && (mask_sup[j] == 0)) { |
| 477 |
Rast_set_d_null_value(&supCell, 1); |
| 478 |
} |
| 479 |
|
| 480 |
if (!Rast_is_d_null_value(&precCell) && corrCell == precCell) { |
| 481 |
pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x]; |
| 482 |
connected = 1; |
| 483 |
pst[pid_corr[j + ad->x]].count++; |
| 484 |
} |
| 485 |
else { |
| 486 |
connected = 0; |
| 487 |
} |
| 488 |
|
| 489 |
if (!Rast_is_d_null_value(&supCell) && corrCell == supCell) { |
| 490 |
|
| 491 |
if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) { |
| 492 |
/* connect or merge */ |
| 493 |
/* after r.clump */ |
| 494 |
if (connected) { |
| 495 |
npatch--; |
| 496 |
|
| 497 |
if (npatch == 0) { |
| 498 |
if (masked) |
| 499 |
close(mask_fd); |
| 500 |
G_fatal_error("npatch == 0 at row %d, col %d", i, |
| 501 |
j); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
old_pid = pid_corr[j + ad->x]; |
| 506 |
new_pid = pid_sup[j + ad->x]; |
| 507 |
pid_corr[j + ad->x] = new_pid; |
| 508 |
if (old_pid > 0) { |
| 509 |
/* merge */ |
| 510 |
/* update left side of the current row */ |
| 511 |
for (k = 0; k < j; k++) { |
| 512 |
if (pid_corr[k + ad->x] == old_pid) |
| 513 |
pid_corr[k + ad->x] = new_pid; |
| 514 |
} |
| 515 |
/* update right side of the previous row */ |
| 516 |
for (k = j + 1; k < ad->cl; k++) { |
| 517 |
if (pid_sup[k + ad->x] == old_pid) |
| 518 |
pid_sup[k + ad->x] = new_pid; |
| 519 |
} |
| 520 |
pst[new_pid].count += pst[old_pid].count; |
| 521 |
pst[old_pid].count = 0; |
| 522 |
|
| 523 |
if (old_pid == pid) |
| 524 |
pid--; |
| 525 |
} |
| 526 |
else { |
| 527 |
pst[new_pid].count++; |
| 528 |
} |
| 529 |
} |
| 530 |
connected = 1; |
| 531 |
} |
| 532 |
|
| 533 |
if (!connected) { |
| 534 |
/* start new patch */ |
| 535 |
npatch++; |
| 536 |
pid++; |
| 537 |
pid_corr[j + ad->x] = pid; |
| 538 |
|
| 539 |
if (pid >= nalloc) { |
| 540 |
pst = (struct pst *)G_realloc(pst, (pid + incr) * |
| 541 |
sizeof(struct pst)); |
| 542 |
|
| 543 |
for (k = nalloc; k < pid + incr; k++) |
| 544 |
pst[k].count = 0; |
| 545 |
|
| 546 |
nalloc = pid + incr; |
| 547 |
} |
| 548 |
|
| 549 |
pst[pid].count = 1; |
| 550 |
pst[pid].type.t = CELL_TYPE; |
| 551 |
pst[pid].type.val.c = corrCell; |
| 552 |
} |
| 553 |
precCell = corrCell; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
if (npatch > 0) { |
| 558 |
double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2; |
| 559 |
double area_units; |
| 560 |
double area_p; |
| 561 |
double cell_size_m; |
| 562 |
double mps; |
| 563 |
double psd; |
| 564 |
double pcv; |
| 565 |
double diff; |
| 566 |
|
| 567 |
/* calculate distance */ |
| 568 |
G_begin_distance_calculations(); |
| 569 |
/* EW Dist at North edge */ |
| 570 |
EW_DIST1 = G_distance(hd.east, hd.north, hd.west, hd.north); |
| 571 |
/* EW Dist at South Edge */ |
| 572 |
EW_DIST2 = G_distance(hd.east, hd.south, hd.west, hd.south); |
| 573 |
/* NS Dist at East edge */ |
| 574 |
NS_DIST1 = G_distance(hd.east, hd.north, hd.east, hd.south); |
| 575 |
/* NS Dist at West edge */ |
| 576 |
NS_DIST2 = G_distance(hd.west, hd.north, hd.west, hd.south); |
| 577 |
|
| 578 |
cell_size_m = (((EW_DIST1 + EW_DIST2) / 2) / hd.cols) * |
| 579 |
(((NS_DIST1 + NS_DIST2) / 2) / hd.rows); |
| 580 |
area_units = cell_size_m * area; |
| 581 |
|
| 582 |
/* mean patch size in hectares */ |
| 583 |
mps = area_units / (npatch * 10000); |
| 584 |
|
| 585 |
/* calculate patch size standard deviation */ |
| 586 |
psd = 0; |
| 587 |
for (old_pid = 1; old_pid <= pid; old_pid++) { |
| 588 |
if (pst[old_pid].count > 0) { |
| 589 |
area_p = cell_size_m * pst[old_pid].count / 10000; |
| 590 |
diff = area_p - mps; |
| 591 |
psd += diff * diff; |
| 592 |
} |
| 593 |
} |
| 594 |
psd = sqrt(psd / npatch); |
| 595 |
pcv = psd * 100 / mps; |
| 596 |
*result = pcv; |
| 597 |
} |
| 598 |
else |
| 599 |
Rast_set_d_null_value(result, 1); |
| 600 |
|
| 601 |
free_exit: |
| 602 |
if (masked) { |
| 603 |
close(mask_fd); |
| 604 |
G_free(mask_buf); |
| 605 |
G_free(mask_sup); |
| 606 |
} |
| 607 |
G_free(buf_null); |
| 608 |
G_free(pid_corr); |
| 609 |
G_free(pid_sup); |
| 610 |
G_free(pst); |
| 611 |
|
| 612 |
return res; |
| 613 |
} |
| 614 |
|
| 615 |
int calculateF(int fd, struct area_entry *ad, double *result) |
| 616 |
{ |
| 617 |
FCELL *buf, *buf_sup, *buf_null; |
| 618 |
FCELL corrCell, precCell, supCell; |
| 619 |
long npatch, area; |
| 620 |
long pid, old_pid, new_pid, *pid_corr, *pid_sup, *ltmp; |
| 621 |
struct pst *pst; |
| 622 |
long nalloc, incr; |
| 623 |
int i, j, k; |
| 624 |
int connected; |
| 625 |
int res = RLI_OK; |
| 626 |
int mask_fd, *mask_buf, *mask_sup, *mask_tmp, masked; |
| 627 |
struct Cell_head hd; |
| 628 |
|
| 629 |
Rast_get_window(&hd); |
| 630 |
|
| 631 |
buf_null = Rast_allocate_f_buf(); |
| 632 |
Rast_set_f_null_value(buf_null, Rast_window_cols()); |
| 633 |
buf_sup = buf_null; |
| 634 |
|
| 635 |
/* initialize patch ids */ |
| 636 |
pid_corr = G_malloc(Rast_window_cols() * sizeof(long)); |
| 637 |
pid_sup = G_malloc(Rast_window_cols() * sizeof(long)); |
| 638 |
|
| 639 |
for (j = 0; j < Rast_window_cols(); j++) { |
| 640 |
pid_corr[j] = 0; |
| 641 |
pid_sup[j] = 0; |
| 642 |
} |
| 643 |
|
| 644 |
/* open mask if needed */ |
| 645 |
mask_fd = -1; |
| 646 |
mask_buf = mask_sup = NULL; |
| 647 |
masked = FALSE; |
| 648 |
if (ad->mask == 1) { |
| 649 |
if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0) { |
| 650 |
G_free(buf_null); |
| 651 |
G_free(pid_corr); |
| 652 |
G_free(pid_sup); |
| 653 |
return RLI_ERRORE; |
| 654 |
} |
| 655 |
mask_buf = G_malloc(ad->cl * sizeof(int)); |
| 656 |
if (mask_buf == NULL) { |
| 657 |
close(mask_fd); |
| 658 |
G_fatal_error("malloc mask_buf failed"); |
| 659 |
} |
| 660 |
mask_sup = G_malloc(ad->cl * sizeof(int)); |
| 661 |
if (mask_sup == NULL) { |
| 662 |
close(mask_fd); |
| 663 |
G_fatal_error("malloc mask_buf failed"); |
| 664 |
} |
| 665 |
for (j = 0; j < ad->cl; j++) |
| 666 |
mask_buf[j] = 0; |
| 667 |
|
| 668 |
masked = TRUE; |
| 669 |
} |
| 670 |
|
| 671 |
/* calculate number of patches */ |
| 672 |
npatch = 0; |
| 673 |
area = 0; |
| 674 |
pid = 0; |
| 675 |
|
| 676 |
/* patch size and type */ |
| 677 |
incr = 1024; |
| 678 |
if (incr > ad->rl) |
| 679 |
incr = ad->rl; |
| 680 |
if (incr > ad->cl) |
| 681 |
incr = ad->cl; |
| 682 |
if (incr < 2) |
| 683 |
incr = 2; |
| 684 |
nalloc = incr; |
| 685 |
pst = G_malloc(nalloc * sizeof(struct pst)); |
| 686 |
for (k = 0; k < nalloc; k++) { |
| 687 |
pst[k].count = 0; |
| 688 |
} |
| 689 |
|
| 690 |
for (i = 0; i < ad->rl; i++) { |
| 691 |
buf = RLI_get_fcell_raster_row(fd, i + ad->y, ad); |
| 692 |
if (i > 0) { |
| 693 |
buf_sup = RLI_get_fcell_raster_row(fd, i - 1 + ad->y, ad); |
| 694 |
} |
| 695 |
|
| 696 |
if (masked) { |
| 697 |
mask_tmp = mask_sup; |
| 698 |
mask_sup = mask_buf; |
| 699 |
mask_buf = mask_tmp; |
| 700 |
if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) { |
| 701 |
res = 0; |
| 702 |
goto free_exit; |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
ltmp = pid_sup; |
| 707 |
pid_sup = pid_corr; |
| 708 |
pid_corr = ltmp; |
| 709 |
|
| 710 |
Rast_set_f_null_value(&precCell, 1); |
| 711 |
|
| 712 |
connected = 0; |
| 713 |
for (j = 0; j < ad->cl; j++) { |
| 714 |
pid_corr[j + ad->x] = 0; |
| 715 |
|
| 716 |
corrCell = buf[j + ad->x]; |
| 717 |
if (masked && (mask_buf[j] == 0)) { |
| 718 |
Rast_set_f_null_value(&corrCell, 1); |
| 719 |
} |
| 720 |
|
| 721 |
if (Rast_is_f_null_value(&corrCell)) { |
| 722 |
connected = 0; |
| 723 |
precCell = corrCell; |
| 724 |
continue; |
| 725 |
} |
| 726 |
|
| 727 |
area++; |
| 728 |
|
| 729 |
supCell = buf_sup[j + ad->x]; |
| 730 |
if (masked && (mask_sup[j] == 0)) { |
| 731 |
Rast_set_f_null_value(&supCell, 1); |
| 732 |
} |
| 733 |
|
| 734 |
if (!Rast_is_f_null_value(&precCell) && corrCell == precCell) { |
| 735 |
pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x]; |
| 736 |
connected = 1; |
| 737 |
pst[pid_corr[j + ad->x]].count++; |
| 738 |
} |
| 739 |
else { |
| 740 |
connected = 0; |
| 741 |
} |
| 742 |
|
| 743 |
if (!Rast_is_f_null_value(&supCell) && corrCell == supCell) { |
| 744 |
|
| 745 |
if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) { |
| 746 |
/* connect or merge */ |
| 747 |
/* after r.clump */ |
| 748 |
if (connected) { |
| 749 |
npatch--; |
| 750 |
|
| 751 |
if (npatch == 0) { |
| 752 |
if (masked) |
| 753 |
close(mask_fd); |
| 754 |
G_fatal_error("npatch == 0 at row %d, col %d", i, |
| 755 |
j); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
old_pid = pid_corr[j + ad->x]; |
| 760 |
new_pid = pid_sup[j + ad->x]; |
| 761 |
pid_corr[j + ad->x] = new_pid; |
| 762 |
if (old_pid > 0) { |
| 763 |
/* merge */ |
| 764 |
/* update left side of the current row */ |
| 765 |
for (k = 0; k < j; k++) { |
| 766 |
if (pid_corr[k + ad->x] == old_pid) |
| 767 |
pid_corr[k + ad->x] = new_pid; |
| 768 |
} |
| 769 |
/* update right side of the previous row */ |
| 770 |
for (k = j + 1; k < ad->cl; k++) { |
| 771 |
if (pid_sup[k + ad->x] == old_pid) |
| 772 |
pid_sup[k + ad->x] = new_pid; |
| 773 |
} |
| 774 |
pst[new_pid].count += pst[old_pid].count; |
| 775 |
pst[old_pid].count = 0; |
| 776 |
|
| 777 |
if (old_pid == pid) |
| 778 |
pid--; |
| 779 |
} |
| 780 |
else { |
| 781 |
pst[new_pid].count++; |
| 782 |
} |
| 783 |
} |
| 784 |
connected = 1; |
| 785 |
} |
| 786 |
|
| 787 |
if (!connected) { |
| 788 |
/* start new patch */ |
| 789 |
npatch++; |
| 790 |
pid++; |
| 791 |
pid_corr[j + ad->x] = pid; |
| 792 |
|
| 793 |
if (pid >= nalloc) { |
| 794 |
pst = (struct pst *)G_realloc(pst, (pid + incr) * |
| 795 |
sizeof(struct pst)); |
| 796 |
|
| 797 |
for (k = nalloc; k < pid + incr; k++) |
| 798 |
pst[k].count = 0; |
| 799 |
|
| 800 |
nalloc = pid + incr; |
| 801 |
} |
| 802 |
|
| 803 |
pst[pid].count = 1; |
| 804 |
pst[pid].type.t = CELL_TYPE; |
| 805 |
pst[pid].type.val.c = corrCell; |
| 806 |
} |
| 807 |
precCell = corrCell; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
if (npatch > 0) { |
| 812 |
double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2; |
| 813 |
double area_units; |
| 814 |
double area_p; |
| 815 |
double cell_size_m; |
| 816 |
double mps; |
| 817 |
double psd; |
| 818 |
double pcv; |
| 819 |
double diff; |
| 820 |
|
| 821 |
/* calculate distance */ |
| 822 |
G_begin_distance_calculations(); |
| 823 |
/* EW Dist at North edge */ |
| 824 |
EW_DIST1 = G_distance(hd.east, hd.north, hd.west, hd.north); |
| 825 |
/* EW Dist at South Edge */ |
| 826 |
EW_DIST2 = G_distance(hd.east, hd.south, hd.west, hd.south); |
| 827 |
/* NS Dist at East edge */ |
| 828 |
NS_DIST1 = G_distance(hd.east, hd.north, hd.east, hd.south); |
| 829 |
/* NS Dist at West edge */ |
| 830 |
NS_DIST2 = G_distance(hd.west, hd.north, hd.west, hd.south); |
| 831 |
|
| 832 |
cell_size_m = (((EW_DIST1 + EW_DIST2) / 2) / hd.cols) * |
| 833 |
(((NS_DIST1 + NS_DIST2) / 2) / hd.rows); |
| 834 |
area_units = cell_size_m * area; |
| 835 |
|
| 836 |
/* mean patch size in hectares */ |
| 837 |
mps = area_units / (npatch * 10000); |
| 838 |
|
| 839 |
/* calculate patch size standard deviation */ |
| 840 |
psd = 0; |
| 841 |
for (old_pid = 1; old_pid <= pid; old_pid++) { |
| 842 |
if (pst[old_pid].count > 0) { |
| 843 |
area_p = cell_size_m * pst[old_pid].count / 10000; |
| 844 |
diff = area_p - mps; |
| 845 |
psd += diff * diff; |
| 846 |
} |
| 847 |
} |
| 848 |
psd = sqrt(psd / npatch); |
| 849 |
pcv = psd * 100 / mps; |
| 850 |
*result = pcv; |
| 851 |
} |
| 852 |
else |
| 853 |
Rast_set_d_null_value(result, 1); |
| 854 |
|
| 855 |
free_exit: |
| 856 |
if (masked) { |
| 857 |
close(mask_fd); |
| 858 |
G_free(mask_buf); |
| 859 |
G_free(mask_sup); |
| 860 |
} |
| 861 |
G_free(buf_null); |
| 862 |
G_free(pid_corr); |
| 863 |
G_free(pid_sup); |
| 864 |
G_free(pst); |
| 865 |
|
| 866 |
return res; |
| 867 |
} |