cats.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. \file 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. \date 2006-2008
  10. */
  11. #include <grass/config.h>
  12. #include <grass/glocale.h>
  13. #include <grass/vedit.h>
  14. /**
  15. \brief Add / remove categories
  16. \param Map vector map
  17. \param List list of selected primitives
  18. \param layer layer number
  19. \param del action (non-zero for delete otherwise add)
  20. \param cats_list list of category numbers
  21. \return number of modified primitives
  22. \return -1 on error
  23. */
  24. int Vedit_modify_cats(struct Map_info *Map, struct ilist *List,
  25. int layer, int del, struct cat_list *Clist)
  26. {
  27. int i, j;
  28. struct line_cats *Cats;
  29. struct line_pnts *Points;
  30. int line, type, cat;
  31. int nlines_modified, rewrite;
  32. /* features defined by cats */
  33. if (Clist->n_ranges <= 0) {
  34. return 0;
  35. }
  36. nlines_modified = 0;
  37. Cats = Vect_new_cats_struct();
  38. Points = Vect_new_line_struct();
  39. /* for each line, set new category */
  40. for (i = 0; i < List->n_values; i++) {
  41. line = List->value[i];
  42. type = Vect_read_line(Map, Points, Cats, line);
  43. if (!Vect_line_alive(Map, line))
  44. continue;
  45. rewrite = 0;
  46. for (j = 0; j < Clist->n_ranges; j++) {
  47. for (cat = Clist->min[j]; cat <= Clist->max[j]; cat++) {
  48. /* add new category */
  49. if (!del) {
  50. if (Vect_cat_set(Cats, layer, cat) < 1) {
  51. G_warning(_("Unable to set category %d for (feature id %d)"),
  52. cat, line);
  53. }
  54. else {
  55. rewrite = 1;
  56. }
  57. }
  58. else { /* delete old category */
  59. if (Vect_field_cat_del(Cats, layer, cat) == 1) {
  60. rewrite = 1;
  61. }
  62. }
  63. }
  64. }
  65. if (rewrite == 0)
  66. continue;
  67. if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
  68. return -1;
  69. }
  70. nlines_modified++;
  71. }
  72. /* destroy structures */
  73. Vect_destroy_line_struct(Points);
  74. Vect_destroy_cats_struct(Cats);
  75. return nlines_modified;
  76. }