spindex_rw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Radim Blazek.
  7. *
  8. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  9. *
  10. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <grass/config.h>
  18. #include <sys/types.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/Vect.h>
  23. int dig_Wr_spindx_head(GVFILE * fp, struct Plus_head *ptr)
  24. {
  25. unsigned char buf[5];
  26. long length = 42;
  27. dig_rewind(fp);
  28. dig_set_cur_port(&(ptr->spidx_port));
  29. /* bytes 1 - 5 */
  30. buf[0] = GV_SIDX_VER_MAJOR;
  31. buf[1] = GV_SIDX_VER_MINOR;
  32. buf[2] = GV_SIDX_EARLIEST_MAJOR;
  33. buf[3] = GV_SIDX_EARLIEST_MINOR;
  34. buf[4] = ptr->spidx_port.byte_order;
  35. if (0 >= dig__fwrite_port_C(buf, 5, fp))
  36. return (-1);
  37. /* get required offset size */
  38. if (ptr->off_t_size == 0) {
  39. /* should not happen, topo is written first */
  40. if (ptr->coor_size > (off_t)PORT_LONG_MAX)
  41. ptr->off_t_size = 8;
  42. else
  43. ptr->off_t_size = 4;
  44. }
  45. /* adjust header size for large files */
  46. if (ptr->off_t_size == 8) {
  47. /* 7 offset values and coor file size: add 8 * 4 */
  48. length += 32;
  49. }
  50. /* bytes 6 - 9 : header size */
  51. if (0 >= dig__fwrite_port_L(&length, 1, fp))
  52. return (0);
  53. /* byte 10 : dimension 2D or 3D */
  54. buf[0] = ptr->spidx_with_z;
  55. if (0 >= dig__fwrite_port_C(buf, 1, fp))
  56. return (-1);
  57. /* bytes 11 - 38 (large files 11 - 66) : Offsets */
  58. if (0 >= dig__fwrite_port_O(&(ptr->Node_spidx_offset), 1, fp, ptr->off_t_size))
  59. return (-1);
  60. if (0 >= dig__fwrite_port_O(&(ptr->Edge_spidx_offset), 1, fp, ptr->off_t_size))
  61. return (-1);
  62. if (0 >= dig__fwrite_port_O(&(ptr->Line_spidx_offset), 1, fp, ptr->off_t_size))
  63. return (-1);
  64. if (0 >= dig__fwrite_port_O(&(ptr->Area_spidx_offset), 1, fp, ptr->off_t_size))
  65. return (-1);
  66. if (0 >= dig__fwrite_port_O(&(ptr->Isle_spidx_offset), 1, fp, ptr->off_t_size))
  67. return (-1);
  68. if (0 >= dig__fwrite_port_O(&(ptr->Volume_spidx_offset), 1, fp, ptr->off_t_size))
  69. return (-1);
  70. if (0 >= dig__fwrite_port_O(&(ptr->Hole_spidx_offset), 1, fp, ptr->off_t_size))
  71. return (-1);
  72. G_debug(3, "spidx offset node = %ld line = %ld, area = %ld isle = %ld",
  73. ptr->Node_spidx_offset, ptr->Line_spidx_offset,
  74. ptr->Area_spidx_offset, ptr->Isle_spidx_offset);
  75. /* bytes 39 - 42 (large files 67 - 74) : Offsets */
  76. if (0 >= dig__fwrite_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size))
  77. return (-1);
  78. G_debug(2, "spidx body offset %ld", dig_ftell(fp));
  79. return (0);
  80. }
  81. int dig_Rd_spindx_head(GVFILE * fp, struct Plus_head *ptr)
  82. {
  83. unsigned char buf[5];
  84. int byte_order;
  85. long coor_size;
  86. dig_rewind(fp);
  87. /* bytes 1 - 5 */
  88. if (0 >= dig__fread_port_C(buf, 5, fp))
  89. return (-1);
  90. ptr->spidx_Version_Major = buf[0];
  91. ptr->spidx_Version_Minor = buf[1];
  92. ptr->spidx_Back_Major = buf[2];
  93. ptr->spidx_Back_Minor = buf[3];
  94. byte_order = buf[4];
  95. G_debug(2,
  96. "Sidx header: file version %d.%d , supported from GRASS version %d.%d",
  97. ptr->spidx_Version_Major, ptr->spidx_Version_Minor,
  98. ptr->spidx_Back_Major, ptr->spidx_Back_Minor);
  99. G_debug(2, " byte order %d", byte_order);
  100. /* check version numbers */
  101. if (ptr->spidx_Version_Major > GV_SIDX_VER_MAJOR ||
  102. ptr->spidx_Version_Minor > GV_SIDX_VER_MINOR) {
  103. /* The file was created by GRASS library with higher version than this one */
  104. if (ptr->spidx_Back_Major > GV_SIDX_VER_MAJOR ||
  105. ptr->spidx_Back_Minor > GV_SIDX_VER_MINOR) {
  106. /* This version of GRASS lib is lower than the oldest which can read this format */
  107. G_fatal_error
  108. ("Spatial index format version %d.%d is not supported by this release."
  109. " Try to rebuild topology or upgrade GRASS.",
  110. ptr->spidx_Version_Major, ptr->spidx_Version_Minor);
  111. return (-1);
  112. }
  113. G_warning
  114. ("Your GRASS version does not fully support spatial index format %d.%d of the vector."
  115. " Consider to rebuild topology or upgrade GRASS.",
  116. ptr->spidx_Version_Major, ptr->spidx_Version_Minor);
  117. }
  118. dig_init_portable(&(ptr->spidx_port), byte_order);
  119. dig_set_cur_port(&(ptr->spidx_port));
  120. /* bytes 6 - 9 : header size */
  121. if (0 >= dig__fread_port_L(&(ptr->spidx_head_size), 1, fp))
  122. return (-1);
  123. G_debug(2, " header size %ld", ptr->spidx_head_size);
  124. /* byte 10 : dimension 2D or 3D */
  125. if (0 >= dig__fread_port_C(buf, 1, fp))
  126. return (-1);
  127. ptr->spidx_with_z = buf[0];
  128. G_debug(2, " with_z %d", ptr->spidx_with_z);
  129. /* get required offset size */
  130. if (ptr->off_t_size == 0) {
  131. /* should not happen, topo is opened first */
  132. if (ptr->coor_size > (off_t)PORT_LONG_MAX)
  133. ptr->off_t_size = 8;
  134. else
  135. ptr->off_t_size = 4;
  136. }
  137. /* as long as topo is always opened first, off_t size check is not needed here */
  138. /* bytes 11 - 38 (large files 11 - 66) : Offsets */
  139. if (0 >= dig__fread_port_O(&(ptr->Node_spidx_offset), 1, fp, ptr->off_t_size))
  140. return (-1);
  141. if (0 >= dig__fread_port_O(&(ptr->Edge_spidx_offset), 1, fp, ptr->off_t_size))
  142. return (-1);
  143. if (0 >= dig__fread_port_O(&(ptr->Line_spidx_offset), 1, fp, ptr->off_t_size))
  144. return (-1);
  145. if (0 >= dig__fread_port_O(&(ptr->Area_spidx_offset), 1, fp, ptr->off_t_size))
  146. return (-1);
  147. if (0 >= dig__fread_port_O(&(ptr->Isle_spidx_offset), 1, fp, ptr->off_t_size))
  148. return (-1);
  149. if (0 >= dig__fread_port_O(&(ptr->Volume_spidx_offset), 1, fp, ptr->off_t_size))
  150. return (-1);
  151. if (0 >= dig__fread_port_O(&(ptr->Hole_spidx_offset), 1, fp, ptr->off_t_size))
  152. return (-1);
  153. /* bytes 39 - 42 (large files 67 - 74) : Offsets */
  154. if (0 >= dig__fread_port_O(&coor_size, 1, fp, ptr->off_t_size))
  155. return (-1);
  156. G_debug(2, " coor size %ld", coor_size);
  157. dig_fseek(fp, ptr->spidx_head_size, SEEK_SET);
  158. return (0);
  159. }
  160. int rtree_dump_node(FILE * fp, struct Node *n, int with_z);
  161. /* Dump RTree branch to file */
  162. int rtree_dump_branch(FILE * fp, struct Branch *b, int with_z, int level)
  163. {
  164. struct Rect *r;
  165. r = &(b->rect);
  166. if (level == 0)
  167. fprintf(fp, " id = %d ", (int)b->child);
  168. fprintf(fp, " %f %f %f %f %f %f\n", r->boundary[0], r->boundary[1],
  169. r->boundary[2], r->boundary[3], r->boundary[4], r->boundary[5]);
  170. if (level > 0) {
  171. rtree_dump_node(fp, b->child, with_z);
  172. }
  173. return 0;
  174. }
  175. /* Dump RTree node to file */
  176. int rtree_dump_node(FILE * fp, struct Node *n, int with_z)
  177. {
  178. int i, nn;
  179. fprintf(fp, "Node level=%d count=%d\n", n->level, n->count);
  180. if (n->level > 0)
  181. nn = NODECARD;
  182. else
  183. nn = LEAFCARD;
  184. for (i = 0; i < nn; i++) {
  185. if (n->branch[i].child) {
  186. fprintf(fp, " Branch %d", i);
  187. rtree_dump_branch(fp, &n->branch[i], with_z, n->level);
  188. }
  189. }
  190. return 0;
  191. }
  192. int rtree_write_node(GVFILE * fp, struct Node *n, int with_z);
  193. /* Write RTree branch to file */
  194. int rtree_write_branch(GVFILE * fp, struct Branch *b, int with_z, int level)
  195. {
  196. struct Rect *r;
  197. int i;
  198. r = &(b->rect);
  199. /* rectangle */
  200. if (with_z) {
  201. if (0 >= dig__fwrite_port_D(&(r->boundary[0]), 6, fp))
  202. return (-1);
  203. }
  204. else {
  205. if (0 >= dig__fwrite_port_D(&(r->boundary[0]), 2, fp))
  206. return (-1);
  207. if (0 >= dig__fwrite_port_D(&(r->boundary[3]), 2, fp))
  208. return (-1);
  209. }
  210. if (level == 0) { /* write data (element id) */
  211. i = (int) b->child;
  212. if (0 >= dig__fwrite_port_I(&i, 1, fp))
  213. return (-1);
  214. }
  215. else {
  216. rtree_write_node(fp, b->child, with_z);
  217. }
  218. return 0;
  219. }
  220. /* Write RTree node to file */
  221. int rtree_write_node(GVFILE * fp, struct Node *n, int with_z)
  222. {
  223. int i, nn;
  224. /* level ( 0 = leaf, data ) */
  225. if (0 >= dig__fwrite_port_I(&(n->level), 1, fp))
  226. return (-1);
  227. /* count */
  228. if (0 >= dig__fwrite_port_I(&(n->count), 1, fp))
  229. return (-1);
  230. if (n->level > 0)
  231. nn = NODECARD;
  232. else
  233. nn = LEAFCARD;
  234. for (i = 0; i < nn; i++) {
  235. if (n->branch[i].child) {
  236. rtree_write_branch(fp, &n->branch[i], with_z, n->level);
  237. }
  238. }
  239. return 0;
  240. }
  241. int rtree_read_node(GVFILE * fp, struct Node *n, int with_z);
  242. /* Read RTree branch from file */
  243. int rtree_read_branch(GVFILE * fp, struct Branch *b, int with_z, int level)
  244. {
  245. struct Rect *r;
  246. int i;
  247. G_debug(3, "rtree_read_branch()");
  248. r = &(b->rect);
  249. /* rectangle */
  250. if (with_z) {
  251. if (0 >= dig__fread_port_D(&(r->boundary[0]), 6, fp))
  252. return (-1);
  253. }
  254. else {
  255. if (0 >= dig__fread_port_D(&(r->boundary[0]), 2, fp))
  256. return (-1);
  257. if (0 >= dig__fread_port_D(&(r->boundary[3]), 2, fp))
  258. return (-1);
  259. r->boundary[2] = 0;
  260. r->boundary[5] = 0;
  261. }
  262. if (level == 0) { /* read data (element id) */
  263. if (0 >= dig__fread_port_I(&i, 1, fp))
  264. return (-1);
  265. b->child = (struct Node *)i;
  266. }
  267. else {
  268. /* create new node */
  269. b->child = RTreeNewNode();
  270. rtree_read_node(fp, b->child, with_z);
  271. }
  272. return 0;
  273. }
  274. /* Read RTree node from file */
  275. int rtree_read_node(GVFILE * fp, struct Node *n, int with_z)
  276. {
  277. int level, count, i;
  278. G_debug(3, "rtree_read_node()");
  279. /* level ( 0 = leaf, data ) */
  280. if (0 >= dig__fread_port_I(&level, 1, fp))
  281. return (-1);
  282. n->level = level;
  283. /* count */
  284. if (0 >= dig__fread_port_I(&count, 1, fp))
  285. return (-1);
  286. n->count = count;
  287. for (i = 0; i < count; i++) {
  288. if (0 > rtree_read_branch(fp, &n->branch[i], with_z, level))
  289. return (-1);
  290. }
  291. return 0;
  292. }
  293. /* Write spatial index */
  294. int dig_write_spidx(GVFILE * fp, struct Plus_head *Plus)
  295. {
  296. dig_set_cur_port(&(Plus->spidx_port));
  297. dig_rewind(fp);
  298. dig_Wr_spindx_head(fp, Plus);
  299. Plus->Node_spidx_offset = dig_ftell(fp);
  300. rtree_write_node(fp, Plus->Node_spidx, Plus->with_z);
  301. Plus->Line_spidx_offset = dig_ftell(fp);
  302. rtree_write_node(fp, Plus->Line_spidx, Plus->with_z);
  303. Plus->Area_spidx_offset = dig_ftell(fp);
  304. rtree_write_node(fp, Plus->Area_spidx, Plus->with_z);
  305. Plus->Isle_spidx_offset = dig_ftell(fp);
  306. rtree_write_node(fp, Plus->Isle_spidx, Plus->with_z);
  307. dig_rewind(fp);
  308. dig_Wr_spindx_head(fp, Plus); /* rewrite with offsets */
  309. return 0;
  310. }
  311. /* Read spatial index file */
  312. int dig_read_spidx(GVFILE * fp, struct Plus_head *Plus)
  313. {
  314. G_debug(1, "dig_read_spindx()");
  315. /* TODO: free old tree */
  316. dig_spidx_init(Plus);
  317. dig_rewind(fp);
  318. dig_Rd_spindx_head(fp, Plus);
  319. dig_set_cur_port(&(Plus->spidx_port));
  320. dig_fseek(fp, Plus->Node_spidx_offset, 0);
  321. rtree_read_node(fp, Plus->Node_spidx, Plus->with_z);
  322. dig_fseek(fp, Plus->Line_spidx_offset, 0);
  323. rtree_read_node(fp, Plus->Line_spidx, Plus->with_z);
  324. dig_fseek(fp, Plus->Area_spidx_offset, 0);
  325. rtree_read_node(fp, Plus->Area_spidx, Plus->with_z);
  326. dig_fseek(fp, Plus->Isle_spidx_offset, 0);
  327. rtree_read_node(fp, Plus->Isle_spidx, Plus->with_z);
  328. return 0;
  329. }
  330. /* Dump spatial index */
  331. int dig_dump_spidx(FILE * fp, struct Plus_head *Plus)
  332. {
  333. fprintf(fp, "Nodes\n");
  334. rtree_dump_node(fp, Plus->Node_spidx, Plus->with_z);
  335. fprintf(fp, "Lines\n");
  336. rtree_dump_node(fp, Plus->Line_spidx, Plus->with_z);
  337. fprintf(fp, "Areas\n");
  338. rtree_dump_node(fp, Plus->Area_spidx, Plus->with_z);
  339. fprintf(fp, "Isles\n");
  340. rtree_dump_node(fp, Plus->Isle_spidx, Plus->with_z);
  341. return 0;
  342. }