main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /****************************************************************************
  2. *
  3. * MODULE: r.li.shape
  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 shape index
  10. * COPYRIGHT: (C) 2006-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. int main(int argc, char *argv[])
  24. {
  25. struct Option *raster, *conf, *output;
  26. struct GModule *module;
  27. G_gisinit(argv[0]);
  28. module = G_define_module();
  29. module->description = _("Calculates shape index on a raster map");
  30. G_add_keyword(_("raster"));
  31. G_add_keyword(_("landscape structure analysis"));
  32. G_add_keyword(_("patch index"));
  33. /* define options */
  34. raster = G_define_standard_option(G_OPT_R_INPUT);
  35. conf = G_define_standard_option(G_OPT_F_INPUT);
  36. conf->key = "config";
  37. conf->description = _("Configuration file");
  38. conf->required = YES;
  39. output = G_define_standard_option(G_OPT_R_OUTPUT);
  40. /** add other options for index parameters here */
  41. if (G_parser(argc, argv))
  42. exit(EXIT_FAILURE);
  43. return calculateIndex(conf->answer, shape_index, NULL, raster->answer,
  44. output->answer);
  45. }
  46. int shape_index(int fd, char **par, struct area_entry *ad, double *result)
  47. {
  48. double area;
  49. struct Cell_head hd;
  50. CELL complete_value;
  51. double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2;
  52. int mask_fd = -1, null_count = 0;
  53. int i = 0, k = 0;
  54. int *mask_buf;
  55. Rast_set_c_null_value(&complete_value, 1);
  56. Rast_get_cellhd(ad->raster, "", &hd);
  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. for (i = 0; i < ad->rl; i++) {
  63. if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0)
  64. return 0;
  65. for (k = 0; k < ad->cl; k++) {
  66. if (mask_buf[k] == 0) {
  67. null_count++;
  68. }
  69. }
  70. }
  71. }
  72. /*calculate distance */
  73. G_begin_distance_calculations();
  74. /* EW Dist at North edge */
  75. EW_DIST1 = G_distance(hd.east, hd.north, hd.west, hd.north);
  76. /* EW Dist at South Edge */
  77. EW_DIST2 = G_distance(hd.east, hd.south, hd.west, hd.south);
  78. /* NS Dist at East edge */
  79. NS_DIST1 = G_distance(hd.east, hd.north, hd.east, hd.south);
  80. /* NS Dist at West edge */
  81. NS_DIST2 = G_distance(hd.west, hd.north, hd.west, hd.south);
  82. area = (((EW_DIST1 + EW_DIST2) / 2) / hd.cols) *
  83. (((NS_DIST1 + NS_DIST2) / 2) / hd.rows) *
  84. (ad->rl * ad->cl - null_count);
  85. *result = area;
  86. return 1;
  87. }