open.c 29 KB

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