open.c 36 KB

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