write_sfa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*!
  2. \file lib/vector/Vlib/write_sfa.c
  3. \brief Vector library - write vector feature - simple feature access (level 2)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. See write_ogr.c (OGR interface) and write_pg.c (PostGIS interface)
  6. for imlementation issues.
  7. \todo SFA version of V2__delete_area_cats_from_cidx_nat()
  8. \todo function to delete corresponding entry in fidx
  9. \todo SFA version of V2__add_area_cats_to_cidx_nat
  10. \todo SFA version of V2__add_line_to_topo_nat
  11. (C) 2011-2012 by Martin Landa, and the GRASS Development Team
  12. This program is free software under the GNU General Public License
  13. (>=v2). Read the file COPYING that comes with GRASS for details.
  14. \author Martin Landa <landa.martin gmail.com>
  15. */
  16. #include <grass/vector.h>
  17. #include <grass/glocale.h>
  18. #include "local_proto.h"
  19. #ifdef HAVE_POSTGRES
  20. #include "pg_local_proto.h"
  21. #endif
  22. #if defined HAVE_OGR || defined HAVE_POSTGRES
  23. static void V2__add_line_to_topo_sfa(struct Map_info *, int, const struct line_pnts *,
  24. const struct line_cats *);
  25. #endif
  26. /*!
  27. \brief Writes feature on level 2 (OGR/PostGIS interface, pseudo-topological level)
  28. \param Map pointer to Map_info structure
  29. \param type feature type (see V1_write_line_ogr() for list of supported types)
  30. \param points pointer to line_pnts structure (feature geometry)
  31. \param cats pointer to line_cats structure (feature categories)
  32. \return feature index in offset array (related to pseudo-topology)
  33. \return -1 on error
  34. */
  35. off_t V2_write_line_sfa(struct Map_info *Map, int type,
  36. const struct line_pnts *points, const struct line_cats *cats)
  37. {
  38. #if defined HAVE_OGR || defined HAVE_POSTGRES
  39. int line;
  40. off_t offset;
  41. struct Plus_head *plus;
  42. struct bound_box box;
  43. struct Format_info_offset *offset_info;
  44. line = 0;
  45. plus = &(Map->plus);
  46. G_debug(3, "V2_write_line_sfa(): type = %d (format = %d)",
  47. type, Map->format);
  48. if (Map->format == GV_FORMAT_POSTGIS) {
  49. offset_info = &(Map->fInfo.pg.offset);
  50. offset = V1_write_line_pg(Map, type, points, cats);
  51. }
  52. else {
  53. offset_info = &(Map->fInfo.pg.offset);
  54. offset = V1_write_line_ogr(Map, type, points, cats);
  55. }
  56. if (offset < 0)
  57. return -1;
  58. /* Update topology */
  59. if (plus->built >= GV_BUILD_BASE) {
  60. dig_line_box(points, &box);
  61. line = dig_add_line(plus, type, points, &box, offset);
  62. G_debug(3, "\tline added to topo with line = %d", line);
  63. if (line == 1)
  64. Vect_box_copy(&(plus->box), &box);
  65. else
  66. Vect_box_extend(&(plus->box), &box);
  67. if (type == GV_BOUNDARY) {
  68. int ret, cline;
  69. long fid;
  70. double x, y;
  71. struct bound_box box;
  72. struct line_pnts *CPoints;
  73. /* add virtual centroid to pseudo-topology */
  74. ret = Vect_get_point_in_poly(points, &x, &y);
  75. if (ret == 0) {
  76. CPoints = Vect_new_line_struct();
  77. Vect_append_point(CPoints, x, y, 0.0);
  78. fid = offset_info->array[offset];
  79. dig_line_box(CPoints, &box);
  80. cline = dig_add_line(plus, GV_CENTROID,
  81. CPoints, &box, fid);
  82. G_debug(4, "\tCentroid: x = %f, y = %f, cat = %lu, line = %d",
  83. x, y, fid, cline);
  84. dig_cidx_add_cat(plus, 1, (int) fid,
  85. cline, GV_CENTROID);
  86. Vect_destroy_line_struct(CPoints);
  87. }
  88. else {
  89. G_warning(_("Unable to calculate centroid for area"));
  90. }
  91. }
  92. V2__add_line_to_topo_sfa(Map, line, points, cats);
  93. }
  94. G_debug(3, "updated lines : %d , updated nodes : %d", plus->uplist.n_uplines,
  95. plus->uplist.n_upnodes);
  96. /* returns int line, but is defined as off_t for compatibility with
  97. * Write_line_array in write.c */
  98. return line;
  99. #else
  100. G_fatal_error(_("GRASS is not compiled with OGR/PostgreSQL support"));
  101. return -1;
  102. #endif
  103. }
  104. /*!
  105. \brief Rewrites feature at the given offset on level 2 (OGR/PostGIS
  106. interface, pseudo-topological level)
  107. Note: Topology must be built at level >= GV_BUILD_BASE
  108. \param Map pointer to Map_info structure
  109. \param line feature id to be rewritten
  110. \param type feature type (see V1_write_line_ogr() for supported types)
  111. \param offset unused (kept for consistency)
  112. \param points pointer to line_pnts structure (feature geometry)
  113. \param cats pointer to line_cats structure feature categories
  114. \return offset where line was rewritten
  115. \return -1 on error
  116. */
  117. off_t V2_rewrite_line_sfa(struct Map_info *Map, int line, int type, off_t offset,
  118. const struct line_pnts *points, const struct line_cats *cats)
  119. {
  120. G_debug(3, "V2_rewrite_line_sfa(): line=%d type=%d offset=%"PRI_OFF_T,
  121. line, type, offset);
  122. if (line < 1 || line > Map->plus.n_lines) {
  123. G_warning(_("Attempt to access feature with invalid id (%d)"), line);
  124. return -1;
  125. }
  126. #if defined HAVE_OGR || defined HAVE_POSTGRES
  127. if (type != V2_read_line_sfa(Map, NULL, NULL, line)) {
  128. G_warning(_("Unable to rewrite feature (incompatible feature types)"));
  129. return -1;
  130. }
  131. if (V2_delete_line_sfa(Map, line) != 0)
  132. return -1;
  133. return V2_write_line_sfa(Map, type, points, cats);
  134. #else
  135. G_fatal_error(_("GRASS is not compiled with OGR/PostgreSQL support"));
  136. return -1;
  137. #endif
  138. }
  139. /*!
  140. \brief Deletes feature on level 2 (OGR/PostGIS interface)
  141. Note: Topology must be built at level >= GV_BUILD_BASE
  142. \todo Update fidx
  143. \param pointer to Map_info structure
  144. \param line feature id to be deleted
  145. \return 0 on success
  146. \return -1 on error
  147. */
  148. int V2_delete_line_sfa(struct Map_info *Map, int line)
  149. {
  150. #if defined HAVE_OGR || defined HAVE_POSTGRES
  151. int ret, i, type, first;
  152. struct P_line *Line;
  153. struct Plus_head *plus;
  154. static struct line_cats *Cats = NULL;
  155. static struct line_pnts *Points = NULL;
  156. G_debug(3, "V2_delete_line_sfa(): line = %d", line);
  157. type = first = 0;
  158. Line = NULL;
  159. plus = &(Map->plus);
  160. if (line < 1 || line > Map->plus.n_lines) {
  161. G_warning(_("Attempt to access feature with invalid id (%d)"), line);
  162. return -1;
  163. }
  164. if (plus->built >= GV_BUILD_BASE) {
  165. Line = Map->plus.Line[line];
  166. if (Line == NULL)
  167. G_fatal_error(_("Attempt to delete dead feature"));
  168. type = Line->type;
  169. }
  170. if (!Cats) {
  171. Cats = Vect_new_cats_struct();
  172. }
  173. if (!Points) {
  174. Points = Vect_new_line_struct();
  175. }
  176. type = V2_read_line_sfa(Map, Points, Cats, line);
  177. if (type < 0)
  178. return -1;
  179. /* Update category index */
  180. if (plus->update_cidx) {
  181. for (i = 0; i < Cats->n_cats; i++) {
  182. dig_cidx_del_cat(plus, Cats->field[i], Cats->cat[i], line, type);
  183. }
  184. }
  185. /* Update fidx */
  186. /* TODO */
  187. /* delete the line from coor */
  188. if (Map->format == GV_FORMAT_POSTGIS)
  189. ret = V1_delete_line_pg(Map, Line->offset);
  190. else
  191. ret = V1_delete_line_ogr(Map, Line->offset);
  192. if (ret == -1) {
  193. return ret;
  194. }
  195. /* Update topology */
  196. if (plus->built >= GV_BUILD_AREAS && type == GV_BOUNDARY) {
  197. /* TODO */
  198. /* remove centroid together with boundary (is really an OGR polygon) */
  199. }
  200. /* Delete reference from area */
  201. if (plus->built >= GV_BUILD_CENTROIDS && type == GV_CENTROID) {
  202. /* for OGR mapsets, virtual centroid will be removed when
  203. * polygon is removed */
  204. }
  205. /* delete the line from topo */
  206. dig_del_line(plus, line, Points->x[0], Points->y[0], Points->z[0]);
  207. /* Rebuild areas/isles and attach centroids and isles */
  208. if (plus->built >= GV_BUILD_AREAS && type == GV_BOUNDARY) {
  209. /* maybe not needed VERIFY */
  210. }
  211. return ret;
  212. #else
  213. G_fatal_error(_("GRASS is not compiled with OGR/PostgreSQL support"));
  214. return -1;
  215. #endif
  216. }
  217. /*!
  218. \brief Writes area on topological level (Simple Features interface,
  219. internal use only)
  220. \param Map pointer to Map_info structure
  221. \param points feature geometry (exterior + interior rings)
  222. \param nparts number of parts including exterior ring
  223. \param cats feature categories
  224. \return feature offset
  225. \return -1 on error
  226. */
  227. off_t V2__write_area_sfa(struct Map_info *Map,
  228. const struct line_pnts **points, int nparts,
  229. const struct line_cats *cats)
  230. {
  231. if (Map->format == GV_FORMAT_OGR) {
  232. #ifdef HAVE_OGR
  233. return V2__write_area_ogr(Map, points, nparts, cats);
  234. #else
  235. G_fatal_error(_("GRASS is not compiled with OGR support"));
  236. return -1;
  237. #endif
  238. }
  239. else if (Map->format == GV_FORMAT_POSTGIS) {
  240. #ifdef HAVE_POSTGRES
  241. return V2__write_area_pg(Map, points, nparts, cats);
  242. #else
  243. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  244. return -1;
  245. #endif
  246. }
  247. else {
  248. G_warning(_("Unsupported vector map format (%d)"), Map->format);
  249. }
  250. return -1;
  251. }
  252. #if defined HAVE_OGR || defined HAVE_POSTGRES
  253. /*!
  254. \brief Add feature to topo file (internal use only)
  255. \param Map pointer to Map_info structure
  256. \param line feature id
  257. \param points pointer to line_pnts structure (feature geometry)
  258. \param cats pointer to line_cats structure (feature categories)
  259. */
  260. void V2__add_line_to_topo_sfa(struct Map_info *Map, int line,
  261. const struct line_pnts *points,
  262. const struct line_cats *cats)
  263. {
  264. int first, s, i;
  265. int type, area, side;
  266. struct Plus_head *plus;
  267. struct P_line *Line;
  268. struct bound_box box, abox;
  269. G_debug(3, "V2__add_line_to_topo_sfa(): line = %d npoints = %d", line,
  270. points->n_points);
  271. plus = &(Map->plus);
  272. Line = plus->Line[line];
  273. type = Line->type;
  274. if (plus->built >= GV_BUILD_AREAS &&
  275. type == GV_BOUNDARY) {
  276. struct P_topo_b *topo = (struct P_topo_b *)Line->topo;
  277. if (topo->N1 != topo->N2) {
  278. G_warning(_("Boundary is not closed. Skipping."));
  279. return;
  280. }
  281. /* Build new areas/isles */
  282. for (s = 0; s < 2; s++) {
  283. side = (s == 0 ? GV_LEFT : GV_RIGHT);
  284. area = Vect_build_line_area(Map, line, side);
  285. if (area > 0) { /* area */
  286. Vect_get_area_box(Map, area, &box);
  287. if (first) {
  288. Vect_box_copy(&abox, &box);
  289. first = FALSE;
  290. }
  291. else
  292. Vect_box_extend(&abox, &box);
  293. }
  294. else if (area < 0) {
  295. /* isle -> must be attached -> add to abox */
  296. Vect_get_isle_box(Map, -area, &box);
  297. if (first) {
  298. Vect_box_copy(&abox, &box);
  299. first = FALSE;
  300. }
  301. else
  302. Vect_box_extend(&abox, &box);
  303. }
  304. G_debug(4, "Vect_build_line_area(): -> area = %d", area);
  305. }
  306. /* Attach centroid/isle to the new area */
  307. if (plus->built >= GV_BUILD_ATTACH_ISLES)
  308. Vect_attach_isles(Map, &abox);
  309. if (plus->built >= GV_BUILD_CENTROIDS)
  310. Vect_attach_centroids(Map, &abox);
  311. }
  312. /* Add category index */
  313. for (i = 0; i < cats->n_cats; i++) {
  314. dig_cidx_add_cat_sorted(plus, cats->field[i], cats->cat[i], line,
  315. type);
  316. }
  317. return;
  318. }
  319. #endif /* HAVE_OGR || HAVE_POSTGRES */