open.c 43 KB

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