overlay.c 6.7 KB

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