constraint.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*!
  2. \file lib/vector/Vlib/constraint.c
  3. \brief Vector library - constraints
  4. Higher level functions for reading/writing/manipulating vectors.
  5. These routines can affect the Vect_read_next_line() funtions by
  6. restricting what they return. They are applied on a per map basis.
  7. These do not affect the lower level direct read functions.
  8. Normally, all 'Alive' lines will be returned unless overridden by
  9. this function. You can specified all the types you are interested
  10. in (by oring their types together). You can use this to say exclude
  11. 'boundary' type features.
  12. By default all DEAD lines are ignored by the Vect_read_next_line()
  13. functions. This too can be overridden by including their types.
  14. (C) 2001-2009, 2011 by the GRASS Development Team
  15. This program is free software under the GNU General Public License
  16. (>=v2). Read the file COPYING that comes with GRASS for details.
  17. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  18. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  19. */
  20. #include <grass/vector.h>
  21. /*!
  22. \brief Set constraint region
  23. Vect_read_next_line() will read only features inside of given
  24. region.
  25. \param Map vector map
  26. \param n,s,e,w,t,b north, south, east, west, top, and bottom coordinates
  27. \return 0 on success
  28. \return -1 on error (invalid region)
  29. */
  30. int Vect_set_constraint_region(struct Map_info *Map,
  31. double n, double s, double e, double w,
  32. double t, double b)
  33. {
  34. if (n <= s)
  35. return -1;
  36. if (e <= w)
  37. return -1;
  38. Map->constraint.region_flag = TRUE;
  39. Map->constraint.box.N = n;
  40. Map->constraint.box.S = s;
  41. Map->constraint.box.E = e;
  42. Map->constraint.box.W = w;
  43. Map->constraint.box.T = t;
  44. Map->constraint.box.B = b;
  45. Map->head.proj = G_projection();
  46. return 0;
  47. }
  48. /*!
  49. \brief Get constraint box
  50. Constraint box can be defined by Vect_set_constraint_region().
  51. \param Map vector map
  52. \param[out] Box bounding box
  53. \return 0 on success
  54. \return -1 no region constraint defined
  55. */
  56. int Vect_get_constraint_box(const struct Map_info *Map, struct bound_box * Box)
  57. {
  58. if (!Map->constraint.region_flag)
  59. return -1;
  60. Box->N = Map->constraint.box.N;
  61. Box->S = Map->constraint.box.S;
  62. Box->E = Map->constraint.box.E;
  63. Box->W = Map->constraint.box.W;
  64. Box->T = Map->constraint.box.T;
  65. Box->B = Map->constraint.box.B;
  66. return 0;
  67. }
  68. /*!
  69. \brief Set constraint type
  70. Vect_read_next_line() will read only features of given type.
  71. \param Map vector map
  72. \param type constraint type (GV_POINT, GV_LINE, ...)
  73. \return 0 on success
  74. \return -1 invalid feature type
  75. */
  76. int Vect_set_constraint_type(struct Map_info *Map, int type)
  77. {
  78. if (!(type & (GV_POINTS | GV_LINES | GV_FACE | GV_KERNEL)))
  79. return -1;
  80. Map->constraint.type = type;
  81. Map->constraint.type_flag = TRUE;
  82. return 0;
  83. }
  84. /*!
  85. \brief Remove constraints
  86. \param Map vector map
  87. \return 0
  88. */
  89. void Vect_remove_constraints(struct Map_info *Map)
  90. {
  91. Map->constraint.region_flag = FALSE;
  92. Map->constraint.type_flag = FALSE;
  93. Map->constraint.field_flag = FALSE;
  94. }
  95. /*!
  96. \brief Set constraint field
  97. Vect_read_next_line() will read only features of given type. Note
  98. that categories must be read otherwise this constraint is ignored.
  99. Note: Field is called layer on user level.
  100. \param Map vector map
  101. \param field field number (-1 for all fields)
  102. \return 0 on success
  103. \return -1 invalid feature type
  104. */
  105. int Vect_set_constraint_field(struct Map_info *Map, int field)
  106. {
  107. if (field == -1) {
  108. Map->constraint.field_flag = FALSE;
  109. return 0;
  110. }
  111. if (field < 1) {
  112. return -1;
  113. }
  114. Map->constraint.field = field;
  115. Map->constraint.field_flag = TRUE;
  116. return 0;
  117. }