open.c 36 KB

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