open.c 45 KB

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