open.c 36 KB

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