123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732 |
- /****************************************************************************
- *
- * MODULE: r.li.renyi
- *
- * PURPOSE: calculates renyi's diversity index
- *
- * AUTHOR(S): Luca Delucchi, Fondazione Edmund Mach
- * Serena Pallecchi student of Computer Science University of Pisa (Italy)
- * Rewrite: Markus Metz
- *
- * COPYRIGHT:
- * This program is free software under the GPL (>=v2)
- * Read the COPYING file that comes with GRASS for details.
- *
- ***************************************************************************/
- #include <stdlib.h>
- #include <fcntl.h>
- #include <math.h>
- #include <grass/gis.h>
- #include <grass/raster.h>
- #include <grass/glocale.h>
- #include "../r.li.daemon/daemon.h"
- #include "../r.li.daemon/avlDefs.h"
- #include "../r.li.daemon/avl.h"
- /* template is shannon */
- rli_func renyi;
- int calculate(int fd, struct area_entry *ad, char **par, double *result);
- int calculateD(int fd, struct area_entry *ad, char **par, double *result);
- int calculateF(int fd, struct area_entry *ad, char **par, double *result);
- int main(int argc, char *argv[])
- {
- struct Option *raster, *conf, *output, *alpha;
- struct GModule *module;
- char **par = NULL;
- G_gisinit(argv[0]);
- module = G_define_module();
- module->description =
- _("Calculates Renyi's diversity index on a raster map");
- G_add_keyword(_("raster"));
- G_add_keyword(_("landscape structure analysis"));
- G_add_keyword(_("diversity index"));
- /* define options */
- raster = G_define_standard_option(G_OPT_R_INPUT);
- conf = G_define_standard_option(G_OPT_F_INPUT);
- conf->key = "config";
- conf->description = _("Configuration file");
- conf->required = YES;
- alpha = G_define_option();
- alpha->key = "alpha";
- alpha->description =
- _("Alpha value is the order of the generalized entropy");
- alpha->type = TYPE_STRING;
- alpha->required = YES;
- output = G_define_standard_option(G_OPT_R_OUTPUT);
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
- if (atof(alpha->answer) == 1) {
- G_fatal_error
- ("If alpha = 1 Renyi index is not defined. (Ricotta et al., 2003, Environ. Model. Softw.)");
- exit(RLI_ERRORE);
- }
- else if (atof(alpha->answer) < 0.) {
- G_fatal_error
- ("Alpha must be > 0 otherwise Renyi index is not defined. (Ricotta et al., 2003, Environ. Model. Softw.)");
- exit(RLI_ERRORE);
- }
- else {
- par = &alpha->answer;
- }
- return calculateIndex(conf->answer, renyi, par, raster->answer,
- output->answer);
- }
- int renyi(int fd, char **par, struct area_entry *ad, double *result)
- {
- int ris = RLI_OK;
- double indice = 0;
- if (!par)
- G_fatal_error("par is NULL");
- switch (ad->data_type) {
- case CELL_TYPE:
- {
- ris = calculate(fd, ad, par, &indice);
- break;
- }
- case DCELL_TYPE:
- {
- ris = calculateD(fd, ad, par, &indice);
- break;
- }
- case FCELL_TYPE:
- {
- ris = calculateF(fd, ad, par, &indice);
- break;
- }
- default:
- {
- G_fatal_error("data type unknown");
- return RLI_ERRORE;
- }
- }
- if (ris != RLI_OK) {
- return RLI_ERRORE;
- }
- *result = indice;
- return RLI_OK;
- }
- int calculate(int fd, struct area_entry *ad, char **par, double *result)
- {
- CELL *buf;
- CELL corrCell;
- CELL precCell;
- int i, j;
- int mask_fd = -1, *mask_buf = NULL;
- int ris = 0;
- int masked = FALSE;
- long m = 0;
- long tot = 0;
- long zero = 0;
- long totCorr = 1;
- long area = 0;
- avl_tree albero = NULL;
- AVL_table array;
- generic_cell uc;
- uc.t = CELL_TYPE;
- /* open mask if needed */
- if (ad->mask == 1) {
- if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
- return RLI_ERRORE;
- mask_buf = G_malloc(ad->cl * sizeof(int));
- if (mask_buf == NULL) {
- G_fatal_error("malloc mask_buf failed");
- return RLI_ERRORE;
- }
- masked = TRUE;
- }
- Rast_set_c_null_value(&precCell, 1);
- for (j = 0; j < ad->rl; j++) { /* for each row */
- if (masked) {
- if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
- G_fatal_error("mask read failed");
- return RLI_ERRORE;
- }
- }
- buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
- for (i = 0; i < ad->cl; i++) { /* for each cell in the row */
- corrCell = buf[i + ad->x];
- if ((masked) && (mask_buf[i] == 0)) {
- Rast_set_c_null_value(&corrCell, 1);
- }
- if (!(Rast_is_null_value(&corrCell, uc.t))) {
- /* total patch area */
- area++;
- }
- if (!(Rast_is_null_value(&precCell, uc.t)) &&
- corrCell == precCell) {
- totCorr++;
- }
- else if (!(Rast_is_null_value(&precCell, uc.t)) &&
- corrCell != precCell) {
-
- /* add precCell to search tree */
- if (albero == NULL) {
- uc.val.c = precCell;
- albero = avl_make(uc, totCorr);
- if (albero == NULL) {
- G_fatal_error("avl_make error");
- return RLI_ERRORE;
- }
- m++;
- }
- else {
- uc.val.c = precCell;
- ris = avl_add(&albero, uc, totCorr);
- switch (ris) {
- case AVL_ERR:
- {
- G_fatal_error("avl_add error");
- return RLI_ERRORE;
- }
- case AVL_ADD:
- {
- m++;
- break;
- }
- case AVL_PRES:
- {
- break;
- }
- default:
- {
- G_fatal_error("avl_make unknown error");
- return RLI_ERRORE;
- }
- }
- }
- totCorr = 1;
- } /* endif not equal cells */
- precCell = corrCell;
- }
- }
- /* last closing */
- if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
- if (albero == NULL) {
- uc.val.c = precCell;
- albero = avl_make(uc, totCorr);
- if (albero == NULL) {
- G_fatal_error("avl_make error");
- return RLI_ERRORE;
- }
- m++;
- }
- else {
- uc.val.c = precCell;
- ris = avl_add(&albero, uc, totCorr);
- switch (ris) {
- case AVL_ERR:
- {
- G_fatal_error("avl_add error");
- return RLI_ERRORE;
- }
- case AVL_ADD:
- {
- m++;
- break;
- }
- case AVL_PRES:
- {
- break;
- }
- default:
- {
- G_fatal_error("avl_add unknown error");
- return RLI_ERRORE;
- }
- }
- }
- }
- if (area > 0) {
- double alpha;
- double pi;
- double t;
- double sum, sum2;
- alpha = atof(par[0]);
- array = G_malloc(m * sizeof(AVL_tableRow));
- if (array == NULL) {
- G_fatal_error("malloc array failed");
- return RLI_ERRORE;
- }
- tot = avl_to_array(albero, zero, array);
- if (tot != m) {
- G_warning("avl_to_array unexpected value. the result could be wrong");
- return RLI_ERRORE;
- }
- /* calculate renyi index */
- sum = 0;
- sum2 = 0;
- for (i = 0; i < m; i++) {
- t = array[i].tot;
- pi = t / area;
- sum += pow(pi, alpha);
- sum2 += pi;
- }
- G_free(array);
- /* correction for numerical instability */
- if (sum2 != 1)
- sum += 1 - sum2;
- if ((alpha < 1 && sum < 1) || (alpha > 1 && sum > 1)) {
- G_warning("Renyi index calculation reached numerical instability. "
- "This can happen with alpha close to 1. The result will be set to zero.");
- sum = 1;
- }
- *result = (1 / (1 - alpha)) * log(sum);
- }
- else
- Rast_set_d_null_value(result, 1);
- avl_destroy(albero);
- if (masked) {
- close(mask_fd);
- G_free(mask_buf);
- }
- return RLI_OK;
- }
- int calculateD(int fd, struct area_entry *ad, char **par, double *result)
- {
- DCELL *buf;
- DCELL corrCell;
- DCELL precCell;
- int i, j;
- int mask_fd = -1, *mask_buf = NULL;
- int ris = 0;
- int masked = FALSE;
- long m = 0;
- long tot = 0;
- long zero = 0;
- long totCorr = 1;
- long area = 0;
- avl_tree albero = NULL;
- AVL_table array;
- generic_cell uc;
- uc.t = DCELL_TYPE;
- /* open mask if needed */
- if (ad->mask == 1) {
- if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
- return RLI_ERRORE;
- mask_buf = G_malloc(ad->cl * sizeof(int));
- if (mask_buf == NULL) {
- G_fatal_error("malloc mask_buf failed");
- return RLI_ERRORE;
- }
- masked = TRUE;
- }
- Rast_set_d_null_value(&precCell, 1);
- for (j = 0; j < ad->rl; j++) { /* for each row */
- if (masked) {
- if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
- G_fatal_error("mask read failed");
- return RLI_ERRORE;
- }
- }
- buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
- for (i = 0; i < ad->cl; i++) { /* for each cell in the row */
- corrCell = buf[i + ad->x];
- if ((masked) && (mask_buf[i] == 0)) {
- Rast_set_d_null_value(&corrCell, 1);
- }
- if (!(Rast_is_null_value(&corrCell, uc.t))) {
- /* total patch area */
- area++;
- }
- if (!(Rast_is_null_value(&precCell, uc.t)) &&
- corrCell == precCell) {
- totCorr++;
- }
- else if (!(Rast_is_null_value(&precCell, uc.t)) &&
- corrCell != precCell) {
-
- /* add precCell to search tree */
- if (albero == NULL) {
- uc.val.dc = precCell;
- albero = avl_make(uc, totCorr);
- if (albero == NULL) {
- G_fatal_error("avl_make error");
- return RLI_ERRORE;
- }
- m++;
- }
- else {
- uc.val.dc = precCell;
- ris = avl_add(&albero, uc, totCorr);
- switch (ris) {
- case AVL_ERR:
- {
- G_fatal_error("avl_add error");
- return RLI_ERRORE;
- }
- case AVL_ADD:
- {
- m++;
- break;
- }
- case AVL_PRES:
- {
- break;
- }
- default:
- {
- G_fatal_error("avl_make unknown error");
- return RLI_ERRORE;
- }
- }
- }
- totCorr = 1;
- } /* endif not equal cells */
- precCell = corrCell;
- }
- }
- /* last closing */
- if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
- if (albero == NULL) {
- uc.val.dc = precCell;
- albero = avl_make(uc, totCorr);
- if (albero == NULL) {
- G_fatal_error("avl_make error");
- return RLI_ERRORE;
- }
- m++;
- }
- else {
- uc.val.dc = precCell;
- ris = avl_add(&albero, uc, totCorr);
- switch (ris) {
- case AVL_ERR:
- {
- G_fatal_error("avl_add error");
- return RLI_ERRORE;
- }
- case AVL_ADD:
- {
- m++;
- break;
- }
- case AVL_PRES:
- {
- break;
- }
- default:
- {
- G_fatal_error("avl_add unknown error");
- return RLI_ERRORE;
- }
- }
- }
- }
- if (area > 0) {
- double alpha;
- double pi;
- double t;
- double sum, sum2;
- alpha = atof(par[0]);
- array = G_malloc(m * sizeof(AVL_tableRow));
- if (array == NULL) {
- G_fatal_error("malloc array failed");
- return RLI_ERRORE;
- }
- tot = avl_to_array(albero, zero, array);
- if (tot != m) {
- G_warning("avl_to_array unexpected value. the result could be wrong");
- return RLI_ERRORE;
- }
- /* calculate renyi index */
- sum = 0;
- sum2 = 0;
- for (i = 0; i < m; i++) {
- t = array[i].tot;
- pi = t / area;
- sum += pow(pi, alpha);
- sum += pi;
- }
- G_free(array);
- /* correction for numerical instability */
- if (sum2 != 1)
- sum += 1 - sum2;
- if ((alpha < 1 && sum < 1) || (alpha > 1 && sum > 1)) {
- G_warning("Renyi index calculation reached numerical instability. "
- "This can happen with alpha close to 1. The result will be set to zero.");
- sum = 1;
- }
- *result = (1 / (1 - alpha)) * log(sum);
- }
- else
- Rast_set_d_null_value(result, 1);
- avl_destroy(albero);
- if (masked) {
- close(mask_fd);
- G_free(mask_buf);
- }
- return RLI_OK;
- }
- int calculateF(int fd, struct area_entry *ad, char **par, double *result)
- {
- FCELL *buf;
- FCELL corrCell;
- FCELL precCell;
- int i, j;
- int mask_fd = -1, *mask_buf = NULL;
- int ris = 0;
- int masked = FALSE;
- long m = 0;
- long tot = 0;
- long zero = 0;
- long totCorr = 1;
- long area = 0;
- avl_tree albero = NULL;
- AVL_table array;
- generic_cell uc;
- uc.t = FCELL_TYPE;
- /* open mask if needed */
- if (ad->mask == 1) {
- if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
- return RLI_ERRORE;
- mask_buf = G_malloc(ad->cl * sizeof(int));
- if (mask_buf == NULL) {
- G_fatal_error("malloc mask_buf failed");
- return RLI_ERRORE;
- }
- masked = TRUE;
- }
- Rast_set_f_null_value(&precCell, 1);
- for (j = 0; j < ad->rl; j++) { /* for each row */
- if (masked) {
- if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
- G_fatal_error("mask read failed");
- return RLI_ERRORE;
- }
- }
- buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
- for (i = 0; i < ad->cl; i++) { /* for each cell in the row */
- corrCell = buf[i + ad->x];
- if ((masked) && (mask_buf[i] == 0)) {
- Rast_set_f_null_value(&corrCell, 1);
- }
- if (!(Rast_is_null_value(&corrCell, uc.t))) {
- /* total patch area */
- area++;
- }
- if (!(Rast_is_null_value(&precCell, uc.t)) &&
- corrCell == precCell) {
- totCorr++;
- }
- else if (!(Rast_is_null_value(&precCell, uc.t)) &&
- corrCell != precCell) {
-
- /* add precCell to search tree */
- if (albero == NULL) {
- uc.val.fc = precCell;
- albero = avl_make(uc, totCorr);
- if (albero == NULL) {
- G_fatal_error("avl_make error");
- return RLI_ERRORE;
- }
- m++;
- }
- else {
- uc.val.fc = precCell;
- ris = avl_add(&albero, uc, totCorr);
- switch (ris) {
- case AVL_ERR:
- {
- G_fatal_error("avl_add error");
- return RLI_ERRORE;
- }
- case AVL_ADD:
- {
- m++;
- break;
- }
- case AVL_PRES:
- {
- break;
- }
- default:
- {
- G_fatal_error("avl_make unknown error");
- return RLI_ERRORE;
- }
- }
- }
- totCorr = 1;
- } /* endif not equal cells */
- precCell = corrCell;
- }
- }
- /* last closing */
- if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
- if (albero == NULL) {
- uc.val.fc = precCell;
- albero = avl_make(uc, totCorr);
- if (albero == NULL) {
- G_fatal_error("avl_make error");
- return RLI_ERRORE;
- }
- m++;
- }
- else {
- uc.val.fc = precCell;
- ris = avl_add(&albero, uc, totCorr);
- switch (ris) {
- case AVL_ERR:
- {
- G_fatal_error("avl_add error");
- return RLI_ERRORE;
- }
- case AVL_ADD:
- {
- m++;
- break;
- }
- case AVL_PRES:
- {
- break;
- }
- default:
- {
- G_fatal_error("avl_add unknown error");
- return RLI_ERRORE;
- }
- }
- }
- }
- if (area > 0) {
- double alpha;
- double pi;
- double t;
- double sum, sum2;
- alpha = atof(par[0]);
- array = G_malloc(m * sizeof(AVL_tableRow));
- if (array == NULL) {
- G_fatal_error("malloc array failed");
- return RLI_ERRORE;
- }
- tot = avl_to_array(albero, zero, array);
- if (tot != m) {
- G_warning("avl_to_array unexpected value. the result could be wrong");
- return RLI_ERRORE;
- }
- /* calculate renyi index */
- sum = 0;
- sum2 = 0;
- for (i = 0; i < m; i++) {
- t = array[i].tot;
- pi = t / area;
- sum += pow(pi, alpha);
- sum2 += pi;
- }
- G_free(array);
- /* correction for numerical instability */
- if (sum2 != 1)
- sum += 1 - sum2;
- if ((alpha < 1 && sum < 1) || (alpha > 1 && sum > 1)) {
- G_warning("Renyi index calculation reached numerical instability. "
- "This can happen with alpha close to 1. The result will be set to zero.");
- sum = 1;
- }
- *result = (1 / (1 - alpha)) * log(sum);
- }
- else
- Rast_set_d_null_value(result, 1);
- avl_destroy(albero);
- if (masked) {
- close(mask_fd);
- G_free(mask_buf);
- }
- return RLI_OK;
- }
|