open.c 29 KB

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