open.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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 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. #ifdef HAVE_POSTGRES
  22. #include "pg_local_proto.h"
  23. #endif
  24. /*
  25. \brief Number of levels
  26. - 1 without topology
  27. - 2 with 2D topology
  28. \todo Implement
  29. - 3 with 3D topology
  30. */
  31. #define MAX_OPEN_LEVEL 2
  32. static int open_old_dummy()
  33. {
  34. return 0;
  35. }
  36. static int open_new_dummy()
  37. {
  38. return 0;
  39. }
  40. #if !defined HAVE_OGR || !defined HAVE_POSTGRES
  41. static int format()
  42. {
  43. G_fatal_error(_("Requested format is not compiled in this version"));
  44. return 0;
  45. }
  46. #endif
  47. static int Open_level = 0;
  48. static int (*Open_old_array[][2]) () = {
  49. {
  50. open_old_dummy, V1_open_old_nat}
  51. #ifdef HAVE_OGR
  52. , {
  53. open_old_dummy, V1_open_old_ogr}
  54. , {
  55. open_old_dummy, V1_open_old_ogr}
  56. #else
  57. , {
  58. open_old_dummy, format}
  59. , {
  60. open_old_dummy, format}
  61. #endif
  62. #ifdef HAVE_POSTGRES
  63. , {
  64. open_old_dummy, V1_open_old_pg}
  65. #else
  66. , {
  67. open_old_dummy, format}
  68. #endif
  69. };
  70. static int (*Open_new_array[][2]) () = {
  71. {
  72. open_new_dummy, V1_open_new_nat}
  73. #ifdef HAVE_OGR
  74. , {
  75. open_new_dummy, V1_open_new_ogr}
  76. , {
  77. open_new_dummy, V1_open_new_ogr}
  78. #else
  79. , {
  80. open_new_dummy, format}
  81. , {
  82. open_new_dummy, format}
  83. #endif
  84. #ifdef HAVE_POSTGRES
  85. , {
  86. open_old_dummy, V1_open_new_pg}
  87. #else
  88. , {
  89. open_old_dummy, format}
  90. #endif
  91. };
  92. static int open_old(struct Map_info *, const char *, const char *,
  93. 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. \return level of openness (1, 2)
  131. \return -1 in error
  132. */
  133. int open_old(struct Map_info *Map, const char *name, const char *mapset,
  134. const char *layer, int update, int head_only)
  135. {
  136. char buf[GNAME_MAX + 10], buf2[GMAPSET_MAX + 10], xname[GNAME_MAX],
  137. xmapset[GMAPSET_MAX];
  138. FILE *fp;
  139. int level, level_request;
  140. int format, ret;
  141. int ogr_mapset;
  142. const char *fmapset;
  143. G_debug(1, "Vect__open_old(): name='%s' mapset='%s' layer='%s' update=%d",
  144. name, mapset, layer, update);
  145. /* zero Map_info structure */
  146. G_zero(Map, sizeof(struct Map_info));
  147. /* TODO: Open header for update ('dbln') */
  148. level_request = Open_level;
  149. Open_level = 0;
  150. /* initialize Map->head */
  151. Vect__init_head(Map);
  152. /* initialize support structures for 2D, update to 3D when reading
  153. support files */
  154. Map->plus.spidx_with_z = Map->plus.with_z = Map->head.with_z = WITHOUT_Z;
  155. /* initialize Map->plus */
  156. dig_init_plus(&(Map->plus));
  157. /* check OGR mapset */
  158. ogr_mapset = FALSE;
  159. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  160. if (strcasecmp(xmapset, "ogr") == 0) {
  161. /* unique OGR mapset detected */
  162. G_debug(1, "OGR mapset detected");
  163. ogr_mapset = TRUE;
  164. Map->fInfo.ogr.dsn = G_store(xname);
  165. if (layer) {
  166. Map->fInfo.ogr.layer_name = G_store(layer); /* no layer to be open */
  167. }
  168. }
  169. else {
  170. sprintf(buf, "%s/%s", GV_DIRECTORY, xname);
  171. sprintf(buf2, "%s@%s", GV_COOR_ELEMENT, xmapset);
  172. }
  173. Map->name = G_store(xname);
  174. Map->mapset = G_store(xmapset);
  175. }
  176. else {
  177. sprintf(buf, "%s/%s", GV_DIRECTORY, name);
  178. sprintf(buf2, "%s", GV_COOR_ELEMENT);
  179. Map->name = G_store(name);
  180. if (mapset)
  181. Map->mapset = G_store(mapset);
  182. else
  183. Map->mapset = G_store("");
  184. }
  185. if (!ogr_mapset) {
  186. /* try to find vector map (not for OGR mapset) */
  187. fmapset = G_find_vector2(Map->name, Map->mapset);
  188. if (fmapset == NULL) {
  189. if (mapset && strcmp(mapset, G_mapset()) == 0)
  190. G_fatal_error(_("Vector map <%s> not found in current mapset"),
  191. Vect_get_name(Map));
  192. else
  193. G_fatal_error(_("Vector map <%s> not found"),
  194. Vect_get_full_name(Map));
  195. return -1;
  196. }
  197. Map->mapset = G_store(fmapset);
  198. }
  199. Map->location = G_store(G_location());
  200. Map->gisdbase = G_store(G_gisdbase());
  201. if (update && !ogr_mapset && (0 != strcmp(Map->mapset, G_mapset()))) {
  202. G_warning(_("Vector map which is not in the current mapset cannot be opened for update"));
  203. return -1;
  204. }
  205. G_debug(1, "Map name: %s", Map->name);
  206. G_debug(1, "Map mapset: %s", Map->mapset);
  207. /* read vector format information */
  208. if (ogr_mapset) {
  209. format = GV_FORMAT_OGR_DIRECT;
  210. }
  211. else {
  212. format = 0;
  213. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  214. G_debug(1, "open format file: '%s/%s/%s'", Map->mapset, buf,
  215. GV_FRMT_ELEMENT);
  216. fp = G_fopen_old(buf, GV_FRMT_ELEMENT, Map->mapset);
  217. if (fp == NULL) {
  218. G_debug(1, "Vector format: %d (native)", format);
  219. format = GV_FORMAT_NATIVE;
  220. }
  221. else {
  222. format = dig_read_frmt_ascii(fp, &(Map->fInfo));
  223. fclose(fp);
  224. G_debug(1, "Vector format: %d (non-native)", format);
  225. if (format < 0) {
  226. G_fatal_error(_("Unable to open vector map <%s>"),
  227. Vect_get_full_name(Map));
  228. return -1;
  229. }
  230. }
  231. }
  232. Map->format = format;
  233. /* read vector head (ignored for OGR mapset) */
  234. if (!ogr_mapset && Vect__read_head(Map) != 0) {
  235. G_fatal_error(_("Unable to read header file of vector map <%s>"),
  236. Vect_get_full_name(Map));
  237. }
  238. /* projection is not written to head but zone ??? */
  239. if (Vect_get_zone(Map) == -1)
  240. Vect_set_zone(Map, G_zone());
  241. Vect_set_proj(Map, G_projection());
  242. G_debug(1, "Level request = %d", level_request);
  243. /* There are only 2 possible open levels, 1 and 2. Try first to
  244. open 'support' files (topo, sidx, cidx), these files are the same
  245. for all formats. If it is not possible and requested level is
  246. 2, return error, otherwise call Open_old_array[format][1], to
  247. open remaining files/sources (level 1)
  248. */
  249. /* try to open support files if level was not requested or
  250. * requested level is 2 (format independent) */
  251. if (level_request == 0 || level_request > 1) {
  252. level = 2; /* we expect success */
  253. /* open topo */
  254. ret = -1;
  255. if (Map->format == GV_FORMAT_POSTGIS)
  256. /* try to read full-topology for PostGIS links */
  257. ret = Vect_open_topo_pg(Map, head_only);
  258. if (ret != 0) {
  259. /* read topology for native format
  260. read pseudo-topology for OGR/PostGIS links */
  261. ret = Vect_open_topo(Map, head_only);
  262. if (ret == 1) { /* topo file is not available */
  263. G_debug(1, "topo file for vector '%s' not available.",
  264. Vect_get_full_name(Map));
  265. level = 1;
  266. }
  267. else if (ret == -1) {
  268. G_fatal_error(_("Unable to open topology file for vector map <%s>"),
  269. Vect_get_full_name(Map));
  270. }
  271. }
  272. /* open spatial index */
  273. if (level >= 2) {
  274. ret = Vect_open_sidx(Map, (update != 0));
  275. if (ret == 1) { /* sidx file is not available */
  276. G_debug(1, "sidx file for vector '%s' not available.",
  277. Vect_get_full_name(Map));
  278. level = 1;
  279. }
  280. else if (ret == -1) {
  281. G_fatal_error(_("Unable to open spatial index file for vector map <%s>"),
  282. Vect_get_full_name(Map));
  283. }
  284. /* check with_z consistency */
  285. if ((Map->plus.with_z != 0 && Map->plus.spidx_with_z == 0) ||
  286. (Map->plus.with_z == 0 && Map->plus.spidx_with_z != 0)) {
  287. G_warning("Vector map <%s>: topology is %s, but spatial index is %s",
  288. Vect_get_full_name(Map), (Map->plus.with_z != 0 ? "3D" : "2D"),
  289. (Map->plus.spidx_with_z != 0 ? "3D" : "2D"));
  290. level = 1;
  291. }
  292. }
  293. /* open category index */
  294. if (level >= 2) {
  295. ret = Vect_cidx_open(Map, head_only);
  296. if (ret == 1) { /* category index is not available */
  297. G_debug(1,
  298. "cidx file for vector '%s' not available.",
  299. Vect_get_full_name(Map));
  300. dig_free_plus(&(Map->plus)); /* free topology */
  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. level = 1;
  314. }
  315. }
  316. #endif
  317. #ifdef HAVE_POSTGRES
  318. /* open OGR (pseudo-topology access only) specific support
  319. * files */
  320. if (level == 2 && Map->format == GV_FORMAT_POSTGIS) {
  321. if (V2_open_old_pg(Map) < 0) {
  322. dig_free_plus(&(Map->plus));
  323. level = 1;
  324. }
  325. }
  326. #endif
  327. if (level_request == 2 && level < 2) {
  328. if (!ogr_mapset) {
  329. /* for direct OGR read access is built pseudo-topology on the fly */
  330. G_warning(_("Unable to open vector map <%s> on level %d. "
  331. "Try to rebuild vector topology by v.build."),
  332. Vect_get_full_name(Map), level_request);
  333. return -1;
  334. }
  335. }
  336. }
  337. else {
  338. level = 1; /* i.e. requested level is 1 */
  339. }
  340. /* open level 1 files / sources (format specific) */
  341. if (!head_only || ogr_mapset || format == GV_FORMAT_POSTGIS) {
  342. /* no need to open coordinates */
  343. if (0 != (*Open_old_array[format][1]) (Map, update)) { /* cannot open */
  344. if (level >= 2) { /* support files opened */
  345. dig_free_plus(&(Map->plus));
  346. }
  347. if (level_request == 0)
  348. G_fatal_error(_("Unable to open vector map <%s>"),
  349. Vect_get_full_name(Map));
  350. else
  351. G_fatal_error(_("Unable to open vector map <%s> on level %d. "
  352. "Try to rebuild vector topology by v.build."),
  353. Vect_get_full_name(Map), level_request);
  354. return -1;
  355. }
  356. if (ogr_mapset && !head_only && level_request != 1) {
  357. /* build pseudo-topology on the fly */
  358. int verbose;
  359. verbose = G_verbose();
  360. G_message(_("Building topology for OGR layer <%s> from datasource '%s'..."),
  361. Map->fInfo.ogr.layer_name, Map->fInfo.ogr.dsn);
  362. G_set_verbose(0);
  363. if (Vect_build(Map)) {
  364. level = 2;
  365. }
  366. G_set_verbose(verbose);
  367. if (level < level_request)
  368. G_fatal_error(_("Unable to open vector map <%s> on level %d"),
  369. Map->fInfo.ogr.layer_name, level_request);
  370. }
  371. if (level < 2 && Map->head.with_z) {
  372. /* topo has been initialized as 2D, update to 3D */
  373. dig_free_plus(&(Map->plus));
  374. Map->plus.with_z = Map->head.with_z;
  375. dig_init_plus(&(Map->plus));
  376. }
  377. }
  378. else if (level > 1) {
  379. /* take dimension from topo if topo is available */
  380. Map->head.with_z = Map->plus.with_z;
  381. }
  382. /* set status */
  383. Map->open = VECT_OPEN_CODE;
  384. Map->level = level;
  385. Map->head_only = head_only;
  386. Map->support_updated = FALSE;
  387. if (update) {
  388. Map->mode = GV_MODE_RW;
  389. Map->plus.mode = GV_MODE_RW;
  390. }
  391. else {
  392. Map->mode = GV_MODE_READ;
  393. Map->plus.mode = GV_MODE_READ;
  394. }
  395. if (head_only) {
  396. Map->head_only = TRUE;
  397. }
  398. else {
  399. Map->head_only = FALSE;
  400. }
  401. G_debug(1, "Vect_open_old(): vector opened on level %d", level);
  402. if (level == 1) { /* without topology */
  403. Map->plus.built = GV_BUILD_NONE;
  404. }
  405. else { /* level 2, with topology */
  406. Map->plus.built = GV_BUILD_ALL; /* highest level of topology for level 2 */
  407. }
  408. Map->plus.uplist.do_uplist = FALSE;
  409. /* read db links */
  410. Map->dblnk = Vect_new_dblinks_struct();
  411. Vect_read_dblinks(Map);
  412. /* open history file */
  413. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  414. if (update && !ogr_mapset) { /* native only */
  415. Map->hist_fp = G_fopen_modify(buf, GV_HIST_ELEMENT);
  416. if (Map->hist_fp == NULL) {
  417. G_warning(_("Unable to open history file for vector map <%s>"),
  418. Vect_get_full_name(Map));
  419. return -1;
  420. }
  421. G_fseek(Map->hist_fp, (off_t) 0, SEEK_END);
  422. Vect_hist_write(Map,
  423. "---------------------------------------------------------------------------------\n");
  424. }
  425. else {
  426. if (Map->format == GV_FORMAT_NATIVE || Map->format == GV_FORMAT_OGR ||
  427. Map->format == GV_FORMAT_POSTGIS) {
  428. Map->hist_fp = G_fopen_old(buf, GV_HIST_ELEMENT, Map->mapset);
  429. /* If NULL (does not exist) then Vect_hist_read() handle that */
  430. }
  431. else {
  432. Map->hist_fp = NULL;
  433. }
  434. }
  435. if (!head_only) { /* cannot rewind if not fully opened */
  436. Vect_rewind(Map);
  437. }
  438. /* delete support files if native format was opened for update (not head_only) */
  439. if (update && !head_only) {
  440. char file_path[GPATH_MAX];
  441. sprintf(buf, "%s/%s", GV_DIRECTORY, name);
  442. G_file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
  443. if (access(file_path, F_OK) == 0) /* topo file exists? */
  444. unlink(file_path);
  445. G_file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
  446. if (access(file_path, F_OK) == 0) /* sidx file exists? */
  447. unlink(file_path);
  448. G_file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
  449. if (access(file_path, F_OK) == 0) /* cidx file exists? */
  450. unlink(file_path);
  451. if (format == GV_FORMAT_OGR || format == GV_FORMAT_POSTGIS) {
  452. G_file_name(file_path, buf, GV_FIDX_ELEMENT, G_mapset());
  453. if (access(file_path, F_OK) == 0) /* fidx file exists? */
  454. unlink(file_path);
  455. }
  456. }
  457. return level;
  458. }
  459. /*!
  460. \brief Open existing vector map for reading
  461. This function is replaced by Vect_open_old2() to handle also direct
  462. OGR support.
  463. Calls G_fatal_error() on failure.
  464. \param[out] Map pointer to Map_info structure
  465. \param name name of vector map to open
  466. \param mapset mapset name ("" for search path)
  467. \return 1 open on level 1 (without topology)
  468. \return 2 open on level 2 (with topology)
  469. \return -1 on error
  470. */
  471. int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
  472. {
  473. return open_old(Map, name, mapset, NULL, 0, 0);
  474. }
  475. /*!
  476. \brief Open existing vector map for reading
  477. Calls G_fatal_error() on failure.
  478. \param[out] Map pointer to Map_info structure
  479. \param name name of vector map to open (datasource for direct OGR access)
  480. \param mapset mapset name ("" for search path, "OGR" for direct OGR access)
  481. \param layer layer name (OGR layer for direct OGR access)
  482. \return 1 open on level 1 (without topology)
  483. \return 2 open on level 2 (with topology)
  484. \return -1 on error
  485. */
  486. int Vect_open_old2(struct Map_info *Map, const char *name, const char *mapset,
  487. const char *layer)
  488. {
  489. return open_old(Map, name, mapset, layer, 0, 0);
  490. }
  491. /*!
  492. \brief Open existing vector map for reading/writing
  493. This function is replaced by Vect_open_update2() to handle also
  494. direct OGR support.
  495. By default list of updated features is not maintained, see
  496. Vect_set_updated() for details.
  497. Calls G_fatal_error() on failure.
  498. \param[out] Map pointer to Map_info structure
  499. \param name name of vector map to update
  500. \param mapset mapset name
  501. \return 1 open on level 1 (without topology)
  502. \return 2 open on level 2 (with topology)
  503. \return -1 on error
  504. */
  505. int Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
  506. {
  507. return open_old(Map, name, mapset, NULL, 1, 0);
  508. }
  509. /*!
  510. \brief Open existing vector map for reading/writing
  511. By default list of updated features is not maintained, see
  512. Vect_set_updated() for details.
  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_update2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
  523. {
  524. return open_old(Map, name, mapset, layer, 1, 0);
  525. }
  526. /*!
  527. \brief Reads only info about vector map (headers)
  528. Reads from headers of 'head', 'dbln', 'topo' and 'cidx' file.
  529. This function is replaced by Vect_open_old_head2() to handle also
  530. direct OGR support.
  531. Calls G_fatal_error() on failure.
  532. \param[out] Map pointer to Map_info structure
  533. \param name name of vector map to read
  534. \param mapset mapset name ("" for search path)
  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_old_head(struct Map_info *Map, const char *name, const char *mapset)
  540. {
  541. return open_old(Map, name, mapset, NULL, 0, 1);
  542. }
  543. /*!
  544. \brief Reads only info about vector map (headers)
  545. Reads from headers of 'head', 'dbln', 'topo' and 'cidx' file.
  546. Calls G_fatal_error() on failure.
  547. \param[out] Map pointer to Map_info structure
  548. \param name name of vector map to read (dsn for OGR)
  549. \param mapset mapset name ("" for search path)
  550. \param layer layer name (OGR format)
  551. \param[out] Map pointer to Map_info structure
  552. \param name name of vector map to open (datasource for direct OGR access)
  553. \param mapset mapset name ("" for search path, "OGR" for direct OGR access)
  554. \param layer layer name (OGR layer for direct OGR access)
  555. \return 1 open on level 1 (without topology)
  556. \return 2 open on level 2 (with topology)
  557. \return -1 on error
  558. */
  559. int Vect_open_old_head2(struct Map_info *Map, const char *name, const char *mapset,
  560. const char *layer)
  561. {
  562. return open_old(Map, name, mapset, layer, 0, 1);
  563. }
  564. /*! \brief Open header file of existing vector map for updating
  565. (mostly for database link updates)
  566. \param[out] Map pointer to Map_info structure
  567. \param name name of vector map to update
  568. \param mapset mapset name
  569. \return 1 open on level 1 (without topology)
  570. \return 2 open on level 2 (with topology)
  571. \return -1 on error
  572. */
  573. int Vect_open_update_head(struct Map_info *Map, const char *name,
  574. const char *mapset)
  575. {
  576. return open_old(Map, name, mapset, NULL, 1, 1);
  577. }
  578. /*!
  579. \brief Create new vector map for reading/writing
  580. By default list of updated features is not maintained, see
  581. Vect_set_updated() for details.
  582. By default map format is native (GV_FORMAT_NATIVE). If OGR file is
  583. found in the current mapset then the map (ie. OGR layer) is created
  584. in given OGR datasource (GV_FORMAT_OGR). Similarly if PG file exists
  585. then the map (ie. PostGIS table) is created using PostGIS interface
  586. (GV_FORMAT_POSTGIS). The format of map is stored in Map->format.
  587. \param[out] Map pointer to Map_info structure
  588. \param name name of vector map to be created
  589. \param with_z WITH_Z for 3D vector data otherwise WITHOUT_Z
  590. \return 1 on success
  591. \return -1 on error
  592. */
  593. int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
  594. {
  595. int ret;
  596. char xname[GNAME_MAX], xmapset[GMAPSET_MAX], buf[GPATH_MAX];
  597. G_debug(2, "Vect_open_new(): name = %s with_z = %d", name, with_z);
  598. /* zero Map_info structure */
  599. G_zero(Map, sizeof(struct Map_info));
  600. /* init header info */
  601. Vect__init_head(Map);
  602. /* check for fully-qualified map name */
  603. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  604. if (strcmp(xmapset, G_mapset()) != 0) {
  605. G_fatal_error(_("<%s> is not the current mapset (%s)"), name,
  606. G_mapset());
  607. return -1;
  608. }
  609. name = xname;
  610. }
  611. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  612. if (Vect_legal_filename(name) < 0) {
  613. G_fatal_error(_("Vector map name is not SQL compliant"));
  614. return -1;
  615. }
  616. /* store basic info (map@mapset) */
  617. Map->name = G_store(name);
  618. Map->mapset = G_store(G_mapset());
  619. Map->location = G_store(G_location());
  620. Map->gisdbase = G_store(G_gisdbase());
  621. /* determine output format */
  622. if (strcmp(G_program_name(), "v.external") != 0)
  623. Map->format = map_format(Map);
  624. else
  625. Map->format = GV_FORMAT_NATIVE;
  626. if (Map->format != GV_FORMAT_OGR_DIRECT &&
  627. getenv("GRASS_VECTOR_PGFILE") == NULL) { /* GRASS_VECTOR_PGFILE defined by v.out.postgis */
  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. if (getenv("GRASS_VECTOR_PGFILE") == NULL) /* GRASS_VECTOR_PGFILE defined by v.out.postgis */
  660. Vect_delete(name); /* clean up */
  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. if (mode == 0) {
  875. /* free old indices */
  876. dig_spidx_free(Plus);
  877. /* initialize file based indices */
  878. Plus->Spidx_file = 1;
  879. dig_spidx_init(Plus);
  880. }
  881. /* load head */
  882. if (dig_Rd_spidx_head(&(Plus->spidx_fp), Plus) == -1) {
  883. fclose(Plus->spidx_fp.file);
  884. return -1;
  885. }
  886. G_debug(1, "Sidx head: coor size = %lu, coor mtime = %ld",
  887. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  888. /* do checks */
  889. err = 0;
  890. if (CInfo.size != Plus->coor_size) {
  891. G_warning(_("Size of 'coor' file differs from value saved in sidx file"));
  892. err = 1;
  893. }
  894. /* Do not check mtime because mtime is changed by copy */
  895. /*
  896. if ( CInfo.mtime != Plus->coor_mtime ) {
  897. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  898. err = 1;
  899. }
  900. */
  901. if (err) {
  902. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  903. Map->name, Map->mapset);
  904. fclose(Plus->spidx_fp.file);
  905. return -1;
  906. }
  907. }
  908. if (mode) {
  909. /* open new spatial index */
  910. Plus->Spidx_new = TRUE;
  911. /* file based or memory based */
  912. if (getenv("GRASS_VECTOR_LOWMEM")) {
  913. /* free old indices */
  914. dig_spidx_free(Plus);
  915. /* initialize file based indices */
  916. Plus->Spidx_file = 1;
  917. dig_spidx_init(Plus);
  918. }
  919. G_debug(1, "%s based spatial index",
  920. Plus->Spidx_file == 0 ? "Memory" : "File");
  921. if (mode == 1) {
  922. /* load spatial index for update */
  923. if (dig_Rd_spidx(&(Plus->spidx_fp), Plus) == -1) {
  924. fclose(Plus->spidx_fp.file);
  925. return -1;
  926. }
  927. }
  928. }
  929. Plus->Spidx_built = TRUE;
  930. return 0;
  931. }
  932. /* check for external formats definition */
  933. int map_format(struct Map_info *Map)
  934. {
  935. char *def_file;
  936. if (G_find_file2("", "OGR", G_mapset())) {
  937. /* OGR */
  938. FILE *fp;
  939. const char *p;
  940. struct Key_Value *key_val;
  941. struct Format_info_ogr *ogr_info;
  942. G_debug(2, " using OGR format");
  943. Map->format = GV_FORMAT_OGR_DIRECT;
  944. fp = G_fopen_old("", "OGR", G_mapset());
  945. if (!fp) {
  946. G_fatal_error(_("Unable to open OGR file"));
  947. }
  948. key_val = G_fread_key_value(fp);
  949. fclose(fp);
  950. ogr_info = &(Map->fInfo.ogr);
  951. /* format */
  952. p = G_find_key_value("format", key_val);
  953. if (p)
  954. ogr_info->driver_name = G_store(p);
  955. /* dsn */
  956. p = G_find_key_value("dsn", key_val);
  957. if (p)
  958. ogr_info->dsn = G_store(p);
  959. /* options */
  960. p = G_find_key_value("options", key_val);
  961. if (p)
  962. ogr_info->layer_options = G_tokenize(p, ",");
  963. ogr_info->layer_name = G_store(Map->name);
  964. }
  965. def_file = getenv("GRASS_VECTOR_PGFILE"); /* GRASS_VECTOR_PGFILE defined by v.out.postgis */
  966. if (G_find_file2("", def_file ? def_file : "PG", G_mapset())) {
  967. /* PostGIS */
  968. if (Map->fInfo.ogr.driver_name) {
  969. G_warning(_("OGR output also detected, using OGR"));
  970. }
  971. else {
  972. FILE *fp;
  973. const char *p;
  974. struct Key_Value *key_val;
  975. struct Format_info_pg *pg_info;
  976. G_debug(2, " using PostGIS format");
  977. Map->format = GV_FORMAT_POSTGIS;
  978. fp = G_fopen_old("", def_file ? def_file : "PG", G_mapset());
  979. if (!fp) {
  980. G_fatal_error(_("Unable to open PG file"));
  981. }
  982. key_val = G_fread_key_value(fp);
  983. fclose(fp);
  984. pg_info = &(Map->fInfo.pg);
  985. /* conninfo */
  986. p = G_find_key_value("conninfo", key_val);
  987. if (p) {
  988. pg_info->conninfo = G_store(p);
  989. G_debug(1, "PG: conninfo = '%s'", pg_info->conninfo);
  990. }
  991. /* schema (default: public) */
  992. p = G_find_key_value("schema", key_val);
  993. if (p)
  994. pg_info->schema_name = G_store(p);
  995. else
  996. pg_info->schema_name = G_store("public");
  997. G_debug(1, "PG: schema_name = '%s'", pg_info->schema_name);
  998. /* fid column (default: FID_COLUMN) */
  999. p = G_find_key_value("fid", key_val);
  1000. if (p)
  1001. pg_info->fid_column = G_store(p);
  1002. else
  1003. #ifdef HAVE_POSTGRES
  1004. pg_info->fid_column = G_store(GV_PG_FID_COLUMN);
  1005. #endif
  1006. G_debug(1, "PG: fid_column = '%s'", pg_info->fid_column);
  1007. /* geometry column (default: GEOMETRY_COLUMN) */
  1008. p = G_find_key_value("geometry_name", key_val);
  1009. if (p)
  1010. pg_info->geom_column = G_store(p);
  1011. else
  1012. #ifdef HAVE_POSTGRES
  1013. pg_info->geom_column = G_store(GV_PG_GEOMETRY_COLUMN);
  1014. #endif
  1015. G_debug(1, "PG: geom_column = '%s'", pg_info->geom_column);
  1016. /* srid (default: 0) */
  1017. p = G_find_key_value("srid", key_val);
  1018. if (p)
  1019. pg_info->srid = atoi(p);
  1020. G_debug(1, "PG: srid = %d", pg_info->srid);
  1021. /* table name */
  1022. Map->fInfo.pg.table_name = G_store(Map->name);
  1023. }
  1024. }
  1025. return Map->format;
  1026. }