write_nat.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*!
  2. \file lib/vector/Vlib/write_nat.c
  3. \brief Vector library - write/modify/delete vector feature (native format)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2013 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  9. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  10. \author V*_restore_line() by Martin Landa <landa.martin gmail.com> (2008)
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include <grass/vector.h>
  16. #include <grass/glocale.h>
  17. #include "local_proto.h"
  18. static struct line_cats *Cats;
  19. static struct line_pnts *Points;
  20. static off_t V1__rewrite_line_nat(struct Map_info *, off_t, int,
  21. const struct line_pnts *, const struct line_cats *);
  22. static void V2__delete_area_cats_from_cidx_nat(struct Map_info *, int);
  23. static void V2__add_area_cats_to_cidx_nat(struct Map_info *, int);
  24. /*!
  25. \brief Writes feature to 'coor' file at level 1 (internal use only)
  26. \param Map pointer to Map_info structure
  27. \param type feature type (GV_POINT, GV_LINE, ...)
  28. \param points feature geometry
  29. \param cats feature categories
  30. \return feature offset into file
  31. \return -1 on error
  32. */
  33. off_t V1_write_line_nat(struct Map_info *Map, int type,
  34. const struct line_pnts *points, const struct line_cats *cats)
  35. {
  36. off_t offset;
  37. if (dig_fseek(&(Map->dig_fp), 0L, SEEK_END) == -1) /* set to end of file */
  38. return -1;
  39. offset = dig_ftell(&(Map->dig_fp));
  40. G_debug(3, "V1_write_line_nat(): offset = %lu", offset);
  41. if (offset == -1)
  42. return -1;
  43. return V1__rewrite_line_nat(Map, offset, type, points, cats);
  44. }
  45. /*!
  46. \brief Writes feature to 'coor' file at topological level (internal use only)
  47. Note: Function returns feature id, but is defined as off_t for
  48. compatibility with level 1 functions.
  49. \param Map pointer to Map_info structure
  50. \param type feature type (GV_POINT, GV_LINE, ...)
  51. \param points feature geometry
  52. \param cats feature categories
  53. \return new feature id
  54. \return 0 topology is not requested to be built (build level < GV_BUILD_BASE)
  55. \return -1 on error
  56. */
  57. off_t V2_write_line_nat(struct Map_info *Map, int type,
  58. const struct line_pnts *points, const struct line_cats *cats)
  59. {
  60. off_t offset;
  61. G_debug(3, "V2_write_line_nat(): type=%d", type);
  62. /* write feature to 'coor' file */
  63. offset = V1_write_line_nat(Map, type, points, cats);
  64. if (offset < 0)
  65. return -1;
  66. /* update topology (build level >= GV_BUILD_BASE) */
  67. return V2__add_line_to_topo_nat(Map, offset, type, points, cats, -1, NULL);
  68. }
  69. /*!
  70. \brief Rewrites feature to 'coor' file at level 1 (internal use only)
  71. If the number of points or cats differs from the original one or the
  72. type is changed: GV_POINTS -> GV_LINES or GV_LINES -> GV_POINTS, the
  73. old one is deleted and the new is appended to the end of the file.
  74. Old feature is deleted (marked as dead), and a new feature written.
  75. \param Map pointer to Map_info structure
  76. \param line feature id to be rewritten
  77. \param offset feature offset
  78. \param type feature type (GV_POINT, GV_LINE, ...)
  79. \param points feature geometry
  80. \param cats feature categories
  81. \return feature offset (rewriten feature)
  82. \return -1 on error
  83. */
  84. off_t V1_rewrite_line_nat(struct Map_info *Map, int line, int type, off_t offset,
  85. const struct line_pnts *points, const struct line_cats *cats)
  86. {
  87. int old_type;
  88. struct line_pnts *old_points;
  89. struct line_cats *old_cats;
  90. off_t new_offset;
  91. G_debug(3, "V1_rewrite_line_nat(): line = %d offset = %lu",
  92. line, (unsigned long) offset);
  93. /* TODO: enable points and cats == NULL */
  94. /* First compare numbers of points and cats with tha old one */
  95. old_points = Vect_new_line_struct();
  96. old_cats = Vect_new_cats_struct();
  97. old_type = V1_read_line_nat(Map, old_points, old_cats, offset);
  98. if (old_type == -1)
  99. return (-1); /* error */
  100. if (old_type != -2 /* EOF -> write new line */
  101. && points->n_points == old_points->n_points
  102. && cats->n_cats == old_cats->n_cats
  103. && (((type & GV_POINTS) && (old_type & GV_POINTS))
  104. || ((type & GV_LINES) && (old_type & GV_LINES)))) {
  105. /* equal -> overwrite the old */
  106. return V1__rewrite_line_nat(Map, offset, type, points, cats);
  107. }
  108. else {
  109. /* differ -> delete the old and append new */
  110. /* delete old */
  111. V1_delete_line_nat(Map, offset);
  112. /* write new */
  113. if (dig_fseek(&(Map->dig_fp), 0L, SEEK_END) == -1) /* end of file */
  114. return -1;
  115. new_offset = dig_ftell(&(Map->dig_fp));
  116. if (new_offset == -1)
  117. return -1;
  118. return V1__rewrite_line_nat(Map, new_offset, type, points, cats);
  119. }
  120. }
  121. /*!
  122. \brief Rewrites feature to 'coor' file at topological level (internal use only)
  123. Note: Topology must be built at level >= GV_BUILD_BASE
  124. Note: Function returns feature id, but is defined as off_t for
  125. compatibility with level 1 functions.
  126. \param Map pointer to Map_info structure
  127. \param type feature type (GV_POINT, GV_LINE, ...)
  128. \param line feature id to be rewritten
  129. \param old_offset feature offset
  130. \param points feature geometry
  131. \param cats feature categories
  132. \return new feature id
  133. \return -1 on error
  134. */
  135. off_t V2_rewrite_line_nat(struct Map_info *Map, int line, int type, off_t old_offset,
  136. const struct line_pnts *points, const struct line_cats *cats)
  137. {
  138. /* TODO: this is just quick shortcut because we have already V2_delete_nat()
  139. * and V2_write_nat() this function first deletes old line
  140. * and then writes new one. It is not very effective if number of points
  141. * and cats was not changed or topology is not changed (nodes not moved,
  142. * angles not changed etc.) */
  143. off_t offset;
  144. if (0 != V2_delete_line_nat(Map, line))
  145. return -1;
  146. G_debug(3, "V2_write_line_nat(), line = %d", line);
  147. /* rewrite feature in 'coor' file */
  148. offset = V1_rewrite_line_nat(Map, line, type, old_offset, points, cats);
  149. if (offset < 0)
  150. return -1;
  151. /* update topology */
  152. return V2__add_line_to_topo_nat(Map, offset, type, points, cats, -1, NULL);
  153. }
  154. /*!
  155. \brief Deletes feature at level 1 (internal use only)
  156. \param Map pointer Map_info structure
  157. \param offset feature offset
  158. \return 0 on success
  159. \return -1 on error
  160. */
  161. int V1_delete_line_nat(struct Map_info *Map, off_t offset)
  162. {
  163. char rhead;
  164. struct gvfile *dig_fp;
  165. G_debug(3, "V1_delete_line_nat(): offset = %lu", (unsigned long) offset);
  166. dig_set_cur_port(&(Map->head.port));
  167. dig_fp = &(Map->dig_fp);
  168. if (dig_fseek(dig_fp, offset, 0) == -1)
  169. return -1;
  170. /* read old */
  171. if (0 >= dig__fread_port_C(&rhead, 1, dig_fp))
  172. return -1;
  173. rhead &= 0xFE;
  174. if (dig_fseek(dig_fp, offset, 0) == -1)
  175. return -1;
  176. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp))
  177. return -1;
  178. if (0 != dig_fflush(dig_fp))
  179. return -1;
  180. return 0;
  181. }
  182. /*!
  183. \brief Deletes feature at topological level (internal use only)
  184. Note: Topology must be built at level >= GV_BUILD_BASE
  185. \param pointer to Map_info structure
  186. \param line feature id
  187. \return 0 on success
  188. \return -1 on error
  189. */
  190. int V2_delete_line_nat(struct Map_info *Map, int line)
  191. {
  192. int type;
  193. struct P_line *Line;
  194. struct Plus_head *plus;
  195. G_debug(3, "V2_delete_line_nat(): line = %d", line);
  196. Line = NULL;
  197. plus = &(Map->plus);
  198. if (line < 1 || line > plus->n_lines) {
  199. G_warning(_("Attempt to access feature with invalid id (%d)"), line);
  200. return -1;
  201. }
  202. Line = Map->plus.Line[line];
  203. if (Line == NULL) {
  204. G_warning(_("Attempt to access dead feature %d"), line);
  205. return -1;
  206. }
  207. /* read the line */
  208. if (!Points) {
  209. Points = Vect_new_line_struct();
  210. }
  211. if (!Cats) {
  212. Cats = Vect_new_cats_struct();
  213. }
  214. type = V2_read_line_nat(Map, Points, Cats, line);
  215. if (type < 0)
  216. return -1;
  217. /* delete feature from coor file */
  218. if (0 != V1_delete_line_nat(Map, Line->offset))
  219. return -1;
  220. /* delete feature from topology */
  221. if (0 != V2__delete_line_from_topo_nat(Map, line, type, Points, Cats))
  222. return -1;
  223. return 0;
  224. }
  225. /*!
  226. \brief Restores feature at level 1 (internal use only)
  227. \param Map pointer to Map_info structure
  228. \param offset feature offset
  229. \return 0 on success
  230. \return -1 on error
  231. */
  232. int V1_restore_line_nat(struct Map_info *Map, off_t offset)
  233. {
  234. char rhead;
  235. struct gvfile *dig_fp;
  236. G_debug(3, "V1_restore_line_nat(), offset = %lu", (unsigned long) offset);
  237. dig_set_cur_port(&(Map->head.port));
  238. dig_fp = &(Map->dig_fp);
  239. if (dig_fseek(dig_fp, offset, 0) == -1)
  240. return -1;
  241. /* read old */
  242. if (0 >= dig__fread_port_C(&rhead, 1, dig_fp))
  243. return -1;
  244. /* mark as alive */
  245. rhead |= 1;
  246. /* write new */
  247. if (dig_fseek(dig_fp, offset, 0) == -1)
  248. return -1;
  249. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp))
  250. return -1;
  251. if (0 != dig_fflush(dig_fp))
  252. return -1;
  253. return 0;
  254. }
  255. /*!
  256. \brief Restores feature at topological level (internal use only)
  257. Note: This function requires build level >= GV_BUILD_BASE.
  258. \param Map pointer to Map_info structure
  259. \param line feature id
  260. \param offset feature offset
  261. \return 0 on success
  262. \return -1 on error
  263. */
  264. int V2_restore_line_nat(struct Map_info *Map, int line, off_t offset)
  265. {
  266. int type;
  267. struct Plus_head *plus;
  268. struct P_line *Line;
  269. plus = &(Map->plus);
  270. G_debug(3, "V2_restore_line_nat(), line = %d", line);
  271. if (line < 1 || line > plus->n_lines) {
  272. G_warning(_("Attempt to access feature with invalid id (%d)"), line);
  273. return -1;
  274. }
  275. Line = Map->plus.Line[line];
  276. if (Line != NULL) {
  277. G_warning(_("Attempt to access alive feature %d"), line);
  278. return -1;
  279. }
  280. /* restore feature in 'coor' file */
  281. if (0 != V1_restore_line_nat(Map, offset))
  282. return -1;
  283. /* read feature geometry */
  284. if (!Points)
  285. Points = Vect_new_line_struct();
  286. if (!Cats)
  287. Cats = Vect_new_cats_struct();
  288. type = V1_read_line_nat(Map, Points, Cats, offset);
  289. if (type < 0)
  290. return -1;
  291. /* update topology */
  292. return V2__add_line_to_topo_nat(Map, offset, type, Points, Cats, line, NULL) > 0 ? 0 : -1;
  293. }
  294. /*** static or internal subroutines bellow ****/
  295. /*!
  296. \brief Rewrites feature at the given offset
  297. \param Map pointer to Map_info structure
  298. \param offset feature offset
  299. \param type feature type (GV_POINT, GV_LINE, ...)
  300. \param points feature geometry
  301. \param cats feature categories
  302. \return feature offset
  303. \return -1 on error
  304. */
  305. off_t V1__rewrite_line_nat(struct Map_info *Map,
  306. off_t offset, int type,
  307. const struct line_pnts *points, const struct line_cats *cats)
  308. {
  309. int i, n_points;
  310. char rhead, nc;
  311. short field;
  312. struct gvfile *dig_fp;
  313. dig_set_cur_port(&(Map->head.port));
  314. dig_fp = &(Map->dig_fp);
  315. if (dig_fseek(dig_fp, offset, 0) == -1)
  316. return -1;
  317. /* first byte: 0 bit: 1 - alive, 0 - dead
  318. * 1 bit: 1 - categories, 0 - no category
  319. * 2-3 bit: store type
  320. * 4-5 bit: reserved for store type expansion
  321. * 6-7 bit: not used
  322. */
  323. rhead = (char)dig_type_to_store(type);
  324. rhead <<= 2;
  325. if (cats->n_cats > 0) {
  326. rhead |= 0x02;
  327. }
  328. rhead |= 0x01; /* written/rewritten is always alive */
  329. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp)) {
  330. return -1;
  331. }
  332. if (cats->n_cats > 0) {
  333. if (Map->head.coor_version.minor == 1) { /* coor format 5.1 */
  334. if (0 >= dig__fwrite_port_I(&(cats->n_cats), 1, dig_fp))
  335. return -1;
  336. }
  337. else { /* coor format 5.0 */
  338. nc = (char)cats->n_cats;
  339. if (0 >= dig__fwrite_port_C(&nc, 1, dig_fp))
  340. return -1;
  341. }
  342. if (cats->n_cats > 0) {
  343. if (Map->head.coor_version.minor == 1) { /* coor format 5.1 */
  344. if (0 >=
  345. dig__fwrite_port_I(cats->field, cats->n_cats, dig_fp))
  346. return -1;
  347. }
  348. else { /* coor format 5.0 */
  349. for (i = 0; i < cats->n_cats; i++) {
  350. field = (short)cats->field[i];
  351. if (0 >= dig__fwrite_port_S(&field, 1, dig_fp))
  352. return -1;
  353. }
  354. }
  355. if (0 >= dig__fwrite_port_I(cats->cat, cats->n_cats, dig_fp))
  356. return -1;
  357. }
  358. }
  359. if (type & GV_POINTS) {
  360. n_points = 1;
  361. }
  362. else {
  363. n_points = points->n_points;
  364. if (0 >= dig__fwrite_port_I(&n_points, 1, dig_fp))
  365. return -1;
  366. }
  367. if (0 >= dig__fwrite_port_D(points->x, n_points, dig_fp))
  368. return -1;
  369. if (0 >= dig__fwrite_port_D(points->y, n_points, dig_fp))
  370. return -1;
  371. if (Map->head.with_z) {
  372. if (0 >= dig__fwrite_port_D(points->z, n_points, dig_fp))
  373. return -1;
  374. }
  375. if (0 != dig_fflush(dig_fp))
  376. return -1;
  377. return offset;
  378. }
  379. /*!
  380. \brief Deletes area (i.e. centroid) categories from category
  381. index (internal use only)
  382. Call G_fatal_error() when area do not exits.
  383. \param Map pointer to Map_info structure
  384. \param area area id
  385. */
  386. void V2__delete_area_cats_from_cidx_nat(struct Map_info *Map, int area)
  387. {
  388. int i;
  389. struct P_area *Area;
  390. static struct line_cats *Cats = NULL;
  391. G_debug(3, "V2__delete_area_cats_from_cidx_nat(), area = %d", area);
  392. Area = Map->plus.Area[area];
  393. if (!Area)
  394. G_fatal_error(_("%s: Area %d does not exist"),
  395. "delete_area_cats_from_cidx()", area);
  396. if (Area->centroid == 0) /* no centroid found */
  397. return;
  398. if (!Cats)
  399. Cats = Vect_new_cats_struct();
  400. Vect_read_line(Map, NULL, Cats, Area->centroid);
  401. for (i = 0; i < Cats->n_cats; i++) {
  402. dig_cidx_del_cat(&(Map->plus), Cats->field[i], Cats->cat[i], area,
  403. GV_AREA);
  404. }
  405. }
  406. /*!
  407. \brief Adds area (i.e. centroid) categories from category index
  408. (internal use only)
  409. Call G_fatal_error() when area do not exits.
  410. \param Map pointer to Map_info structure
  411. \param area area id
  412. */
  413. void V2__add_area_cats_to_cidx_nat(struct Map_info *Map, int area)
  414. {
  415. int i;
  416. struct P_area *Area;
  417. static struct line_cats *Cats = NULL;
  418. G_debug(3, "V2__add_area_cats_to_cidx_nat(), area = %d", area);
  419. Area = Map->plus.Area[area];
  420. if (!Area)
  421. G_fatal_error(_("%s: Area %d does not exist"),
  422. "add_area_cats_to_cidx():", area);
  423. if (Area->centroid == 0) /* no centroid found */
  424. return;
  425. if (!Cats)
  426. Cats = Vect_new_cats_struct();
  427. V2_read_line_nat(Map, NULL, Cats, Area->centroid);
  428. for (i = 0; i < Cats->n_cats; i++) {
  429. dig_cidx_add_cat_sorted(&(Map->plus), Cats->field[i], Cats->cat[i],
  430. area, GV_AREA);
  431. }
  432. }
  433. /*!
  434. \brief Delete feature from topology (internal use only)
  435. Note: This function requires build level >= GV_BUILD_BASE.
  436. Also updates category index if requested.
  437. Calls G_warning() on error.
  438. \param Map pointer to Map_info struct
  439. \param line feature id to be removed
  440. \param Points feature geometry (pointer to line_pnts struct)
  441. \param external_routine external subroutine to execute (used by PostGIS Topology)
  442. \return 0 on success
  443. \return -1 on failure
  444. */
  445. int V2__delete_line_from_topo_nat(struct Map_info *Map, int line, int type,
  446. const struct line_pnts *points, const struct line_cats *cats)
  447. {
  448. int i, first;
  449. int adjacent[4], n_adjacent;
  450. struct bound_box box, abox;
  451. struct Plus_head *plus;
  452. struct P_line *Line;
  453. n_adjacent = 0;
  454. plus = &(Map->plus);
  455. if (line < 1 || line > plus->n_lines) {
  456. G_warning(_("Attempt to access feature with invalid id (%d)"), line);
  457. return -1;
  458. }
  459. Line = Map->plus.Line[line];
  460. if (!Line) {
  461. G_warning(_("Attempt to access dead feature %d"), line);
  462. return -1;
  463. }
  464. /* delete feature from category index */
  465. if (plus->update_cidx && cats) {
  466. for (i = 0; i < cats->n_cats; i++) {
  467. dig_cidx_del_cat(plus, cats->field[i], cats->cat[i], line, type);
  468. }
  469. }
  470. /* update areas when deleting boundary from topology */
  471. if (plus->built >= GV_BUILD_AREAS && Line->type == GV_BOUNDARY) {
  472. int next_line;
  473. struct P_topo_b *topo = (struct P_topo_b *)Line->topo;
  474. /* store adjacent boundaries at nodes (will be used to rebuild area/isle) */
  475. /* adjacent are stored: > 0 - we want right side; < 0 - we want left side */
  476. n_adjacent = 0;
  477. next_line = dig_angle_next_line(plus, line, GV_RIGHT, GV_BOUNDARY, NULL);
  478. if (next_line != 0 && abs(next_line) != line) {
  479. /* N1, to the right -> we want the right side for > 0 and left for < 0 */
  480. adjacent[n_adjacent] = next_line;
  481. n_adjacent++;
  482. }
  483. next_line = dig_angle_next_line(plus, line, GV_LEFT, GV_BOUNDARY, NULL);
  484. if (next_line != 0 && abs(next_line) != line) {
  485. /* N1, to the left -> we want the left side for > 0 and right for < 0 */
  486. adjacent[n_adjacent] = -next_line;
  487. n_adjacent++;
  488. }
  489. next_line = dig_angle_next_line(plus, -line, GV_RIGHT, GV_BOUNDARY, NULL);
  490. if (next_line != 0 && abs(next_line) != line) {
  491. /* N2, to the right -> we want the right side for > 0 and left for < 0 */
  492. adjacent[n_adjacent] = next_line;
  493. n_adjacent++;
  494. }
  495. next_line = dig_angle_next_line(plus, -line, GV_LEFT, GV_BOUNDARY, NULL);
  496. if (next_line != 0 && abs(next_line) != line) {
  497. /* N2, to the left -> we want the left side for > 0 and right for < 0 */
  498. adjacent[n_adjacent] = -next_line;
  499. n_adjacent++;
  500. }
  501. /* delete area(s) and islands this line forms */
  502. first = 1;
  503. if (topo->left > 0) { /* delete area */
  504. Vect_get_area_box(Map, topo->left, &box);
  505. if (first) {
  506. Vect_box_copy(&abox, &box);
  507. first = 0;
  508. }
  509. else
  510. Vect_box_extend(&abox, &box);
  511. if (plus->update_cidx) {
  512. V2__delete_area_cats_from_cidx_nat(Map, topo->left);
  513. }
  514. dig_del_area(plus, topo->left);
  515. }
  516. else if (topo->left < 0) { /* delete isle */
  517. dig_del_isle(plus, -topo->left);
  518. }
  519. if (topo->right > 0) { /* delete area */
  520. Vect_get_area_box(Map, topo->right, &box);
  521. if (first) {
  522. Vect_box_copy(&abox, &box);
  523. first = 0;
  524. }
  525. else
  526. Vect_box_extend(&abox, &box);
  527. if (plus->update_cidx) {
  528. V2__delete_area_cats_from_cidx_nat(Map, topo->right);
  529. }
  530. dig_del_area(plus, topo->right);
  531. }
  532. else if (topo->right < 0) { /* delete isle */
  533. dig_del_isle(plus, -topo->right);
  534. }
  535. }
  536. /* delete reference from area */
  537. if (plus->built >= GV_BUILD_CENTROIDS && Line->type == GV_CENTROID) {
  538. struct P_area *Area;
  539. struct P_topo_c *topo = (struct P_topo_c *)Line->topo;
  540. if (topo->area > 0) {
  541. G_debug(3, "Remove centroid %d from area %d", (int) line, topo->area);
  542. if (plus->update_cidx) {
  543. V2__delete_area_cats_from_cidx_nat(Map, topo->area);
  544. }
  545. Area = Map->plus.Area[topo->area];
  546. if (Area)
  547. Area->centroid = 0;
  548. else
  549. G_warning(_("Attempt to access dead area %d"), topo->area);
  550. }
  551. }
  552. /* delete the line from topo */
  553. if (0 != dig_del_line(plus, line, points->x[0], points->y[0], points->z[0]))
  554. return -1;
  555. /* rebuild areas/isles and attach centroids and isles */
  556. if (plus->built >= GV_BUILD_AREAS && type == GV_BOUNDARY) {
  557. int i, side, area;
  558. int new_areas[4], nnew_areas = 0;
  559. /* Rebuild areas/isles */
  560. for (i = 0; i < n_adjacent; i++) {
  561. side = (adjacent[i] > 0 ? GV_RIGHT : GV_LEFT);
  562. G_debug(3, "Build area for line = %d, side = %d", adjacent[i],
  563. side);
  564. area = Vect_build_line_area(Map, abs(adjacent[i]), side);
  565. if (area > 0) { /* area */
  566. Vect_get_area_box(Map, area, &box);
  567. if (first) {
  568. Vect_box_copy(&abox, &box);
  569. first = 0;
  570. }
  571. else
  572. Vect_box_extend(&abox, &box);
  573. new_areas[nnew_areas] = area;
  574. nnew_areas++;
  575. }
  576. else if (area < 0) {
  577. /* isle -> must be attached -> add to abox */
  578. Vect_get_isle_box(Map, -area, &box);
  579. if (first) {
  580. Vect_box_copy(&abox, &box);
  581. first = 0;
  582. }
  583. else
  584. Vect_box_extend(&abox, &box);
  585. }
  586. }
  587. /* reattach all centroids/isles in deleted areas + new area.
  588. * because isles are selected by box it covers also possible new isle created above */
  589. if (!first) { /* i.e. old area/isle was deleted or new one created */
  590. /* reattach isles */
  591. if (plus->built >= GV_BUILD_ATTACH_ISLES)
  592. Vect_attach_isles(Map, &abox);
  593. /* reattach centroids */
  594. if (plus->built >= GV_BUILD_CENTROIDS)
  595. Vect_attach_centroids(Map, &abox);
  596. }
  597. if (plus->update_cidx) {
  598. for (i = 0; i < nnew_areas; i++) {
  599. V2__add_area_cats_to_cidx_nat(Map, new_areas[i]);
  600. }
  601. }
  602. }
  603. if (plus->uplist.do_uplist) {
  604. G_debug(3, "updated lines : %d , updated nodes : %d", plus->uplist.n_uplines,
  605. plus->uplist.n_upnodes);
  606. }
  607. return 0;
  608. }
  609. /*!
  610. \brief Add feature (line) to topology (internal use only)
  611. Also updates category index if requested.
  612. Update areas. Areas are modified if:
  613. 1) first or/and last point are existing nodes ->
  614. - drop areas/islands whose boundaries are neighbour to this boundary at these nodes
  615. - try build areas and islands for this boundary and neighbour boundaries going through these nodes
  616. Question: may be by adding line created new area/isle which doesn't go through nodes of this line
  617. <pre>
  618. old new line
  619. +----+----+ +----+----+ +----+----+
  620. | A1 | A2 | + / -> | A1 | /| or + \ -> | A1 | A2 | \
  621. | | | | | | | | |
  622. +----+----+ +----+----+ +----+----+
  623. I1 I1 I1 I1
  624. </pre>
  625. - re-attach all centroids/isles inside new area(s)
  626. - attach new isle to area outside
  627. 2) line is closed ring (node at the end is new, so it is not case above)
  628. - build new area/isle
  629. - check if it is island or contains island(s)
  630. - re-attach all centroids/isles inside new area(s)
  631. - attach new isle to area outside
  632. Note that 1) and 2) is done by the same code.
  633. \param Map pointer to Map_info structure
  634. \param line feature id to be added
  635. \param points pointer to line_pnts structure (feature's geometry)
  636. \param cats pointer to line_cats structure (feature's categories)
  637. \param external_routine pointer to external routine (used by PostGIS Topology)
  638. \return feature id to be added
  639. \return 0 nothing to do (build level must be >= GV_BUILD_BASE)
  640. \return -1 on error
  641. */
  642. int V2__add_line_to_topo_nat(struct Map_info *Map, off_t offset, int type,
  643. const struct line_pnts *points, const struct line_cats *cats,
  644. int restore_line,
  645. int (*external_routine) (const struct Map_info *, int))
  646. {
  647. int first, s, n, i, line;
  648. int node, next_line, area, side, sel_area, new_area[2];
  649. struct Plus_head *plus;
  650. struct P_line *Line, *NLine;
  651. struct P_node *Node;
  652. struct P_area *Area;
  653. struct bound_box box, abox;
  654. plus = &(Map->plus);
  655. G_debug(3, "V2__add_line_to_topo_nat(): offset = %ld (build level = %d)", offset, plus->built);
  656. if (plus->built < GV_BUILD_BASE) /* nothing to build */
  657. return 0;
  658. /* add line to topology */
  659. dig_line_box(points, &box);
  660. if (restore_line > 0)
  661. line = dig_restore_line(plus, restore_line, type, points, &box, offset);
  662. else
  663. line = dig_add_line(plus, type, points, &box, offset);
  664. G_debug(3, " line added to topo with id = %d", line);
  665. Line = plus->Line[line];
  666. /* extend map bounding box */
  667. if (line == 1)
  668. Vect_box_copy(&(plus->box), &box);
  669. else
  670. Vect_box_extend(&(plus->box), &box);
  671. /* build areas on left/right side */
  672. if (plus->built >= GV_BUILD_AREAS &&
  673. type == GV_BOUNDARY) {
  674. struct P_topo_b *topo = (struct P_topo_b *)Line->topo;
  675. /* delete neighbour areas/isles */
  676. first = TRUE;
  677. for (s = 0; s < 2; s++) { /* for each node */
  678. node = (s == 0 ? topo->N1 : topo->N2);
  679. G_debug(3,
  680. " delete neighbour areas/isles: %s node = %d",
  681. (s == 0 ? "first" : "second"), node);
  682. Node = plus->Node[node];
  683. n = 0;
  684. for (i = 0; i < Node->n_lines; i++) {
  685. NLine = plus->Line[abs(Node->lines[i])];
  686. if (NLine->type == GV_BOUNDARY)
  687. n++;
  688. }
  689. G_debug(3, " number of boundaries at node = %d", n);
  690. if (n > 2) {
  691. /* more than 2 boundaries at node ( >= 2 old + 1 new ) */
  692. /* Line above (to the right), it is enough to check to
  693. the right, because if area/isle exists it is the
  694. same to the left */
  695. if (!s)
  696. next_line =
  697. dig_angle_next_line(plus, line, GV_RIGHT,
  698. GV_BOUNDARY, NULL);
  699. else
  700. next_line =
  701. dig_angle_next_line(plus, -line, GV_RIGHT,
  702. GV_BOUNDARY, NULL);
  703. if (next_line != 0) { /* there is a boundary to the right */
  704. NLine = plus->Line[abs(next_line)];
  705. topo = (struct P_topo_b *)NLine->topo;
  706. if (next_line > 0) /* the boundary is connected by 1. node */
  707. /* we are interested just in this side (close to our line) */
  708. area = topo->right;
  709. else if (next_line < 0) /* the boundary is connected by 2. node */
  710. area = topo->left;
  711. G_debug(3, " next_line = %d area = %d", next_line,
  712. area);
  713. if (area > 0) { /* is area */
  714. Vect_get_area_box(Map, area, &box);
  715. if (first) {
  716. Vect_box_copy(&abox, &box);
  717. first = FALSE;
  718. }
  719. else
  720. Vect_box_extend(&abox, &box);
  721. if (plus->update_cidx) {
  722. V2__delete_area_cats_from_cidx_nat(Map, area);
  723. }
  724. dig_del_area(plus, area);
  725. if (external_routine) /* call external subroutine if defined */
  726. external_routine(Map, area);
  727. }
  728. else if (area < 0) { /* is isle */
  729. dig_del_isle(plus, -area);
  730. if (external_routine) /* call external subroutine if defined */
  731. external_routine(Map, area);
  732. }
  733. }
  734. }
  735. }
  736. /* Build new areas/isles.
  737. * It's true that we deleted also adjacent areas/isles, but
  738. * if they form new one our boundary must participate, so
  739. * we need to build areas/isles just for our boundary */
  740. for (s = 0; s < 2; s++) {
  741. side = (s == 0 ? GV_LEFT : GV_RIGHT);
  742. area = Vect_build_line_area(Map, line, side);
  743. if (area > 0) { /* area */
  744. Vect_get_area_box(Map, area, &box);
  745. if (first) {
  746. Vect_box_copy(&abox, &box);
  747. first = FALSE;
  748. }
  749. else
  750. Vect_box_extend(&abox, &box);
  751. }
  752. else if (area < 0) {
  753. /* isle -> must be attached -> add to abox */
  754. Vect_get_isle_box(Map, -area, &box);
  755. if (first) {
  756. Vect_box_copy(&abox, &box);
  757. first = FALSE;
  758. }
  759. else
  760. Vect_box_extend(&abox, &box);
  761. }
  762. new_area[s] = area;
  763. }
  764. /* Reattach all centroids/isles in deleted areas + new area.
  765. * Because isles are selected by box it covers also possible
  766. * new isle created above */
  767. if (!first) { /* i.e. old area/isle was deleted or new one created */
  768. /* Reattach isles */
  769. if (plus->built >= GV_BUILD_ATTACH_ISLES)
  770. Vect_attach_isles(Map, &abox);
  771. /* Reattach centroids */
  772. if (plus->built >= GV_BUILD_CENTROIDS)
  773. Vect_attach_centroids(Map, &abox);
  774. }
  775. /* Add to category index */
  776. if (plus->update_cidx) {
  777. for (s = 0; s < 2; s++) {
  778. if (new_area[s] > 0) {
  779. V2__add_area_cats_to_cidx_nat(Map, new_area[s]);
  780. }
  781. }
  782. }
  783. }
  784. /* attach centroid */
  785. if (plus->built >= GV_BUILD_CENTROIDS) {
  786. struct P_topo_c *topo;
  787. if (type == GV_CENTROID) {
  788. sel_area = Vect_find_area(Map, points->x[0], points->y[0]);
  789. G_debug(3, " new centroid %d is in area %d", line, sel_area);
  790. if (sel_area > 0) {
  791. Area = plus->Area[sel_area];
  792. Line = plus->Line[line];
  793. topo = (struct P_topo_c *)Line->topo;
  794. if (Area->centroid == 0) { /* first centroid */
  795. G_debug(3, " first centroid -> attach to area");
  796. Area->centroid = line;
  797. topo->area = sel_area;
  798. if (plus->update_cidx) {
  799. V2__add_area_cats_to_cidx_nat(Map, sel_area);
  800. }
  801. }
  802. else { /* duplicate centroid */
  803. G_debug(3,
  804. " duplicate centroid -> do not attach to area");
  805. topo->area = -sel_area;
  806. }
  807. }
  808. }
  809. }
  810. /* add category index */
  811. if (plus->update_cidx && cats) {
  812. for (i = 0; i < cats->n_cats; i++) {
  813. dig_cidx_add_cat_sorted(plus, cats->field[i], cats->cat[i], line,
  814. type);
  815. }
  816. }
  817. if (plus->uplist.do_uplist) {
  818. G_debug(3, "updated lines : %d , updated nodes : %d", plus->uplist.n_uplines,
  819. plus->uplist.n_upnodes);
  820. }
  821. return line;
  822. }