open.c 23 KB

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