constraint.c 4.2 KB

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