open.c 28 KB

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