cindex_rw.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /****************************************************************************
  2. *
  3. * MODULE: Vector library
  4. *
  5. * AUTHOR(S): Radim Blazek.
  6. *
  7. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  8. *
  9. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <grass/config.h>
  17. #include <stdlib.h>
  18. #include <sys/types.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/vector.h>
  22. int dig_write_cidx_head(GVFILE * fp, struct Plus_head *plus)
  23. {
  24. int i;
  25. unsigned char buf[5];
  26. long length = 9;
  27. G_debug(3, "dig_write_cidx_head()");
  28. dig_rewind(fp);
  29. dig_set_cur_port(&(plus->cidx_port));
  30. /* Head of header */
  31. /* bytes 1 - 5 */
  32. buf[0] = GV_CIDX_VER_MAJOR;
  33. buf[1] = GV_CIDX_VER_MINOR;
  34. buf[2] = GV_CIDX_EARLIEST_MAJOR;
  35. buf[3] = GV_CIDX_EARLIEST_MINOR;
  36. buf[4] = plus->cidx_port.byte_order;
  37. if (0 >= dig__fwrite_port_C(buf, 5, fp))
  38. return (-1);
  39. /* get required offset size */
  40. if (plus->off_t_size == 0) {
  41. /* should not happen, topo is written first */
  42. if (plus->coor_size > (off_t)PORT_LONG_MAX)
  43. plus->off_t_size = 8;
  44. else
  45. plus->off_t_size = 4;
  46. }
  47. /* bytes 6 - 9 : header size */
  48. if (0 >= dig__fwrite_port_L(&length, 1, fp))
  49. return (0);
  50. /* Body of header - info about all fields */
  51. /* Number of fields */
  52. if (0 >= dig__fwrite_port_I(&(plus->n_cidx), 1, fp))
  53. return (-1);
  54. for (i = 0; i < plus->n_cidx; i++) {
  55. int t;
  56. struct Cat_index *ci;
  57. ci = &(plus->cidx[i]);
  58. G_debug(3, "cidx %d head offset: %ld", i, dig_ftell(fp));
  59. /* Field number */
  60. if (0 >= dig__fwrite_port_I(&(ci->field), 1, fp))
  61. return (-1);
  62. /* Number of categories */
  63. if (0 >= dig__fwrite_port_I(&(ci->n_cats), 1, fp))
  64. return (-1);
  65. /* Number of unique categories */
  66. if (0 >= dig__fwrite_port_I(&(ci->n_ucats), 1, fp))
  67. return (-1);
  68. /* Number of types */
  69. if (0 >= dig__fwrite_port_I(&(ci->n_types), 1, fp))
  70. return (-1);
  71. /* Types */
  72. for (t = 0; t < ci->n_types; t++) {
  73. int wtype;
  74. /* type */
  75. wtype = dig_type_to_store(ci->type[t][0]);
  76. if (0 >= dig__fwrite_port_I(&wtype, 1, fp))
  77. return (-1);
  78. /* number of items */
  79. if (0 >= dig__fwrite_port_I(&(ci->type[t][1]), 1, fp))
  80. return (-1);
  81. }
  82. /* Offset */
  83. if (0 >= dig__fwrite_port_O(&(ci->offset), 1, fp, plus->off_t_size))
  84. return (0);
  85. G_debug(3, "cidx %d offset: %ld", i, ci->offset);
  86. }
  87. G_debug(3, "cidx body offset %ld", dig_ftell(fp));
  88. return (0);
  89. }
  90. /* return: 0 OK, -1 error */
  91. int dig_read_cidx_head(GVFILE * fp, struct Plus_head *plus)
  92. {
  93. unsigned char buf[5];
  94. int i, byte_order;
  95. dig_rewind(fp);
  96. /* bytes 1 - 5 */
  97. if (0 >= dig__fread_port_C(buf, 5, fp))
  98. return (-1);
  99. plus->cidx_Version_Major = buf[0];
  100. plus->cidx_Version_Minor = buf[1];
  101. plus->cidx_Back_Major = buf[2];
  102. plus->cidx_Back_Minor = buf[3];
  103. byte_order = buf[4];
  104. G_debug(3,
  105. "Cidx header: file version %d.%d , supported from GRASS version %d.%d",
  106. plus->cidx_Version_Major, plus->cidx_Version_Minor,
  107. plus->cidx_Back_Major, plus->cidx_Back_Minor);
  108. G_debug(3, " byte order %d", byte_order);
  109. /* check version numbers */
  110. if (plus->cidx_Version_Major > GV_CIDX_VER_MAJOR ||
  111. plus->cidx_Version_Minor > GV_CIDX_VER_MINOR) {
  112. /* The file was created by GRASS library with higher version than this one */
  113. if (plus->cidx_Back_Major > GV_CIDX_VER_MAJOR ||
  114. plus->cidx_Back_Minor > GV_CIDX_VER_MINOR) {
  115. /* This version of GRASS lib is lower than the oldest which can read this format */
  116. G_fatal_error
  117. ("Category index format version %d.%d is not supported by this release."
  118. " Try to rebuild topology or upgrade GRASS.",
  119. plus->cidx_Version_Major, plus->cidx_Version_Minor);
  120. return (-1);
  121. }
  122. G_warning
  123. ("Your GRASS version does not fully support category index format %d.%d of the vector."
  124. " Consider to rebuild topology or upgrade GRASS.",
  125. plus->cidx_Version_Major, plus->cidx_Version_Minor);
  126. }
  127. dig_init_portable(&(plus->cidx_port), byte_order);
  128. dig_set_cur_port(&(plus->cidx_port));
  129. /* bytes 6 - 9 : header size */
  130. if (0 >= dig__fread_port_L(&(plus->cidx_head_size), 1, fp))
  131. return (-1);
  132. G_debug(3, " header size %ld", plus->cidx_head_size);
  133. /* get required offset size */
  134. if (plus->off_t_size == 0) {
  135. /* should not happen, topo is opened first */
  136. if (plus->coor_size > (off_t)PORT_LONG_MAX)
  137. plus->off_t_size = 8;
  138. else
  139. plus->off_t_size = 4;
  140. }
  141. /* Body of header - info about all fields */
  142. /* Number of fields */
  143. if (0 >= dig__fread_port_I(&(plus->n_cidx), 1, fp))
  144. return (-1);
  145. /* alloc space */
  146. plus->a_cidx = plus->n_cidx;
  147. plus->cidx =
  148. (struct Cat_index *)G_malloc(plus->a_cidx * sizeof(struct Cat_index));
  149. for (i = 0; i < plus->n_cidx; i++) {
  150. int t;
  151. struct Cat_index *ci;
  152. ci = &(plus->cidx[i]);
  153. ci->cat = NULL;
  154. ci->a_cats = 0;
  155. /* Field number */
  156. if (0 >= dig__fread_port_I(&(ci->field), 1, fp))
  157. return (-1);
  158. /* Number of categories */
  159. if (0 >= dig__fread_port_I(&(ci->n_cats), 1, fp))
  160. return (-1);
  161. /* Number of unique categories */
  162. if (0 >= dig__fread_port_I(&(ci->n_ucats), 1, fp))
  163. return (-1);
  164. /* Number of types */
  165. if (0 >= dig__fread_port_I(&(ci->n_types), 1, fp))
  166. return (-1);
  167. /* Types */
  168. for (t = 0; t < ci->n_types; t++) {
  169. int rtype;
  170. /* type */
  171. if (0 >= dig__fread_port_I(&rtype, 1, fp))
  172. return (-1);
  173. ci->type[t][0] = dig_type_from_store(rtype);
  174. /* number of items */
  175. if (0 >= dig__fread_port_I(&(ci->type[t][1]), 1, fp))
  176. return (-1);
  177. }
  178. /* Offset */
  179. if (0 >= dig__fread_port_O(&(ci->offset), 1, fp, plus->off_t_size))
  180. return (0);
  181. }
  182. if (dig_fseek(fp, plus->cidx_head_size, SEEK_SET) == -1)
  183. return (-1);
  184. return (0);
  185. }
  186. /* Write spatial index */
  187. int dig_write_cidx(GVFILE * fp, struct Plus_head *plus)
  188. {
  189. int i;
  190. dig_set_cur_port(&(plus->cidx_port));
  191. dig_rewind(fp);
  192. dig_write_cidx_head(fp, plus);
  193. /* Write category-type-id for each field */
  194. for (i = 0; i < plus->n_cidx; i++) {
  195. int j;
  196. struct Cat_index *ci;
  197. ci = &(plus->cidx[i]);
  198. ci->offset = dig_ftell(fp);
  199. /* convert type */
  200. for (j = 0; j < ci->n_cats; j++)
  201. ci->cat[j][1] = dig_type_to_store(ci->cat[j][1]);
  202. if (0 >= dig__fwrite_port_I((int *)ci->cat, 3 * ci->n_cats, fp))
  203. return (-1);
  204. /* Return back */
  205. for (j = 0; j < ci->n_cats; j++)
  206. ci->cat[j][1] = dig_type_from_store(ci->cat[j][1]);
  207. }
  208. dig_write_cidx_head(fp, plus); /* rewrite with offsets */
  209. return 0;
  210. }
  211. /* Read spatial index file
  212. * returns 0 - OK
  213. * 1 - error
  214. */
  215. int dig_read_cidx(GVFILE * fp, struct Plus_head *plus, int head_only)
  216. {
  217. int i;
  218. G_debug(3, "dig_read_cidx()");
  219. dig_cidx_init(plus);
  220. dig_rewind(fp);
  221. if (dig_read_cidx_head(fp, plus) == -1) {
  222. G_debug(3, "Cannot read cidx head");
  223. return 1;
  224. }
  225. if (head_only) {
  226. plus->cidx_up_to_date = 1; /* OK ? */
  227. return 0;
  228. }
  229. dig_set_cur_port(&(plus->cidx_port));
  230. /* Read category-type-id for each field */
  231. for (i = 0; i < plus->n_cidx; i++) {
  232. int j;
  233. struct Cat_index *ci;
  234. ci = &(plus->cidx[i]);
  235. ci->a_cats = ci->n_cats;
  236. ci->cat = G_malloc(ci->a_cats * 3 * sizeof(int));
  237. if (dig_fseek(fp, ci->offset, 0) == -1)
  238. return 1;
  239. if (0 >= dig__fread_port_I((int *)ci->cat, 3 * ci->n_cats, fp))
  240. return 1;
  241. /* convert type */
  242. for (j = 0; j < ci->n_cats; j++)
  243. ci->cat[j][1] = dig_type_from_store(ci->cat[j][1]);
  244. }
  245. plus->cidx_up_to_date = 1;
  246. return 0;
  247. }