open.c 29 KB

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