open.c 29 KB

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