open.c 37 KB

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