shannon.c 13 KB

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