open.c 46 KB

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