main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /****************************************************************************
  2. *
  3. * MODULE: r.li.patchnum
  4. * AUTHOR(S): Claudio Porta and Lucio Davide Spano (original contributors)
  5. * students of Computer Science University of Pisa (Italy)
  6. * Commission from Faunalia Pontedera (PI) www.faunalia.it
  7. * Fixes: Markus Neteler <neteler itc.it>
  8. *
  9. * PURPOSE: calculates patch number index
  10. * COPYRIGHT: (C) 2007-2007 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 <grass/gis.h>
  20. #include <grass/raster.h>
  21. #include <grass/glocale.h>
  22. #include "../r.li.daemon/daemon.h"
  23. #include "../r.li.daemon/defs.h"
  24. int main(int argc, char *argv[])
  25. {
  26. struct Option *raster, *conf, *output;
  27. struct GModule *module;
  28. G_gisinit(argv[0]);
  29. module = G_define_module();
  30. module->description =
  31. _("Calculates patch number index on a raster map, using a 4 neighbour algorithm.");
  32. G_add_keyword(_("raster"));
  33. G_add_keyword(_("landscape structure analysis"));
  34. G_add_keyword(_("patch index"));
  35. /* define options */
  36. raster = G_define_standard_option(G_OPT_R_INPUT);
  37. conf = G_define_standard_option(G_OPT_F_INPUT);
  38. conf->key = "config";
  39. conf->description = _("Configuration file");
  40. conf->required = YES;
  41. output = G_define_standard_option(G_OPT_R_OUTPUT);
  42. if (G_parser(argc, argv))
  43. exit(EXIT_FAILURE);
  44. return calculateIndex(conf->answer, patch_number, NULL, raster->answer,
  45. output->answer);
  46. }
  47. int patch_number(int fd, char **par, struct area_entry *ad, double *result)
  48. {
  49. CELL *buf, *sup;
  50. int count = 0, i, j, connected = 0, complete_line = 1, other_above = 0;
  51. struct Cell_head hd;
  52. CELL complete_value;
  53. int mask_fd = -1, *mask_buf, *mask_sup, null_count = 0;
  54. Rast_set_c_null_value(&complete_value, 1);
  55. Rast_get_cellhd(ad->raster, "", &hd);
  56. sup = Rast_allocate_c_buf();
  57. /* open mask if needed */
  58. if (ad->mask == 1) {
  59. if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
  60. return 0;
  61. mask_buf = malloc(ad->cl * sizeof(int));
  62. mask_sup = malloc(ad->cl * sizeof(int));
  63. }
  64. /*calculate number of patch */
  65. for (i = 0; i < ad->rl; i++) {
  66. buf = RLI_get_cell_raster_row(fd, i + ad->y, ad);
  67. if (i > 0) {
  68. sup = RLI_get_cell_raster_row(fd, i - 1 + ad->y, ad);
  69. }
  70. /* mask values */
  71. if (ad->mask == 1) {
  72. int k;
  73. if (i > 0) {
  74. int *tmp;
  75. tmp = mask_sup;
  76. mask_buf = mask_sup;
  77. }
  78. if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0)
  79. return 0;
  80. for (k = 0; k < ad->cl; k++) {
  81. if (mask_buf[k] == 0) {
  82. Rast_set_c_null_value(mask_buf + k, 1);
  83. null_count++;
  84. }
  85. }
  86. }
  87. if (complete_line) {
  88. if (!Rast_is_null_value(&(buf[ad->x]), CELL_TYPE) &&
  89. buf[ad->x] != complete_value)
  90. count++;
  91. for (j = 0; j < ad->cl - 1; j++) {
  92. if (buf[j + ad->x] != buf[j + 1 + ad->x]) {
  93. complete_line = 0;
  94. if (!Rast_is_null_value(&(buf[j + 1 + ad->x]), CELL_TYPE)
  95. && buf[j + 1 + ad->x] != complete_value)
  96. count++;
  97. }
  98. }
  99. if (complete_line) {
  100. complete_value = buf[ad->x];
  101. }
  102. }
  103. else {
  104. complete_line = 1;
  105. connected = 0;
  106. other_above = 0;
  107. for (j = 0; j < ad->cl; j++) {
  108. if (sup[j + ad->x] == buf[j + ad->x]) {
  109. connected = 1;
  110. if (other_above) {
  111. other_above = 0;
  112. count--;
  113. }
  114. }
  115. else {
  116. if (connected &&
  117. !Rast_is_null_value(&(buf[j + ad->x]), CELL_TYPE))
  118. other_above = 1;
  119. }
  120. if (j < ad->cl - 1 && buf[j + ad->x] != buf[j + 1 + ad->x]) {
  121. complete_line = 0;
  122. if (!connected &&
  123. !Rast_is_null_value(&(buf[j + ad->x]), CELL_TYPE)) {
  124. count++;
  125. connected = 0;
  126. other_above = 0;
  127. }
  128. else {
  129. connected = 0;
  130. other_above = 0;
  131. }
  132. }
  133. }
  134. if (!connected &&
  135. sup[ad->cl - 1 + ad->x] != buf[ad->cl - 1 + ad->x]) {
  136. if (!Rast_is_null_value
  137. (&(buf[ad->cl - 1 + ad->x]), CELL_TYPE)) {
  138. count++;
  139. complete_line = 0;
  140. }
  141. }
  142. if (complete_line)
  143. complete_value = buf[ad->x];
  144. }
  145. }
  146. *result = count;
  147. G_free(sup);
  148. return RLI_OK;
  149. }