open.c 45 KB

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