open.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. dig_fseek(&(Map->dig_fp), 0L, SEEK_END);
  694. G_debug(2, "dig_ftell = %d", dig_ftell(&(Map->dig_fp)));
  695. Info->size = dig_ftell(&(Map->dig_fp));
  696. Info->mtime = 0L;
  697. break;
  698. case GV_FORMAT_OGR:
  699. case GV_FORMAT_OGR_DIRECT:
  700. Info->size = 0L;
  701. Info->mtime = 0L;
  702. break;
  703. }
  704. G_debug(1, "Info->size = %lu, Info->mtime = %ld",
  705. (unsigned long)Info->size, Info->mtime);
  706. return 1;
  707. }
  708. /*!
  709. * \brief Gets vector map format (as string)
  710. *
  711. * Note: string is allocated by G_store(). Free allocated memory with
  712. * G_free().
  713. *
  714. * Currently are implemeted:
  715. * - Native format (native)
  716. * - OGR format (ogr)
  717. *
  718. * \param Map pointer to Map_info structure
  719. *
  720. * \return maptype string on success
  721. * \return error message on error
  722. */
  723. const char *Vect_maptype_info(const struct Map_info *Map)
  724. {
  725. char maptype[1000];
  726. switch (Map->format) {
  727. case GV_FORMAT_NATIVE:
  728. sprintf(maptype, "native");
  729. break;
  730. case GV_FORMAT_OGR:
  731. case GV_FORMAT_OGR_DIRECT:
  732. sprintf(maptype, "ogr");
  733. break;
  734. default:
  735. sprintf(maptype, _("unknown %d (update Vect_maptype_info)"),
  736. Map->format);
  737. }
  738. return G_store(maptype);
  739. }
  740. /*!
  741. \brief Gets vector map format
  742. Currently are implemeted:
  743. - Native format (GV_FORMAT_NATIVE)
  744. - OGR format (GV_FORMAT_OGR)
  745. \param Map pointer to Map_info structure
  746. \return maptype code
  747. */
  748. int Vect_maptype(const struct Map_info *Map)
  749. {
  750. return Map->format;
  751. }
  752. /*!
  753. * \brief Open topology file ('topo')
  754. *
  755. * \param[in,out] Map pointer to Map_info structure
  756. * \param head_only open only head
  757. *
  758. * \return 0 on success
  759. * \return 1 file does not exist
  760. * \return -1 on error
  761. */
  762. int Vect_open_topo(struct Map_info *Map, int head_only)
  763. {
  764. int err, ret;
  765. char buf[500], file_path[2000];
  766. struct gvfile fp;
  767. struct Coor_info CInfo;
  768. struct Plus_head *Plus;
  769. struct stat info;
  770. G_debug(1, "Vect_open_topo(): name = %s mapset= %s", Map->name,
  771. Map->mapset);
  772. Plus = &(Map->plus);
  773. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  774. G__file_name(file_path, buf, GV_TOPO_ELEMENT, Map->mapset);
  775. if (stat(file_path, &info) != 0) /* does not exist */
  776. return 1;
  777. dig_file_init(&fp);
  778. fp.file = G_fopen_old(buf, GV_TOPO_ELEMENT, Map->mapset);
  779. if (fp.file == NULL) { /* topo file is not available */
  780. G_debug(1, "Cannot open topo file for vector '%s@%s'.",
  781. Map->name, Map->mapset);
  782. return -1;
  783. }
  784. /* get coor info */
  785. Vect_coor_info(Map, &CInfo);
  786. /* load head */
  787. if (dig_Rd_Plus_head(&fp, Plus) == -1)
  788. return -1;
  789. G_debug(1, "Topo head: coor size = %lu, coor mtime = %ld",
  790. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  791. /* do checks */
  792. err = 0;
  793. if (CInfo.size != Plus->coor_size) {
  794. G_warning(_("Size of 'coor' file differs from value saved in topology file"));
  795. err = 1;
  796. }
  797. /* Do not check mtime because mtime is changed by copy */
  798. /*
  799. if ( CInfo.mtime != Plus->coor_mtime ) {
  800. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  801. err = 1;
  802. }
  803. */
  804. if (err) {
  805. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  806. Map->name, Map->mapset);
  807. return -1;
  808. }
  809. /* load file to the memory */
  810. /* dig_file_load ( &fp); */
  811. /* load topo to memory */
  812. ret = dig_load_plus(Plus, &fp, head_only);
  813. fclose(fp.file);
  814. /* dig_file_free ( &fp); */
  815. if (ret == 0)
  816. return -1;
  817. return 0;
  818. }
  819. /*!
  820. * \brief Open spatial index file ('sidx')
  821. *
  822. * \param[in,out] Map pointer to Map_info
  823. * \param mode 0 old, 1 update, 2 new
  824. *
  825. * \return 0 on success
  826. * \return -1 on error
  827. */
  828. int Vect_open_sidx(struct Map_info *Map, int mode)
  829. {
  830. char buf[500], file_path[2000];
  831. int err;
  832. struct Coor_info CInfo;
  833. struct Plus_head *Plus;
  834. struct stat info;
  835. G_debug(1, "Vect_open_sidx(): name = %s mapset= %s mode = %s", Map->name,
  836. Map->mapset, mode == 0 ? "old" : (mode == 1 ? "update" : "new"));
  837. if (Map->plus.Spidx_built == 1) {
  838. G_warning("Spatial index already opened");
  839. return 0;
  840. }
  841. Plus = &(Map->plus);
  842. dig_file_init(&(Map->plus.spidx_fp));
  843. if (mode < 2) {
  844. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  845. G__file_name(file_path, buf, GV_SIDX_ELEMENT, Map->mapset);
  846. if (stat(file_path, &info) != 0) /* does not exist */
  847. return 1;
  848. Map->plus.spidx_fp.file =
  849. G_fopen_old(buf, GV_SIDX_ELEMENT, Map->mapset);
  850. if (Map->plus.spidx_fp.file == NULL) { /* sidx file is not available */
  851. G_debug(1, "Cannot open spatial index file for vector '%s@%s'.",
  852. Map->name, Map->mapset);
  853. return -1;
  854. }
  855. /* get coor info */
  856. Vect_coor_info(Map, &CInfo);
  857. /* initialize spatial index */
  858. Map->plus.Spidx_new = 0;
  859. /* load head */
  860. if (dig_Rd_spidx_head(&(Map->plus.spidx_fp), Plus) == -1) {
  861. fclose(Map->plus.spidx_fp.file);
  862. return -1;
  863. }
  864. G_debug(1, "Sidx head: coor size = %lu, coor mtime = %ld",
  865. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  866. /* do checks */
  867. err = 0;
  868. if (CInfo.size != Plus->coor_size) {
  869. G_warning(_("Size of 'coor' file differs from value saved in sidx file"));
  870. err = 1;
  871. }
  872. /* Do not check mtime because mtime is changed by copy */
  873. /*
  874. if ( CInfo.mtime != Plus->coor_mtime ) {
  875. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  876. err = 1;
  877. }
  878. */
  879. if (err) {
  880. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  881. Map->name, Map->mapset);
  882. fclose(Map->plus.spidx_fp.file);
  883. return -1;
  884. }
  885. }
  886. if (mode) {
  887. /* open new spatial index */
  888. Map->plus.Spidx_new = 1;
  889. if (mode == 1) {
  890. /* load spatial index for update */
  891. if (dig_Rd_spidx(&(Map->plus.spidx_fp), Plus) == -1) {
  892. fclose(Map->plus.spidx_fp.file);
  893. return -1;
  894. }
  895. }
  896. }
  897. Map->plus.Spidx_built = 1;
  898. return 0;
  899. }