open.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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-2008 by the GRASS Development Team
  9. *
  10. * This program is free software under the
  11. * GNU General Public License (>=v2).
  12. * Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. * \author Original author CERL, probably Dave Gerdes or Mike
  16. * Higgins. Update to GRASS 5.7 Radim Blazek and David D. Gray.
  17. *
  18. * \date 2001-2008
  19. */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <grass/glocale.h>
  27. #include <grass/gis.h>
  28. #include <grass/Vect.h>
  29. #define MAX_OPEN_LEVEL 2
  30. static int open_old_dummy()
  31. {
  32. return 0;
  33. }
  34. #ifndef HAVE_OGR
  35. static int format()
  36. {
  37. G_fatal_error(_("Requested format is not compiled in this version"));
  38. return 0;
  39. }
  40. #endif
  41. static int Open_level = 0;
  42. static int (*Open_old_array[][2]) () = {
  43. {
  44. open_old_dummy, V1_open_old_nat}
  45. #ifdef HAVE_OGR
  46. , {
  47. open_old_dummy, V1_open_old_ogr}
  48. #else
  49. , {
  50. open_old_dummy, format}
  51. #endif
  52. };
  53. static void fatal_error(int ferror, char *errmsg)
  54. {
  55. switch (ferror) {
  56. case GV_FATAL_EXIT:
  57. G_fatal_error(errmsg);
  58. break;
  59. case GV_FATAL_PRINT:
  60. G_warning(errmsg);
  61. break;
  62. case GV_FATAL_RETURN:
  63. break;
  64. }
  65. }
  66. /*!
  67. * \brief Predetermine level at which a map will be opened for
  68. * reading.
  69. *
  70. * If it can't open that level, the open will fail. The specified
  71. * level must be set before any call to open. The default is to try
  72. * to open the highest level possible, and keep stepping down until
  73. * success.
  74. *
  75. * NOTE!! This should only be used to set when you wish to force
  76. * a lower level open. If you require a higher level, then just
  77. * check the return to verify the level instead of forcing it.
  78. * This is because future releases will have higher levels which
  79. * will be downward compatible and which your programs should
  80. * support by default.
  81. *
  82. * \param[in] level vector (topo) level
  83. *
  84. * \return 0 on success
  85. * \return 1 on error
  86. */
  87. int Vect_set_open_level(int level)
  88. {
  89. Open_level = level;
  90. if (Open_level < 1 || Open_level > MAX_OPEN_LEVEL) {
  91. G_warning(_("Programmer requested unknown open level %d"),
  92. Open_level);
  93. Open_level = 0;
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. /*!
  99. * \brief Open old vector for reading.
  100. *
  101. * In case of error, the functions respect fatal error settings.
  102. *
  103. * \param[out] Map vector map
  104. * \param[in] name name of vector map to open
  105. * \param[in] mapset mapset name
  106. * \param[in] update open for update
  107. * \param[in] head_only read only header info from 'head', 'dbln', 'topo', 'cidx' is not opened. The header may be opened on level 2 only.
  108. *
  109. * \return level of openness (1, 2)
  110. * \return -1 in error
  111. */
  112. int
  113. Vect__open_old(struct Map_info *Map, const char *name, const char *mapset,
  114. int update, int head_only)
  115. {
  116. char buf[GNAME_MAX + 10], buf2[GMAPSET_MAX + 10], xname[GNAME_MAX],
  117. xmapset[GMAPSET_MAX], errmsg[2000];
  118. FILE *fp;
  119. int level, level_request, ferror;
  120. int format, ret;
  121. char *fmapset;
  122. G_debug(1, "Vect_open_old(): name = %s mapset= %s update = %d", name,
  123. mapset, update);
  124. /* TODO: Open header for update ('dbln') */
  125. ferror = Vect_get_fatal_error();
  126. Vect_set_fatal_error(GV_FATAL_EXIT);
  127. level_request = Open_level;
  128. Open_level = 0;
  129. Vect__init_head(Map);
  130. dig_init_plus(&(Map->plus));
  131. if (G__name_is_fully_qualified(name, xname, xmapset)) {
  132. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, xname);
  133. sprintf(buf2, "%s@%s", GRASS_VECT_COOR_ELEMENT, xmapset);
  134. Map->name = G_store(xname);
  135. Map->mapset = G_store(xmapset);
  136. }
  137. else {
  138. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, name);
  139. sprintf(buf2, "%s", GRASS_VECT_COOR_ELEMENT);
  140. Map->name = G_store(name);
  141. if (mapset)
  142. Map->mapset = G_store(mapset);
  143. else
  144. Map->mapset = G_store("");
  145. }
  146. fmapset = G_find_vector2(Map->name, Map->mapset);
  147. if (fmapset == NULL) {
  148. sprintf(errmsg, _("Vector map <%s> not found"),
  149. Vect_get_full_name(Map));
  150. fatal_error(ferror, errmsg);
  151. return -1;
  152. }
  153. Map->mapset = G_store(fmapset);
  154. Map->location = G_store(G_location());
  155. Map->gisdbase = G_store(G_gisdbase());
  156. if (update && (0 != strcmp(Map->mapset, G_mapset()))) {
  157. G_warning(_("Vector map which is not in the current mapset cannot be opened for update"));
  158. return -1;
  159. }
  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"));
  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, (long)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[in] name name of vector map
  362. * \param[in] 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[in] name name of vector map to update
  378. * \param[in] 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[in] name name of vector map to read
  409. * \param[in] 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[in] name name of vector map to update
  426. * \param[in] 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[in] name name of vector map
  453. * \param[in] 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_file2(GRASS_VECT_DIRECTORY, 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[in] 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(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 = (long)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 = %ld, Info->mtime = %ld", Info->size,
  570. Info->mtime);
  571. return 1;
  572. }
  573. /*!
  574. * \brief Gets maptype (native, shape, postgis)
  575. *
  576. * \param[in] Map vector map
  577. *
  578. * \return maptype string on success
  579. * \return error message on error
  580. */
  581. const char *Vect_maptype_info(struct Map_info *Map)
  582. {
  583. char maptype[1000];
  584. switch (Map->format) {
  585. case GV_FORMAT_NATIVE:
  586. sprintf(maptype, "native");
  587. break;
  588. case GV_FORMAT_OGR:
  589. sprintf(maptype, "ogr");
  590. break;
  591. default:
  592. sprintf(maptype, "unknown %d (update Vect_maptype_info)",
  593. Map->format);
  594. }
  595. return G_store(maptype);
  596. }
  597. /*!
  598. * \brief Open topo file
  599. *
  600. * \param[in,out] Map vector map
  601. * \param[in] head_only open only head
  602. *
  603. * \return 0 on success
  604. * \return 1 file does not exist
  605. * \return -1 on error
  606. */
  607. int Vect_open_topo(struct Map_info *Map, int head_only)
  608. {
  609. int err, ret;
  610. char buf[500], file_path[2000];
  611. GVFILE fp;
  612. struct Coor_info CInfo;
  613. struct Plus_head *Plus;
  614. struct stat info;
  615. G_debug(1, "Vect_open_topo(): name = %s mapset= %s", Map->name,
  616. Map->mapset);
  617. Plus = &(Map->plus);
  618. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  619. G__file_name(file_path, buf, GV_TOPO_ELEMENT, Map->mapset);
  620. if (stat(file_path, &info) != 0) /* does not exist */
  621. return 1;
  622. dig_file_init(&fp);
  623. fp.file = G_fopen_old(buf, GV_TOPO_ELEMENT, Map->mapset);
  624. if (fp.file == NULL) { /* topo file is not available */
  625. G_debug(1, "Cannot open topo file for vector '%s@%s'.",
  626. Map->name, Map->mapset);
  627. return -1;
  628. }
  629. /* get coor info */
  630. Vect_coor_info(Map, &CInfo);
  631. /* load head */
  632. if (dig_Rd_Plus_head(&fp, Plus) == -1)
  633. return -1;
  634. G_debug(1, "Topo head: coor size = %ld, coor mtime = %ld",
  635. Plus->coor_size, Plus->coor_mtime);
  636. /* do checks */
  637. err = 0;
  638. if (CInfo.size != Plus->coor_size) {
  639. G_warning(_("Size of 'coor' file differs from value saved in topology file"));
  640. err = 1;
  641. }
  642. /* Do not check mtime because mtime is changed by copy */
  643. /*
  644. if ( CInfo.mtime != Plus->coor_mtime ) {
  645. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  646. err = 1;
  647. }
  648. */
  649. if (err) {
  650. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  651. Map->name, Map->mapset);
  652. return -1;
  653. }
  654. /* load file to the memory */
  655. /* dig_file_load ( &fp); */
  656. /* load topo to memory */
  657. ret = dig_load_plus(Plus, &fp, head_only);
  658. fclose(fp.file);
  659. /* dig_file_free ( &fp); */
  660. if (ret == 0)
  661. return -1;
  662. return 0;
  663. }
  664. /*!
  665. * \brief Open spatial index file
  666. *
  667. * \param[in,out] Map vector map
  668. *
  669. * \return 0 on success
  670. * \return -1 on error
  671. */
  672. int Vect_open_spatial_index(struct Map_info *Map)
  673. {
  674. char buf[500];
  675. GVFILE fp;
  676. /* struct Coor_info CInfo; */
  677. struct Plus_head *Plus;
  678. G_debug(1, "Vect_open_spatial_index(): name = %s mapset= %s", Map->name,
  679. Map->mapset);
  680. Plus = &(Map->plus);
  681. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  682. dig_file_init(&fp);
  683. fp.file = G_fopen_old(buf, GV_SIDX_ELEMENT, Map->mapset);
  684. if (fp.file == NULL) { /* spatial index file is not available */
  685. G_debug(1, "Cannot open spatial index file for vector '%s@%s'.",
  686. Map->name, Map->mapset);
  687. return -1;
  688. }
  689. /* TODO: checks */
  690. /* load head */
  691. /*
  692. dig_Rd_spindx_head (fp, Plus);
  693. G_debug ( 1, "Spindx head: coor size = %ld, coor mtime = %ld",
  694. Plus->coor_size, Plus->coor_mtime);
  695. */
  696. /* do checks */
  697. /*
  698. err = 0;
  699. if ( CInfo.size != Plus->coor_size ) {
  700. G_warning ( "Size of 'coor' file differs from value saved in topo file.\n");
  701. err = 1;
  702. }
  703. */
  704. /* Do not check mtime because mtime is changed by copy */
  705. /*
  706. if ( CInfo.mtime != Plus->coor_mtime ) {
  707. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  708. err = 1;
  709. }
  710. */
  711. /*
  712. if ( err ) {
  713. G_warning ( "Please rebuild topology for vector '%s@%s'\n", Map->name,
  714. Map->mapset );
  715. return -1;
  716. }
  717. */
  718. /* load file to the memory */
  719. /* dig_file_load ( &fp); */
  720. /* load topo to memory */
  721. dig_spidx_init(Plus);
  722. dig_read_spidx(&fp, Plus);
  723. fclose(fp.file);
  724. /* dig_file_free ( &fp); */
  725. return 0;
  726. }