write_nat.c 28 KB

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