123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691 |
- /****************************************************************************
- *
- * MODULE: r.li.edgedensity
- * AUTHOR(S): Serena Pallecchi student of Computer Science University of Pisa (Italy)
- * Commission from Faunalia Pontedera (PI) www.faunalia.it
- * Rewrite: Markus Metz
- *
- * PURPOSE: calculates edge density index
- * COPYRIGHT: (C) 2006-2014 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
- #include <stdlib.h>
- #include <fcntl.h> /* for O_RDONLY usage */
- #include <math.h>
- #include <grass/gis.h>
- #include <grass/raster.h>
- #include <grass/glocale.h>
- #include "../r.li.daemon/avlDefs.h"
- #include "../r.li.daemon/avl.h"
- #include "../r.li.daemon/daemon.h"
- rli_func edgedensity;
- 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);
- static int brdr = 1;
- int main(int argc, char *argv[])
- {
- struct Option *raster, *conf, *output, *class;
- struct Flag *flag_brdr;
- struct GModule *module;
- char **par = NULL;
- G_gisinit(argv[0]);
- module = G_define_module();
- module->description =
- _("Calculates edge density index on a raster map, using a 4 neighbour algorithm");
- G_add_keyword(_("raster"));
- G_add_keyword(_("landscape structure analysis"));
- G_add_keyword(_("patch 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;
- output = G_define_standard_option(G_OPT_R_OUTPUT);
- class = G_define_option();
- class->key = "patch_type";
- class->type = TYPE_STRING;
- class->required = NO;
- class->multiple = NO;
- class->label = _("The value of the patch type");
- class->description = _("It can be integer, double or float; "
- "it will be changed in function of map type");
- flag_brdr = G_define_flag();
- flag_brdr->key = 'b';
- flag_brdr->description = _("Exclude border edges");
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
- if (class->answer == NULL)
- par = NULL;
- else
- par = &class->answer;
- brdr = flag_brdr->answer == 0;
- return calculateIndex(conf->answer, edgedensity, par, raster->answer,
- output->answer);
- }
- int edgedensity(int fd, char **par, struct area_entry *ad, double *result)
- {
- int ris = -1;
- double indice = 0;
- 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, *buf_sup, *buf_null;
- CELL corrCell, precCell, supCell;
- CELL ptype;
- long nedges, area;
- int i, j;
- int mask_fd, *mask_buf, *mask_sup, *mask_tmp, masked;
- struct Cell_head hd;
- Rast_get_window(&hd);
- /* open mask if needed */
- mask_fd = -1;
- mask_buf = mask_sup = NULL;
- masked = FALSE;
- 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;
- }
- mask_sup = G_malloc(ad->cl * sizeof(int));
- if (mask_sup == NULL) {
- G_fatal_error("malloc mask_buf failed");
- return RLI_ERRORE;
- }
- for (j = 0; j < ad->cl; j++)
- mask_buf[j] = 0;
- masked = TRUE;
- }
- buf_null = Rast_allocate_c_buf();
- if (buf_null == NULL) {
- G_fatal_error("malloc buf_null failed");
- return RLI_ERRORE;
- }
- /* the first time buf_sup is all null */
- Rast_set_c_null_value(buf_null, Rast_window_cols());
- buf_sup = buf_null;
- if (par != NULL) { /* only 1 class */
- char *sval;
- sval = par[0];
- ptype = atoi(sval);
- }
- else
- Rast_set_c_null_value(&ptype, 1);
- nedges = 0;
- area = 0;
- /* for each raster row */
- for (i = 0; i < ad->rl; i++) {
- /* read row of raster */
- buf = RLI_get_cell_raster_row(fd, i + ad->y, ad);
- if (i > 0) /* not first row */
- buf_sup = RLI_get_cell_raster_row(fd, i - 1 + ad->y, ad);
- /* read mask if needed */
- if (masked) {
- mask_tmp = mask_sup;
- mask_sup = mask_buf;
- mask_buf = mask_tmp;
- if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0)
- return RLI_ERRORE;
- }
- Rast_set_c_null_value(&precCell, 1);
- for (j = 0; j < ad->cl; j++) {
- corrCell = buf[j + ad->x];
- if (masked && mask_buf[j] == 0) {
- Rast_set_c_null_value(&corrCell, 1);
- }
- supCell = buf_sup[j + ad->x];
- if (masked && (mask_sup[j] == 0)) {
- Rast_set_c_null_value(&supCell, 1);
- }
- if (brdr) {
- if (!Rast_is_c_null_value(&corrCell)) {
- area++;
- if (Rast_is_c_null_value(&ptype) || corrCell == ptype) {
- if (Rast_is_c_null_value(&precCell) || precCell != corrCell) {
- nedges++;
- }
- if (Rast_is_c_null_value(&supCell) || supCell != corrCell) {
- nedges++;
- }
- /* right and bottom */
- if (i == ad->rl - 1)
- nedges++;
- if (j == ad->cl - 1)
- nedges++;
- }
- }
- else /* corrCell == NULL */ {
- if (!Rast_is_c_null_value(&precCell)) {
- if (Rast_is_c_null_value(&ptype) || precCell == ptype) {
- nedges++;
- }
- }
- if (!Rast_is_c_null_value(&supCell)) {
- if (Rast_is_c_null_value(&ptype) || supCell == ptype) {
- nedges++;
- }
- }
- }
- }
- else {
- /* exclude border edges */
- if (!Rast_is_c_null_value(&corrCell)) {
- area++;
- if (Rast_is_c_null_value(&ptype) || corrCell == ptype) {
- if (j > 0 && !(masked && mask_buf[j - 1] == 0) &&
- (Rast_is_c_null_value(&precCell) || precCell != corrCell)) {
- nedges++;
- }
- if (i > 0 && !(masked && mask_sup[i - 1] == 0) &&
- (Rast_is_c_null_value(&supCell) || supCell != corrCell)) {
- nedges++;
- }
- }
- }
- else if (Rast_is_c_null_value(&corrCell) && !(masked && mask_buf[j] == 0)) {
- if (!Rast_is_c_null_value(&precCell)) {
- if (Rast_is_c_null_value(&ptype) || precCell == ptype) {
- nedges++;
- }
- }
- if (!Rast_is_c_null_value(&supCell)) {
- if (Rast_is_c_null_value(&ptype) || supCell == ptype) {
- nedges++;
- }
- }
- }
- }
- precCell = corrCell;
- }
- }
- /* calculate index */
- if (area > 0) {
- double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2;
- double elength, cell_size;
- /* calculate distance */
- G_begin_distance_calculations();
- /* EW Dist at North edge */
- EW_DIST1 = G_distance(hd.east, hd.north, hd.west, hd.north);
- /* EW Dist at South Edge */
- EW_DIST2 = G_distance(hd.east, hd.south, hd.west, hd.south);
- /* NS Dist at East edge */
- NS_DIST1 = G_distance(hd.east, hd.north, hd.east, hd.south);
- /* NS Dist at West edge */
- NS_DIST2 = G_distance(hd.west, hd.north, hd.west, hd.south);
- elength = ((EW_DIST1 + EW_DIST2) / (2 * hd.cols) +
- (NS_DIST1 + NS_DIST2) / (2 * hd.rows)) / 2;
- cell_size = ((EW_DIST1 + EW_DIST2) / (2 * hd.cols)) *
- ((NS_DIST1 + NS_DIST2) / (2 * hd.rows));
- *result = (double) nedges * elength * 10000 / (area * cell_size);
- }
- else
- Rast_set_d_null_value(result, 1);
- if (masked) {
- close(mask_fd);
- G_free(mask_buf);
- G_free(mask_sup);
- }
- G_free(buf_null);
- return RLI_OK;
- }
- int calculateD(int fd, struct area_entry *ad, char **par, double *result)
- {
- DCELL *buf, *buf_sup, *buf_null;
- DCELL corrCell, precCell, supCell;
- DCELL ptype;
- long nedges, area;
- int i, j;
- int mask_fd, *mask_buf, *mask_sup, *mask_tmp, masked;
- struct Cell_head hd;
- Rast_get_window(&hd);
- /* open mask if needed */
- mask_fd = -1;
- mask_buf = mask_sup = NULL;
- masked = FALSE;
- 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;
- }
- mask_sup = G_malloc(ad->cl * sizeof(int));
- if (mask_sup == NULL) {
- G_fatal_error("malloc mask_buf failed");
- return RLI_ERRORE;
- }
- for (j = 0; j < ad->cl; j++)
- mask_buf[j] = 0;
- masked = TRUE;
- }
- buf_null = Rast_allocate_d_buf();
- if (buf_null == NULL) {
- G_fatal_error("malloc buf_null failed");
- return RLI_ERRORE;
- }
- /* the first time buf_sup is all null */
- Rast_set_d_null_value(buf_null, Rast_window_cols());
- buf_sup = buf_null;
- if (par != NULL) { /* only 1 class */
- char *sval;
- sval = par[0];
- ptype = atof(sval);
- }
- else
- Rast_set_d_null_value(&ptype, 1);
- nedges = 0;
- area = 0;
- /* for each raster row */
- for (i = 0; i < ad->rl; i++) {
- /* read row of raster */
- buf = RLI_get_dcell_raster_row(fd, i + ad->y, ad);
- if (i > 0) /* not first row */
- buf_sup = RLI_get_dcell_raster_row(fd, i - 1 + ad->y, ad);
- /* read mask if needed */
- if (masked) {
- mask_tmp = mask_sup;
- mask_sup = mask_buf;
- mask_buf = mask_tmp;
- if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0)
- return RLI_ERRORE;
- }
- Rast_set_d_null_value(&precCell, 1);
- for (j = 0; j < ad->cl; j++) {
- corrCell = buf[j + ad->x];
- if (masked && mask_buf[j] == 0) {
- Rast_set_d_null_value(&corrCell, 1);
- }
- supCell = buf_sup[j + ad->x];
- if (masked && (mask_sup[j] == 0)) {
- Rast_set_d_null_value(&supCell, 1);
- }
- if (brdr) {
- if (!Rast_is_d_null_value(&corrCell)) {
- area++;
- if (Rast_is_d_null_value(&ptype) || corrCell == ptype) {
- if (Rast_is_d_null_value(&precCell) || precCell != corrCell) {
- nedges++;
- }
- if (Rast_is_d_null_value(&supCell) || supCell != corrCell) {
- nedges++;
- }
- /* right and bottom */
- if (i == ad->rl - 1)
- nedges++;
- if (j == ad->cl - 1)
- nedges++;
- }
- }
- else /* corrCell == NULL */ {
- if (!Rast_is_d_null_value(&precCell)) {
- if (Rast_is_d_null_value(&ptype) || precCell == ptype) {
- nedges++;
- }
- }
- if (!Rast_is_d_null_value(&supCell)) {
- if (Rast_is_d_null_value(&ptype) || supCell == ptype) {
- nedges++;
- }
- }
- }
- }
- else {
- /* exclude border edges */
- if (!Rast_is_d_null_value(&corrCell)) {
- area++;
- if (Rast_is_d_null_value(&ptype) || corrCell == ptype) {
- if (j > 0 && !(masked && mask_buf[j - 1] == 0) &&
- (Rast_is_d_null_value(&precCell) || precCell != corrCell)) {
- nedges++;
- }
- if (i > 0 && !(masked && mask_sup[i - 1] == 0) &&
- (Rast_is_d_null_value(&supCell) || supCell != corrCell)) {
- nedges++;
- }
- }
- }
- else if (Rast_is_d_null_value(&corrCell) && !(masked && mask_buf[j] == 0)) {
- if (!Rast_is_d_null_value(&precCell)) {
- if (Rast_is_d_null_value(&ptype) || precCell == ptype) {
- nedges++;
- }
- }
- if (!Rast_is_d_null_value(&supCell)) {
- if (Rast_is_d_null_value(&ptype) || supCell == ptype) {
- nedges++;
- }
- }
- }
- }
- precCell = corrCell;
- }
- }
- /* calculate index */
- if (area > 0) {
- double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2;
- double elength, cell_size;
- /* calculate distance */
- G_begin_distance_calculations();
- /* EW Dist at North edge */
- EW_DIST1 = G_distance(hd.east, hd.north, hd.west, hd.north);
- /* EW Dist at South Edge */
- EW_DIST2 = G_distance(hd.east, hd.south, hd.west, hd.south);
- /* NS Dist at East edge */
- NS_DIST1 = G_distance(hd.east, hd.north, hd.east, hd.south);
- /* NS Dist at West edge */
- NS_DIST2 = G_distance(hd.west, hd.north, hd.west, hd.south);
- elength = ((EW_DIST1 + EW_DIST2) / (2 * hd.cols) +
- (NS_DIST1 + NS_DIST2) / (2 * hd.rows)) / 2;
- cell_size = ((EW_DIST1 + EW_DIST2) / (2 * hd.cols)) *
- ((NS_DIST1 + NS_DIST2) / (2 * hd.rows));
- *result = (double) nedges * elength * 10000 / (area * cell_size);
- }
- else
- Rast_set_d_null_value(result, 1);
- if (masked) {
- close(mask_fd);
- G_free(mask_buf);
- G_free(mask_sup);
- }
- G_free(buf_null);
- return RLI_OK;
- }
- int calculateF(int fd, struct area_entry *ad, char **par, double *result)
- {
- FCELL *buf, *buf_sup, *buf_null;
- FCELL corrCell, precCell, supCell;
- FCELL ptype;
- long nedges, area;
- int i, j;
- int mask_fd, *mask_buf, *mask_sup, *mask_tmp, masked;
- struct Cell_head hd;
- Rast_get_window(&hd);
- /* open mask if needed */
- mask_fd = -1;
- mask_buf = mask_sup = NULL;
- masked = FALSE;
- 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;
- }
- mask_sup = G_malloc(ad->cl * sizeof(int));
- if (mask_sup == NULL) {
- G_fatal_error("malloc mask_buf failed");
- return RLI_ERRORE;
- }
- for (j = 0; j < ad->cl; j++)
- mask_buf[j] = 0;
- masked = TRUE;
- }
- buf_null = Rast_allocate_f_buf();
- if (buf_null == NULL) {
- G_fatal_error("malloc buf_null failed");
- return RLI_ERRORE;
- }
- /* the first time buf_sup is all null */
- Rast_set_f_null_value(buf_null, Rast_window_cols());
- buf_sup = buf_null;
- if (par != NULL) { /* only 1 class */
- char *sval;
- sval = par[0];
- ptype = atof(sval);
- }
- else
- Rast_set_f_null_value(&ptype, 1);
- nedges = 0;
- area = 0;
- /* for each raster row */
- for (i = 0; i < ad->rl; i++) {
- /* read row of raster */
- buf = RLI_get_fcell_raster_row(fd, i + ad->y, ad);
- if (i > 0) /* not first row */
- buf_sup = RLI_get_fcell_raster_row(fd, i - 1 + ad->y, ad);
- /* read mask if needed */
- if (masked) {
- mask_tmp = mask_sup;
- mask_sup = mask_buf;
- mask_buf = mask_tmp;
- if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0)
- return RLI_ERRORE;
- }
- Rast_set_f_null_value(&precCell, 1);
- for (j = 0; j < ad->cl; j++) {
- corrCell = buf[j + ad->x];
- if (masked && mask_buf[j] == 0) {
- Rast_set_f_null_value(&corrCell, 1);
- }
- supCell = buf_sup[j + ad->x];
- if (masked && (mask_sup[j] == 0)) {
- Rast_set_f_null_value(&supCell, 1);
- }
- if (brdr) {
- if (!Rast_is_f_null_value(&corrCell)) {
- area++;
- if (Rast_is_f_null_value(&ptype) || corrCell == ptype) {
- if (Rast_is_f_null_value(&precCell) || precCell != corrCell) {
- nedges++;
- }
- if (Rast_is_f_null_value(&supCell) || supCell != corrCell) {
- nedges++;
- }
- /* right and bottom */
- if (i == ad->rl - 1)
- nedges++;
- if (j == ad->cl - 1)
- nedges++;
- }
- }
- else /* corrCell == NULL */ {
- if (!Rast_is_f_null_value(&precCell)) {
- if (Rast_is_f_null_value(&ptype) || precCell == ptype) {
- nedges++;
- }
- }
- if (!Rast_is_f_null_value(&supCell)) {
- if (Rast_is_f_null_value(&ptype) || supCell == ptype) {
- nedges++;
- }
- }
- }
- }
- else {
- /* exclude border edges */
- if (!Rast_is_f_null_value(&corrCell)) {
- area++;
- if (Rast_is_f_null_value(&ptype) || corrCell == ptype) {
- if (j > 0 && !(masked && mask_buf[j - 1] == 0) &&
- (Rast_is_f_null_value(&precCell) || precCell != corrCell)) {
- nedges++;
- }
- if (i > 0 && !(masked && mask_sup[i - 1] == 0) &&
- (Rast_is_f_null_value(&supCell) || supCell != corrCell)) {
- nedges++;
- }
- }
- }
- else if (Rast_is_f_null_value(&corrCell) && !(masked && mask_buf[j] == 0)) {
- if (!Rast_is_f_null_value(&precCell)) {
- if (Rast_is_f_null_value(&ptype) || precCell == ptype) {
- nedges++;
- }
- }
- if (!Rast_is_f_null_value(&supCell)) {
- if (Rast_is_f_null_value(&ptype) || supCell == ptype) {
- nedges++;
- }
- }
- }
- }
- precCell = corrCell;
- }
- }
- /* calculate index */
- if (area > 0) {
- double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2;
- double elength, cell_size;
- /* calculate distance */
- G_begin_distance_calculations();
- /* EW Dist at North edge */
- EW_DIST1 = G_distance(hd.east, hd.north, hd.west, hd.north);
- /* EW Dist at South Edge */
- EW_DIST2 = G_distance(hd.east, hd.south, hd.west, hd.south);
- /* NS Dist at East edge */
- NS_DIST1 = G_distance(hd.east, hd.north, hd.east, hd.south);
- /* NS Dist at West edge */
- NS_DIST2 = G_distance(hd.west, hd.north, hd.west, hd.south);
- elength = ((EW_DIST1 + EW_DIST2) / (2 * hd.cols) +
- (NS_DIST1 + NS_DIST2) / (2 * hd.rows)) / 2;
- cell_size = ((EW_DIST1 + EW_DIST2) / (2 * hd.cols)) *
- ((NS_DIST1 + NS_DIST2) / (2 * hd.rows));
- *result = (double) nedges * elength * 10000 / (area * cell_size);
- }
- else
- Rast_set_d_null_value(result, 1);
- if (masked) {
- close(mask_fd);
- G_free(mask_buf);
- G_free(mask_sup);
- }
- G_free(buf_null);
- return RLI_OK;
- }
|