open.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. if (level < 2 && Map->head.with_z) {
  361. /* topo has been initialized as 2D, update to 3D */
  362. dig_free_plus(&(Map->plus));
  363. dig_spidx_free(&(Map->plus));
  364. dig_cidx_free(&(Map->plus));
  365. Map->plus.with_z = Map->head.with_z;
  366. dig_init_plus(&(Map->plus));
  367. }
  368. }
  369. else if (level > 1) {
  370. /* take dimension from topo if topo is available */
  371. Map->head.with_z = Map->plus.with_z;
  372. }
  373. /* set status */
  374. Map->open = VECT_OPEN_CODE;
  375. Map->level = level;
  376. Map->head_only = head_only;
  377. Map->support_updated = FALSE;
  378. if (update) {
  379. Map->mode = GV_MODE_RW;
  380. Map->plus.mode = GV_MODE_RW;
  381. }
  382. else {
  383. Map->mode = GV_MODE_READ;
  384. Map->plus.mode = GV_MODE_READ;
  385. }
  386. if (head_only) {
  387. Map->head_only = TRUE;
  388. }
  389. else {
  390. Map->head_only = FALSE;
  391. }
  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.uplist.do_uplist = FALSE;
  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. G_warning(_("Unable to open history file for vector map <%s>"),
  409. Vect_get_full_name(Map));
  410. return -1;
  411. }
  412. G_fseek(Map->hist_fp, (off_t) 0, SEEK_END);
  413. Vect_hist_write(Map,
  414. "---------------------------------------------------------------------------------\n");
  415. }
  416. else {
  417. if (Map->format == GV_FORMAT_NATIVE || Map->format == GV_FORMAT_OGR ||
  418. Map->format == GV_FORMAT_POSTGIS) {
  419. Map->hist_fp = G_fopen_old(buf, GV_HIST_ELEMENT, Map->mapset);
  420. /* If NULL (does not exist) then Vect_hist_read() handle that */
  421. }
  422. else {
  423. Map->hist_fp = NULL;
  424. }
  425. }
  426. if (!head_only) { /* cannot rewind if not fully opened */
  427. Vect_rewind(Map);
  428. }
  429. /* delete support files if native format was opened for update (not head_only) */
  430. if (update && !head_only) {
  431. char file_path[GPATH_MAX];
  432. sprintf(buf, "%s/%s", GV_DIRECTORY, name);
  433. G_file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
  434. if (access(file_path, F_OK) == 0) /* topo file exists? */
  435. unlink(file_path);
  436. G_file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
  437. if (access(file_path, F_OK) == 0) /* sidx file exists? */
  438. unlink(file_path);
  439. G_file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
  440. if (access(file_path, F_OK) == 0) /* cidx file exists? */
  441. unlink(file_path);
  442. if (format == GV_FORMAT_OGR || format == GV_FORMAT_POSTGIS) {
  443. G_file_name(file_path, buf, GV_FIDX_ELEMENT, G_mapset());
  444. if (access(file_path, F_OK) == 0) /* fidx file exists? */
  445. unlink(file_path);
  446. }
  447. }
  448. return level;
  449. }
  450. /*!
  451. \brief Open existing vector map for reading
  452. This function is replaced by Vect_open_old2() to handle also direct
  453. OGR support.
  454. Calls G_fatal_error() on failure.
  455. \param[out] Map pointer to Map_info structure
  456. \param name name of vector map to open
  457. \param mapset mapset name ("" for search path)
  458. \return 1 open on level 1 (without topology)
  459. \return 2 open on level 2 (with topology)
  460. \return -1 on error
  461. */
  462. int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
  463. {
  464. return open_old(Map, name, mapset, NULL, 0, 0);
  465. }
  466. /*!
  467. \brief Open existing vector map for reading
  468. Calls G_fatal_error() on failure.
  469. \param[out] Map pointer to Map_info structure
  470. \param name name of vector map to open (datasource for direct OGR access)
  471. \param mapset mapset name ("" for search path, "OGR" for direct OGR access)
  472. \param layer layer name (OGR layer for direct OGR access)
  473. \return 1 open on level 1 (without topology)
  474. \return 2 open on level 2 (with topology)
  475. \return -1 on error
  476. */
  477. int Vect_open_old2(struct Map_info *Map, const char *name, const char *mapset,
  478. const char *layer)
  479. {
  480. return open_old(Map, name, mapset, layer, 0, 0);
  481. }
  482. /*!
  483. \brief Open existing vector map for reading/writing
  484. This function is replaced by Vect_open_update2() to handle also
  485. direct OGR support.
  486. By default list of updated features is not maintained, see
  487. Vect_set_updated() for details.
  488. Calls G_fatal_error() on failure.
  489. \param[out] Map pointer to Map_info structure
  490. \param name name of vector map to update
  491. \param mapset mapset name
  492. \return 1 open on level 1 (without topology)
  493. \return 2 open on level 2 (with topology)
  494. \return -1 on error
  495. */
  496. int Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
  497. {
  498. return open_old(Map, name, mapset, NULL, 1, 0);
  499. }
  500. /*!
  501. \brief Open existing vector map for reading/writing
  502. By default list of updated features is not maintained, see
  503. Vect_set_updated() for details.
  504. Calls G_fatal_error() on failure.
  505. \param[out] Map pointer to Map_info structure
  506. \param name name of vector map to open (datasource for direct OGR access)
  507. \param mapset mapset name ("" for search path, "OGR" for direct OGR access)
  508. \param layer layer name (OGR layer for direct OGR access)
  509. \return 1 open on level 1 (without topology)
  510. \return 2 open on level 2 (with topology)
  511. \return -1 on error
  512. */
  513. int Vect_open_update2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
  514. {
  515. return open_old(Map, name, mapset, layer, 1, 0);
  516. }
  517. /*!
  518. \brief Reads only info about vector map (headers)
  519. Reads from headers of 'head', 'dbln', 'topo' and 'cidx' file.
  520. This function is replaced by Vect_open_old_head2() to handle also
  521. direct OGR support.
  522. Calls G_fatal_error() on failure.
  523. \param[out] Map pointer to Map_info structure
  524. \param name name of vector map to read
  525. \param mapset mapset name ("" for search path)
  526. \return 1 open on level 1 (without topology)
  527. \return 2 open on level 2 (with topology)
  528. \return -1 on error
  529. */
  530. int Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
  531. {
  532. return open_old(Map, name, mapset, NULL, 0, 1);
  533. }
  534. /*!
  535. \brief Reads only info about vector map (headers)
  536. Reads from headers of 'head', 'dbln', 'topo' and 'cidx' file.
  537. Calls G_fatal_error() on failure.
  538. \param[out] Map pointer to Map_info structure
  539. \param name name of vector map to read (dsn for OGR)
  540. \param mapset mapset name ("" for search path)
  541. \param layer layer name (OGR format)
  542. \param[out] Map pointer to Map_info structure
  543. \param name name of vector map to open (datasource for direct OGR access)
  544. \param mapset mapset name ("" for search path, "OGR" for direct OGR access)
  545. \param layer layer name (OGR layer for direct OGR access)
  546. \return 1 open on level 1 (without topology)
  547. \return 2 open on level 2 (with topology)
  548. \return -1 on error
  549. */
  550. int Vect_open_old_head2(struct Map_info *Map, const char *name, const char *mapset,
  551. const char *layer)
  552. {
  553. return open_old(Map, name, mapset, layer, 0, 1);
  554. }
  555. /*! \brief Open header file of existing vector map for updating
  556. (mostly for database link updates)
  557. \param[out] Map pointer to Map_info structure
  558. \param name name of vector map to update
  559. \param mapset mapset name
  560. \return 1 open on level 1 (without topology)
  561. \return 2 open on level 2 (with topology)
  562. \return -1 on error
  563. */
  564. int Vect_open_update_head(struct Map_info *Map, const char *name,
  565. const char *mapset)
  566. {
  567. return open_old(Map, name, mapset, NULL, 1, 1);
  568. }
  569. /*!
  570. \brief Create new vector map for reading/writing
  571. By default list of updated features is not maintained, see
  572. Vect_set_updated() for details.
  573. \param[in,out] Map pointer to Map_info structure
  574. \param name name of vector map
  575. \param with_z non-zero value for 3D vector data
  576. \return 1 on success
  577. \return -1 on error
  578. */
  579. int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
  580. {
  581. int ret;
  582. char buf[500];
  583. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  584. G_debug(2, "Vect_open_new(): name = %s", name);
  585. G_zero(Map, sizeof(struct Map_info));
  586. /* init header */
  587. Vect__init_head(Map);
  588. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  589. if (strcmp(xmapset, G_mapset()) != 0) {
  590. G_fatal_error(_("<%s> is not the current mapset (%s)"), name,
  591. G_mapset());
  592. return -1;
  593. }
  594. name = xname;
  595. }
  596. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  597. if (Vect_legal_filename(name) < 0) {
  598. G_fatal_error(_("Vector map name is not SQL compliant"));
  599. return -1;
  600. }
  601. /* determine output format native or ogr */
  602. Map->format = GV_FORMAT_NATIVE;
  603. if (strcmp(G_program_name(), "v.external") != 0) {
  604. if (G_find_file2("", "OGR", G_mapset())) {
  605. /* OGR */
  606. FILE *fp;
  607. const char *p;
  608. struct Key_Value *key_val;
  609. struct Format_info_ogr *ogr_info;
  610. G_debug(2, " using OGR format");
  611. Map->format = GV_FORMAT_OGR_DIRECT;
  612. fp = G_fopen_old("", "OGR", G_mapset());
  613. if (!fp) {
  614. G_fatal_error(_("Unable to open OGR file"));
  615. }
  616. key_val = G_fread_key_value(fp);
  617. fclose(fp);
  618. ogr_info = &(Map->fInfo.ogr);
  619. /* format */
  620. p = G_find_key_value("format", key_val);
  621. if (p)
  622. ogr_info->driver_name = G_store(p);
  623. /* dsn */
  624. p = G_find_key_value("dsn", key_val);
  625. if (p)
  626. ogr_info->dsn = G_store(p);
  627. /* options */
  628. p = G_find_key_value("options", key_val);
  629. if (p)
  630. ogr_info->layer_options = G_tokenize(p, ",");
  631. ogr_info->layer_name = G_store(name);
  632. }
  633. if (G_find_file2("", "PG", G_mapset())) {
  634. /* PostGIS */
  635. if (Map->fInfo.ogr.driver_name) {
  636. G_warning(_("OGR output also detected, using OGR"));
  637. }
  638. else {
  639. FILE *fp;
  640. const char *p;
  641. struct Key_Value *key_val;
  642. struct Format_info_pg *pg_info;
  643. G_debug(2, " using PostGIS format");
  644. Map->format = GV_FORMAT_POSTGIS;
  645. fp = G_fopen_old("", "PG", G_mapset());
  646. if (!fp) {
  647. G_fatal_error(_("Unable to open PG file"));
  648. }
  649. key_val = G_fread_key_value(fp);
  650. fclose(fp);
  651. pg_info = &(Map->fInfo.pg);
  652. /* conninfo */
  653. p = G_find_key_value("conninfo", key_val);
  654. if (p) {
  655. pg_info->conninfo = G_store(p);
  656. G_debug(1, "PG: conninfo = '%s'", pg_info->conninfo);
  657. }
  658. /* schema (default: public) */
  659. p = G_find_key_value("schema", key_val);
  660. if (p)
  661. pg_info->schema_name = G_store(p);
  662. else
  663. pg_info->schema_name = G_store("public");
  664. G_debug(1, "PG: schema_name = '%s'", pg_info->schema_name);
  665. /* fid column (default: ogc_fid) */
  666. p = G_find_key_value("fid", key_val);
  667. if (p)
  668. pg_info->fid_column = G_store(p);
  669. else
  670. pg_info->fid_column = G_store("ogc_fid");
  671. G_debug(1, "PG: fid_column = '%s'", pg_info->fid_column);
  672. /* geometry column (default: wkb_geometry) */
  673. p = G_find_key_value("geometry_name", key_val);
  674. if (p)
  675. pg_info->geom_column = G_store(p);
  676. else
  677. pg_info->geom_column = G_store("wkb_geometry");
  678. G_debug(1, "PG: geom_column = '%s'", pg_info->geom_column);
  679. /* table name */
  680. Map->fInfo.pg.table_name = G_store(name);
  681. }
  682. }
  683. }
  684. if (Map->format == GV_FORMAT_NATIVE) {
  685. /* native */
  686. G_debug(2, " using native format");
  687. /* check if map already exists */
  688. if (G_find_vector2(name, G_mapset()) != NULL) {
  689. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  690. name);
  691. ret = Vect_delete(name);
  692. if (ret == -1) {
  693. G_warning(_("Unable to delete vector map <%s>"), name);
  694. return -1;
  695. }
  696. }
  697. }
  698. Map->name = G_store(name);
  699. Map->mapset = G_store(G_mapset());
  700. Map->location = G_store(G_location());
  701. Map->gisdbase = G_store(G_gisdbase());
  702. /* set 2D/3D */
  703. Map->plus.spidx_with_z = Map->plus.with_z = Map->head.with_z = (with_z != 0);
  704. if ((*Open_new_array[Map->format][1]) (Map, name, with_z) < 0) {
  705. G_fatal_error(_("Unable to create vector map <%s>"),
  706. name);
  707. return -1;
  708. }
  709. if (Map->format == GV_FORMAT_NATIVE) {
  710. /* Open history file */
  711. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  712. Map->hist_fp = G_fopen_new(buf, GV_HIST_ELEMENT);
  713. if (Map->hist_fp == NULL) {
  714. G_warning(_("Unable to open history file of vector map <%s>"),
  715. name);
  716. return -1;
  717. }
  718. }
  719. Open_level = 0;
  720. /* initialize topo */
  721. Map->plus.Spidx_file = 0;
  722. dig_init_plus(&(Map->plus));
  723. /* open new spatial index */
  724. Vect_open_sidx(Map, 2);
  725. Map->open = VECT_OPEN_CODE;
  726. Map->level = 1;
  727. Map->head_only = FALSE;
  728. Map->support_updated = FALSE;
  729. Map->plus.built = GV_BUILD_NONE;
  730. Map->mode = GV_MODE_RW;
  731. Map->plus.uplist.do_uplist = FALSE;
  732. Vect_set_proj(Map, G_projection());
  733. Vect_set_zone(Map, G_zone());
  734. Map->dblnk = Vect_new_dblinks_struct();
  735. return 1;
  736. }
  737. /*!
  738. \brief Update Coor_info structure
  739. \param Map pointer to Map_info structure
  740. \param[out] Info pointer to Coor_info structure
  741. \return 1 on success
  742. \return 0 on error
  743. */
  744. int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info)
  745. {
  746. char buf[GPATH_MAX], path[GPATH_MAX];
  747. STRUCT_STAT stat_buf;
  748. switch (Map->format) {
  749. case GV_FORMAT_NATIVE:
  750. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  751. G_file_name(path, buf, GV_COOR_ELEMENT, Map->mapset);
  752. G_debug(1, "get coor info: %s", path);
  753. if (0 != stat(path, &stat_buf)) {
  754. G_warning(_("Unable to stat file <%s>"), path);
  755. Info->size = -1L;
  756. Info->mtime = -1L;
  757. }
  758. else {
  759. Info->size = (off_t)stat_buf.st_size; /* file size */
  760. Info->mtime = (long)stat_buf.st_mtime; /* last modified time */
  761. }
  762. /* stat does not give correct size on MINGW
  763. * if the file is opened */
  764. #ifdef __MINGW32__
  765. if (Map->open == VECT_OPEN_CODE) {
  766. dig_fseek(&(Map->dig_fp), 0L, SEEK_END);
  767. G_debug(2, "dig_ftell = %d", dig_ftell(&(Map->dig_fp)));
  768. Info->size = dig_ftell(&(Map->dig_fp));
  769. }
  770. #endif
  771. break;
  772. case GV_FORMAT_OGR:
  773. case GV_FORMAT_OGR_DIRECT:
  774. case GV_FORMAT_POSTGIS:
  775. Info->size = 0L;
  776. Info->mtime = 0L;
  777. break;
  778. }
  779. G_debug(1, "Info->size = %lu, Info->mtime = %ld",
  780. (unsigned long)Info->size, Info->mtime);
  781. return 1;
  782. }
  783. /*!
  784. \brief Gets vector map format (as string)
  785. Note: string is allocated by G_store(). Free allocated memory with
  786. G_free().
  787. Currently are implemeted:
  788. - Native format (native)
  789. - OGR format (ogr)
  790. - PostGIS format (postgis)
  791. \param Map pointer to Map_info structure
  792. \return maptype string on success (allocated by G_store())
  793. \return error message on error
  794. */
  795. const char *Vect_maptype_info(const struct Map_info *Map)
  796. {
  797. char maptype[1000];
  798. switch (Map->format) {
  799. case GV_FORMAT_NATIVE:
  800. sprintf(maptype, "native");
  801. break;
  802. case GV_FORMAT_OGR:
  803. case GV_FORMAT_OGR_DIRECT:
  804. sprintf(maptype, "OGR");
  805. break;
  806. case GV_FORMAT_POSTGIS:
  807. sprintf(maptype, "PostGIS");
  808. break;
  809. default:
  810. sprintf(maptype, _("unknown %d (update Vect_maptype_info)"),
  811. Map->format);
  812. }
  813. return G_store(maptype);
  814. }
  815. /*!
  816. \brief Gets vector map format
  817. Currently are implemeted:
  818. - Native format (GV_FORMAT_NATIVE)
  819. - OGR format linked via v.external (GV_FORMAT_OGR)
  820. - OGR format (GV_FORMAT_OGR_DIRECT)
  821. - PostGIS fomat (GV_FORMAT_POSTGIS)
  822. \param Map pointer to Map_info structure
  823. \return maptype code
  824. */
  825. int Vect_maptype(const struct Map_info *Map)
  826. {
  827. return Map->format;
  828. }
  829. /*!
  830. \brief Open topology file ('topo')
  831. \param[in,out] Map pointer to Map_info structure
  832. \param head_only TRUE to read only header
  833. \return 0 on success
  834. \return 1 file does not exist
  835. \return -1 on error
  836. */
  837. int Vect_open_topo(struct Map_info *Map, int head_only)
  838. {
  839. int err, ret;
  840. char buf[GPATH_MAX], file_path[GPATH_MAX];
  841. struct gvfile fp;
  842. struct Coor_info CInfo;
  843. struct Plus_head *Plus;
  844. G_debug(1, "Vect_open_topo(): name = %s mapset= %s", Map->name,
  845. Map->mapset);
  846. Plus = &(Map->plus);
  847. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  848. G_file_name(file_path, buf, GV_TOPO_ELEMENT, Map->mapset);
  849. if (access(file_path, F_OK) != 0) /* does not exist */
  850. return 1;
  851. dig_file_init(&fp);
  852. fp.file = G_fopen_old(buf, GV_TOPO_ELEMENT, Map->mapset);
  853. if (fp.file == NULL) { /* topo file is not available */
  854. G_debug(1, "Cannot open topo file for vector '%s@%s'.",
  855. Map->name, Map->mapset);
  856. return -1;
  857. }
  858. /* get coor info */
  859. /* NOTE: coor file not yet opened */
  860. Vect_coor_info(Map, &CInfo);
  861. /* load head */
  862. if (dig_Rd_Plus_head(&fp, Plus) == -1)
  863. return -1;
  864. G_debug(1, "Topo head: coor size = %lu, coor mtime = %ld",
  865. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  866. /* do checks */
  867. err = 0;
  868. if (CInfo.size != Plus->coor_size) {
  869. G_warning(_("Size of 'coor' file differs from value saved in topology file"));
  870. err = 1;
  871. }
  872. /* Do not check mtime because mtime is changed by copy */
  873. /*
  874. if ( CInfo.mtime != Plus->coor_mtime ) {
  875. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  876. err = 1;
  877. }
  878. */
  879. if (err) {
  880. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  881. Map->name, Map->mapset);
  882. return -1;
  883. }
  884. /* load file to the memory */
  885. /* dig_file_load ( &fp); */
  886. /* load topo to memory */
  887. ret = dig_load_plus(Plus, &fp, head_only);
  888. fclose(fp.file);
  889. /* dig_file_free ( &fp); */
  890. if (ret == 0)
  891. return -1;
  892. return 0;
  893. }
  894. /*!
  895. \brief Open spatial index file ('sidx')
  896. \param[in,out] Map pointer to Map_info
  897. \param mode 0 old, 1 update, 2 new
  898. \return 0 on success
  899. \return -1 on error
  900. */
  901. int Vect_open_sidx(struct Map_info *Map, int mode)
  902. {
  903. char buf[500], file_path[2000];
  904. int err;
  905. struct Coor_info CInfo;
  906. struct Plus_head *Plus;
  907. G_debug(1, "Vect_open_sidx(): name = %s mapset= %s mode = %s", Map->name,
  908. Map->mapset, mode == 0 ? "old" : (mode == 1 ? "update" : "new"));
  909. if (Map->plus.Spidx_built == TRUE) {
  910. G_warning("Spatial index already opened");
  911. return 0;
  912. }
  913. Plus = &(Map->plus);
  914. dig_file_init(&(Map->plus.spidx_fp));
  915. if (mode < 2) {
  916. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  917. G_file_name(file_path, buf, GV_SIDX_ELEMENT, Map->mapset);
  918. if (access(file_path, F_OK) != 0) /* does not exist */
  919. return 1;
  920. Map->plus.spidx_fp.file =
  921. G_fopen_old(buf, GV_SIDX_ELEMENT, Map->mapset);
  922. if (Map->plus.spidx_fp.file == NULL) { /* sidx file is not available */
  923. G_debug(1, "Cannot open spatial index file for vector '%s@%s'.",
  924. Map->name, Map->mapset);
  925. return -1;
  926. }
  927. /* get coor info */
  928. /* NOTE: coor file not yet opened */
  929. Vect_coor_info(Map, &CInfo);
  930. /* initialize spatial index */
  931. Map->plus.Spidx_new = FALSE;
  932. /* load head */
  933. if (dig_Rd_spidx_head(&(Map->plus.spidx_fp), Plus) == -1) {
  934. fclose(Map->plus.spidx_fp.file);
  935. return -1;
  936. }
  937. G_debug(1, "Sidx head: coor size = %lu, coor mtime = %ld",
  938. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  939. /* do checks */
  940. err = 0;
  941. if (CInfo.size != Plus->coor_size) {
  942. G_warning(_("Size of 'coor' file differs from value saved in sidx file"));
  943. err = 1;
  944. }
  945. /* Do not check mtime because mtime is changed by copy */
  946. /*
  947. if ( CInfo.mtime != Plus->coor_mtime ) {
  948. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  949. err = 1;
  950. }
  951. */
  952. if (err) {
  953. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  954. Map->name, Map->mapset);
  955. fclose(Map->plus.spidx_fp.file);
  956. return -1;
  957. }
  958. }
  959. if (mode) {
  960. /* open new spatial index */
  961. Map->plus.Spidx_new = TRUE;
  962. /* file based or memory based */
  963. if (getenv("GRASS_VECTOR_LOWMEM")) {
  964. /* free old indices */
  965. dig_spidx_free(Plus);
  966. /* initialize file based indices */
  967. Map->plus.Spidx_file = 1;
  968. dig_spidx_init(Plus);
  969. }
  970. G_debug(1, "%s based spatial index",
  971. Map->plus.Spidx_file == 0 ? "Memory" : "File");
  972. if (mode == 1) {
  973. /* load spatial index for update */
  974. if (dig_Rd_spidx(&(Map->plus.spidx_fp), Plus) == -1) {
  975. fclose(Map->plus.spidx_fp.file);
  976. return -1;
  977. }
  978. }
  979. }
  980. Map->plus.Spidx_built = TRUE;
  981. return 0;
  982. }