open.c 27 KB

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