open.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. /*!
  2. \file lib/vector/Vlib/open.c
  3. \brief Vector library - Open vector map (native or OGR/PostGIS format)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009, 2012 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  9. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  10. \author Update to GRASS 7 Martin Landa <landa.martin gmail.com> (better OGR support and native PostGIS access)
  11. */
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <grass/vector.h>
  19. #include <grass/glocale.h>
  20. /* PG-related defines */
  21. #define FID_COLUMN "fid"
  22. #define GEOMETRY_COLUMN "geom"
  23. /*
  24. \brief Number of levels
  25. Native format:
  26. - 1 without topology
  27. - 2 with topology
  28. OGR-links:
  29. - 1 without topology
  30. - 2 with pseudo-topology (simple features access)
  31. PG-links:
  32. - 1 without topology
  33. - 2 with pseudo-topology (simple features access)
  34. - 3 with topology (PostGIS Topology access)
  35. */
  36. #define MAX_OPEN_LEVEL 3
  37. static int open_old_dummy()
  38. {
  39. return 0;
  40. }
  41. static int open_new_dummy()
  42. {
  43. return 0;
  44. }
  45. #if !defined HAVE_OGR || !defined HAVE_POSTGRES
  46. static int format()
  47. {
  48. G_fatal_error(_("Requested format is not compiled in this version"));
  49. return 0;
  50. }
  51. #endif
  52. static int Open_level = 0;
  53. static int (*Open_old_array[][2]) () = {
  54. {
  55. open_old_dummy, V1_open_old_nat}
  56. #ifdef HAVE_OGR
  57. , {
  58. open_old_dummy, V1_open_old_ogr}
  59. , {
  60. open_old_dummy, V1_open_old_ogr}
  61. #else
  62. , {
  63. open_old_dummy, format}
  64. , {
  65. open_old_dummy, format}
  66. #endif
  67. #ifdef HAVE_POSTGRES
  68. , {
  69. open_old_dummy, V1_open_old_pg}
  70. #else
  71. , {
  72. open_old_dummy, format}
  73. #endif
  74. };
  75. static int (*Open_new_array[][2]) () = {
  76. {
  77. open_new_dummy, V1_open_new_nat}
  78. #ifdef HAVE_OGR
  79. , {
  80. open_new_dummy, V1_open_new_ogr}
  81. , {
  82. open_new_dummy, V1_open_new_ogr}
  83. #else
  84. , {
  85. open_new_dummy, format}
  86. , {
  87. open_new_dummy, format}
  88. #endif
  89. #ifdef HAVE_POSTGRES
  90. , {
  91. open_old_dummy, V1_open_new_pg}
  92. #else
  93. , {
  94. open_old_dummy, format}
  95. #endif
  96. };
  97. static int open_old(struct Map_info *, const char *, const char *,
  98. const char *, int, int);
  99. /*!
  100. \brief Predetermine level at which a vector map will be opened for
  101. reading.
  102. If it can't open that level, the open will fail. The specified level
  103. must be set before any call to open. The default is to try to open
  104. the highest level possible, and keep stepping down until success.
  105. NOTE: This should only be used to set when you wish to force a lower
  106. level open. If you require a higher level, then just check the
  107. return to verify the level instead of forcing it. This is because
  108. future releases will have higher levels which will be downward
  109. compatible and which your programs should support by default.
  110. \param level vector access level
  111. \return 0 on success
  112. \return 1 on error (invalid access level)
  113. */
  114. int Vect_set_open_level(int level)
  115. {
  116. Open_level = level;
  117. if (Open_level < 1 || Open_level > MAX_OPEN_LEVEL) {
  118. G_warning(_("Programmer requested unknown access level %d"),
  119. Open_level);
  120. Open_level = 0;
  121. return 1;
  122. }
  123. return 0;
  124. }
  125. /*!
  126. \brief Open existing vector map for reading (internal use only)
  127. \param[out] Map pointer to Map_info structure
  128. \param name name of vector map to open
  129. \param mapset mapset name ("" for search path)
  130. \param layer layer name (OGR format only)
  131. \param update non-zero to open for update otherwise read-only mode
  132. \param head_only read only header info from 'head', 'dbln', 'topo',
  133. 'cidx' is not opened. The header may be opened on level 2 only.
  134. \return level of openness (1, 2)
  135. \return -1 in error
  136. */
  137. int open_old(struct Map_info *Map, const char *name, const char *mapset,
  138. const char *layer, int update, int head_only)
  139. {
  140. char buf[GNAME_MAX + 10], buf2[GMAPSET_MAX + 10], xname[GNAME_MAX],
  141. xmapset[GMAPSET_MAX];
  142. FILE *fp;
  143. int level, level_request;
  144. int format, ret;
  145. int ogr_mapset;
  146. const char *fmapset;
  147. G_debug(1, "Vect__open_old(): name='%s' mapset='%s' layer='%s' update=%d",
  148. name, mapset, layer, update);
  149. /* zero Map_info structure */
  150. G_zero(Map, sizeof(struct Map_info));
  151. /* TODO: Open header for update ('dbln') */
  152. level_request = Open_level;
  153. Open_level = 0;
  154. /* initialize Map->head */
  155. Vect__init_head(Map);
  156. /* initialize support structures for 2D, update to 3D when reading
  157. support files */
  158. Map->plus.spidx_with_z = Map->plus.with_z = Map->head.with_z = WITHOUT_Z;
  159. /* initialize Map->plus */
  160. dig_init_plus(&(Map->plus));
  161. /* check OGR mapset */
  162. ogr_mapset = FALSE;
  163. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  164. if (strcasecmp(xmapset, "ogr") == 0) {
  165. /* unique OGR mapset detected */
  166. G_debug(1, "OGR mapset detected");
  167. ogr_mapset = TRUE;
  168. Map->fInfo.ogr.dsn = G_store(xname);
  169. if (layer) {
  170. Map->fInfo.ogr.layer_name = G_store(layer); /* no layer to be open */
  171. }
  172. }
  173. else {
  174. sprintf(buf, "%s/%s", GV_DIRECTORY, xname);
  175. sprintf(buf2, "%s@%s", GV_COOR_ELEMENT, xmapset);
  176. }
  177. Map->name = G_store(xname);
  178. Map->mapset = G_store(xmapset);
  179. }
  180. else {
  181. sprintf(buf, "%s/%s", GV_DIRECTORY, name);
  182. sprintf(buf2, "%s", GV_COOR_ELEMENT);
  183. Map->name = G_store(name);
  184. if (mapset)
  185. Map->mapset = G_store(mapset);
  186. else
  187. Map->mapset = G_store("");
  188. }
  189. if (!ogr_mapset) {
  190. /* try to find vector map (not for OGR mapset) */
  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. Map->location = G_store(G_location());
  204. Map->gisdbase = G_store(G_gisdbase());
  205. if (update && !ogr_mapset && (0 != strcmp(Map->mapset, G_mapset()))) {
  206. G_warning(_("Vector map which is not in the current mapset cannot be opened for update"));
  207. return -1;
  208. }
  209. G_debug(1, "Map name: %s", Map->name);
  210. G_debug(1, "Map mapset: %s", Map->mapset);
  211. /* read vector format information */
  212. if (ogr_mapset) {
  213. format = GV_FORMAT_OGR_DIRECT;
  214. }
  215. else {
  216. format = 0;
  217. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  218. G_debug(1, "open format file: '%s/%s/%s'", Map->mapset, buf,
  219. GV_FRMT_ELEMENT);
  220. fp = G_fopen_old(buf, GV_FRMT_ELEMENT, Map->mapset);
  221. if (fp == NULL) {
  222. G_debug(1, "Vector format: %d (native)", format);
  223. format = GV_FORMAT_NATIVE;
  224. }
  225. else {
  226. format = dig_read_frmt_ascii(fp, &(Map->fInfo));
  227. fclose(fp);
  228. G_debug(1, "Vector format: %d (non-native)", format);
  229. if (format < 0) {
  230. G_fatal_error(_("Unable to open vector map <%s>"),
  231. Vect_get_full_name(Map));
  232. return -1;
  233. }
  234. }
  235. }
  236. Map->format = format;
  237. /* read vector head (ignored for OGR mapset) */
  238. if (!ogr_mapset && Vect__read_head(Map) != 0) {
  239. G_fatal_error(_("Unable to read header file of vector map <%s>"),
  240. Vect_get_full_name(Map));
  241. }
  242. /* projection is not written to head but zone ??? */
  243. if (Vect_get_zone(Map) == -1)
  244. Vect_set_zone(Map, G_zone());
  245. Vect_set_proj(Map, G_projection());
  246. G_debug(1, "Level request = %d", level_request);
  247. /* There are only 2 possible open levels, 1 and 2. Try first to
  248. open 'support' files (topo, sidx, cidx), these files are the same
  249. for all formats. If it is not possible and requested level is
  250. 2, return error, otherwise call Open_old_array[format][1], to
  251. open remaining files/sources (level 1)
  252. */
  253. /* try to open support files if level was not requested or
  254. * requested level is 2 (format independent) */
  255. if (level_request == 0 || level_request > 1) {
  256. level = 2; /* we expect success */
  257. /* open topo */
  258. ret = -1;
  259. if (Map->format == GV_FORMAT_POSTGIS)
  260. /* try to read full-topology for PostGIS links */
  261. ret = Vect_open_topo_pg(Map, head_only);
  262. if (ret != 0) {
  263. /* read topology for native format
  264. read pseudo-topology for OGR/PostGIS links */
  265. ret = Vect_open_topo(Map, head_only);
  266. if (ret == 1) { /* topo file is not available */
  267. G_debug(1, "topo file for vector '%s' not available.",
  268. Vect_get_full_name(Map));
  269. level = 1;
  270. }
  271. else if (ret == -1) {
  272. G_fatal_error(_("Unable to open topology file for vector map <%s>"),
  273. Vect_get_full_name(Map));
  274. }
  275. }
  276. else {
  277. level = 3; /* PostGIS topology available */
  278. }
  279. /* open spatial index */
  280. if (level >= 2) {
  281. ret = Vect_open_sidx(Map, (update != 0));
  282. if (ret == 1) { /* sidx file is not available */
  283. G_debug(1, "sidx file for vector '%s' not available.",
  284. Vect_get_full_name(Map));
  285. level = 1;
  286. }
  287. else if (ret == -1) {
  288. G_fatal_error(_("Unable to open spatial index file for vector map <%s>"),
  289. Vect_get_full_name(Map));
  290. }
  291. /* check with_z consistency */
  292. if ((Map->plus.with_z != 0 && Map->plus.spidx_with_z == 0) ||
  293. (Map->plus.with_z == 0 && Map->plus.spidx_with_z != 0)) {
  294. G_warning("Vector map <%s>: topology is %s, but spatial index is %s",
  295. Vect_get_full_name(Map), (Map->plus.with_z != 0 ? "3D" : "2D"),
  296. (Map->plus.spidx_with_z != 0 ? "3D" : "2D"));
  297. level = 1;
  298. }
  299. }
  300. /* open category index */
  301. if (level >= 2) {
  302. ret = Vect_cidx_open(Map, head_only);
  303. if (ret == 1) { /* category index is not available */
  304. G_debug(1,
  305. "cidx file for vector '%s' not available.",
  306. Vect_get_full_name(Map));
  307. dig_free_plus(&(Map->plus)); /* free topology */
  308. dig_spidx_free(&(Map->plus)); /* free spatial index */
  309. level = 1;
  310. }
  311. else if (ret == -1) { /* file exists, but cannot be opened */
  312. G_fatal_error(_("Unable to open category index file for vector map <%s>"),
  313. Vect_get_full_name(Map));
  314. }
  315. }
  316. #ifdef HAVE_OGR
  317. /* open OGR specific support files */
  318. if (level == 2 && Map->format == GV_FORMAT_OGR) {
  319. if (V2_open_old_ogr(Map) < 0) {
  320. dig_free_plus(&(Map->plus));
  321. dig_spidx_free(&(Map->plus));
  322. dig_cidx_free(&(Map->plus));
  323. level = 1;
  324. }
  325. }
  326. #endif
  327. #ifdef HAVE_POSTGRES
  328. /* open OGR (pseudo-topology access only) specific support
  329. * files */
  330. if (level == 2 && Map->format == GV_FORMAT_POSTGIS) {
  331. if (V2_open_old_pg(Map) < 0) {
  332. dig_free_plus(&(Map->plus));
  333. dig_spidx_free(&(Map->plus));
  334. dig_cidx_free(&(Map->plus));
  335. level = 1;
  336. }
  337. }
  338. #endif
  339. if (level_request == 2 && level < 2) {
  340. if (!ogr_mapset) {
  341. /* for direct OGR read access is built pseudo-topology on the fly */
  342. G_warning(_("Unable to open vector map <%s> on level %d. "
  343. "Try to rebuild vector topology by v.build."),
  344. Vect_get_full_name(Map), level_request);
  345. return -1;
  346. }
  347. }
  348. }
  349. else {
  350. level = 1; /* i.e. requested level is 1 */
  351. }
  352. /* open level 1 files / sources (format specific) */
  353. if (!head_only || ogr_mapset || format == GV_FORMAT_POSTGIS) {
  354. /* no need to open coordinates */
  355. if (0 != (*Open_old_array[format][1]) (Map, update)) { /* cannot open */
  356. if (level >= 2) { /* support files opened */
  357. dig_free_plus(&(Map->plus));
  358. dig_spidx_free(&(Map->plus));
  359. dig_cidx_free(&(Map->plus));
  360. }
  361. if (level_request == 0)
  362. G_fatal_error(_("Unable to open vector map <%s>"),
  363. Vect_get_full_name(Map));
  364. else
  365. G_fatal_error(_("Unable to open vector map <%s> on level %d. "
  366. "Try to rebuild vector topology by v.build."),
  367. Vect_get_full_name(Map), level_request);
  368. return -1;
  369. }
  370. if (ogr_mapset && !head_only && level_request != 1) {
  371. /* build pseudo-topology on the fly */
  372. int verbose;
  373. verbose = G_verbose();
  374. G_message(_("Building topology for OGR layer <%s> from datasource '%s'..."),
  375. Map->fInfo.ogr.layer_name, Map->fInfo.ogr.dsn);
  376. G_set_verbose(0);
  377. if (Vect_build(Map)) {
  378. level = 2;
  379. }
  380. G_set_verbose(verbose);
  381. if (level < level_request)
  382. G_fatal_error(_("Unable to open vector map <%s> on level %d"),
  383. Map->fInfo.ogr.layer_name, level_request);
  384. }
  385. if (level < 2 && Map->head.with_z) {
  386. /* topo has been initialized as 2D, update to 3D */
  387. dig_free_plus(&(Map->plus));
  388. dig_spidx_free(&(Map->plus));
  389. dig_cidx_free(&(Map->plus));
  390. Map->plus.with_z = Map->head.with_z;
  391. dig_init_plus(&(Map->plus));
  392. }
  393. }
  394. else if (level > 1) {
  395. /* take dimension from topo if topo is available */
  396. Map->head.with_z = Map->plus.with_z;
  397. }
  398. /* set status */
  399. Map->open = VECT_OPEN_CODE;
  400. Map->level = level;
  401. Map->head_only = head_only;
  402. Map->support_updated = FALSE;
  403. if (update) {
  404. Map->mode = GV_MODE_RW;
  405. Map->plus.mode = GV_MODE_RW;
  406. }
  407. else {
  408. Map->mode = GV_MODE_READ;
  409. Map->plus.mode = GV_MODE_READ;
  410. }
  411. if (head_only) {
  412. Map->head_only = TRUE;
  413. }
  414. else {
  415. Map->head_only = FALSE;
  416. }
  417. G_debug(1, "Vect_open_old(): vector opened on level %d", level);
  418. if (level == 1) { /* without topology */
  419. Map->plus.built = GV_BUILD_NONE;
  420. }
  421. else { /* level 2, with topology */
  422. Map->plus.built = GV_BUILD_ALL; /* highest level of topology for level 2 */
  423. }
  424. Map->plus.uplist.do_uplist = FALSE;
  425. /* read db links */
  426. Map->dblnk = Vect_new_dblinks_struct();
  427. Vect_read_dblinks(Map);
  428. /* open history file */
  429. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  430. if (update && !ogr_mapset) { /* native only */
  431. Map->hist_fp = G_fopen_modify(buf, GV_HIST_ELEMENT);
  432. if (Map->hist_fp == NULL) {
  433. G_warning(_("Unable to open history file for vector map <%s>"),
  434. Vect_get_full_name(Map));
  435. return -1;
  436. }
  437. G_fseek(Map->hist_fp, (off_t) 0, SEEK_END);
  438. Vect_hist_write(Map,
  439. "---------------------------------------------------------------------------------\n");
  440. }
  441. else {
  442. if (Map->format == GV_FORMAT_NATIVE || Map->format == GV_FORMAT_OGR ||
  443. Map->format == GV_FORMAT_POSTGIS) {
  444. Map->hist_fp = G_fopen_old(buf, GV_HIST_ELEMENT, Map->mapset);
  445. /* If NULL (does not exist) then Vect_hist_read() handle that */
  446. }
  447. else {
  448. Map->hist_fp = NULL;
  449. }
  450. }
  451. if (!head_only) { /* cannot rewind if not fully opened */
  452. Vect_rewind(Map);
  453. }
  454. /* delete support files if native format was opened for update (not head_only) */
  455. if (update && !head_only) {
  456. char file_path[GPATH_MAX];
  457. sprintf(buf, "%s/%s", GV_DIRECTORY, name);
  458. G_file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
  459. if (access(file_path, F_OK) == 0) /* topo file exists? */
  460. unlink(file_path);
  461. G_file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
  462. if (access(file_path, F_OK) == 0) /* sidx file exists? */
  463. unlink(file_path);
  464. G_file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
  465. if (access(file_path, F_OK) == 0) /* cidx file exists? */
  466. unlink(file_path);
  467. if (format == GV_FORMAT_OGR ||
  468. (format == GV_FORMAT_POSTGIS && level == 2)) {
  469. G_file_name(file_path, buf, GV_FIDX_ELEMENT, G_mapset());
  470. if (access(file_path, F_OK) == 0) /* fidx file exists? */
  471. unlink(file_path);
  472. }
  473. }
  474. return level;
  475. }
  476. /*!
  477. \brief Open existing vector map for reading
  478. This function is replaced by Vect_open_old2() to handle also direct
  479. OGR support.
  480. Calls G_fatal_error() on failure.
  481. \param[out] Map pointer to Map_info structure
  482. \param name name of vector map to open
  483. \param mapset mapset name ("" for search path)
  484. \return 1 open on level 1 (without topology)
  485. \return 2 open on level 2 (with topology)
  486. \return -1 on error
  487. */
  488. int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
  489. {
  490. return open_old(Map, name, mapset, NULL, 0, 0);
  491. }
  492. /*!
  493. \brief Open existing vector map for reading
  494. Calls G_fatal_error() on failure.
  495. \param[out] Map pointer to Map_info structure
  496. \param name name of vector map to open (datasource for direct OGR access)
  497. \param mapset mapset name ("" for search path, "OGR" for direct OGR access)
  498. \param layer layer name (OGR layer for direct OGR access)
  499. \return 1 open on level 1 (without topology)
  500. \return 2 open on level 2 (with topology)
  501. \return -1 on error
  502. */
  503. int Vect_open_old2(struct Map_info *Map, const char *name, const char *mapset,
  504. const char *layer)
  505. {
  506. return open_old(Map, name, mapset, layer, 0, 0);
  507. }
  508. /*!
  509. \brief Open existing vector map for reading/writing
  510. This function is replaced by Vect_open_update2() to handle also
  511. direct OGR support.
  512. By default list of updated features is not maintained, see
  513. Vect_set_updated() for details.
  514. Calls G_fatal_error() on failure.
  515. \param[out] Map pointer to Map_info structure
  516. \param name name of vector map to update
  517. \param mapset mapset name
  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_update(struct Map_info *Map, const char *name, const char *mapset)
  523. {
  524. return open_old(Map, name, mapset, NULL, 1, 0);
  525. }
  526. /*!
  527. \brief Open existing vector map for reading/writing
  528. By default list of updated features is not maintained, see
  529. Vect_set_updated() for details.
  530. Calls G_fatal_error() on failure.
  531. \param[out] Map pointer to Map_info structure
  532. \param name name of vector map to open (datasource for direct OGR access)
  533. \param mapset mapset name ("" for search path, "OGR" for direct OGR access)
  534. \param layer layer name (OGR layer for direct OGR access)
  535. \return 1 open on level 1 (without topology)
  536. \return 2 open on level 2 (with topology)
  537. \return -1 on error
  538. */
  539. int Vect_open_update2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
  540. {
  541. return open_old(Map, name, mapset, layer, 1, 0);
  542. }
  543. /*!
  544. \brief Reads only info about vector map (headers)
  545. Reads from headers of 'head', 'dbln', 'topo' and 'cidx' file.
  546. This function is replaced by Vect_open_old_head2() to handle also
  547. direct OGR support.
  548. Calls G_fatal_error() on failure.
  549. \param[out] Map pointer to Map_info structure
  550. \param name name of vector map to read
  551. \param mapset mapset name ("" for search path)
  552. \return 1 open on level 1 (without topology)
  553. \return 2 open on level 2 (with topology)
  554. \return -1 on error
  555. */
  556. int Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
  557. {
  558. return open_old(Map, name, mapset, NULL, 0, 1);
  559. }
  560. /*!
  561. \brief Reads only info about vector map (headers)
  562. Reads from headers of 'head', 'dbln', 'topo' and 'cidx' file.
  563. Calls G_fatal_error() on failure.
  564. \param[out] Map pointer to Map_info structure
  565. \param name name of vector map to read (dsn for OGR)
  566. \param mapset mapset name ("" for search path)
  567. \param layer layer name (OGR format)
  568. \param[out] Map pointer to Map_info structure
  569. \param name name of vector map to open (datasource for direct OGR access)
  570. \param mapset mapset name ("" for search path, "OGR" for direct OGR access)
  571. \param layer layer name (OGR layer for direct OGR access)
  572. \return 1 open on level 1 (without topology)
  573. \return 2 open on level 2 (with topology)
  574. \return -1 on error
  575. */
  576. int Vect_open_old_head2(struct Map_info *Map, const char *name, const char *mapset,
  577. const char *layer)
  578. {
  579. return open_old(Map, name, mapset, layer, 0, 1);
  580. }
  581. /*! \brief Open header file of existing vector map for updating
  582. (mostly for database link updates)
  583. \param[out] Map pointer to Map_info structure
  584. \param name name of vector map to update
  585. \param mapset mapset name
  586. \return 1 open on level 1 (without topology)
  587. \return 2 open on level 2 (with topology)
  588. \return -1 on error
  589. */
  590. int Vect_open_update_head(struct Map_info *Map, const char *name,
  591. const char *mapset)
  592. {
  593. return open_old(Map, name, mapset, NULL, 1, 1);
  594. }
  595. /*!
  596. \brief Create new vector map for reading/writing
  597. By default list of updated features is not maintained, see
  598. Vect_set_updated() for details.
  599. \param[in,out] Map pointer to Map_info structure
  600. \param name name of vector map
  601. \param with_z non-zero value for 3D vector data
  602. \return 1 on success
  603. \return -1 on error
  604. */
  605. int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
  606. {
  607. int ret;
  608. char buf[500];
  609. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  610. G_debug(2, "Vect_open_new(): name = %s", name);
  611. G_zero(Map, sizeof(struct Map_info));
  612. /* init header */
  613. Vect__init_head(Map);
  614. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  615. if (strcmp(xmapset, G_mapset()) != 0) {
  616. G_fatal_error(_("<%s> is not the current mapset (%s)"), name,
  617. G_mapset());
  618. return -1;
  619. }
  620. name = xname;
  621. }
  622. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  623. if (Vect_legal_filename(name) < 0) {
  624. G_fatal_error(_("Vector map name is not SQL compliant"));
  625. return -1;
  626. }
  627. /* determine output format native or ogr */
  628. Map->format = GV_FORMAT_NATIVE;
  629. if (strcmp(G_program_name(), "v.external") != 0) {
  630. if (G_find_file2("", "OGR", G_mapset())) {
  631. /* OGR */
  632. FILE *fp;
  633. const char *p;
  634. struct Key_Value *key_val;
  635. struct Format_info_ogr *ogr_info;
  636. G_debug(2, " using OGR format");
  637. Map->format = GV_FORMAT_OGR_DIRECT;
  638. fp = G_fopen_old("", "OGR", G_mapset());
  639. if (!fp) {
  640. G_fatal_error(_("Unable to open OGR file"));
  641. }
  642. key_val = G_fread_key_value(fp);
  643. fclose(fp);
  644. ogr_info = &(Map->fInfo.ogr);
  645. /* format */
  646. p = G_find_key_value("format", key_val);
  647. if (p)
  648. ogr_info->driver_name = G_store(p);
  649. /* dsn */
  650. p = G_find_key_value("dsn", key_val);
  651. if (p)
  652. ogr_info->dsn = G_store(p);
  653. /* options */
  654. p = G_find_key_value("options", key_val);
  655. if (p)
  656. ogr_info->layer_options = G_tokenize(p, ",");
  657. ogr_info->layer_name = G_store(name);
  658. }
  659. if (G_find_file2("", "PG", G_mapset())) {
  660. /* PostGIS */
  661. if (Map->fInfo.ogr.driver_name) {
  662. G_warning(_("OGR output also detected, using OGR"));
  663. }
  664. else {
  665. FILE *fp;
  666. const char *p;
  667. struct Key_Value *key_val;
  668. struct Format_info_pg *pg_info;
  669. G_debug(2, " using PostGIS format");
  670. Map->format = GV_FORMAT_POSTGIS;
  671. fp = G_fopen_old("", "PG", G_mapset());
  672. if (!fp) {
  673. G_fatal_error(_("Unable to open PG file"));
  674. }
  675. key_val = G_fread_key_value(fp);
  676. fclose(fp);
  677. pg_info = &(Map->fInfo.pg);
  678. /* conninfo */
  679. p = G_find_key_value("conninfo", key_val);
  680. if (p) {
  681. pg_info->conninfo = G_store(p);
  682. G_debug(1, "PG: conninfo = '%s'", pg_info->conninfo);
  683. }
  684. /* schema (default: public) */
  685. p = G_find_key_value("schema", key_val);
  686. if (p)
  687. pg_info->schema_name = G_store(p);
  688. else
  689. pg_info->schema_name = G_store("public");
  690. G_debug(1, "PG: schema_name = '%s'", pg_info->schema_name);
  691. /* fid column (default: ogc_fid) */
  692. p = G_find_key_value("fid", key_val);
  693. if (p)
  694. pg_info->fid_column = G_store(p);
  695. else
  696. pg_info->fid_column = G_store(FID_COLUMN);
  697. G_debug(1, "PG: fid_column = '%s'", pg_info->fid_column);
  698. /* geometry column (default: wkb_geometry) */
  699. p = G_find_key_value("geometry_name", key_val);
  700. if (p)
  701. pg_info->geom_column = G_store(p);
  702. else
  703. pg_info->geom_column = G_store(GEOMETRY_COLUMN);
  704. G_debug(1, "PG: geom_column = '%s'", pg_info->geom_column);
  705. /* table name */
  706. Map->fInfo.pg.table_name = G_store(name);
  707. }
  708. }
  709. }
  710. if (Map->format == GV_FORMAT_NATIVE) {
  711. /* native */
  712. G_debug(2, " using native format");
  713. /* check if map already exists */
  714. if (G_find_vector2(name, G_mapset()) != NULL) {
  715. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  716. name);
  717. ret = Vect_delete(name);
  718. if (ret == -1) {
  719. G_warning(_("Unable to delete vector map <%s>"), name);
  720. return -1;
  721. }
  722. }
  723. }
  724. Map->name = G_store(name);
  725. Map->mapset = G_store(G_mapset());
  726. Map->location = G_store(G_location());
  727. Map->gisdbase = G_store(G_gisdbase());
  728. /* set 2D/3D */
  729. Map->plus.spidx_with_z = Map->plus.with_z = Map->head.with_z = (with_z != 0);
  730. if ((*Open_new_array[Map->format][1]) (Map, name, with_z) < 0) {
  731. G_fatal_error(_("Unable to create vector map <%s>"),
  732. name);
  733. return -1;
  734. }
  735. if (Map->format == GV_FORMAT_NATIVE) {
  736. /* Open history file */
  737. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  738. Map->hist_fp = G_fopen_new(buf, GV_HIST_ELEMENT);
  739. if (Map->hist_fp == NULL) {
  740. G_warning(_("Unable to open history file of vector map <%s>"),
  741. name);
  742. return -1;
  743. }
  744. }
  745. Open_level = 0;
  746. /* initialize topo */
  747. Map->plus.Spidx_file = 0;
  748. dig_init_plus(&(Map->plus));
  749. /* open new spatial index */
  750. Vect_open_sidx(Map, 2);
  751. Map->open = VECT_OPEN_CODE;
  752. Map->level = 1;
  753. Map->head_only = FALSE;
  754. Map->support_updated = FALSE;
  755. Map->plus.built = GV_BUILD_NONE;
  756. Map->mode = GV_MODE_RW;
  757. Map->plus.uplist.do_uplist = FALSE;
  758. Vect_set_proj(Map, G_projection());
  759. Vect_set_zone(Map, G_zone());
  760. Map->dblnk = Vect_new_dblinks_struct();
  761. return 1;
  762. }
  763. /*!
  764. \brief Update Coor_info structure
  765. \param Map pointer to Map_info structure
  766. \param[out] Info pointer to Coor_info structure
  767. \return 1 on success
  768. \return 0 on error
  769. */
  770. int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info)
  771. {
  772. char buf[GPATH_MAX], path[GPATH_MAX];
  773. STRUCT_STAT stat_buf;
  774. switch (Map->format) {
  775. case GV_FORMAT_NATIVE:
  776. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  777. G_file_name(path, buf, GV_COOR_ELEMENT, Map->mapset);
  778. G_debug(1, "get coor info: %s", path);
  779. if (0 != stat(path, &stat_buf)) {
  780. G_warning(_("Unable to stat file <%s>"), path);
  781. Info->size = -1L;
  782. Info->mtime = -1L;
  783. }
  784. else {
  785. Info->size = (off_t)stat_buf.st_size; /* file size */
  786. Info->mtime = (long)stat_buf.st_mtime; /* last modified time */
  787. }
  788. /* stat does not give correct size on MINGW
  789. * if the file is opened */
  790. #ifdef __MINGW32__
  791. if (Map->open == VECT_OPEN_CODE) {
  792. dig_fseek(&(Map->dig_fp), 0L, SEEK_END);
  793. G_debug(2, "dig_ftell = %d", dig_ftell(&(Map->dig_fp)));
  794. Info->size = dig_ftell(&(Map->dig_fp));
  795. }
  796. #endif
  797. break;
  798. case GV_FORMAT_OGR:
  799. case GV_FORMAT_OGR_DIRECT:
  800. case GV_FORMAT_POSTGIS:
  801. Info->size = 0L;
  802. Info->mtime = 0L;
  803. break;
  804. }
  805. G_debug(1, "Info->size = %lu, Info->mtime = %ld",
  806. (unsigned long)Info->size, Info->mtime);
  807. return 1;
  808. }
  809. /*!
  810. \brief Gets vector map format (as string)
  811. Note: string is allocated by G_store(). Free allocated memory with
  812. G_free().
  813. Currently are implemeted:
  814. - Native format (native)
  815. - OGR format (ogr)
  816. - PostGIS format (postgis)
  817. \param Map pointer to Map_info structure
  818. \return maptype string on success (allocated by G_store())
  819. \return error message on error
  820. */
  821. const char *Vect_maptype_info(const struct Map_info *Map)
  822. {
  823. char maptype[1000];
  824. switch (Map->format) {
  825. case GV_FORMAT_NATIVE:
  826. sprintf(maptype, "native");
  827. break;
  828. case GV_FORMAT_OGR:
  829. case GV_FORMAT_OGR_DIRECT:
  830. sprintf(maptype, "OGR");
  831. break;
  832. case GV_FORMAT_POSTGIS:
  833. sprintf(maptype, "PostGIS");
  834. break;
  835. default:
  836. sprintf(maptype, _("unknown %d (update Vect_maptype_info)"),
  837. Map->format);
  838. }
  839. return G_store(maptype);
  840. }
  841. /*!
  842. \brief Gets vector map format
  843. Currently are implemeted:
  844. - Native format (GV_FORMAT_NATIVE)
  845. - OGR format linked via v.external (GV_FORMAT_OGR)
  846. - OGR format (GV_FORMAT_OGR_DIRECT)
  847. - PostGIS fomat (GV_FORMAT_POSTGIS)
  848. \param Map pointer to Map_info structure
  849. \return maptype code
  850. */
  851. int Vect_maptype(const struct Map_info *Map)
  852. {
  853. return Map->format;
  854. }
  855. /*!
  856. \brief Open topology file ('topo')
  857. \param[in,out] Map pointer to Map_info structure
  858. \param head_only TRUE to read only header
  859. \return 0 on success
  860. \return 1 file does not exist
  861. \return -1 on error
  862. */
  863. int Vect_open_topo(struct Map_info *Map, int head_only)
  864. {
  865. int err, ret;
  866. char buf[GPATH_MAX], file_path[GPATH_MAX];
  867. struct gvfile fp;
  868. struct Coor_info CInfo;
  869. struct Plus_head *Plus;
  870. G_debug(1, "Vect_open_topo(): name = %s mapset= %s", Map->name,
  871. Map->mapset);
  872. Plus = &(Map->plus);
  873. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  874. G_file_name(file_path, buf, GV_TOPO_ELEMENT, Map->mapset);
  875. if (access(file_path, F_OK) != 0) /* does not exist */
  876. return 1;
  877. dig_file_init(&fp);
  878. fp.file = G_fopen_old(buf, GV_TOPO_ELEMENT, Map->mapset);
  879. if (fp.file == NULL) { /* topo file is not available */
  880. G_debug(1, "Cannot open topo file for vector '%s@%s'.",
  881. Map->name, Map->mapset);
  882. return -1;
  883. }
  884. /* get coor info */
  885. /* NOTE: coor file not yet opened */
  886. Vect_coor_info(Map, &CInfo);
  887. /* load head */
  888. if (dig_Rd_Plus_head(&fp, Plus) == -1)
  889. return -1;
  890. G_debug(1, "Topo head: coor size = %lu, coor mtime = %ld",
  891. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  892. /* do checks */
  893. err = 0;
  894. if (CInfo.size != Plus->coor_size) {
  895. G_warning(_("Size of 'coor' file differs from value saved in topology file"));
  896. err = 1;
  897. }
  898. /* Do not check mtime because mtime is changed by copy */
  899. /*
  900. if ( CInfo.mtime != Plus->coor_mtime ) {
  901. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  902. err = 1;
  903. }
  904. */
  905. if (err) {
  906. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  907. Map->name, Map->mapset);
  908. return -1;
  909. }
  910. /* load file to the memory */
  911. /* dig_file_load ( &fp); */
  912. /* load topo to memory */
  913. ret = dig_load_plus(Plus, &fp, head_only);
  914. fclose(fp.file);
  915. /* dig_file_free(&fp); */
  916. return ret == 0 ? -1 : 0;
  917. }
  918. /*!
  919. \brief Open spatial index file ('sidx')
  920. \param[in,out] Map pointer to Map_info
  921. \param mode 0 old, 1 update, 2 new
  922. \return 0 on success
  923. \return -1 on error
  924. */
  925. int Vect_open_sidx(struct Map_info *Map, int mode)
  926. {
  927. char buf[GPATH_MAX], file_path[GPATH_MAX];
  928. int err;
  929. struct Coor_info CInfo;
  930. struct Plus_head *Plus;
  931. G_debug(1, "Vect_open_sidx(): name = %s mapset= %s mode = %s", Map->name,
  932. Map->mapset, mode == 0 ? "old" : (mode == 1 ? "update" : "new"));
  933. Plus = &(Map->plus);
  934. if (Plus->Spidx_built) {
  935. G_debug(1, "Spatial index already opened");
  936. return 0;
  937. }
  938. dig_file_init(&(Plus->spidx_fp));
  939. if (mode < 2) {
  940. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  941. G_file_name(file_path, buf, GV_SIDX_ELEMENT, Map->mapset);
  942. if (access(file_path, F_OK) != 0) /* does not exist */
  943. return 1;
  944. Plus->spidx_fp.file =
  945. G_fopen_old(buf, GV_SIDX_ELEMENT, Map->mapset);
  946. if (Plus->spidx_fp.file == NULL) { /* sidx file is not available */
  947. G_debug(1, "Cannot open spatial index file for vector '%s@%s'.",
  948. Map->name, Map->mapset);
  949. return -1;
  950. }
  951. /* get coor info */
  952. /* NOTE: coor file not yet opened */
  953. Vect_coor_info(Map, &CInfo);
  954. /* initialize spatial index */
  955. Plus->Spidx_new = FALSE;
  956. /* load head */
  957. if (dig_Rd_spidx_head(&(Plus->spidx_fp), Plus) == -1) {
  958. fclose(Plus->spidx_fp.file);
  959. return -1;
  960. }
  961. G_debug(1, "Sidx head: coor size = %lu, coor mtime = %ld",
  962. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  963. /* do checks */
  964. err = 0;
  965. if (CInfo.size != Plus->coor_size) {
  966. G_warning(_("Size of 'coor' file differs from value saved in sidx file"));
  967. err = 1;
  968. }
  969. /* Do not check mtime because mtime is changed by copy */
  970. /*
  971. if ( CInfo.mtime != Plus->coor_mtime ) {
  972. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  973. err = 1;
  974. }
  975. */
  976. if (err) {
  977. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  978. Map->name, Map->mapset);
  979. fclose(Plus->spidx_fp.file);
  980. return -1;
  981. }
  982. }
  983. if (mode) {
  984. /* open new spatial index */
  985. Plus->Spidx_new = TRUE;
  986. /* file based or memory based */
  987. if (getenv("GRASS_VECTOR_LOWMEM")) {
  988. /* free old indices */
  989. dig_spidx_free(Plus);
  990. /* initialize file based indices */
  991. Plus->Spidx_file = 1;
  992. dig_spidx_init(Plus);
  993. }
  994. G_debug(1, "%s based spatial index",
  995. Plus->Spidx_file == 0 ? "Memory" : "File");
  996. if (mode == 1) {
  997. /* load spatial index for update */
  998. if (dig_Rd_spidx(&(Plus->spidx_fp), Plus) == -1) {
  999. fclose(Plus->spidx_fp.file);
  1000. return -1;
  1001. }
  1002. }
  1003. }
  1004. Plus->Spidx_built = TRUE;
  1005. return 0;
  1006. }