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