overlay.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*!
  2. \file lib/vector/Vlib/overlay.c
  3. \brief Vector library - overlays
  4. Higher level functions for reading/writing/manipulating vectors.
  5. This is file is just example and starting point for writing overlay
  6. functions!!!
  7. (C) 2001-2009 by the GRASS Development Team
  8. This program is free software under the GNU General Public License
  9. (>=v2). Read the file COPYING that comes with GRASS for details.
  10. \author Radim Blazek
  11. */
  12. #include <string.h>
  13. #include <grass/vector.h>
  14. #include <grass/glocale.h>
  15. int Vect_overlay_and(struct Map_info *, int, struct ilist *, struct ilist *,
  16. struct Map_info *, int, struct ilist *, struct ilist *,
  17. struct Map_info *);
  18. /*!
  19. \brief Get operator code from string
  20. \param str operator code string
  21. \return operator code
  22. \return -1 on error
  23. */
  24. int Vect_overlay_str_to_operator(const char *str)
  25. {
  26. if (strcmp(str, GV_ON_AND) == 0)
  27. return GV_O_AND;
  28. else if (strcmp(str, GV_ON_OVERLAP) == 0)
  29. return GV_O_OVERLAP;
  30. return -1;
  31. }
  32. /*!
  33. \brief Overlay 2 vector maps and create new one
  34. \param AMap vector map A
  35. \param AList unused ?
  36. \param AAList unused ?
  37. \param BMap vector map B
  38. \param BList unused ?
  39. \param BAList unused ?
  40. \param operator operator code
  41. \param[out] OMap output vector map
  42. \return 0 on success
  43. */
  44. int Vect_overlay(struct Map_info *AMap, int atype, struct ilist *AList, struct ilist *AAList, /* map A */
  45. struct Map_info *BMap, int btype, struct ilist *BList, struct ilist *BAList, /* map B */
  46. int operator, struct Map_info *OMap)
  47. { /* output map */
  48. switch (operator) {
  49. case GV_O_AND:
  50. Vect_overlay_and(AMap, atype, AList, AAList, BMap, btype, BList,
  51. BAList, OMap);
  52. break;
  53. default:
  54. G_fatal_error("Vect_overlay(): %s", _("unknown operator"));
  55. }
  56. return 0;
  57. }
  58. /*!
  59. \brief Overlay 2 vector maps with AND.
  60. AND supports:point line area
  61. point + - +
  62. line - - -
  63. area + - -
  64. \param AMap vector map A
  65. \param atype feature type for A
  66. \param AList unused ?
  67. \param AAList unused ?
  68. \param BMap vector map B
  69. \param btype feature type for B
  70. \param BList unused ?
  71. \param BAList unused ?
  72. \param operator operator code
  73. \param OMap output vector map
  74. \return 1 on success
  75. \return 0 on error
  76. */
  77. int
  78. Vect_overlay_and(struct Map_info *AMap, int atype, struct ilist *AList,
  79. struct ilist *AAList, struct Map_info *BMap, int btype,
  80. struct ilist *BList, struct ilist *BAList,
  81. struct Map_info *OMap)
  82. {
  83. int i, j, k, node, line, altype, bltype, oltype, area, centr;
  84. struct line_pnts *Points;
  85. struct line_cats *ACats, *BCats, *OCats;
  86. struct ilist *AOList, *BOList;
  87. /* TODO: support Lists */
  88. Points = Vect_new_line_struct();
  89. ACats = Vect_new_cats_struct();
  90. BCats = Vect_new_cats_struct();
  91. OCats = Vect_new_cats_struct();
  92. AOList = Vect_new_list();
  93. BOList = Vect_new_list();
  94. /* TODO: support all types; at present only point x point, area x point and point x area supported */
  95. if ((atype & GV_LINES) || (btype & GV_LINES))
  96. G_warning(_("Overlay: line/boundary types not supported by AND operator"));
  97. if ((atype & GV_AREA) && (btype & GV_AREA))
  98. G_warning(_("Overlay: area x area types not supported by AND operator"));
  99. /* TODO: more points in one node in one map */
  100. /* point x point: select all points with identical coordinates in both maps */
  101. if ((atype & GV_POINTS) && (btype & GV_POINTS)) { /* both points and centroids */
  102. G_debug(3, "overlay: AND: point x point");
  103. for (i = 1; i <= Vect_get_num_lines(AMap); i++) {
  104. altype = Vect_read_line(AMap, Points, ACats, i);
  105. if (!(altype & GV_POINTS))
  106. continue;
  107. node =
  108. Vect_find_node(BMap, Points->x[0], Points->y[0], Points->z[0],
  109. 0, 1);
  110. G_debug(3, "overlay: node = %d", node);
  111. if (node == 0)
  112. continue;
  113. Vect_reset_cats(OCats);
  114. for (j = 0; j < Vect_get_node_n_lines(BMap, node); j++) {
  115. line = Vect_get_node_line(BMap, node, j);
  116. bltype = Vect_read_line(BMap, NULL, BCats, line);
  117. if (!(bltype & GV_POINTS))
  118. continue;
  119. /* Identical points found -> write out */
  120. /* TODO: do something if fields in ACats and BCats are identical */
  121. for (k = 0; k < ACats->n_cats; k++)
  122. Vect_cat_set(OCats, ACats->field[k], ACats->cat[k]);
  123. for (k = 0; k < BCats->n_cats; k++)
  124. Vect_cat_set(OCats, BCats->field[k], BCats->cat[k]);
  125. /* TODO: what to do if one type is GV_POINT and second GV_CENTROID */
  126. oltype = altype;
  127. Vect_write_line(OMap, oltype, Points, OCats);
  128. Vect_list_append(AOList, i); /* add to list of written lines */
  129. Vect_list_append(BOList, line);
  130. break;
  131. }
  132. }
  133. }
  134. /* TODO: check only labeled areas */
  135. /* point x area: select points from A in areas in B */
  136. if ((atype & GV_POINTS) && (btype & GV_AREA)) { /* both points and centroids */
  137. G_debug(3, "overlay: AND: point x area");
  138. for (i = 1; i <= Vect_get_num_lines(AMap); i++) {
  139. altype = Vect_read_line(AMap, Points, ACats, i);
  140. if (!(altype & GV_POINTS))
  141. continue;
  142. area = Vect_find_area(BMap, Points->x[0], Points->y[0]);
  143. if (area == 0)
  144. continue;
  145. Vect_reset_cats(OCats);
  146. /* TODO: do something if fields in ACats and BCats are identical */
  147. for (k = 0; k < ACats->n_cats; k++)
  148. Vect_cat_set(OCats, ACats->field[k], ACats->cat[k]);
  149. centr = Vect_get_area_centroid(BMap, area);
  150. if (centr > 0) {
  151. bltype = Vect_read_line(BMap, NULL, BCats, centr);
  152. for (k = 0; k < BCats->n_cats; k++)
  153. Vect_cat_set(OCats, BCats->field[k], BCats->cat[k]);
  154. }
  155. /* Check if not yet written */
  156. if (!(Vect_val_in_list(AOList, i))) {
  157. Vect_write_line(OMap, altype, Points, OCats);
  158. Vect_list_append(AOList, i);
  159. }
  160. }
  161. }
  162. /* area x point: select points from B in areas in A */
  163. if ((btype & GV_POINTS) && (atype & GV_AREA)) { /* both points and centroids */
  164. G_debug(3, "overlay: AND: area x point");
  165. for (i = 1; i <= Vect_get_num_lines(BMap); i++) {
  166. bltype = Vect_read_line(BMap, Points, BCats, i);
  167. if (!(bltype & GV_POINTS))
  168. continue;
  169. area = Vect_find_area(AMap, Points->x[0], Points->y[0]);
  170. if (area == 0)
  171. continue;
  172. Vect_reset_cats(OCats);
  173. /* TODO: do something if fields in ACats and BCats are identical */
  174. for (k = 0; k < BCats->n_cats; k++)
  175. Vect_cat_set(OCats, BCats->field[k], BCats->cat[k]);
  176. centr = Vect_get_area_centroid(AMap, area);
  177. if (centr > 0) {
  178. altype = Vect_read_line(AMap, NULL, ACats, centr);
  179. for (k = 0; k < ACats->n_cats; k++)
  180. Vect_cat_set(OCats, ACats->field[k], ACats->cat[k]);
  181. }
  182. /* Check if not yet written */
  183. if (!(Vect_val_in_list(BOList, i))) {
  184. Vect_write_line(OMap, bltype, Points, OCats);
  185. Vect_list_append(BOList, i);
  186. }
  187. }
  188. }
  189. return 0;
  190. }