open.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /*!
  2. * \file vector/Vlib/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 vector 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 to
  70. * 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 a
  74. * lower level open. If you require a higher level, then just check
  75. * the return to verify the level instead of forcing it. This is
  76. * because future releases will have higher levels which will be
  77. * downward compatible and which your programs should support by
  78. * 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. int ogr_mapset;
  120. const char *fmapset;
  121. G_debug(1, "Vect__open_old(): name = %s mapset= %s update = %d", name,
  122. mapset, update);
  123. /* zero Map_info structure */
  124. G_zero(Map, sizeof(struct Map_info));
  125. /* TODO: Open header for update ('dbln') */
  126. ferror = Vect_get_fatal_error();
  127. Vect_set_fatal_error(GV_FATAL_EXIT);
  128. level_request = Open_level;
  129. Open_level = 0;
  130. /* initialize Map->head */
  131. Vect__init_head(Map);
  132. /* initialize Map->plus */
  133. dig_init_plus(&(Map->plus));
  134. ogr_mapset = 0;
  135. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  136. if (strcmp(xmapset, "OGR") == 0) {
  137. /* unique OGR mapset detected */
  138. G_debug(1, "OGR mapset detected");
  139. ogr_mapset = 1;
  140. Map->fInfo.ogr.dsn = xname;
  141. Map->fInfo.ogr.layer_name = NULL; /* no layer to be open */
  142. }
  143. else {
  144. sprintf(buf, "%s/%s", GV_DIRECTORY, xname);
  145. sprintf(buf2, "%s@%s", GV_COOR_ELEMENT, xmapset);
  146. }
  147. Map->name = G_store(xname);
  148. Map->mapset = G_store(xmapset);
  149. }
  150. else {
  151. sprintf(buf, "%s/%s", GV_DIRECTORY, name);
  152. sprintf(buf2, "%s", GV_COOR_ELEMENT);
  153. Map->name = G_store(name);
  154. if (mapset)
  155. Map->mapset = G_store(mapset);
  156. else
  157. Map->mapset = G_store("");
  158. }
  159. fmapset = G_find_vector2(Map->name, Map->mapset);
  160. if (fmapset == NULL) {
  161. sprintf(errmsg, _("Vector map <%s> not found"),
  162. Vect_get_full_name(Map));
  163. fatal_error(ferror, errmsg);
  164. return -1;
  165. }
  166. Map->mapset = G_store(fmapset);
  167. Map->location = G_store(G_location());
  168. Map->gisdbase = G_store(G_gisdbase());
  169. if (update && (0 != strcmp(Map->mapset, G_mapset()))) {
  170. G_warning(_("Vector map which is not in the current mapset cannot be opened for update"));
  171. return -1;
  172. }
  173. G_debug(1, "Map name: %s", Map->name);
  174. G_debug(1, "Map mapset: %s", Map->mapset);
  175. /* Read vector format information */
  176. if (ogr_mapset) {
  177. format = GV_FORMAT_OGR;
  178. }
  179. else {
  180. format = 0;
  181. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  182. G_debug(1, "open format file: '%s/%s/%s'", Map->mapset, buf,
  183. GV_FRMT_ELEMENT);
  184. fp = G_fopen_old(buf, GV_FRMT_ELEMENT, Map->mapset);
  185. if (fp == NULL) {
  186. G_debug(1, "Vector format: %d (native)", format);
  187. format = GV_FORMAT_NATIVE;
  188. }
  189. else {
  190. format = dig_read_frmt_ascii(fp, &(Map->fInfo));
  191. fclose(fp);
  192. G_debug(1, "Vector format: %d (non-native)", format);
  193. if (format < 0) {
  194. sprintf(errmsg, _("Unable to open vector map <%s>"),
  195. Vect_get_full_name(Map));
  196. fatal_error(ferror, errmsg);
  197. return -1;
  198. }
  199. }
  200. }
  201. Map->format = format;
  202. /* Read vector head */
  203. if (!ogr_mapset && Vect__read_head(Map) != 0) {
  204. sprintf(errmsg,
  205. _("Unable to open vector map <%s> on level %d. "
  206. "Try to rebuild vector topology by v.build."),
  207. Vect_get_full_name(Map), level_request);
  208. G_warning(_("Unable to read head file of vector <%s>"),
  209. Vect_get_full_name(Map));
  210. }
  211. G_debug(1, "Level request = %d", level_request);
  212. /* There are only 2 possible open levels, 1 and 2. Try first to open 'support' files
  213. * (topo,sidx,cidx), these files are the same for all formats.
  214. * If it is not possible and requested level is 2, return error,
  215. * otherwise call Open_old_array[format][1], to open remaining files/sources (level 1)
  216. */
  217. /* Try to open support files if level was not requested or requested level is 2 (format independent) */
  218. if (level_request == 0 || level_request == 2) {
  219. level = 2; /* We expect success */
  220. /* open topo */
  221. ret = Vect_open_topo(Map, head_only);
  222. if (ret == 1) { /* topo file is not available */
  223. G_debug(1, "topo file for vector '%s' not available.",
  224. Vect_get_full_name(Map));
  225. level = 1;
  226. }
  227. else if (ret == -1) {
  228. G_fatal_error(_("Unable to open topology file for vector map <%s>"),
  229. Vect_get_full_name(Map));
  230. }
  231. /* open spatial index */
  232. if (level == 2) {
  233. ret = Vect_open_sidx(Map, (update != 0));
  234. if (ret == 1) { /* sidx file is not available */
  235. G_debug(1, "sidx file for vector '%s' not available.",
  236. Vect_get_full_name(Map));
  237. level = 1;
  238. }
  239. else if (ret == -1) {
  240. G_fatal_error(_("Unable to open spatial index file for vector map <%s>"),
  241. Vect_get_full_name(Map));
  242. }
  243. }
  244. /* open category index */
  245. if (level == 2) {
  246. ret = Vect_cidx_open(Map, head_only);
  247. if (ret == 1) { /* category index is not available */
  248. G_debug(1,
  249. "cidx file for vector '%s' not available.",
  250. Vect_get_full_name(Map));
  251. dig_free_plus(&(Map->plus)); /* free topology */
  252. dig_spidx_free(&(Map->plus)); /* free spatial index */
  253. level = 1;
  254. }
  255. else if (ret == -1) { /* file exists, but cannot be opened */
  256. G_fatal_error(_("Unable to open category index file for vector map <%s>"),
  257. Vect_get_full_name(Map));
  258. }
  259. }
  260. #ifdef HAVE_OGR
  261. /* Open OGR specific support files */
  262. if (level == 2 && Map->format == GV_FORMAT_OGR) {
  263. if (V2_open_old_ogr(Map) < 0) {
  264. dig_free_plus(&(Map->plus));
  265. dig_spidx_free(&(Map->plus));
  266. dig_cidx_free(&(Map->plus));
  267. level = 1;
  268. }
  269. }
  270. #endif
  271. if (level_request == 2 && level < 2) {
  272. if (ogr_mapset) {
  273. G_warning(_("Topology level (2) is not supported when reading "
  274. "OGR layers directly. For topology level "
  275. "is required link to OGR layer via v.external command."));
  276. }
  277. else {
  278. sprintf(errmsg,
  279. _("Unable to open vector map <%s> on level %d. "
  280. "Try to rebuild vector topology by v.build."),
  281. Vect_get_full_name(Map), level_request);
  282. fatal_error(ferror, errmsg);
  283. return -1;
  284. }
  285. }
  286. }
  287. else {
  288. level = 1; /* I.e. requested level is 1 */
  289. }
  290. /* Open level 1 files / sources (format specific) */
  291. if (!head_only) { /* No need to open coordinates */
  292. if (0 != (*Open_old_array[format][1]) (Map, update)) { /* Cannot open */
  293. if (level == 2) { /* support files opened */
  294. dig_free_plus(&(Map->plus));
  295. dig_spidx_free(&(Map->plus));
  296. dig_cidx_free(&(Map->plus));
  297. }
  298. sprintf(errmsg,
  299. _("Unable to open vector map <%s> on level %d. "
  300. "Try to rebuild vector topology by v.build."),
  301. Vect_get_full_name(Map), level_request);
  302. fatal_error(ferror, errmsg);
  303. return -1;
  304. }
  305. }
  306. else {
  307. Map->head.with_z = Map->plus.with_z; /* take dimension from topo */
  308. }
  309. /* Set status */
  310. Map->open = VECT_OPEN_CODE;
  311. Map->level = level;
  312. Map->head_only = head_only;
  313. Map->support_updated = 0;
  314. if (update) {
  315. Map->mode = GV_MODE_RW;
  316. Map->plus.mode = GV_MODE_RW;
  317. }
  318. else {
  319. Map->mode = GV_MODE_READ;
  320. Map->plus.mode = GV_MODE_READ;
  321. }
  322. if (head_only) {
  323. Map->head_only = 1;
  324. }
  325. else {
  326. Map->head_only = 0;
  327. }
  328. Map->Constraint_region_flag = 0;
  329. Map->Constraint_type_flag = 0;
  330. G_debug(1, "Vect_open_old(): vector opened on level %d", level);
  331. if (level == 1) { /* without topology */
  332. Map->plus.built = GV_BUILD_NONE;
  333. }
  334. else { /* level 2, with topology */
  335. Map->plus.built = GV_BUILD_ALL; /* Highest level of topology for level 2 */
  336. }
  337. Map->plus.do_uplist = 0;
  338. Map->dblnk = Vect_new_dblinks_struct();
  339. Vect_read_dblinks(Map);
  340. /* Open history file */
  341. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  342. if (update) { /* native only */
  343. Map->hist_fp = G_fopen_modify(buf, GV_HIST_ELEMENT);
  344. if (Map->hist_fp == NULL) {
  345. sprintf(errmsg,
  346. _("Unable to open history file for vector map <%s>"),
  347. Vect_get_full_name(Map));
  348. fatal_error(ferror, errmsg);
  349. return (-1);
  350. }
  351. fseek(Map->hist_fp, (off_t) 0, SEEK_END);
  352. Vect_hist_write(Map,
  353. "---------------------------------------------------------------------------------\n");
  354. }
  355. else {
  356. if (Map->format == GV_FORMAT_NATIVE || Map->format == GV_FORMAT_OGR) {
  357. Map->hist_fp =
  358. G_fopen_old(buf, GV_HIST_ELEMENT, Map->mapset);
  359. /* If NULL (does not exist) then Vect_hist_read() handle that */
  360. }
  361. else {
  362. Map->hist_fp = NULL;
  363. }
  364. }
  365. if (!head_only) { /* Cannot rewind if not fully opened */
  366. Vect_rewind(Map);
  367. }
  368. /* Delete support files if native format was opened for update (not head_only) */
  369. if (update && !head_only) {
  370. char file_path[2000];
  371. struct stat info;
  372. sprintf(buf, "%s/%s", GV_DIRECTORY, name);
  373. G__file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
  374. if (stat(file_path, &info) == 0) /* file exists? */
  375. unlink(file_path);
  376. G__file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
  377. if (stat(file_path, &info) == 0) /* file exists? */
  378. unlink(file_path);
  379. G__file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
  380. if (stat(file_path, &info) == 0) /* file exists? */
  381. unlink(file_path);
  382. }
  383. return (level);
  384. }
  385. /*!
  386. * \brief Open existing vector for reading
  387. *
  388. * In case of error, the functions respect fatal error settings.
  389. *
  390. * \param[out] Map vector map
  391. * \param name name of vector map
  392. * \param mapset mapset name
  393. *
  394. * \return level of openness [1, 2, (3)]
  395. * \return -1 on error
  396. */
  397. int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
  398. {
  399. return (Vect__open_old(Map, name, mapset, 0, 0));
  400. }
  401. /*!
  402. * \brief Open existing vector for reading/writing
  403. *
  404. * In case of error, the functions respect fatal error settings.
  405. *
  406. * \param[out] Map vector map
  407. * \param name name of vector map to update
  408. * \param mapset mapset name
  409. *
  410. * \return level of openness [1, 2, (3)]
  411. * \return -1 on error
  412. */
  413. int
  414. Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
  415. {
  416. int ret;
  417. ret = Vect__open_old(Map, name, mapset, 1, 0);
  418. if (ret > 0) {
  419. Map->plus.do_uplist = 1;
  420. Map->plus.uplines = NULL;
  421. Map->plus.n_uplines = 0;
  422. Map->plus.alloc_uplines = 0;
  423. Map->plus.upnodes = NULL;
  424. Map->plus.n_upnodes = 0;
  425. Map->plus.alloc_upnodes = 0;
  426. /* read spatial index */
  427. /* Build spatial index from topo */
  428. /* Vect_build_sidx_from_topo(Map); */
  429. }
  430. return ret;
  431. }
  432. /*!
  433. * \brief Reads only info about vector map from headers of 'head',
  434. * 'dbln', 'topo' and 'cidx' file.
  435. *
  436. * In case of error, the functions respect fatal error settings.
  437. *
  438. * \param[out] Map pointer to Map_info structure
  439. * \param name name of vector map to read
  440. * \param mapset mapset name
  441. *
  442. * \return level of openness [1, 2, (3)]
  443. * \return -1 on error
  444. */
  445. int Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
  446. {
  447. return (Vect__open_old(Map, name, mapset, 0, 1));
  448. }
  449. /*!
  450. * \brief Open old vector head for updating (mostly for database link updates)
  451. *
  452. * In case of error, the functions respect fatal error settings.
  453. *
  454. * \param[out] Map vector map
  455. * \param name name of vector map to update
  456. * \param mapset mapset name
  457. *
  458. * \return level of openness [1, 2, (3)]
  459. * \return -1 on error
  460. */
  461. int
  462. Vect_open_update_head(struct Map_info *Map, const char *name,
  463. const char *mapset)
  464. {
  465. int ret;
  466. ret = Vect__open_old(Map, name, mapset, 1, 1);
  467. if (ret > 0) { /* Probably not important */
  468. Map->plus.do_uplist = 1;
  469. Map->plus.uplines = NULL;
  470. Map->plus.n_uplines = 0;
  471. Map->plus.alloc_uplines = 0;
  472. Map->plus.upnodes = NULL;
  473. Map->plus.n_upnodes = 0;
  474. Map->plus.alloc_upnodes = 0;
  475. }
  476. return ret;
  477. }
  478. /*!
  479. * \brief Open new vector for reading/writing
  480. *
  481. * \param[in,out] Map pointer to Map_info structure
  482. * \param name name of vector map
  483. * \param with_z non-zero value for 3D vector data
  484. *
  485. * \return 1 on success
  486. * \return -1 on error
  487. */
  488. int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
  489. {
  490. int ret, ferror;
  491. char errmsg[2000], buf[500];
  492. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  493. G_debug(2, "Vect_open_new(): name = %s", name);
  494. Vect__init_head(Map);
  495. ferror = Vect_get_fatal_error();
  496. Vect_set_fatal_error(GV_FATAL_EXIT);
  497. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  498. if (strcmp(xmapset, G_mapset()) != 0) {
  499. sprintf(errmsg, _("%s is not in the current mapset (%s)"), name,
  500. G_mapset());
  501. fatal_error(ferror, errmsg);
  502. }
  503. name = xname;
  504. }
  505. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  506. if (Vect_legal_filename(name) < 0) {
  507. sprintf(errmsg, _("Vector map name is not SQL compliant"));
  508. fatal_error(ferror, errmsg);
  509. return (-1);
  510. }
  511. /* Check if map already exists */
  512. if (G_find_vector2(name, G_mapset()) != NULL) {
  513. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  514. name);
  515. ret = Vect_delete(name);
  516. if (ret == -1) {
  517. sprintf(errmsg, _("Unable to delete vector map <%s>"), name);
  518. fatal_error(ferror, errmsg);
  519. return (-1);
  520. }
  521. }
  522. Map->name = G_store(name);
  523. Map->mapset = G_store(G_mapset());
  524. Map->location = G_store(G_location());
  525. Map->gisdbase = G_store(G_gisdbase());
  526. Map->format = GV_FORMAT_NATIVE;
  527. if (V1_open_new_nat(Map, name, with_z) < 0) {
  528. sprintf(errmsg, _("Unable to create vector map <%s>"),
  529. Vect_get_full_name(Map));
  530. fatal_error(ferror, errmsg);
  531. return (-1);
  532. }
  533. /* Open history file */
  534. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  535. Map->hist_fp = G_fopen_new(buf, GV_HIST_ELEMENT);
  536. if (Map->hist_fp == NULL) {
  537. sprintf(errmsg, _("Unable to open history file for vector map <%s>"),
  538. Vect_get_full_name(Map));
  539. fatal_error(ferror, errmsg);
  540. return (-1);
  541. }
  542. Open_level = 0;
  543. /* initialize topo */
  544. dig_init_plus(&(Map->plus));
  545. /* initialize spatial index */
  546. Vect_open_sidx(Map, 2);
  547. Map->open = VECT_OPEN_CODE;
  548. Map->level = 1;
  549. Map->head_only = 0;
  550. Map->support_updated = 0;
  551. Map->plus.built = GV_BUILD_NONE;
  552. Map->mode = GV_MODE_RW;
  553. Map->Constraint_region_flag = 0;
  554. Map->Constraint_type_flag = 0;
  555. Map->head.with_z = with_z;
  556. Map->plus.do_uplist = 0;
  557. Map->dblnk = Vect_new_dblinks_struct();
  558. return 1;
  559. }
  560. /*!
  561. * \brief Update Coor_info structure
  562. *
  563. * \param Map pointer to Map_info structure
  564. * \param[out] Info pointer to Coor_info structure
  565. *
  566. * \return 1 on success
  567. * \return 0 on error
  568. */
  569. int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info)
  570. {
  571. char buf[2000], path[2000];
  572. struct stat stat_buf;
  573. switch (Map->format) {
  574. case GV_FORMAT_NATIVE:
  575. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  576. G__file_name(path, buf, GV_COOR_ELEMENT, Map->mapset);
  577. G_debug(1, "get coor info: %s", path);
  578. if (0 != stat(path, &stat_buf)) {
  579. G_warning(_("Unable to stat file <%s>"), path);
  580. Info->size = -1L;
  581. Info->mtime = -1L;
  582. }
  583. else {
  584. Info->size = (off_t) stat_buf.st_size; /* file size */
  585. Info->mtime = (long)stat_buf.st_mtime; /* last modified time */
  586. }
  587. /* stat does not give correct size on MINGW
  588. * if the file is opened */
  589. #ifdef __MINGW32__
  590. if (Map->open == VECT_OPEN_CODE) {
  591. dig_fseek(&(Map->dig_fp), 0L, SEEK_END);
  592. G_debug(2, "ftell = %d", dig_ftell(&(Map->dig_fp)));
  593. Info->size = dig_ftell(&(Map->dig_fp));
  594. }
  595. #endif
  596. break;
  597. case GV_FORMAT_OGR:
  598. Info->size = 0L;
  599. Info->mtime = 0L;
  600. break;
  601. }
  602. G_debug(1, "Info->size = %lu, Info->mtime = %ld",
  603. (unsigned long)Info->size, Info->mtime);
  604. return 1;
  605. }
  606. /*!
  607. * \brief Gets maptype (native, shape, postgis)
  608. *
  609. * Note: string is allocated by G_store(). Free allocated memory with
  610. * G_free().
  611. *
  612. * \param Map vector map
  613. *
  614. * \return maptype string on success
  615. * \return error message on error
  616. */
  617. const char *Vect_maptype_info(const struct Map_info *Map)
  618. {
  619. char maptype[1000];
  620. switch (Map->format) {
  621. case GV_FORMAT_NATIVE:
  622. sprintf(maptype, "native");
  623. break;
  624. case GV_FORMAT_OGR:
  625. sprintf(maptype, "ogr");
  626. break;
  627. default:
  628. sprintf(maptype, "unknown %d (update Vect_maptype_info)",
  629. Map->format);
  630. }
  631. return G_store(maptype);
  632. }
  633. /*!
  634. * \brief Open topo file
  635. *
  636. * \param[in,out] Map pointer to Map_info structure
  637. * \param head_only open only head
  638. *
  639. * \return 0 on success
  640. * \return 1 file does not exist
  641. * \return -1 on error
  642. */
  643. int Vect_open_topo(struct Map_info *Map, int head_only)
  644. {
  645. int err, ret;
  646. char buf[500], file_path[2000];
  647. struct gvfile fp;
  648. struct Coor_info CInfo;
  649. struct Plus_head *Plus;
  650. struct stat info;
  651. G_debug(1, "Vect_open_topo(): name = %s mapset= %s", Map->name,
  652. Map->mapset);
  653. Plus = &(Map->plus);
  654. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  655. G__file_name(file_path, buf, GV_TOPO_ELEMENT, Map->mapset);
  656. if (stat(file_path, &info) != 0) /* does not exist */
  657. return 1;
  658. dig_file_init(&fp);
  659. fp.file = G_fopen_old(buf, GV_TOPO_ELEMENT, Map->mapset);
  660. if (fp.file == NULL) { /* topo file is not available */
  661. G_debug(1, "Cannot open topo file for vector '%s@%s'.",
  662. Map->name, Map->mapset);
  663. return -1;
  664. }
  665. /* get coor info */
  666. Vect_coor_info(Map, &CInfo);
  667. /* load head */
  668. if (dig_Rd_Plus_head(&fp, Plus) == -1)
  669. return -1;
  670. G_debug(1, "Topo head: coor size = %lu, coor mtime = %ld",
  671. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  672. /* do checks */
  673. err = 0;
  674. if (CInfo.size != Plus->coor_size) {
  675. G_warning(_("Size of 'coor' file differs from value saved in topology file"));
  676. err = 1;
  677. }
  678. /* Do not check mtime because mtime is changed by copy */
  679. /*
  680. if ( CInfo.mtime != Plus->coor_mtime ) {
  681. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  682. err = 1;
  683. }
  684. */
  685. if (err) {
  686. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  687. Map->name, Map->mapset);
  688. return -1;
  689. }
  690. /* load file to the memory */
  691. /* dig_file_load ( &fp); */
  692. /* load topo to memory */
  693. ret = dig_load_plus(Plus, &fp, head_only);
  694. fclose(fp.file);
  695. /* dig_file_free ( &fp); */
  696. if (ret == 0)
  697. return -1;
  698. return 0;
  699. }
  700. /*!
  701. * \brief Open spatial index file ('sidx')
  702. *
  703. * \param[in,out] Map pointer to Map_info
  704. * \param mode 0 old, 1 update, 2 new
  705. *
  706. * \return 0 on success
  707. * \return -1 on error
  708. */
  709. int Vect_open_sidx(struct Map_info *Map, int mode)
  710. {
  711. char buf[500], file_path[2000];
  712. int err;
  713. struct Coor_info CInfo;
  714. struct Plus_head *Plus;
  715. struct stat info;
  716. G_debug(1, "Vect_open_sidx(): name = %s mapset= %s mode = %s", Map->name,
  717. Map->mapset, mode == 0 ? "old" : (mode == 1 ? "update" : "new"));
  718. if (Map->plus.Spidx_built == 1) {
  719. G_warning("Spatial index already opened");
  720. return 0;
  721. }
  722. Plus = &(Map->plus);
  723. dig_file_init(&(Map->plus.spidx_fp));
  724. if (mode < 2) {
  725. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  726. G__file_name(file_path, buf, GV_SIDX_ELEMENT, Map->mapset);
  727. if (stat(file_path, &info) != 0) /* does not exist */
  728. return 1;
  729. Map->plus.spidx_fp.file =
  730. G_fopen_old(buf, GV_SIDX_ELEMENT, Map->mapset);
  731. if (Map->plus.spidx_fp.file == NULL) { /* sidx file is not available */
  732. G_debug(1, "Cannot open spatial index file for vector '%s@%s'.",
  733. Map->name, Map->mapset);
  734. return -1;
  735. }
  736. /* get coor info */
  737. Vect_coor_info(Map, &CInfo);
  738. /* initialize spatial index */
  739. Map->plus.Spidx_new = 0;
  740. dig_spidx_init(Plus);
  741. /* load head */
  742. if (dig_Rd_spidx_head(&(Map->plus.spidx_fp), Plus) == -1) {
  743. fclose(Map->plus.spidx_fp.file);
  744. return -1;
  745. }
  746. G_debug(1, "Sidx head: coor size = %lu, coor mtime = %ld",
  747. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  748. /* do checks */
  749. err = 0;
  750. if (CInfo.size != Plus->coor_size) {
  751. G_warning(_("Size of 'coor' file differs from value saved in sidx file"));
  752. err = 1;
  753. }
  754. /* Do not check mtime because mtime is changed by copy */
  755. /*
  756. if ( CInfo.mtime != Plus->coor_mtime ) {
  757. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  758. err = 1;
  759. }
  760. */
  761. if (err) {
  762. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  763. Map->name, Map->mapset);
  764. fclose(Map->plus.spidx_fp.file);
  765. return -1;
  766. }
  767. }
  768. if (mode) {
  769. /* open new spatial index */
  770. Map->plus.Spidx_new = 1;
  771. dig_spidx_init(Plus);
  772. if (mode == 1) {
  773. /* load spatial index for update */
  774. if (dig_Rd_spidx(&(Map->plus.spidx_fp), Plus) == -1) {
  775. fclose(Map->plus.spidx_fp.file);
  776. return -1;
  777. }
  778. }
  779. }
  780. Map->plus.Spidx_built = 1;
  781. return 0;
  782. }