open.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /*!
  2. * \file open.c
  3. *
  4. * \brief Vector library - Open vector map
  5. *
  6. * Higher level functions for reading/writing/manipulating vectors.
  7. *
  8. * (C) 2001-2009 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public License
  11. * (>=v2). Read the file COPYING that comes with GRASS for details.
  12. *
  13. * \author Original author CERL, probably Dave Gerdes or Mike
  14. * Higgins.
  15. * \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  16. */
  17. #include <grass/config.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <grass/glocale.h>
  25. #include <grass/gis.h>
  26. #include <grass/vector.h>
  27. #define MAX_OPEN_LEVEL 2
  28. static int open_old_dummy()
  29. {
  30. return 0;
  31. }
  32. #ifndef HAVE_OGR
  33. static int format()
  34. {
  35. G_fatal_error(_("Requested format is not compiled in this version"));
  36. return 0;
  37. }
  38. #endif
  39. static int Open_level = 0;
  40. static int (*Open_old_array[][2]) () = {
  41. {
  42. open_old_dummy, V1_open_old_nat}
  43. #ifdef HAVE_OGR
  44. , {
  45. open_old_dummy, V1_open_old_ogr}
  46. #else
  47. , {
  48. open_old_dummy, format}
  49. #endif
  50. };
  51. static void fatal_error(int ferror, char *errmsg)
  52. {
  53. switch (ferror) {
  54. case GV_FATAL_EXIT:
  55. G_fatal_error(errmsg);
  56. break;
  57. case GV_FATAL_PRINT:
  58. G_warning(errmsg);
  59. break;
  60. case GV_FATAL_RETURN:
  61. break;
  62. }
  63. }
  64. /*!
  65. * \brief Predetermine level at which a map will be opened for
  66. * reading.
  67. *
  68. * If it can't open that level, the open will fail. The specified
  69. * level must be set before any call to open. The default is to try
  70. * to open the highest level possible, and keep stepping down until
  71. * success.
  72. *
  73. * NOTE!! This should only be used to set when you wish to force
  74. * a lower level open. If you require a higher level, then just
  75. * check the return to verify the level instead of forcing it.
  76. * This is because future releases will have higher levels which
  77. * will be downward compatible and which your programs should
  78. * support by default.
  79. *
  80. * \param level vector (topo) level
  81. *
  82. * \return 0 on success
  83. * \return 1 on error
  84. */
  85. int Vect_set_open_level(int level)
  86. {
  87. Open_level = level;
  88. if (Open_level < 1 || Open_level > MAX_OPEN_LEVEL) {
  89. G_warning(_("Programmer requested unknown open level %d"),
  90. Open_level);
  91. Open_level = 0;
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. /*!
  97. * \brief Open old vector for reading.
  98. *
  99. * In case of error, the functions respect fatal error settings.
  100. *
  101. * \param[out] Map vector map
  102. * \param name name of vector map to open
  103. * \param mapset mapset name
  104. * \param update open for update
  105. * \param head_only read only header info from 'head', 'dbln', 'topo', 'cidx' is not opened. The header may be opened on level 2 only.
  106. *
  107. * \return level of openness (1, 2)
  108. * \return -1 in error
  109. */
  110. int
  111. Vect__open_old(struct Map_info *Map, const char *name, const char *mapset,
  112. int update, int head_only)
  113. {
  114. char buf[GNAME_MAX + 10], buf2[GMAPSET_MAX + 10], xname[GNAME_MAX],
  115. xmapset[GMAPSET_MAX], errmsg[2000];
  116. FILE *fp;
  117. int level, level_request, ferror;
  118. int format, ret;
  119. const char *fmapset;
  120. G_debug(1, "Vect_open_old(): name = %s mapset= %s update = %d", name,
  121. mapset, update);
  122. /* TODO: Open header for update ('dbln') */
  123. ferror = Vect_get_fatal_error();
  124. Vect_set_fatal_error(GV_FATAL_EXIT);
  125. level_request = Open_level;
  126. Open_level = 0;
  127. Vect__init_head(Map);
  128. dig_init_plus(&(Map->plus));
  129. if (G__name_is_fully_qualified(name, xname, xmapset)) {
  130. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, xname);
  131. sprintf(buf2, "%s@%s", GRASS_VECT_COOR_ELEMENT, xmapset);
  132. Map->name = G_store(xname);
  133. Map->mapset = G_store(xmapset);
  134. }
  135. else {
  136. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, name);
  137. sprintf(buf2, "%s", GRASS_VECT_COOR_ELEMENT);
  138. Map->name = G_store(name);
  139. if (mapset)
  140. Map->mapset = G_store(mapset);
  141. else
  142. Map->mapset = G_store("");
  143. }
  144. fmapset = G_find_vector2(Map->name, Map->mapset);
  145. if (fmapset == NULL) {
  146. sprintf(errmsg, _("Vector map <%s> not found"),
  147. Vect_get_full_name(Map));
  148. fatal_error(ferror, errmsg);
  149. return -1;
  150. }
  151. Map->mapset = G_store(fmapset);
  152. Map->location = G_store(G_location());
  153. Map->gisdbase = G_store(G_gisdbase());
  154. if (update && (0 != strcmp(Map->mapset, G_mapset()))) {
  155. G_warning(_("Vector map which is not in the current mapset cannot be opened for update"));
  156. return -1;
  157. }
  158. G_debug(1, "Map name: %s", Map->name);
  159. G_debug(1, "Map mapset: %s", Map->mapset);
  160. /* Read vector format information */
  161. format = 0;
  162. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  163. G_debug(1, "open format file: '%s/%s/%s'", Map->mapset, buf,
  164. GRASS_VECT_FRMT_ELEMENT);
  165. fp = G_fopen_old(buf, GRASS_VECT_FRMT_ELEMENT, Map->mapset);
  166. if (fp == NULL) {
  167. G_debug(1, "Vector format: %d (native)", format);
  168. format = GV_FORMAT_NATIVE;
  169. }
  170. else {
  171. format = dig_read_frmt_ascii(fp, &(Map->fInfo));
  172. fclose(fp);
  173. G_debug(1, "Vector format: %d (non-native)", format);
  174. if (format < 0) {
  175. sprintf(errmsg, _("Unable to open vector map <%s>"),
  176. Vect_get_full_name(Map));
  177. fatal_error(ferror, errmsg);
  178. return -1;
  179. }
  180. }
  181. Map->format = format;
  182. /* Read vector head */
  183. if (Vect__read_head(Map) != GRASS_OK) {
  184. sprintf(errmsg,
  185. _("Unable to open vector map <%s> on level %d. "
  186. "Try to rebuild vector topology by v.build."),
  187. Vect_get_full_name(Map), level_request);
  188. G_warning(_("Unable to read head file of vector <%s>"), Vect_get_full_name(Map));
  189. }
  190. G_debug(1, "Level request = %d", level_request);
  191. /* There are only 2 possible open levels, 1 and 2. Try first to open 'support' files
  192. * (topo,sidx,cidx), these files are the same for all formats.
  193. * If it is not possible and requested level is 2, return error,
  194. * otherwise call Open_old_array[format][1], to open remaining files/sources (level 1)
  195. */
  196. /* Try to open support files if level was not requested or requested level is 2 (format independent) */
  197. if (level_request == 0 || level_request == 2) {
  198. level = 2; /* We expect success */
  199. /* open topo */
  200. ret = Vect_open_topo(Map, head_only);
  201. if (ret == 1) { /* topo file is not available */
  202. G_debug(1, "Topo file for vector '%s' not available.",
  203. Vect_get_full_name(Map));
  204. level = 1;
  205. }
  206. else if (ret == -1) {
  207. G_fatal_error(_("Unable to open topology file for vector map <%s>"),
  208. Vect_get_full_name(Map));
  209. }
  210. /* open spatial index, not needed for head_only */
  211. /* spatial index is not loaded anymore */
  212. /*
  213. if ( level == 2 && !head_only ) {
  214. if ( Vect_open_spatial_index(Map) == -1 ) {
  215. G_debug( 1, "Cannot open spatial index file for vector '%s'.", Vect_get_full_name (Map) );
  216. dig_free_plus ( &(Map->plus) );
  217. level = 1;
  218. }
  219. }
  220. */
  221. /* open category index */
  222. if (level == 2) {
  223. ret = Vect_cidx_open(Map, head_only);
  224. if (ret == 1) { /* category index is not available */
  225. G_debug(1,
  226. "Category index file for vector '%s' not available.",
  227. Vect_get_full_name(Map));
  228. dig_free_plus(&(Map->plus)); /* free topology */
  229. dig_spidx_free(&(Map->plus)); /* free spatial index */
  230. level = 1;
  231. }
  232. else if (ret == -1) { /* file exists, but cannot be opened */
  233. G_fatal_error(_("Unable to open category index file for vector map <%s>"),
  234. Vect_get_full_name(Map));
  235. }
  236. }
  237. #ifdef HAVE_OGR
  238. /* Open OGR specific support files */
  239. if (level == 2 && Map->format == GV_FORMAT_OGR) {
  240. if (V2_open_old_ogr(Map) < 0) {
  241. dig_free_plus(&(Map->plus));
  242. dig_spidx_free(&(Map->plus));
  243. dig_cidx_free(&(Map->plus));
  244. level = 1;
  245. }
  246. }
  247. #endif
  248. if (level_request == 2 && level < 2) {
  249. sprintf(errmsg,
  250. _("Unable to open vector map <%s> on level %d. "
  251. "Try to rebuild vector topology by v.build."),
  252. Vect_get_full_name(Map), level_request);
  253. fatal_error(ferror, errmsg);
  254. return -1;
  255. }
  256. }
  257. else {
  258. level = 1; /* I.e. requested level is 1 */
  259. }
  260. /* Open level 1 files / sources (format specific) */
  261. if (!head_only) { /* No need to open coordinates */
  262. if (0 != (*Open_old_array[format][1]) (Map, update)) { /* Cannot open */
  263. if (level == 2) { /* support files opened */
  264. dig_free_plus(&(Map->plus));
  265. dig_spidx_free(&(Map->plus));
  266. dig_cidx_free(&(Map->plus));
  267. }
  268. sprintf(errmsg,
  269. _("Unable to open vector map <%s> on level %d. "
  270. "Try to rebuild vector topology by v.build."),
  271. Vect_get_full_name(Map), level_request);
  272. fatal_error(ferror, errmsg);
  273. return -1;
  274. }
  275. }
  276. else {
  277. Map->head.with_z = Map->plus.with_z; /* take dimension from topo */
  278. }
  279. /* Set status */
  280. Map->open = VECT_OPEN_CODE;
  281. Map->level = level;
  282. Map->head_only = head_only;
  283. Map->support_updated = 0;
  284. if (update) {
  285. Map->mode = GV_MODE_RW;
  286. Map->plus.mode = GV_MODE_RW;
  287. }
  288. else {
  289. Map->mode = GV_MODE_READ;
  290. Map->plus.mode = GV_MODE_READ;
  291. }
  292. if (head_only) {
  293. Map->head_only = 1;
  294. }
  295. else {
  296. Map->head_only = 0;
  297. }
  298. Map->Constraint_region_flag = 0;
  299. Map->Constraint_type_flag = 0;
  300. G_debug(1, "Vect_open_old(): vector opened on level %d", level);
  301. if (level == 1) { /* without topology */
  302. Map->plus.built = GV_BUILD_NONE;
  303. }
  304. else { /* level 2, with topology */
  305. Map->plus.built = GV_BUILD_ALL; /* Highest level of topology for level 2 */
  306. }
  307. Map->plus.do_uplist = 0;
  308. Map->dblnk = Vect_new_dblinks_struct();
  309. Vect_read_dblinks(Map);
  310. /* Open history file */
  311. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  312. if (update) { /* native only */
  313. Map->hist_fp = G_fopen_modify(buf, GRASS_VECT_HIST_ELEMENT);
  314. if (Map->hist_fp == NULL) {
  315. sprintf(errmsg,
  316. _("Unable to open history file for vector map <%s>"),
  317. Vect_get_full_name(Map));
  318. fatal_error(ferror, errmsg);
  319. return (-1);
  320. }
  321. fseek(Map->hist_fp, (off_t)0, SEEK_END);
  322. Vect_hist_write(Map,
  323. "---------------------------------------------------------------------------------\n");
  324. }
  325. else {
  326. if (Map->format == GV_FORMAT_NATIVE || Map->format == GV_FORMAT_OGR) {
  327. Map->hist_fp =
  328. G_fopen_old(buf, GRASS_VECT_HIST_ELEMENT, Map->mapset);
  329. /* If NULL (does not exist) then Vect_hist_read() handle that */
  330. }
  331. else {
  332. Map->hist_fp = NULL;
  333. }
  334. }
  335. if (!head_only) { /* Cannot rewind if not fully opened */
  336. Vect_rewind(Map);
  337. }
  338. /* Delete support files if native format was opened for update (not head_only) */
  339. if (update && !head_only) {
  340. char file_path[2000];
  341. struct stat info;
  342. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, name);
  343. G__file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
  344. if (stat(file_path, &info) == 0) /* file exists? */
  345. unlink(file_path);
  346. G__file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
  347. if (stat(file_path, &info) == 0) /* file exists? */
  348. unlink(file_path);
  349. G__file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
  350. if (stat(file_path, &info) == 0) /* file exists? */
  351. unlink(file_path);
  352. }
  353. return (level);
  354. }
  355. /*!
  356. * \brief Open existing vector for reading
  357. *
  358. * In case of error, the functions respect fatal error settings.
  359. *
  360. * \param[out] Map vector map
  361. * \param name name of vector map
  362. * \param mapset mapset name
  363. *
  364. * \return level of openness [1, 2, (3)]
  365. * \return -1 on error
  366. */
  367. int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
  368. {
  369. return (Vect__open_old(Map, name, mapset, 0, 0));
  370. }
  371. /*!
  372. * \brief Open existing vector for reading/writing
  373. *
  374. * In case of error, the functions respect fatal error settings.
  375. *
  376. * \param[out] Map vector map
  377. * \param name name of vector map to update
  378. * \param mapset mapset name
  379. *
  380. * \return level of openness [1, 2, (3)]
  381. * \return -1 on error
  382. */
  383. int
  384. Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
  385. {
  386. int ret;
  387. ret = Vect__open_old(Map, name, mapset, 1, 0);
  388. if (ret > 0) {
  389. Map->plus.do_uplist = 1;
  390. Map->plus.uplines = NULL;
  391. Map->plus.n_uplines = 0;
  392. Map->plus.alloc_uplines = 0;
  393. Map->plus.upnodes = NULL;
  394. Map->plus.n_upnodes = 0;
  395. Map->plus.alloc_upnodes = 0;
  396. /* Build spatial index from topo */
  397. Vect_build_sidx_from_topo(Map);
  398. }
  399. return ret;
  400. }
  401. /*!
  402. * \brief Reads only info about vector from headers of 'head', 'dbln',
  403. * 'topo' and 'cidx' file.
  404. *
  405. * In case of error, the functions respect fatal error settings.
  406. *
  407. * \param[out] Map vector map
  408. * \param name name of vector map to read
  409. * \param mapset mapset name
  410. *
  411. * \return level of openness [1, 2, (3)]
  412. * \return -1 on error
  413. */
  414. int
  415. Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
  416. {
  417. return (Vect__open_old(Map, name, mapset, 0, 1));
  418. }
  419. /*!
  420. * \brief Open old vector head for updating (mostly for database link updates)
  421. *
  422. * In case of error, the functions respect fatal error settings.
  423. *
  424. * \param[out] Map vector map
  425. * \param name name of vector map to update
  426. * \param mapset mapset name
  427. *
  428. * \return level of openness [1, 2, (3)]
  429. * \return -1 on error
  430. */
  431. int
  432. Vect_open_update_head(struct Map_info *Map, const char *name,
  433. const char *mapset)
  434. {
  435. int ret;
  436. ret = Vect__open_old(Map, name, mapset, 1, 1);
  437. if (ret > 0) { /* Probably not important */
  438. Map->plus.do_uplist = 1;
  439. Map->plus.uplines = NULL;
  440. Map->plus.n_uplines = 0;
  441. Map->plus.alloc_uplines = 0;
  442. Map->plus.upnodes = NULL;
  443. Map->plus.n_upnodes = 0;
  444. Map->plus.alloc_upnodes = 0;
  445. }
  446. return ret;
  447. }
  448. /*!
  449. * \brief Open new vector for reading/writing
  450. *
  451. * \param[out] Map vector map
  452. * \param name name of vector map
  453. * \param with_z 2D/3D vector map
  454. *
  455. * \return 1 on success
  456. * \return -1 on error
  457. */
  458. int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
  459. {
  460. int ret, ferror;
  461. char errmsg[2000], buf[200];
  462. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  463. G_debug(2, "Vect_open_new(): name = %s", name);
  464. Vect__init_head(Map);
  465. ferror = Vect_get_fatal_error();
  466. Vect_set_fatal_error(GV_FATAL_EXIT);
  467. if (G__name_is_fully_qualified(name, xname, xmapset)) {
  468. if (strcmp(xmapset, G_mapset()) != 0) {
  469. sprintf(errmsg, _("%s is not in the current mapset (%s)"), name,
  470. G_mapset());
  471. fatal_error(ferror, errmsg);
  472. }
  473. name = xname;
  474. }
  475. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  476. if (Vect_legal_filename(name) < 0) {
  477. sprintf(errmsg, _("Vector map name is not SQL compliant"));
  478. fatal_error(ferror, errmsg);
  479. return (-1);
  480. }
  481. /* Check if map already exists */
  482. if (G_find_vector2(name, G_mapset()) != NULL) {
  483. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  484. name);
  485. ret = Vect_delete(name);
  486. if (ret == -1) {
  487. sprintf(errmsg, _("Unable to delete vector map <%s>"), name);
  488. fatal_error(ferror, errmsg);
  489. return (-1);
  490. }
  491. }
  492. Map->name = G_store(name);
  493. Map->mapset = G_store(G_mapset());
  494. Map->location = G_store(G_location());
  495. Map->gisdbase = G_store(G_gisdbase());
  496. Map->format = GV_FORMAT_NATIVE;
  497. if (V1_open_new_nat(Map, name, with_z) < 0) {
  498. sprintf(errmsg, _("Unable to create vector map <%s>"),
  499. Vect_get_full_name(Map));
  500. fatal_error(ferror, errmsg);
  501. return (-1);
  502. }
  503. /* Open history file */
  504. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  505. Map->hist_fp = G_fopen_new(buf, GRASS_VECT_HIST_ELEMENT);
  506. if (Map->hist_fp == NULL) {
  507. sprintf(errmsg, _("Unable to open history file for vector map <%s>"),
  508. Vect_get_full_name(Map));
  509. fatal_error(ferror, errmsg);
  510. return (-1);
  511. }
  512. Open_level = 0;
  513. dig_init_plus(&(Map->plus));
  514. Map->open = VECT_OPEN_CODE;
  515. Map->level = 1;
  516. Map->head_only = 0;
  517. Map->support_updated = 0;
  518. Map->plus.built = GV_BUILD_NONE;
  519. Map->mode = GV_MODE_RW;
  520. Map->Constraint_region_flag = 0;
  521. Map->Constraint_type_flag = 0;
  522. Map->head.with_z = with_z;
  523. Map->plus.do_uplist = 0;
  524. Map->dblnk = Vect_new_dblinks_struct();
  525. return 1;
  526. }
  527. /*!
  528. * \brief Update Coor_info structure
  529. *
  530. * \param Map vector map
  531. * \param[out] Info Coor_info structure
  532. *
  533. * \return 1 on success
  534. * \return 0 on error
  535. */
  536. int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info)
  537. {
  538. char buf[2000], path[2000];
  539. struct stat stat_buf;
  540. switch (Map->format) {
  541. case GV_FORMAT_NATIVE:
  542. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  543. G__file_name(path, buf, GRASS_VECT_COOR_ELEMENT, Map->mapset);
  544. G_debug(1, "get coor info: %s", path);
  545. if (0 != stat(path, &stat_buf)) {
  546. G_warning(_("Unable to stat file <%s>"), path);
  547. Info->size = -1L;
  548. Info->mtime = -1L;
  549. }
  550. else {
  551. Info->size = (off_t)stat_buf.st_size; /* file size */
  552. Info->mtime = (long)stat_buf.st_mtime; /* last modified time */
  553. }
  554. /* stat does not give correct size on MINGW
  555. * if the file is opened */
  556. #ifdef __MINGW32__
  557. if (Map->open == VECT_OPEN_CODE) {
  558. dig_fseek(&(Map->dig_fp), 0L, SEEK_END);
  559. G_debug(2, "ftell = %d", dig_ftell(&(Map->dig_fp)));
  560. Info->size = dig_ftell(&(Map->dig_fp));
  561. }
  562. #endif
  563. break;
  564. case GV_FORMAT_OGR:
  565. Info->size = 0L;
  566. Info->mtime = 0L;
  567. break;
  568. }
  569. G_debug(1, "Info->size = %lu, Info->mtime = %ld", (unsigned long) Info->size,
  570. Info->mtime);
  571. return 1;
  572. }
  573. /*!
  574. * \brief Gets maptype (native, shape, postgis)
  575. *
  576. * Note: string is allocated by G_store(). Free allocated memory with
  577. * G_free().
  578. *
  579. * \param Map vector map
  580. *
  581. * \return maptype string on success
  582. * \return error message on error
  583. */
  584. const char *Vect_maptype_info(const struct Map_info *Map)
  585. {
  586. char maptype[1000];
  587. switch (Map->format) {
  588. case GV_FORMAT_NATIVE:
  589. sprintf(maptype, "native");
  590. break;
  591. case GV_FORMAT_OGR:
  592. sprintf(maptype, "ogr");
  593. break;
  594. default:
  595. sprintf(maptype, "unknown %d (update Vect_maptype_info)",
  596. Map->format);
  597. }
  598. return G_store(maptype);
  599. }
  600. /*!
  601. * \brief Open topo file
  602. *
  603. * \param[in,out] Map vector map
  604. * \param head_only open only head
  605. *
  606. * \return 0 on success
  607. * \return 1 file does not exist
  608. * \return -1 on error
  609. */
  610. int Vect_open_topo(struct Map_info *Map, int head_only)
  611. {
  612. int err, ret;
  613. char buf[500], file_path[2000];
  614. GVFILE fp;
  615. struct Coor_info CInfo;
  616. struct Plus_head *Plus;
  617. struct stat info;
  618. G_debug(1, "Vect_open_topo(): name = %s mapset= %s", Map->name,
  619. Map->mapset);
  620. Plus = &(Map->plus);
  621. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  622. G__file_name(file_path, buf, GV_TOPO_ELEMENT, Map->mapset);
  623. if (stat(file_path, &info) != 0) /* does not exist */
  624. return 1;
  625. dig_file_init(&fp);
  626. fp.file = G_fopen_old(buf, GV_TOPO_ELEMENT, Map->mapset);
  627. if (fp.file == NULL) { /* topo file is not available */
  628. G_debug(1, "Cannot open topo file for vector '%s@%s'.",
  629. Map->name, Map->mapset);
  630. return -1;
  631. }
  632. /* get coor info */
  633. Vect_coor_info(Map, &CInfo);
  634. /* load head */
  635. if (dig_Rd_Plus_head(&fp, Plus) == -1)
  636. return -1;
  637. G_debug(1, "Topo head: coor size = %lu, coor mtime = %ld",
  638. (unsigned long) Plus->coor_size, Plus->coor_mtime);
  639. /* do checks */
  640. err = 0;
  641. if (CInfo.size != Plus->coor_size) {
  642. G_warning(_("Size of 'coor' file differs from value saved in topology file"));
  643. err = 1;
  644. }
  645. /* Do not check mtime because mtime is changed by copy */
  646. /*
  647. if ( CInfo.mtime != Plus->coor_mtime ) {
  648. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  649. err = 1;
  650. }
  651. */
  652. if (err) {
  653. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  654. Map->name, Map->mapset);
  655. return -1;
  656. }
  657. /* load file to the memory */
  658. /* dig_file_load ( &fp); */
  659. /* load topo to memory */
  660. ret = dig_load_plus(Plus, &fp, head_only);
  661. fclose(fp.file);
  662. /* dig_file_free ( &fp); */
  663. if (ret == 0)
  664. return -1;
  665. return 0;
  666. }
  667. /*!
  668. * \brief Open spatial index file
  669. *
  670. * \param[in,out] Map vector map
  671. *
  672. * \return 0 on success
  673. * \return -1 on error
  674. */
  675. int Vect_open_spatial_index(struct Map_info *Map)
  676. {
  677. char buf[500];
  678. GVFILE fp;
  679. /* struct Coor_info CInfo; */
  680. struct Plus_head *Plus;
  681. G_debug(1, "Vect_open_spatial_index(): name = %s mapset= %s", Map->name,
  682. Map->mapset);
  683. Plus = &(Map->plus);
  684. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  685. dig_file_init(&fp);
  686. fp.file = G_fopen_old(buf, GV_SIDX_ELEMENT, Map->mapset);
  687. if (fp.file == NULL) { /* spatial index file is not available */
  688. G_debug(1, "Cannot open spatial index file for vector '%s@%s'.",
  689. Map->name, Map->mapset);
  690. return -1;
  691. }
  692. /* TODO: checks */
  693. /* load head */
  694. /*
  695. dig_Rd_spindx_head (fp, Plus);
  696. G_debug ( 1, "Spindx head: coor size = %ld, coor mtime = %ld",
  697. Plus->coor_size, Plus->coor_mtime);
  698. */
  699. /* do checks */
  700. /*
  701. err = 0;
  702. if ( CInfo.size != Plus->coor_size ) {
  703. G_warning ( "Size of 'coor' file differs from value saved in topo file.\n");
  704. err = 1;
  705. }
  706. */
  707. /* Do not check mtime because mtime is changed by copy */
  708. /*
  709. if ( CInfo.mtime != Plus->coor_mtime ) {
  710. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  711. err = 1;
  712. }
  713. */
  714. /*
  715. if ( err ) {
  716. G_warning ( "Please rebuild topology for vector '%s@%s'\n", Map->name,
  717. Map->mapset );
  718. return -1;
  719. }
  720. */
  721. /* load file to the memory */
  722. /* dig_file_load ( &fp); */
  723. /* load topo to memory */
  724. dig_spidx_init(Plus);
  725. dig_read_spidx(&fp, Plus);
  726. fclose(fp.file);
  727. /* dig_file_free ( &fp); */
  728. return 0;
  729. }