zbulk.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*!
  2. \file lib/vector/vedit/zbulk.c
  3. \brief Vedit library - Bulk labeling (automated labeling of vector
  4. features)
  5. (C) 2007-2008 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Martin Landa <landa.martin gmail.com>
  9. */
  10. #include <grass/dbmi.h>
  11. #include <grass/vedit.h>
  12. /*!
  13. \brief Lines z-bulk labeling
  14. Automated labeling (z coordinate assignment) of vector lines (iso-lines).
  15. \param Map pointer to Map_info
  16. \param List list of selected lines
  17. \param point_start_end staring and ending point
  18. \param start starting value
  19. \param step step value
  20. \return number of modified features
  21. \return -1 on error
  22. */
  23. int Vedit_bulk_labeling(struct Map_info *Map, struct ilist *List,
  24. double x1, double y1, double x2, double y2,
  25. double start, double step)
  26. {
  27. int i, cv_i, p_i;
  28. int line, type, temp_line;
  29. int nlines_modified;
  30. double value, dist;
  31. struct line_cats *Cats;
  32. struct line_pnts *Points, *Points_se; /* start - end */
  33. struct bound_box box, box_se;
  34. /* for intersection */
  35. struct line_pnts **Points_a, **Points_b;
  36. int nlines_a, nlines_b;
  37. dbCatValArray cv; /* line_id / dist */
  38. nlines_modified = 0;
  39. value = start;
  40. Points = Vect_new_line_struct();
  41. Points_se = Vect_new_line_struct();
  42. Cats = Vect_new_cats_struct();
  43. db_CatValArray_alloc(&cv, List->n_values);
  44. cv.ctype = DB_C_TYPE_DOUBLE;
  45. cv.n_values = 0;
  46. Vect_append_point(Points_se, x1, y1, -PORT_DOUBLE_MAX);
  47. Vect_append_point(Points_se, x2, y2, PORT_DOUBLE_MAX);
  48. /* write temporaly line */
  49. temp_line = Vect_write_line(Map, GV_LINE, Points_se, Cats);
  50. if (temp_line < 0) {
  51. return -1;
  52. }
  53. Vect_line_box(Points_se, &box_se);
  54. /* determine order of lines */
  55. cv_i = 0;
  56. for (i = 0; i < List->n_values; i++) {
  57. line = List->value[i];
  58. if (!Vect_line_alive(Map, line))
  59. continue;
  60. type = Vect_read_line(Map, Points, NULL, line);
  61. if (!(type & GV_LINE))
  62. continue;
  63. Vect_line_box(Points, &box);
  64. if (Vect_line_check_intersection(Points_se, Points, WITH_Z)) {
  65. Vect_line_intersection(Points_se, Points, &box_se, &box,
  66. &Points_a, &Points_b, &nlines_a, &nlines_b,
  67. WITHOUT_Z);
  68. if (nlines_a < 2 || nlines_b < 1) /* should not happen */
  69. continue;
  70. /* calculate distance start point -> point of intersection */
  71. for (p_i = 0; p_i < Points_a[0]->n_points; p_i++) {
  72. Points_a[0]->z[p_i] = 0;
  73. }
  74. dist = Vect_line_length(Points_a[0]); /* always first line in array? */
  75. cv.value[cv_i].cat = line;
  76. cv.value[cv_i++].val.d = dist;
  77. cv.n_values++;
  78. }
  79. }
  80. /* sort array by distance */
  81. db_CatValArray_sort_by_value(&cv);
  82. /* z bulk-labeling */
  83. for (cv_i = 0; cv_i < cv.n_values; cv_i++) {
  84. line = cv.value[cv_i].cat;
  85. type = Vect_read_line(Map, Points, Cats, line);
  86. for (p_i = 0; p_i < Points->n_points; p_i++) {
  87. Points->z[p_i] = value;
  88. }
  89. if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
  90. return -1;
  91. }
  92. nlines_modified++;
  93. value += step;
  94. }
  95. if (Vect_delete_line(Map, temp_line) < 0) {
  96. return -1;
  97. }
  98. db_CatValArray_free(&cv);
  99. Vect_destroy_line_struct(Points);
  100. Vect_destroy_line_struct(Points_se);
  101. Vect_destroy_cats_struct(Cats);
  102. return nlines_modified;
  103. }