constraint.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*!
  2. \file lib/vector/Vlib/constraint.c
  3. \brief Vector library - constraints for reading features
  4. Higher level functions for reading/writing/manipulating vectors.
  5. These routines can affect the Vect_read_next_line() functions 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-2012 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. #include <grass/glocale.h>
  22. /*!
  23. \brief Set constraint region
  24. Vect_read_next_line() will read only features inside of given
  25. region or features with overlapping bounding box.
  26. \note Constraint is ignored for random access - Vect_read_line().
  27. \param Map pointer to Map_info struct
  28. \param n,s,e,w,t,b bbox definition (north, south, east, west, top, and bottom coordinates)
  29. \return 0 on success
  30. \return -1 on error (invalid region)
  31. */
  32. int Vect_set_constraint_region(struct Map_info *Map,
  33. double n, double s, double e, double w,
  34. double t, double b)
  35. {
  36. if (n <= s)
  37. return -1;
  38. if (e <= w)
  39. return -1;
  40. Map->constraint.region_flag = TRUE;
  41. Map->constraint.box.N = n;
  42. Map->constraint.box.S = s;
  43. Map->constraint.box.E = e;
  44. Map->constraint.box.W = w;
  45. Map->constraint.box.T = t;
  46. Map->constraint.box.B = b;
  47. Map->head.proj = G_projection();
  48. return 0;
  49. }
  50. /*!
  51. \brief Get constraint box
  52. Constraint box can be defined by Vect_set_constraint_region().
  53. \param Map vector map
  54. \param[out] Box bounding box
  55. \return 0 on success
  56. \return -1 no region constraint defined
  57. */
  58. int Vect_get_constraint_box(const struct Map_info *Map, struct bound_box * Box)
  59. {
  60. if (!Map->constraint.region_flag)
  61. return -1;
  62. Box->N = Map->constraint.box.N;
  63. Box->S = Map->constraint.box.S;
  64. Box->E = Map->constraint.box.E;
  65. Box->W = Map->constraint.box.W;
  66. Box->T = Map->constraint.box.T;
  67. Box->B = Map->constraint.box.B;
  68. return 0;
  69. }
  70. /*!
  71. \brief Set constraint type
  72. Vect_read_next_line() will read only features of given
  73. type. Constraint is ignored for random access - Vect_read_line().
  74. \param Map pointer to Map_info struct
  75. \param type constraint feature type (GV_POINT, GV_LINE, ...)
  76. \return 0 on success
  77. \return -1 invalid feature type
  78. */
  79. int Vect_set_constraint_type(struct Map_info *Map, int type)
  80. {
  81. if (!(type & (GV_POINTS | GV_LINES | GV_FACE | GV_KERNEL)))
  82. return -1;
  83. Map->constraint.type = type;
  84. Map->constraint.type_flag = TRUE;
  85. return 0;
  86. }
  87. /*!
  88. \brief Remove all constraints
  89. \param Map pointer to Map_info struct
  90. */
  91. void Vect_remove_constraints(struct Map_info *Map)
  92. {
  93. Map->constraint.region_flag = FALSE;
  94. Map->constraint.type_flag = FALSE;
  95. Map->constraint.field_flag = FALSE;
  96. }
  97. /*!
  98. \brief Set constraint field
  99. Vect_read_next_line() will read only features of given type. Note
  100. that categories must be read otherwise this constraint is
  101. ignored. Constraint is ignored for random access -
  102. Vect_read_line().
  103. Ignored for non-native vector formats.
  104. Note: Field is called layer on user level.
  105. \param Map pointer to Map_info struct
  106. \param field field number (-1 for all fields)
  107. \return 0 on success
  108. \return -1 invalid field
  109. */
  110. int Vect_set_constraint_field(struct Map_info *Map, int field)
  111. {
  112. if (Map->format != GV_FORMAT_NATIVE) {
  113. G_warning(_("Layer constraint ignored for non-native vector formats"));
  114. return -1;
  115. }
  116. if (field == -1) {
  117. Map->constraint.field_flag = FALSE;
  118. return 0;
  119. }
  120. if (field < 1) {
  121. return -1;
  122. }
  123. Map->constraint.field = field;
  124. Map->constraint.field_flag = TRUE;
  125. return 0;
  126. }