close.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <string.h>
  2. #include "global.h"
  3. /*!
  4. \brief Close lines (boundaries)
  5. Using threshold distance (-1 for no limit)
  6. \param[in] Map vector map
  7. \param[in] ltype vector feature type (line | boundary)
  8. \param[in] thresh threshold distance
  9. \return number of modified features
  10. */
  11. int close_lines(struct Map_info *Map, int ltype, double thresh)
  12. {
  13. int nlines, line, type, nlines_modified, newline;
  14. int npoints;
  15. double *x, *y, *z;
  16. double dist;
  17. struct line_pnts *Points;
  18. struct line_cats *Cats;
  19. Points = Vect_new_line_struct();
  20. Cats = Vect_new_cats_struct();
  21. nlines_modified = 0;
  22. Vect_build_partial(Map, GV_BUILD_BASE);
  23. nlines = Vect_get_num_lines(Map);
  24. for (line = 1; line <= nlines; line++) {
  25. if (!Vect_line_alive(Map, line))
  26. continue;
  27. type = Vect_read_line(Map, Points, Cats, line);
  28. if (!(type & ltype))
  29. continue;
  30. npoints = Points->n_points - 1;
  31. x = Points->x;
  32. y = Points->y;
  33. z = Points->z;
  34. dist = Vect_points_distance(x[npoints], y[npoints], z[npoints],
  35. x[0], y[0], z[0], WITHOUT_Z);
  36. if (dist > 0 && (thresh < 0.0 || dist <= thresh)) {
  37. Vect_line_delete_point(Points, npoints);
  38. Vect_append_point(Points, x[0], y[0], z[0]);
  39. newline = Vect_rewrite_line(Map, line, type, Points, Cats);
  40. if (newline < 0) {
  41. G_warning(_("Unable to rewrite line %d"), line);
  42. return -1;
  43. }
  44. nlines_modified++;
  45. }
  46. }
  47. Vect_destroy_line_struct(Points);
  48. Vect_destroy_cats_struct(Cats);
  49. return nlines_modified;
  50. }