open.c 25 KB

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