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