cats.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*!
  2. \file lib/vector/vedit/cats.c
  3. \brief Vedit library - category manipulation
  4. (C) 2006-2008 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Jachym Cepicky <jachym.cepicky gmail.com>
  8. \author Martin Landa <landa.martin gmail.com>
  9. */
  10. #include <grass/glocale.h>
  11. #include <grass/vedit.h>
  12. /*!
  13. \brief Add / remove categories
  14. \param Map pointer to Map_info
  15. \param List list of selected primitives
  16. \param layer layer number
  17. \param del action (non-zero for delete otherwise add)
  18. \param Clist list of category numbers
  19. \return number of modified primitives
  20. \return -1 on error
  21. */
  22. int Vedit_modify_cats(struct Map_info *Map, struct ilist *List,
  23. int layer, int del, struct cat_list *Clist)
  24. {
  25. int i, j;
  26. struct line_cats *Cats;
  27. struct line_pnts *Points;
  28. int line, type, cat;
  29. int nlines_modified, rewrite;
  30. /* features defined by cats */
  31. if (Clist->n_ranges <= 0) {
  32. return 0;
  33. }
  34. nlines_modified = 0;
  35. Cats = Vect_new_cats_struct();
  36. Points = Vect_new_line_struct();
  37. /* for each line, set new category */
  38. for (i = 0; i < List->n_values; i++) {
  39. line = List->value[i];
  40. type = Vect_read_line(Map, Points, Cats, line);
  41. if (!Vect_line_alive(Map, line))
  42. continue;
  43. rewrite = 0;
  44. for (j = 0; j < Clist->n_ranges; j++) {
  45. for (cat = Clist->min[j]; cat <= Clist->max[j]; cat++) {
  46. /* add new category */
  47. if (!del) {
  48. if (Vect_cat_set(Cats, layer, cat) < 1) {
  49. G_warning(_("Unable to set category %d for (feature id %d)"),
  50. cat, line);
  51. }
  52. else {
  53. rewrite = 1;
  54. }
  55. }
  56. else { /* delete old category */
  57. if (Vect_field_cat_del(Cats, layer, cat) > 0) {
  58. rewrite = 1;
  59. }
  60. }
  61. }
  62. }
  63. if (rewrite == 0)
  64. continue;
  65. if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
  66. return -1;
  67. }
  68. nlines_modified++;
  69. }
  70. /* destroy structures */
  71. Vect_destroy_line_struct(Points);
  72. Vect_destroy_cats_struct(Cats);
  73. return nlines_modified;
  74. }