open.c 26 KB

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