open.c 42 KB

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