observation_points.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* obervation_points.c (simlib), 14.mar.2011, SG */
  2. #include <grass/glocale.h>
  3. #include <grass/vector.h>
  4. #include <grass/waterglobs.h>
  5. static void init_points(struct _points *, int);
  6. static void realloc_points(struct _points *, int);
  7. static void insert_next_point(struct _points *p, double x, double y, int cat);
  8. /* ************************************************************************* */
  9. void create_observation_points()
  10. {
  11. int if_log = 0;
  12. struct Map_info Map;
  13. struct line_pnts *pts;
  14. struct line_cats *cts;
  15. double x, y;
  16. int type, cat, i;
  17. if(parm.observation->answer != NULL)
  18. if_log += 1;
  19. if(parm.logfile->answer != NULL)
  20. if_log += 1;
  21. /* Nothing to do */
  22. if(if_log == 0)
  23. return;
  24. if(if_log == 1)
  25. G_fatal_error("Observation vector map and logfile must be provided");
  26. Vect_set_open_level(1);
  27. if (Vect_open_old(&Map, parm.observation->answer, "") < 0)
  28. G_fatal_error(_("Unable to open vector map <%s>"), parm.observation->answer);
  29. Vect_rewind(&Map);
  30. pts = Vect_new_line_struct();
  31. cts = Vect_new_cats_struct();
  32. /* Initialize point structure */
  33. init_points(&points, 128);
  34. /* Read all vector points */
  35. while(1) {
  36. type = Vect_read_next_line(&Map, pts, cts);
  37. if(type == -2) {
  38. break;
  39. }
  40. if(type == -1) {
  41. Vect_close(&Map);
  42. G_fatal_error(_("Unable to read points from map %s"), parm.observation->answer);
  43. }
  44. if(type == GV_POINT) {
  45. x = pts->x[0];
  46. y = pts->y[0];
  47. cat = cts->cat[0];
  48. /* Check region bounds befor inserting point */
  49. if(x <= cellhd.east && x >= cellhd.west && y <= cellhd.north && y >= cellhd.south) {
  50. insert_next_point(&points, x, y, cat);
  51. }
  52. }
  53. }
  54. Vect_close(&Map);
  55. /* Open the logfile */
  56. points.output = fopen(parm.logfile->answer, "w");
  57. if(points.output == NULL)
  58. G_fatal_error(_("Unable to open observation logfile %s for writing"), parm.logfile->answer);
  59. points.is_open = 1;
  60. fprintf(points.output, "STEP ");
  61. /* Write the vector cats as header information in the logfile */
  62. for(i = 0; i < points.npoints; i++)
  63. {
  64. fprintf(points.output, "CAT%.4d ", points.cats[i]);
  65. }
  66. fprintf(points.output, "\n");
  67. return;
  68. }
  69. /* ************************************************************************* */
  70. void init_points(struct _points *p, int size)
  71. {
  72. p->x = (double*)G_calloc(size, sizeof(double));
  73. p->y = (double*)G_calloc(size, sizeof(double));
  74. p->cats = (int*)G_calloc(size, sizeof(int));
  75. p->npoints = 0;
  76. p->npoints_alloc = size;
  77. p->output = NULL;
  78. p->is_open = 0;
  79. }
  80. /* ************************************************************************* */
  81. void realloc_points(struct _points *p, int add_size)
  82. {
  83. p->x = (double*)G_realloc(p->x, (p->npoints_alloc + add_size) * sizeof(double));
  84. p->y = (double*)G_realloc(p->y, (p->npoints_alloc + add_size) * sizeof(double));
  85. p->cats = (int*)G_realloc(p->cats, (p->npoints_alloc + add_size) * sizeof(int));
  86. p->npoints_alloc += add_size;
  87. }
  88. /* ************************************************************************* */
  89. void insert_next_point(struct _points *p, double x, double y, int cat)
  90. {
  91. if(p->npoints == p->npoints_alloc)
  92. realloc_points(p, 128);
  93. G_debug(3, "Insert point %g %g %i id %i\n", x, y, cat, p->npoints);
  94. p->x[p->npoints] = x;
  95. p->y[p->npoints] = y;
  96. p->cats[p->npoints] = cat;
  97. p->npoints++;
  98. }