Debian Codegraph @

raster/r.li/r.li.dominance/dominance.c

raw
1 /****************************************************************************
2 *
3 * MODULE: r.li.dominance
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 * Rewrite: Markus Metz
8 *
9 * PURPOSE: calculates dominance diversity index
10 * COPYRIGHT: (C) 2007-2014 by the GRASS Development Team
11 *
12 * This program is free software under the GNU General Public
13 * License (>=v2). Read the file COPYING that comes with GRASS
14 * for details.
15 *
16 *****************************************************************************/
17
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <math.h>
21
22 #include <grass/gis.h>
23 #include <grass/raster.h>
24 #include <grass/glocale.h>
25
26 #include "../r.li.daemon/daemon.h"
27 #include "../r.li.daemon/avlDefs.h"
28 #include "../r.li.daemon/avl.h"
29
30 /* template is shannon */
31
32 rli_func dominance;
33 int calculate(int fd, struct area_entry *ad, double *result);
34 int calculateD(int fd, struct area_entry *ad, double *result);
35 int calculateF(int fd, struct area_entry *ad, double *result);
36
37 int main(int argc, char *argv[])
38 {
39 struct Option *raster, *conf, *output;
40 struct GModule *module;
41
42 G_gisinit(argv[0]);
43 module = G_define_module();
44 module->description =
45 _("Calculates dominance's diversity index on a raster map");
46 G_add_keyword(_("raster"));
47 G_add_keyword(_("landscape structure analysis"));
48 G_add_keyword(_("diversity index"));
49
50 /* define options */
51
52 raster = G_define_standard_option(G_OPT_R_INPUT);
53
54 conf = G_define_standard_option(G_OPT_F_INPUT);
55 conf->key = "config";
56 conf->description = _("Configuration file");
57 conf->required = YES;
58
59 output = G_define_standard_option(G_OPT_R_OUTPUT);
60
61 if (G_parser(argc, argv))
62 exit(EXIT_FAILURE);
63
64 return calculateIndex(conf->answer, dominance, NULL, raster->answer,
65 output->answer);
66 }
67
68 int dominance(int fd, char **par G_UNUSED, struct area_entry *ad,
69 double *result)
70 {
71 int ris = RLI_OK;
72 double indice = 0;
73
74 switch (ad->data_type) {
75 case CELL_TYPE: {
76 ris = calculate(fd, ad, &indice);
77 break;
78 }
79 case DCELL_TYPE: {
80 ris = calculateD(fd, ad, &indice);
81 break;
82 }
83 case FCELL_TYPE: {
84 ris = calculateF(fd, ad, &indice);
85 break;
86 }
87 default: {
88 G_fatal_error("data type unknown");
89 }
90 }
91
92 if (ris != RLI_OK) {
93 return RLI_ERRORE;
94 }
95
96 *result = indice;
97
98 return RLI_OK;
99 }
100
101 int calculate(int fd, struct area_entry *ad, double *result)
102 {
103 CELL *buf;
104 CELL corrCell;
105 CELL precCell;
106
107 int i, j;
108 int mask_fd = -1, *mask_buf = NULL;
109 int ris = 0;
110 int masked = FALSE;
111 int res = RLI_OK;
112
113 long m = 0;
114 long tot = 0;
115 long zero = 0;
116 long totCorr = 1;
117
118 long area = 0;
119
120 avl_tree albero = NULL;
121 AVL_table array;
122 generic_cell uc;
123
124 uc.t = CELL_TYPE;
125
126 /* open mask if needed */
127 if (ad->mask == 1) {
128 if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
129 return RLI_ERRORE;
130 mask_buf = G_malloc(ad->cl * sizeof(int));
131 if (mask_buf == NULL) {
132 close(mask_fd);
133 G_fatal_error("malloc mask_buf failed");
134 }
135 masked = TRUE;
136 }
137
138 Rast_set_c_null_value(&precCell, 1);
139 for (j = 0; j < ad->rl; j++) { /* for each row */
140 if (masked) {
141 if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
142 close(mask_fd);
143 G_fatal_error("mask read failed");
144 }
145 }
146
147 buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
148
149 for (i = 0; i < ad->cl; i++) { /* for each cell in the row */
150 corrCell = buf[i + ad->x];
151
152 if ((masked) && (mask_buf[i] == 0)) {
153 Rast_set_c_null_value(&corrCell, 1);
154 }
155
156 if (!(Rast_is_null_value(&corrCell, uc.t))) {
157 /* total patch area */
158 area++;
159 }
160
161 if (!(Rast_is_null_value(&precCell, uc.t)) &&
162 corrCell == precCell) {
163
164 totCorr++;
165 }
166 else if (!(Rast_is_null_value(&precCell, uc.t)) &&
167 corrCell != precCell) {
168
169 /* add precCell to search tree */
170 if (albero == NULL) {
171 uc.val.c = precCell;
172 albero = avl_make(uc, totCorr);
173 if (albero == NULL) {
174 if (masked)
175 close(mask_fd);
176 G_fatal_error("avl_make error");
177 }
178 m++;
179 }
180 else {
181 uc.val.c = precCell;
182 ris = avl_add(&albero, uc, totCorr);
183 switch (ris) {
184 case AVL_ERR: {
185 if (masked)
186 close(mask_fd);
187 G_fatal_error("avl_add error");
188 }
189 case AVL_ADD: {
190 m++;
191 break;
192 }
193 case AVL_PRES: {
194 break;
195 }
196 default: {
197 if (masked)
198 close(mask_fd);
199 G_fatal_error("avl_make unknown error");
200 }
201 }
202 }
203 totCorr = 1;
204 } /* endif not equal cells */
205 precCell = corrCell;
206 }
207 }
208
209 /* last closing */
210 if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
211 if (albero == NULL) {
212 uc.val.c = precCell;
213 albero = avl_make(uc, totCorr);
214 if (albero == NULL) {
215 if (masked)
216 close(mask_fd);
217 G_fatal_error("avl_make error");
218 }
219 m++;
220 }
221 else {
222 uc.val.c = precCell;
223 ris = avl_add(&albero, uc, totCorr);
224 switch (ris) {
225 case AVL_ERR: {
226 if (masked)
227 close(mask_fd);
228 G_fatal_error("avl_add error");
229 }
230 case AVL_ADD: {
231 m++;
232 break;
233 }
234 case AVL_PRES: {
235 break;
236 }
237 default: {
238 if (masked)
239 close(mask_fd);
240 G_fatal_error("avl_add unknown error");
241 }
242 }
243 }
244 }
245
246 if (area > 0) {
247 double t;
248 double shannon;
249 double perc, logarithm;
250
251 array = G_malloc(m * sizeof(AVL_tableRow));
252 if (array == NULL) {
253 if (masked)
254 close(mask_fd);
255 G_fatal_error("malloc array failed");
256 }
257 tot = avl_to_array(albero, zero, array);
258 if (tot != m) {
259 G_warning(
260 "avl_to_array unexpected value. the result could be wrong");
261 res = RLI_ERRORE;
262 G_free(array);
263 goto free_exit;
264 }
265
266 /* calculate shannon */
267 shannon = 0;
268 for (i = 0; i < m; i++) {
269 t = array[i].tot;
270 perc = t / area;
271 logarithm = log(perc);
272 shannon += perc * logarithm;
273 }
274 G_free(array);
275
276 *result = log(m) + shannon;
277 }
278 else
279 Rast_set_d_null_value(result, 1);
280
281 free_exit:
282 avl_destroy(albero);
283 if (masked) {
284 close(mask_fd);
285 G_free(mask_buf);
286 }
287
288 return res;
289 }
290
291 int calculateD(int fd, struct area_entry *ad, double *result)
292 {
293 DCELL *buf;
294 DCELL corrCell;
295 DCELL precCell;
296
297 int i, j;
298 int mask_fd = -1, *mask_buf = NULL;
299 int ris = 0;
300 int masked = FALSE;
301 int res = RLI_OK;
302
303 long m = 0;
304 long tot = 0;
305 long zero = 0;
306 long totCorr = 1;
307
308 long area = 0;
309
310 avl_tree albero = NULL;
311 AVL_table array;
312 generic_cell uc;
313
314 uc.t = DCELL_TYPE;
315
316 /* open mask if needed */
317 if (ad->mask == 1) {
318 if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
319 return RLI_ERRORE;
320 mask_buf = G_malloc(ad->cl * sizeof(int));
321 if (mask_buf == NULL) {
322 close(mask_fd);
323 G_fatal_error("malloc mask_buf failed");
324 }
325 masked = TRUE;
326 }
327
328 Rast_set_d_null_value(&precCell, 1);
329 for (j = 0; j < ad->rl; j++) { /* for each row */
330 if (masked) {
331 if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
332 close(mask_fd);
333 G_fatal_error("mask read failed");
334 }
335 }
336
337 buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
338
339 for (i = 0; i < ad->cl; i++) { /* for each cell in the row */
340 corrCell = buf[i + ad->x];
341
342 if ((masked) && (mask_buf[i] == 0)) {
343 Rast_set_d_null_value(&corrCell, 1);
344 }
345
346 if (!(Rast_is_null_value(&corrCell, uc.t))) {
347 /* total patch area */
348 area++;
349 }
350
351 if (!(Rast_is_null_value(&precCell, uc.t)) &&
352 corrCell == precCell) {
353
354 totCorr++;
355 }
356 else if (!(Rast_is_null_value(&precCell, uc.t)) &&
357 corrCell != precCell) {
358
359 /* add precCell to search tree */
360 if (albero == NULL) {
361 uc.val.dc = precCell;
362 albero = avl_make(uc, totCorr);
363 if (albero == NULL) {
364 if (masked)
365 close(mask_fd);
366 G_fatal_error("avl_make error");
367 }
368 m++;
369 }
370 else {
371 uc.val.dc = precCell;
372 ris = avl_add(&albero, uc, totCorr);
373 switch (ris) {
374 case AVL_ERR: {
375 if (masked)
376 close(mask_fd);
377 G_fatal_error("avl_add error");
378 }
379 case AVL_ADD: {
380 m++;
381 break;
382 }
383 case AVL_PRES: {
384 break;
385 }
386 default: {
387 if (masked)
388 close(mask_fd);
389 G_fatal_error("avl_make unknown error");
390 }
391 }
392 }
393 totCorr = 1;
394 } /* endif not equal cells */
395 precCell = corrCell;
396 }
397 }
398
399 /* last closing */
400 if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
401 if (albero == NULL) {
402 uc.val.dc = precCell;
403 albero = avl_make(uc, totCorr);
404 if (albero == NULL) {
405 if (masked)
406 close(mask_fd);
407 G_fatal_error("avl_make error");
408 }
409 m++;
410 }
411 else {
412 uc.val.dc = precCell;
413 ris = avl_add(&albero, uc, totCorr);
414 switch (ris) {
415 case AVL_ERR: {
416 if (masked)
417 close(mask_fd);
418 G_fatal_error("avl_add error");
419 }
420 case AVL_ADD: {
421 m++;
422 break;
423 }
424 case AVL_PRES: {
425 break;
426 }
427 default: {
428 if (masked)
429 close(mask_fd);
430 G_fatal_error("avl_add unknown error");
431 }
432 }
433 }
434 }
435
436 if (area > 0) {
437 double t;
438 double shannon;
439 double perc, logarithm;
440
441 array = G_malloc(m * sizeof(AVL_tableRow));
442 if (array == NULL) {
443 if (masked)
444 close(mask_fd);
445 G_fatal_error("malloc array failed");
446 }
447 tot = avl_to_array(albero, zero, array);
448 if (tot != m) {
449 G_warning(
450 "avl_to_array unexpected value. the result could be wrong");
451 res = RLI_ERRORE;
452 G_free(array);
453 goto free_exit;
454 }
455
456 /* calculate shannon */
457 shannon = 0;
458 for (i = 0; i < m; i++) {
459 t = array[i].tot;
460 perc = t / area;
461 logarithm = log(perc);
462 shannon += perc * logarithm;
463 }
464 G_free(array);
465
466 *result = log(m) + shannon;
467 }
468 else
469 Rast_set_d_null_value(result, 1);
470
471 free_exit:
472 avl_destroy(albero);
473 if (masked) {
474 close(mask_fd);
475 G_free(mask_buf);
476 }
477
478 return res;
479 }
480
481 int calculateF(int fd, struct area_entry *ad, double *result)
482 {
483 FCELL *buf;
484 FCELL corrCell;
485 FCELL precCell;
486
487 int i, j;
488 int mask_fd = -1, *mask_buf = NULL;
489 int ris = 0;
490 int masked = FALSE;
491 int res = RLI_OK;
492
493 long m = 0;
494 long tot = 0;
495 long zero = 0;
496 long totCorr = 1;
497
498 long area = 0;
499
500 avl_tree albero = NULL;
501 AVL_table array;
502 generic_cell uc;
503
504 uc.t = FCELL_TYPE;
505
506 /* open mask if needed */
507 if (ad->mask == 1) {
508 if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
509 return RLI_ERRORE;
510 mask_buf = G_malloc(ad->cl * sizeof(int));
511 if (mask_buf == NULL) {
512 close(mask_fd);
513 G_fatal_error("malloc mask_buf failed");
514 }
515 masked = TRUE;
516 }
517
518 Rast_set_f_null_value(&precCell, 1);
519 for (j = 0; j < ad->rl; j++) { /* for each row */
520 if (masked) {
521 if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
522 close(mask_fd);
523 G_fatal_error("mask read failed");
524 }
525 }
526
527 buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
528
529 for (i = 0; i < ad->cl; i++) { /* for each cell in the row */
530 corrCell = buf[i + ad->x];
531
532 if ((masked) && (mask_buf[i] == 0)) {
533 Rast_set_f_null_value(&corrCell, 1);
534 }
535
536 if (!(Rast_is_null_value(&corrCell, uc.t))) {
537 /* total patch area */
538 area++;
539 }
540
541 if (!(Rast_is_null_value(&precCell, uc.t)) &&
542 corrCell == precCell) {
543
544 totCorr++;
545 }
546 else if (!(Rast_is_null_value(&precCell, uc.t)) &&
547 corrCell != precCell) {
548
549 /* add precCell to search tree */
550 if (albero == NULL) {
551 uc.val.fc = precCell;
552 albero = avl_make(uc, totCorr);
553 if (albero == NULL) {
554 if (masked)
555 close(mask_fd);
556 G_fatal_error("avl_make error");
557 }
558 m++;
559 }
560 else {
561 uc.val.fc = precCell;
562 ris = avl_add(&albero, uc, totCorr);
563 switch (ris) {
564 case AVL_ERR: {
565 if (masked)
566 close(mask_fd);
567 G_fatal_error("avl_add error");
568 }
569 case AVL_ADD: {
570 m++;
571 break;
572 }
573 case AVL_PRES: {
574 break;
575 }
576 default: {
577 if (masked)
578 close(mask_fd);
579 G_fatal_error("avl_make unknown error");
580 }
581 }
582 }
583 totCorr = 1;
584 } /* endif not equal cells */
585 precCell = corrCell;
586 }
587 }
588
589 /* last closing */
590 if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
591 if (albero == NULL) {
592 uc.val.fc = precCell;
593 albero = avl_make(uc, totCorr);
594 if (albero == NULL) {
595 if (masked)
596 close(mask_fd);
597 G_fatal_error("avl_make error");
598 }
599 m++;
600 }
601 else {
602 uc.val.fc = precCell;
603 ris = avl_add(&albero, uc, totCorr);
604 switch (ris) {
605 case AVL_ERR: {
606 if (masked)
607 close(mask_fd);
608 G_fatal_error("avl_add error");
609 }
610 case AVL_ADD: {
611 m++;
612 break;
613 }
614 case AVL_PRES: {
615 break;
616 }
617 default: {
618 if (masked)
619 close(mask_fd);
620 G_fatal_error("avl_add unknown error");
621 }
622 }
623 }
624 }
625
626 if (area > 0) {
627 double t;
628 double shannon;
629 double perc, logarithm;
630
631 array = G_malloc(m * sizeof(AVL_tableRow));
632 if (array == NULL) {
633 if (masked)
634 close(mask_fd);
635 G_fatal_error("malloc array failed");
636 }
637 tot = avl_to_array(albero, zero, array);
638 if (tot != m) {
639 G_warning(
640 "avl_to_array unexpected value. the result could be wrong");
641 res = RLI_ERRORE;
642 G_free(array);
643 goto free_exit;
644 }
645
646 /* calculate shannon */
647 shannon = 0;
648 for (i = 0; i < m; i++) {
649 t = array[i].tot;
650 perc = t / area;
651 logarithm = log(perc);
652 shannon += perc * logarithm;
653 }
654 G_free(array);
655
656 *result = log(m) + shannon;
657 }
658 else
659 Rast_set_d_null_value(result, 1);
660
661 free_exit:
662 avl_destroy(albero);
663 if (masked) {
664 close(mask_fd);
665 G_free(mask_buf);
666 }
667
668 return res;
669 }