open.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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[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") ? 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. 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. 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. Vect__get_element_path(file_path, Map, GV_TOPO_ELEMENT);
  462. if (access(file_path, F_OK) == 0) /* topo file exists? */
  463. unlink(file_path);
  464. Vect__get_element_path(file_path, Map, GV_SIDX_ELEMENT);
  465. if (access(file_path, F_OK) == 0) /* sidx file exists? */
  466. unlink(file_path);
  467. Vect__get_element_path(file_path, Map, GV_CIDX_ELEMENT);
  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. Vect__get_element_path(file_path, Map, GV_FIDX_ELEMENT);
  472. if (access(file_path, F_OK) == 0) /* fidx file exists? */
  473. unlink(file_path);
  474. }
  475. }
  476. return level;
  477. }
  478. /*!
  479. \brief Open existing vector map for reading
  480. This function is replaced by Vect_open_old2() to handle also direct
  481. OGR support.
  482. Calls G_fatal_error() on failure.
  483. \param[out] Map pointer to Map_info structure
  484. \param name name of vector map to open
  485. \param mapset mapset name ("" for search path)
  486. \return 1 open on level 1 (without topology)
  487. \return 2 open on level 2 (with topology)
  488. \return -1 on error
  489. */
  490. int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
  491. {
  492. return Vect__open_old(Map, name, mapset, NULL, FALSE, FALSE, FALSE);
  493. }
  494. /*!
  495. \brief Open existing temporary vector map for reading
  496. Temporary vector maps are stored in the current mapset (directory
  497. <tt>.tmp/<hostname>/vector</tt>).
  498. Calls G_fatal_error() on failure.
  499. \todo Create new vector map if doesn't exist.
  500. \param[out] Map pointer to Map_info structure
  501. \param name name of vector map to open
  502. \param mapset mapset name ("" for search path)
  503. \return 1 open on level 1 (without topology)
  504. \return 2 open on level 2 (with topology)
  505. \return -1 on error
  506. */
  507. int Vect_open_tmp_old(struct Map_info *Map, const char *name, const char *mapset)
  508. {
  509. return Vect__open_old(Map, name, mapset, NULL, FALSE, FALSE, TRUE);
  510. }
  511. /*!
  512. \brief Open existing vector map for reading
  513. Calls G_fatal_error() on failure.
  514. \param[out] Map pointer to Map_info structure
  515. \param name name of vector map to open (datasource for direct OGR access)
  516. \param mapset mapset name ("" for search path, "OGR" for direct OGR access)
  517. \param layer layer name (OGR layer for direct OGR access)
  518. \return 1 open on level 1 (without topology)
  519. \return 2 open on level 2 (with topology)
  520. \return -1 on error
  521. */
  522. int Vect_open_old2(struct Map_info *Map, const char *name, const char *mapset,
  523. const char *layer)
  524. {
  525. return Vect__open_old(Map, name, mapset, layer, FALSE, FALSE, FALSE);
  526. }
  527. /*!
  528. \brief Open existing vector map for reading/writing
  529. This function is replaced by Vect_open_update2() to handle also
  530. direct OGR support.
  531. By default list of updated features is not maintained, see
  532. Vect_set_updated() for details.
  533. Calls G_fatal_error() on failure.
  534. \param[out] Map pointer to Map_info structure
  535. \param name name of vector map to update
  536. \param mapset mapset name
  537. \return 1 open on level 1 (without topology)
  538. \return 2 open on level 2 (with topology)
  539. \return -1 on error
  540. */
  541. int Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
  542. {
  543. return Vect__open_old(Map, name, mapset, NULL, TRUE, FALSE, FALSE);
  544. }
  545. /*!
  546. \brief Open existing temporary vector map for reading/writing
  547. Temporary vector maps are stored in the current mapset (directory
  548. <tt>.tmp/<hostname>/vector</tt>).
  549. By default list of updated features is not maintained, see
  550. Vect_set_updated() for details.
  551. Calls G_fatal_error() on failure.
  552. \todo Create new vector map if doesn't exist.
  553. \param[out] Map pointer to Map_info structure
  554. \param name name of vector map to update
  555. \param mapset mapset name
  556. \return 1 open on level 1 (without topology)
  557. \return 2 open on level 2 (with topology)
  558. \return -1 on error
  559. */
  560. int Vect_open_tmp_update(struct Map_info *Map, const char *name, const char *mapset)
  561. {
  562. return Vect__open_old(Map, name, mapset, NULL, TRUE, FALSE, TRUE);
  563. }
  564. /*!
  565. \brief Open existing vector map for reading/writing
  566. By default list of updated features is not maintained, see
  567. Vect_set_updated() for details.
  568. Calls G_fatal_error() on failure.
  569. \param[out] Map pointer to Map_info structure
  570. \param name name of vector map to open (datasource for direct OGR access)
  571. \param mapset mapset name ("" for search path, "OGR" for direct OGR access)
  572. \param layer layer name (OGR layer for direct OGR access)
  573. \return 1 open on level 1 (without topology)
  574. \return 2 open on level 2 (with topology)
  575. \return -1 on error
  576. */
  577. int Vect_open_update2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
  578. {
  579. return Vect__open_old(Map, name, mapset, layer, TRUE, FALSE, FALSE);
  580. }
  581. /*!
  582. \brief Reads only info about vector map (headers)
  583. Reads from headers of 'head', 'dbln', 'topo' and 'cidx' file.
  584. This function is replaced by Vect_open_old_head2() to handle also
  585. direct OGR support.
  586. Calls G_fatal_error() on failure.
  587. \param[out] Map pointer to Map_info structure
  588. \param name name of vector map to read
  589. \param mapset mapset name ("" for search path)
  590. \return 1 open on level 1 (without topology)
  591. \return 2 open on level 2 (with topology)
  592. \return -1 on error
  593. */
  594. int Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
  595. {
  596. return Vect__open_old(Map, name, mapset, NULL, FALSE, TRUE, FALSE);
  597. }
  598. /*!
  599. \brief Reads only info about vector map (headers)
  600. Reads from headers of 'head', 'dbln', 'topo' and 'cidx' file.
  601. Calls G_fatal_error() on failure.
  602. \param[out] Map pointer to Map_info structure
  603. \param name name of vector map to read (dsn for OGR)
  604. \param mapset mapset name ("" for search path)
  605. \param layer layer name (OGR format)
  606. \param[out] Map pointer to Map_info structure
  607. \param name name of vector map to open (datasource for direct OGR access)
  608. \param mapset mapset name ("" for search path, "OGR" for direct OGR access)
  609. \param layer layer name (OGR layer for direct OGR access)
  610. \return 1 open on level 1 (without topology)
  611. \return 2 open on level 2 (with topology)
  612. \return -1 on error
  613. */
  614. int Vect_open_old_head2(struct Map_info *Map, const char *name, const char *mapset,
  615. const char *layer)
  616. {
  617. return Vect__open_old(Map, name, mapset, layer, FALSE, TRUE, FALSE);
  618. }
  619. /*! \brief Open header file of existing vector map for updating
  620. (mostly for database link updates)
  621. \param[out] Map pointer to Map_info structure
  622. \param name name of vector map to update
  623. \param mapset mapset name
  624. \return 1 open on level 1 (without topology)
  625. \return 2 open on level 2 (with topology)
  626. \return -1 on error
  627. */
  628. int Vect_open_update_head(struct Map_info *Map, const char *name,
  629. const char *mapset)
  630. {
  631. return Vect__open_old(Map, name, mapset, NULL, TRUE, TRUE, FALSE);
  632. }
  633. int open_new(struct Map_info *Map, const char *name, int with_z, int is_tmp)
  634. {
  635. int ret;
  636. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  637. G_debug(1, "Vect_open_new(): name = %s with_z = %d is_tmp = %d",
  638. name, with_z, is_tmp);
  639. /* zero Map_info structure */
  640. G_zero(Map, sizeof(struct Map_info));
  641. /* init header info */
  642. Vect__init_head(Map);
  643. /* check for fully-qualified map name */
  644. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  645. if (strcmp(xmapset, G_mapset()) != 0) {
  646. G_warning(_("Unable to create vector map: <%s> is not in the current mapset (%s)"),
  647. name, G_mapset());
  648. return -1;
  649. }
  650. name = xname;
  651. }
  652. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  653. if (Vect_legal_filename(name) < 0) {
  654. G_fatal_error(_("Unable to create vector map: <%s> is not SQL compliant"),
  655. name);
  656. return -1;
  657. }
  658. /* store basic info */
  659. Map->name = G_store(name);
  660. Map->mapset = G_store(G_mapset());
  661. Map->location = G_store(G_location());
  662. Map->gisdbase = G_store(G_gisdbase());
  663. Map->temporary = is_tmp;
  664. /* determine output format */
  665. Map->format = map_format(Map);
  666. if (Map->format != GV_FORMAT_OGR_DIRECT &&
  667. getenv("GRASS_VECTOR_PGFILE") == NULL) { /* GRASS_VECTOR_PGFILE defined by v.out.postgis */
  668. char *env;
  669. char path[GPATH_MAX];
  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. env = getenv("GRASS_VECTOR_TEMPORARY");
  681. if (!Map->temporary || (env && strcmp(env, "move") == 0)) {
  682. if (G_find_vector2(name, G_mapset()) != NULL) {
  683. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  684. name);
  685. ret = Vect_delete(name);
  686. if (ret == -1) {
  687. G_warning(_("Unable to delete vector map <%s>"), name);
  688. return -1;
  689. }
  690. }
  691. }
  692. /* write header file
  693. note: header & history file is also written for external
  694. formats since vector library create links automatically
  695. when closing the map
  696. */
  697. Map->head.size = 0;
  698. Map->head.head_size = GV_COOR_HEAD_SIZE + 4;
  699. Vect__write_head(Map);
  700. /* create history file */
  701. Vect__get_path(path, Map);
  702. Map->hist_fp = G_fopen_new(path, GV_HIST_ELEMENT);
  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 file_path[GPATH_MAX];
  807. struct stat stat_buf;
  808. switch (Map->format) {
  809. case GV_FORMAT_NATIVE:
  810. Vect__get_element_path(file_path, Map, GV_COOR_ELEMENT);
  811. G_debug(1, "get coor info: %s", file_path);
  812. if (0 != stat(file_path, &stat_buf)) {
  813. G_warning(_("Unable to stat file <%s>"), file_path);
  814. Info->size = -1L;
  815. Info->mtime = -1L;
  816. }
  817. else {
  818. Info->size = (off_t)stat_buf.st_size; /* file size */
  819. Info->mtime = (long)stat_buf.st_mtime; /* last modified time */
  820. }
  821. /* stat does not give correct size on MINGW
  822. * if the file is opened */
  823. #ifdef __MINGW32__
  824. if (Map->open == VECT_OPEN_CODE) {
  825. dig_fseek(&(Map->dig_fp), 0L, SEEK_END);
  826. G_debug(2, "dig_ftell = %d", dig_ftell(&(Map->dig_fp)));
  827. Info->size = dig_ftell(&(Map->dig_fp));
  828. }
  829. #endif
  830. break;
  831. case GV_FORMAT_OGR:
  832. case GV_FORMAT_OGR_DIRECT:
  833. case GV_FORMAT_POSTGIS:
  834. Info->size = 0L;
  835. Info->mtime = 0L;
  836. break;
  837. }
  838. G_debug(1, "Vect_coor_info(): Info->size = %lu, Info->mtime = %ld",
  839. (unsigned long)Info->size, Info->mtime);
  840. return 1;
  841. }
  842. /*!
  843. \brief Gets vector map format (as string)
  844. Note: string is allocated by G_store(). Free allocated memory with
  845. G_free().
  846. Currently are implemeted:
  847. - Native format (native)
  848. - OGR format (ogr)
  849. - PostGIS format (postgis)
  850. \param Map pointer to Map_info structure
  851. \return maptype string on success (allocated by G_store())
  852. \return error message on error
  853. */
  854. const char *Vect_maptype_info(const struct Map_info *Map)
  855. {
  856. char maptype[1000];
  857. switch (Map->format) {
  858. case GV_FORMAT_NATIVE:
  859. sprintf(maptype, "native");
  860. break;
  861. case GV_FORMAT_OGR:
  862. case GV_FORMAT_OGR_DIRECT:
  863. sprintf(maptype, "OGR");
  864. break;
  865. case GV_FORMAT_POSTGIS:
  866. sprintf(maptype, "PostGIS");
  867. break;
  868. default:
  869. sprintf(maptype, _("unknown %d (update Vect_maptype_info)"),
  870. Map->format);
  871. }
  872. return G_store(maptype);
  873. }
  874. /*!
  875. \brief Gets vector map format
  876. Currently are implemeted:
  877. - Native format (GV_FORMAT_NATIVE)
  878. - OGR format linked via v.external (GV_FORMAT_OGR)
  879. - OGR format (GV_FORMAT_OGR_DIRECT)
  880. - PostGIS fomat (GV_FORMAT_POSTGIS)
  881. \param Map pointer to Map_info structure
  882. \return map format code
  883. */
  884. int Vect_maptype(const struct Map_info *Map)
  885. {
  886. if (Map->temporary) {
  887. const struct Format_info *finfo;
  888. finfo = &(Map->fInfo);
  889. if (finfo->ogr.driver_name) {
  890. return GV_FORMAT_OGR;
  891. }
  892. if (finfo->pg.conninfo) {
  893. return GV_FORMAT_POSTGIS;
  894. }
  895. }
  896. return Map->format;
  897. }
  898. /*!
  899. \brief Open topology file ('topo')
  900. \param[in,out] Map pointer to Map_info structure
  901. \param head_only TRUE to read only header
  902. \return 0 on success
  903. \return 1 file does not exist
  904. \return -1 on error
  905. */
  906. int Vect_open_topo(struct Map_info *Map, int head_only)
  907. {
  908. int err, ret;
  909. char file_path[GPATH_MAX], path[GPATH_MAX];
  910. struct gvfile fp;
  911. struct Coor_info CInfo;
  912. struct Plus_head *Plus;
  913. G_debug(1, "Vect_open_topo(): name = %s mapset = %s", Map->name,
  914. Map->mapset);
  915. Plus = &(Map->plus);
  916. Vect__get_path(path, Map);
  917. Vect__get_element_path(file_path, Map, GV_TOPO_ELEMENT);
  918. if (access(file_path, F_OK) != 0) { /* does not exist */
  919. return 1;
  920. }
  921. dig_file_init(&fp);
  922. fp.file = G_fopen_old(path, GV_TOPO_ELEMENT, Map->mapset);
  923. if (fp.file == NULL) { /* topo file is not available */
  924. G_debug(1, "Cannot open topo file for vector '%s@%s'.",
  925. Map->name, Map->mapset);
  926. return -1;
  927. }
  928. /* get coor info */
  929. /* NOTE: coor file not yet opened */
  930. Vect_coor_info(Map, &CInfo);
  931. /* load head */
  932. if (dig_Rd_Plus_head(&fp, Plus) == -1)
  933. return -1;
  934. G_debug(1, "Topo head: coor size = %lu, coor mtime = %ld",
  935. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  936. /* do checks */
  937. err = 0;
  938. if (CInfo.size != Plus->coor_size) {
  939. G_warning(_("Size of 'coor' file differs from value saved in topology file"));
  940. err = 1;
  941. }
  942. /* Do not check mtime because mtime is changed by copy */
  943. /*
  944. if ( CInfo.mtime != Plus->coor_mtime ) {
  945. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  946. err = 1;
  947. }
  948. */
  949. if (err) {
  950. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  951. Map->name, Map->mapset);
  952. return -1;
  953. }
  954. /* load file to the memory */
  955. /* dig_file_load ( &fp); */
  956. /* load topo to memory */
  957. ret = dig_load_plus(Plus, &fp, head_only);
  958. fclose(fp.file);
  959. /* dig_file_free(&fp); */
  960. return ret == 0 ? -1 : 0;
  961. }
  962. /*!
  963. \brief Open spatial index file ('sidx')
  964. \param[in,out] Map pointer to Map_info
  965. \param mode 0 old, 1 update, 2 new
  966. \return 1 if sidx file is not available
  967. \return 0 on success
  968. \return -1 on error
  969. */
  970. int Vect_open_sidx(struct Map_info *Map, int mode)
  971. {
  972. int err;
  973. struct Coor_info CInfo;
  974. struct Plus_head *Plus;
  975. G_debug(1, "Vect_open_sidx(): name = %s mapset= %s mode = %s", Map->name,
  976. Map->mapset, mode == 0 ? "old" : (mode == 1 ? "update" : "new"));
  977. Plus = &(Map->plus);
  978. if (Plus->Spidx_built) {
  979. G_debug(1, "Spatial index already opened");
  980. return 0;
  981. }
  982. dig_file_init(&(Plus->spidx_fp));
  983. if (mode < 2) {
  984. char path[GPATH_MAX], file_path[GPATH_MAX];
  985. Vect__get_path(path, Map);
  986. Vect__get_element_path(file_path, Map, GV_SIDX_ELEMENT);
  987. if (access(file_path, F_OK) != 0) /* does not exist */
  988. return 1;
  989. Plus->spidx_fp.file = G_fopen_old(path, GV_SIDX_ELEMENT, Map->mapset);
  990. if (Plus->spidx_fp.file == NULL) { /* sidx file is not available */
  991. G_debug(1, "Cannot open spatial index file for vector '%s@%s'.",
  992. Map->name, Map->mapset);
  993. return -1;
  994. }
  995. /* get coor info */
  996. /* NOTE: coor file not yet opened */
  997. Vect_coor_info(Map, &CInfo);
  998. /* initialize spatial index */
  999. Plus->Spidx_new = FALSE;
  1000. if (mode == 0) {
  1001. /* free old indices */
  1002. dig_spidx_free(Plus);
  1003. /* initialize file based indices */
  1004. Plus->Spidx_file = 1;
  1005. dig_spidx_init(Plus);
  1006. }
  1007. /* load head */
  1008. if (dig_Rd_spidx_head(&(Plus->spidx_fp), Plus) == -1) {
  1009. fclose(Plus->spidx_fp.file);
  1010. return -1;
  1011. }
  1012. G_debug(1, "Sidx head: coor size = %lu, coor mtime = %ld",
  1013. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  1014. /* do checks */
  1015. err = 0;
  1016. if (CInfo.size != Plus->coor_size) {
  1017. G_warning(_("Size of 'coor' file differs from value saved in sidx file"));
  1018. err = 1;
  1019. }
  1020. /* Do not check mtime because mtime is changed by copy */
  1021. /*
  1022. if ( CInfo.mtime != Plus->coor_mtime ) {
  1023. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  1024. err = 1;
  1025. }
  1026. */
  1027. if (err) {
  1028. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  1029. Map->name, Map->mapset);
  1030. fclose(Plus->spidx_fp.file);
  1031. return -1;
  1032. }
  1033. }
  1034. if (mode) {
  1035. /* open new spatial index */
  1036. Plus->Spidx_new = TRUE;
  1037. /* file based or memory based */
  1038. if (getenv("GRASS_VECTOR_LOWMEM")) {
  1039. /* free old indices */
  1040. dig_spidx_free(Plus);
  1041. /* initialize file based indices */
  1042. Plus->Spidx_file = 1;
  1043. dig_spidx_init(Plus);
  1044. }
  1045. G_debug(1, "%s based spatial index",
  1046. Plus->Spidx_file == 0 ? "Memory" : "File");
  1047. if (mode == 1) {
  1048. /* load spatial index for update */
  1049. if (dig_Rd_spidx(&(Plus->spidx_fp), Plus) == -1) {
  1050. fclose(Plus->spidx_fp.file);
  1051. return -1;
  1052. }
  1053. }
  1054. }
  1055. Plus->Spidx_built = TRUE;
  1056. return 0;
  1057. }
  1058. /* check for external formats definition */
  1059. int map_format(struct Map_info *Map)
  1060. {
  1061. int format;
  1062. char *def_file;
  1063. format = GV_FORMAT_NATIVE;
  1064. /* temporary maps can be stored only in native format */
  1065. if (Map->temporary || getenv("GRASS_VECTOR_EXTERNAL_IGNORE"))
  1066. return format;
  1067. if (G_find_file2("", "OGR", G_mapset())) {
  1068. /* OGR */
  1069. FILE *fp;
  1070. const char *p;
  1071. struct Key_Value *key_val;
  1072. struct Format_info_ogr *ogr_info;
  1073. G_debug(2, " using OGR format");
  1074. if (getenv("GRASS_VECTOR_EXTERNAL_IMMEDIATE")) {
  1075. /* vector features are written directly to OGR layer */
  1076. format = GV_FORMAT_OGR;
  1077. }
  1078. else {
  1079. /* vector features are written to the temporary vector map
  1080. * in the native format and when closing the map
  1081. * transferred to output OGR layer */
  1082. format = GV_FORMAT_NATIVE;
  1083. Map->temporary = TRUE;
  1084. }
  1085. fp = G_fopen_old("", "OGR", G_mapset());
  1086. if (!fp) {
  1087. G_fatal_error(_("Unable to open OGR file"));
  1088. }
  1089. key_val = G_fread_key_value(fp);
  1090. fclose(fp);
  1091. ogr_info = &(Map->fInfo.ogr);
  1092. /* format */
  1093. p = G_find_key_value("format", key_val);
  1094. if (p)
  1095. ogr_info->driver_name = G_store(p);
  1096. /* dsn */
  1097. p = G_find_key_value("dsn", key_val);
  1098. if (p)
  1099. ogr_info->dsn = G_store(p);
  1100. /* options */
  1101. p = G_find_key_value("options", key_val);
  1102. if (p)
  1103. ogr_info->layer_options = G_tokenize(p, ",");
  1104. ogr_info->layer_name = G_store(Map->name);
  1105. }
  1106. def_file = getenv("GRASS_VECTOR_PGFILE"); /* GRASS_VECTOR_PGFILE defined by v.out.postgis */
  1107. if (G_find_file2("", def_file ? def_file : "PG", G_mapset())) {
  1108. /* PostGIS */
  1109. if (Map->fInfo.ogr.driver_name) {
  1110. G_warning(_("OGR output also detected, using OGR"));
  1111. }
  1112. else {
  1113. FILE *fp;
  1114. const char *p;
  1115. struct Key_Value *key_val;
  1116. struct Format_info_pg *pg_info;
  1117. G_debug(2, " using PostGIS format");
  1118. fp = G_fopen_old("", def_file ? def_file : "PG", G_mapset());
  1119. if (!fp) {
  1120. G_fatal_error(_("Unable to open PG file"));
  1121. }
  1122. key_val = G_fread_key_value(fp);
  1123. fclose(fp);
  1124. pg_info = &(Map->fInfo.pg);
  1125. /* conninfo */
  1126. p = G_find_key_value("conninfo", key_val);
  1127. if (p) {
  1128. pg_info->conninfo = G_store(p);
  1129. G_debug(1, "PG: conninfo = '%s'", pg_info->conninfo);
  1130. }
  1131. /* schema (default: public) */
  1132. p = G_find_key_value("schema", key_val);
  1133. if (p)
  1134. pg_info->schema_name = G_store(p);
  1135. else
  1136. pg_info->schema_name = G_store("public");
  1137. G_debug(1, "PG: schema_name = '%s'", pg_info->schema_name);
  1138. /* fid column (default: FID_COLUMN) */
  1139. p = G_find_key_value("fid", key_val);
  1140. if (p)
  1141. pg_info->fid_column = G_store(p);
  1142. else
  1143. #ifdef HAVE_POSTGRES
  1144. pg_info->fid_column = G_store(GV_PG_FID_COLUMN);
  1145. #endif
  1146. G_debug(1, "PG: fid_column = '%s'", pg_info->fid_column);
  1147. /* geometry column (default: GEOMETRY_COLUMN) */
  1148. p = G_find_key_value("geometry_name", key_val);
  1149. if (p)
  1150. pg_info->geom_column = G_store(p);
  1151. else
  1152. #ifdef HAVE_POSTGRES
  1153. pg_info->geom_column = G_store(GV_PG_GEOMETRY_COLUMN);
  1154. #endif
  1155. G_debug(1, "PG: geom_column = '%s'", pg_info->geom_column);
  1156. /* srid (default: 0) */
  1157. p = G_find_key_value("srid", key_val);
  1158. if (p)
  1159. pg_info->srid = atoi(p);
  1160. G_debug(1, "PG: srid = %d", pg_info->srid);
  1161. /* table name */
  1162. Map->fInfo.pg.table_name = G_store(Map->name);
  1163. /* PostGIS topology enabled ? */
  1164. p = G_find_key_value("topology", key_val);
  1165. if (p && G_strcasecmp(p, "yes") == 0) {
  1166. /* define topology name */
  1167. p = G_find_key_value("toposchema_name", key_val);
  1168. if (p)
  1169. pg_info->toposchema_name = G_store(p);
  1170. else
  1171. G_asprintf(&(pg_info->toposchema_name), "topo_%s",
  1172. pg_info->table_name);
  1173. G_debug(1, "PG: topology = yes, schema_name = %s",
  1174. pg_info->toposchema_name);
  1175. }
  1176. G_debug(1, "PG: topology = no");
  1177. if (getenv("GRASS_VECTOR_EXTERNAL_IMMEDIATE")) {
  1178. /* vector features are written directly to PostGIS layer */
  1179. format = GV_FORMAT_POSTGIS;
  1180. }
  1181. else {
  1182. /* vector features are written to the temporary vector map
  1183. * in the native format and when closing the map
  1184. * transferred to output PostGIS layer */
  1185. format = GV_FORMAT_NATIVE;
  1186. Map->temporary = TRUE;
  1187. }
  1188. }
  1189. }
  1190. G_debug(2, "map_format = %d", format);
  1191. return format;
  1192. }
  1193. /*!
  1194. \brief Get map directory name (internal use only)
  1195. \param file_path path string buffer
  1196. \param Map pointer to Map_info struct
  1197. \return buffer containing path
  1198. */
  1199. char *Vect__get_path(char *path, const struct Map_info *Map)
  1200. {
  1201. if (Map->temporary) {
  1202. char path_tmp[GPATH_MAX];
  1203. G_temp_element(path_tmp);
  1204. sprintf(path, "%s/%s/%s", path_tmp, GV_DIRECTORY, Map->name);
  1205. }
  1206. else {
  1207. sprintf(path, "%s/%s", GV_DIRECTORY, Map->name);
  1208. }
  1209. return path;
  1210. }
  1211. /*!
  1212. \brief Get map element full path (internal use only)
  1213. Allocate string should be freed by G_free().
  1214. \param Map pointer to Map_info struct
  1215. \param element element name, eg. GV_TOPO_ELEMENT
  1216. \return allocated buffer containing path
  1217. */
  1218. char *Vect__get_element_path(char *file_path,
  1219. const struct Map_info *Map, const char *element)
  1220. {
  1221. char path[GPATH_MAX];
  1222. Vect__get_path(path, Map);
  1223. if (Map->temporary)
  1224. G_file_name_tmp(file_path, path, element, Map->mapset);
  1225. else
  1226. G_file_name(file_path, path, element, Map->mapset);
  1227. return file_path;
  1228. }