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. sprintf(buf, "%s/%s", GV_DIRECTORY, name);
  416. G__file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
  417. if (access(file_path, F_OK) == 0) /* file exists? */
  418. unlink(file_path);
  419. G__file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
  420. if (access(file_path, F_OK) == 0) /* file exists? */
  421. unlink(file_path);
  422. G__file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
  423. if (access(file_path, F_OK) == 0) /* file exists? */
  424. unlink(file_path);
  425. }
  426. return (level);
  427. }
  428. /*!
  429. * \brief Open existing vector map for reading (native or OGR format
  430. * via v.external)
  431. *
  432. * This function is replaced by Vect_open_old2() to handle also direct
  433. * OGR support.
  434. *
  435. * In case of error, the functions respect fatal error settings.
  436. *
  437. * \param[out] Map pointer to Map_info structure
  438. * \param name name of vector map to open
  439. * \param mapset mapset name
  440. *
  441. * \return level of openness [1, 2, (3)]
  442. * \return -1 on error
  443. */
  444. int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
  445. {
  446. return (Vect__open_old(Map, name, mapset, NULL, 0, 0));
  447. }
  448. /*!
  449. * \brief Open existing vector map for reading (native and OGR format)
  450. *
  451. * In case of error, the functions respect fatal error settings.
  452. *
  453. * \param[out] Map pointer to Map_info structure
  454. * \param name name of vector map to open
  455. * \param mapset mapset name
  456. * \param layer layer name (OGR format)
  457. *
  458. * \return level of openness [1, 2, (3)]
  459. * \return -1 on error
  460. */
  461. int Vect_open_old2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
  462. {
  463. return (Vect__open_old(Map, name, mapset, layer, 0, 0));
  464. }
  465. /*!
  466. * \brief Open existing vector map for reading/writing (native or OGR
  467. * format via v.external)
  468. *
  469. * This function is replaced by Vect_open_update2() to handle also
  470. * direct OGR support.
  471. *
  472. * In case of error, the functions respect fatal error settings.
  473. *
  474. * \param[out] Map pointer to Map_info structure
  475. * \param name name of vector map to update
  476. * \param mapset mapset name
  477. *
  478. * \return level of openness [1, 2, (3)]
  479. * \return -1 on error
  480. */
  481. int Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
  482. {
  483. int ret;
  484. ret = Vect__open_old(Map, name, mapset, NULL, 1, 0);
  485. /* the update lists are unused, a waste of time and memory */
  486. /*
  487. if (ret > 0) {
  488. Map->plus.do_uplist = 1;
  489. Map->plus.uplines = NULL;
  490. Map->plus.n_uplines = 0;
  491. Map->plus.alloc_uplines = 0;
  492. Map->plus.upnodes = NULL;
  493. Map->plus.n_upnodes = 0;
  494. Map->plus.alloc_upnodes = 0;
  495. }
  496. */
  497. return ret;
  498. }
  499. /*!
  500. * \brief Open existing vector map for reading/writing (native or OGR
  501. * format)
  502. *
  503. * In case of error, the functions respect fatal error settings.
  504. *
  505. * \param[out] Map pointer to Map_info structure
  506. * \param name name of vector map to update
  507. * \param mapset mapset name
  508. * \param layer layer name (OGR format)
  509. *
  510. * \return level of openness [1, 2, (3)]
  511. * \return -1 on error
  512. */
  513. int Vect_open_update2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
  514. {
  515. int ret;
  516. ret = Vect__open_old(Map, name, mapset, layer, 1, 0);
  517. return ret;
  518. }
  519. /*!
  520. * \brief Reads only info about vector map from headers of 'head',
  521. * 'dbln', 'topo' and 'cidx' file (native or OGR format via
  522. * v.external)
  523. *
  524. * This function is replaced by Vect_open_old_head2() to handle also
  525. * direct OGR support.
  526. *
  527. * In case of error, the functions respect fatal error settings.
  528. *
  529. * \param[out] Map pointer to Map_info structure
  530. * \param name name of vector map to read (dsn for OGR)
  531. * \param mapset mapset name ("" for search path)
  532. *
  533. * \return level of openness [1, 2, (3)]
  534. * \return -1 on error
  535. */
  536. int Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
  537. {
  538. return (Vect__open_old(Map, name, mapset, NULL, 0, 1));
  539. }
  540. /*!
  541. * \brief Reads only info about vector map from headers of 'head',
  542. * 'dbln', 'topo' and 'cidx' file (native or OGR format)
  543. *
  544. * In case of error, the functions respect fatal error settings.
  545. *
  546. * \param[out] Map pointer to Map_info structure
  547. * \param name name of vector map to read (dsn for OGR)
  548. * \param mapset mapset name ("" for search path)
  549. * \param layer layer name (OGR format)
  550. *
  551. * \return level of openness [1, 2, (3)]
  552. * \return -1 on error
  553. */
  554. int Vect_open_old_head2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
  555. {
  556. return (Vect__open_old(Map, name, mapset, layer, 0, 1));
  557. }
  558. /*!
  559. * \brief Open header file of existing vector map for updating (mostly
  560. * for database link updates)
  561. *
  562. * In case of error, the functions respect fatal error settings.
  563. *
  564. * \param[out] Map pointer to Map_info structure
  565. * \param name name of vector map to update
  566. * \param mapset mapset name
  567. *
  568. * \return level of openness [1, 2, (3)]
  569. * \return -1 on error
  570. */
  571. int Vect_open_update_head(struct Map_info *Map, const char *name,
  572. const char *mapset)
  573. {
  574. int ret;
  575. ret = Vect__open_old(Map, name, mapset, NULL, 1, 1);
  576. /* the update lists are unused, a waste of time and memory */
  577. /*
  578. if (ret > 0) {
  579. Map->plus.do_uplist = 1;
  580. Map->plus.uplines = NULL;
  581. Map->plus.n_uplines = 0;
  582. Map->plus.alloc_uplines = 0;
  583. Map->plus.upnodes = NULL;
  584. Map->plus.n_upnodes = 0;
  585. Map->plus.alloc_upnodes = 0;
  586. }
  587. */
  588. return ret;
  589. }
  590. /*!
  591. * \brief Create new vector map for reading/writing
  592. *
  593. * \param[in,out] Map pointer to Map_info structure
  594. * \param name name of vector map
  595. * \param with_z non-zero value for 3D vector data
  596. *
  597. * \return 1 on success
  598. * \return -1 on error
  599. */
  600. int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
  601. {
  602. int ret, ferror;
  603. char errmsg[2000], buf[500];
  604. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  605. G_debug(2, "Vect_open_new(): name = %s", name);
  606. /* init header */
  607. Vect__init_head(Map);
  608. /* error handling */
  609. ferror = Vect_get_fatal_error();
  610. Vect_set_fatal_error(GV_FATAL_EXIT);
  611. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  612. if (strcmp(xmapset, G_mapset()) != 0) {
  613. sprintf(errmsg, _("%s is not in the current mapset (%s)"), name,
  614. G_mapset());
  615. fatal_error(ferror, errmsg);
  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. sprintf(errmsg, _("Vector map name is not SQL compliant"));
  622. fatal_error(ferror, errmsg);
  623. return (-1);
  624. }
  625. /* Check if map already exists */
  626. if (G_find_vector2(name, G_mapset()) != NULL) {
  627. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  628. name);
  629. ret = Vect_delete(name);
  630. if (ret == -1) {
  631. sprintf(errmsg, _("Unable to delete vector map <%s>"), name);
  632. fatal_error(ferror, errmsg);
  633. return (-1);
  634. }
  635. }
  636. Map->name = G_store(name);
  637. Map->mapset = G_store(G_mapset());
  638. Map->location = G_store(G_location());
  639. Map->gisdbase = G_store(G_gisdbase());
  640. Map->format = GV_FORMAT_NATIVE;
  641. /* set 2D/3D */
  642. Map->plus.spidx_with_z = Map->plus.with_z = Map->head.with_z = (with_z != 0);
  643. if (V1_open_new_nat(Map, name, with_z) < 0) {
  644. sprintf(errmsg, _("Unable to create vector map <%s>"),
  645. Vect_get_full_name(Map));
  646. fatal_error(ferror, errmsg);
  647. return (-1);
  648. }
  649. /* Open history file */
  650. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  651. Map->hist_fp = G_fopen_new(buf, GV_HIST_ELEMENT);
  652. if (Map->hist_fp == NULL) {
  653. sprintf(errmsg, _("Unable to open history file of vector map <%s>"),
  654. Vect_get_full_name(Map));
  655. fatal_error(ferror, errmsg);
  656. return (-1);
  657. }
  658. Open_level = 0;
  659. /* initialize topo */
  660. dig_init_plus(&(Map->plus));
  661. /* open new spatial index */
  662. Vect_open_sidx(Map, 2);
  663. Map->open = VECT_OPEN_CODE;
  664. Map->level = 1;
  665. Map->head_only = 0;
  666. Map->support_updated = 0;
  667. Map->plus.built = GV_BUILD_NONE;
  668. Map->mode = GV_MODE_RW;
  669. Map->Constraint_region_flag = 0;
  670. Map->Constraint_type_flag = 0;
  671. Map->plus.do_uplist = 0;
  672. Vect_set_proj(Map, G_projection());
  673. Vect_set_zone(Map, G_zone());
  674. Map->dblnk = Vect_new_dblinks_struct();
  675. return 1;
  676. }
  677. /*!
  678. * \brief Update Coor_info structure
  679. *
  680. * \param Map pointer to Map_info structure
  681. * \param[out] Info pointer to Coor_info structure
  682. *
  683. * \return 1 on success
  684. * \return 0 on error
  685. */
  686. int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info)
  687. {
  688. char buf[2000], path[2000];
  689. STRUCT_STAT stat_buf;
  690. switch (Map->format) {
  691. case GV_FORMAT_NATIVE:
  692. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  693. G__file_name(path, buf, GV_COOR_ELEMENT, Map->mapset);
  694. G_debug(1, "get coor info: %s", path);
  695. if (0 != stat(path, &stat_buf)) {
  696. G_warning(_("Unable to stat file <%s>"), path);
  697. Info->size = -1L;
  698. Info->mtime = -1L;
  699. }
  700. else {
  701. Info->size = (off_t)stat_buf.st_size; /* file size */
  702. Info->mtime = (long)stat_buf.st_mtime; /* last modified time */
  703. }
  704. /* stat does not give correct size on MINGW
  705. * if the file is opened */
  706. #ifdef __MINGW32__
  707. if (Map->open == VECT_OPEN_CODE) {
  708. dig_fseek(&(Map->dig_fp), 0L, SEEK_END);
  709. G_debug(2, "dig_ftell = %d", dig_ftell(&(Map->dig_fp)));
  710. Info->size = dig_ftell(&(Map->dig_fp));
  711. }
  712. #endif
  713. break;
  714. case GV_FORMAT_OGR:
  715. case GV_FORMAT_OGR_DIRECT:
  716. Info->size = 0L;
  717. Info->mtime = 0L;
  718. break;
  719. }
  720. G_debug(1, "Info->size = %lu, Info->mtime = %ld",
  721. (unsigned long)Info->size, Info->mtime);
  722. return 1;
  723. }
  724. /*!
  725. * \brief Gets vector map format (as string)
  726. *
  727. * Note: string is allocated by G_store(). Free allocated memory with
  728. * G_free().
  729. *
  730. * Currently are implemeted:
  731. * - Native format (native)
  732. * - OGR format (ogr)
  733. *
  734. * \param Map pointer to Map_info structure
  735. *
  736. * \return maptype string on success
  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. default:
  751. sprintf(maptype, _("unknown %d (update Vect_maptype_info)"),
  752. Map->format);
  753. }
  754. return G_store(maptype);
  755. }
  756. /*!
  757. \brief Gets vector map format
  758. Currently are implemeted:
  759. - Native format (GV_FORMAT_NATIVE)
  760. - OGR format (GV_FORMAT_OGR)
  761. \param Map pointer to Map_info structure
  762. \return maptype code
  763. */
  764. int Vect_maptype(const struct Map_info *Map)
  765. {
  766. return Map->format;
  767. }
  768. /*!
  769. * \brief Open topology file ('topo')
  770. *
  771. * \param[in,out] Map pointer to Map_info structure
  772. * \param head_only open only head
  773. *
  774. * \return 0 on success
  775. * \return 1 file does not exist
  776. * \return -1 on error
  777. */
  778. int Vect_open_topo(struct Map_info *Map, int head_only)
  779. {
  780. int err, ret;
  781. char buf[500], file_path[2000];
  782. struct gvfile fp;
  783. struct Coor_info CInfo;
  784. struct Plus_head *Plus;
  785. G_debug(1, "Vect_open_topo(): name = %s mapset= %s", Map->name,
  786. Map->mapset);
  787. Plus = &(Map->plus);
  788. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  789. G__file_name(file_path, buf, GV_TOPO_ELEMENT, Map->mapset);
  790. if (access(file_path, F_OK) != 0) /* does not exist */
  791. return 1;
  792. dig_file_init(&fp);
  793. fp.file = G_fopen_old(buf, GV_TOPO_ELEMENT, Map->mapset);
  794. if (fp.file == NULL) { /* topo file is not available */
  795. G_debug(1, "Cannot open topo file for vector '%s@%s'.",
  796. Map->name, Map->mapset);
  797. return -1;
  798. }
  799. /* get coor info */
  800. /* NOTE: coor file not yet opened */
  801. Vect_coor_info(Map, &CInfo);
  802. /* load head */
  803. if (dig_Rd_Plus_head(&fp, Plus) == -1)
  804. return -1;
  805. G_debug(1, "Topo head: coor size = %lu, coor mtime = %ld",
  806. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  807. /* do checks */
  808. err = 0;
  809. if (CInfo.size != Plus->coor_size) {
  810. G_warning(_("Size of 'coor' file differs from value saved in topology file"));
  811. err = 1;
  812. }
  813. /* Do not check mtime because mtime is changed by copy */
  814. /*
  815. if ( CInfo.mtime != Plus->coor_mtime ) {
  816. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  817. err = 1;
  818. }
  819. */
  820. if (err) {
  821. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  822. Map->name, Map->mapset);
  823. return -1;
  824. }
  825. /* load file to the memory */
  826. /* dig_file_load ( &fp); */
  827. /* load topo to memory */
  828. ret = dig_load_plus(Plus, &fp, head_only);
  829. fclose(fp.file);
  830. /* dig_file_free ( &fp); */
  831. if (ret == 0)
  832. return -1;
  833. return 0;
  834. }
  835. /*!
  836. * \brief Open spatial index file ('sidx')
  837. *
  838. * \param[in,out] Map pointer to Map_info
  839. * \param mode 0 old, 1 update, 2 new
  840. *
  841. * \return 0 on success
  842. * \return -1 on error
  843. */
  844. int Vect_open_sidx(struct Map_info *Map, int mode)
  845. {
  846. char buf[500], file_path[2000];
  847. int err;
  848. struct Coor_info CInfo;
  849. struct Plus_head *Plus;
  850. G_debug(1, "Vect_open_sidx(): name = %s mapset= %s mode = %s", Map->name,
  851. Map->mapset, mode == 0 ? "old" : (mode == 1 ? "update" : "new"));
  852. if (Map->plus.Spidx_built == 1) {
  853. G_warning("Spatial index already opened");
  854. return 0;
  855. }
  856. Plus = &(Map->plus);
  857. dig_file_init(&(Map->plus.spidx_fp));
  858. if (mode < 2) {
  859. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  860. G__file_name(file_path, buf, GV_SIDX_ELEMENT, Map->mapset);
  861. if (access(file_path, F_OK) != 0) /* does not exist */
  862. return 1;
  863. Map->plus.spidx_fp.file =
  864. G_fopen_old(buf, GV_SIDX_ELEMENT, Map->mapset);
  865. if (Map->plus.spidx_fp.file == NULL) { /* sidx file is not available */
  866. G_debug(1, "Cannot open spatial index file for vector '%s@%s'.",
  867. Map->name, Map->mapset);
  868. return -1;
  869. }
  870. /* get coor info */
  871. /* NOTE: coor file not yet opened */
  872. Vect_coor_info(Map, &CInfo);
  873. /* initialize spatial index */
  874. Map->plus.Spidx_new = 0;
  875. /* load head */
  876. if (dig_Rd_spidx_head(&(Map->plus.spidx_fp), Plus) == -1) {
  877. fclose(Map->plus.spidx_fp.file);
  878. return -1;
  879. }
  880. G_debug(1, "Sidx head: coor size = %lu, coor mtime = %ld",
  881. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  882. /* do checks */
  883. err = 0;
  884. if (CInfo.size != Plus->coor_size) {
  885. G_warning(_("Size of 'coor' file differs from value saved in sidx file"));
  886. err = 1;
  887. }
  888. /* Do not check mtime because mtime is changed by copy */
  889. /*
  890. if ( CInfo.mtime != Plus->coor_mtime ) {
  891. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  892. err = 1;
  893. }
  894. */
  895. if (err) {
  896. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  897. Map->name, Map->mapset);
  898. fclose(Map->plus.spidx_fp.file);
  899. return -1;
  900. }
  901. }
  902. if (mode) {
  903. /* open new spatial index */
  904. Map->plus.Spidx_new = 1;
  905. if (mode == 1) {
  906. /* load spatial index for update */
  907. if (dig_Rd_spidx(&(Map->plus.spidx_fp), Plus) == -1) {
  908. fclose(Map->plus.spidx_fp.file);
  909. return -1;
  910. }
  911. }
  912. }
  913. Map->plus.Spidx_built = 1;
  914. return 0;
  915. }