write_nat.c 30 KB

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