overlay.c 6.6 KB

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