field.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*!
  2. \file vector/Vlib/field.c
  3. \brief Vector library - field (layer) related fns.
  4. Higher level functions for reading/writing/manipulating vectors.
  5. \todo see Vect_read_dblinks(); activate auto-FID detection once
  6. OGR_L_GetFIDColumn() is working or solution found if FID not
  7. available
  8. (C) 2001-2009 by the GRASS Development Team
  9. This program is free software under the GNU General Public License
  10. (>=v2). Read the file COPYING that comes with GRASS for details.
  11. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  12. \author Update to GRASS 5.7 by Radim Blazek and David D. Gray.
  13. \author Various updates by Martin Landa <landa.martin gmail.com>, 2009
  14. */
  15. #include <grass/config.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <grass/glocale.h>
  20. #include <grass/gis.h>
  21. #include <grass/dbmi.h>
  22. #include <grass/vector.h>
  23. #ifdef HAVE_GDAL
  24. #include <gdal_version.h> /* needed for FID detection */
  25. #endif /* HAVE_GDAL */
  26. #ifdef HAVE_OGR
  27. #include <ogr_api.h>
  28. #endif
  29. /*!
  30. \brief Create and init new dblinks structure
  31. \return pointer to new dblinks structure
  32. */
  33. struct dblinks *Vect_new_dblinks_struct(void)
  34. {
  35. struct dblinks *p;
  36. p = (struct dblinks *)G_malloc(sizeof(struct dblinks));
  37. if (p) {
  38. p->alloc_fields = p->n_fields = 0;
  39. p->field = NULL;
  40. }
  41. return p;
  42. }
  43. /*!
  44. \brief Reset dblinks structure
  45. \param p pointer to existing dblinks structure
  46. */
  47. void Vect_reset_dblinks(struct dblinks *p)
  48. {
  49. p->n_fields = 0;
  50. }
  51. /*!
  52. \brief Add new db connection to Map_info structure
  53. \param Map pointer to Map_info structure
  54. \param number layer number
  55. \param name layer name (if not given use table name)
  56. \param table table name
  57. \param key key name
  58. \param db database name
  59. \param driver driver name
  60. \return 0 on success
  61. \return -1 on failure
  62. */
  63. int Vect_map_add_dblink(struct Map_info *Map, int number, const char *name,
  64. const char *table, const char *key, const char *db,
  65. const char *driver)
  66. {
  67. int ret;
  68. if (number < 1) {
  69. G_warning(_("Layer number must be 1 or greater"));
  70. return -1;
  71. }
  72. if (Map->mode != GV_MODE_WRITE && Map->mode != GV_MODE_RW) {
  73. G_warning(_("Unable to add attribute link, vector map is "
  74. "not opened in WRITE mode"));
  75. return -1;
  76. }
  77. ret = Vect_add_dblink(Map->dblnk, number, name, table, key, db, driver);
  78. if (ret == -1) {
  79. G_warning(_("Unable to add attribute link"));
  80. return -1;
  81. }
  82. /* write it immediately otherwise it is lost if module crashes */
  83. ret = Vect_write_dblinks(Map);
  84. if (ret == -1) {
  85. G_warning(_("Unable to write attribute links"));
  86. return -1;
  87. }
  88. return 0;
  89. }
  90. /*!
  91. \brief Delete db connection from Map_info structure
  92. \param Map pointer to Map_info structure
  93. \param field layer number
  94. \return 0 deleted
  95. \return -1 error
  96. */
  97. int Vect_map_del_dblink(struct Map_info *Map, int field)
  98. {
  99. int i, j, ret;
  100. struct dblinks *links;
  101. G_debug(4, "Vect_map_del_dblink() field = %d", field);
  102. links = Map->dblnk;
  103. ret = -1;
  104. for (i = 0; i < links->n_fields; i++) {
  105. if (links->field[i].number == field) { /* field found */
  106. for (j = i; j < links->n_fields - 1; j++) {
  107. links->field[j].number = links->field[j + 1].number;
  108. links->field[j].name = links->field[j + 1].name;
  109. links->field[j].table = links->field[j + 1].table;
  110. links->field[j].key = links->field[j + 1].key;
  111. links->field[j].database = links->field[j + 1].database;
  112. links->field[j].driver = links->field[j + 1].driver;
  113. }
  114. ret = 0;
  115. links->n_fields--;
  116. }
  117. }
  118. if (ret == -1)
  119. return -1;
  120. /* write it immediately otherwise it is lost if module crashes */
  121. ret = Vect_write_dblinks(Map);
  122. if (ret == -1) {
  123. G_warning(_("Unable to write database links"));
  124. return -1;
  125. }
  126. return 0;
  127. }
  128. /*!
  129. \brief Check if DB connection exists in dblinks structure
  130. \param Map pointer to Map_info structure
  131. \param field layer number
  132. \return 1 dblink for field exists
  133. \return 0 dblink does not exist for field
  134. */
  135. int Vect_map_check_dblink(const struct Map_info *Map, int field, const char *name)
  136. {
  137. return Vect_check_dblink(Map->dblnk, field, name);
  138. }
  139. /*!
  140. \brief Check if DB connection exists in dblinks structure
  141. \param p pointer to existing dblinks structure
  142. \param field layer number
  143. \return 1 dblink for field exists
  144. \return 0 dblink does not exist for field
  145. */
  146. int Vect_check_dblink(const struct dblinks *p, int field, const char *name)
  147. {
  148. int i;
  149. G_debug(3, "Vect_check_dblink: field %d, name %s", field, (name != NULL ? name : "not given"));
  150. for (i = 0; i < p->n_fields; i++) {
  151. if (p->field[i].number == field) {
  152. return 1;
  153. }
  154. if (name != NULL && p->field[i].name != NULL) {
  155. if (strcmp(p->field[i].name, name) == 0)
  156. return 1;
  157. }
  158. }
  159. return 0;
  160. }
  161. /*!
  162. \brief Add new DB connection to dblinks structure
  163. \param[in,out] p pointer to existing dblinks structure
  164. \param number layer number (1 for OGR)
  165. \param name layer name (layer for OGR) - if not given use table name
  166. \param table table name (layer for OGR)
  167. \param key key name
  168. \param db database name (datasource for OGR)
  169. \param driver driver name (dbf, postgresql, ogr, ...)
  170. \return 0 on success
  171. \return -1 error
  172. */
  173. int Vect_add_dblink(struct dblinks *p, int number, const char *name,
  174. const char *table, const char *key, const char *db,
  175. const char *driver)
  176. {
  177. int ret;
  178. G_debug(3, "Field number <%d>, name <%s>", number, name);
  179. if (!name) {
  180. /* if name is not given, use table name */
  181. name = table;
  182. }
  183. ret = Vect_check_dblink(p, number, name);
  184. if (ret == 1) {
  185. G_warning(_("Layer number %d or name <%s> already exists"), number,
  186. name);
  187. return -1;
  188. }
  189. if (p->n_fields == p->alloc_fields) {
  190. p->alloc_fields += 10;
  191. p->field = (struct field_info *)G_realloc((void *)p->field,
  192. p->alloc_fields *
  193. sizeof(struct field_info));
  194. }
  195. p->field[p->n_fields].number = number;
  196. if (name != NULL) {
  197. p->field[p->n_fields].name = G_store(name);
  198. /* replace all spaces with underscore, otherwise dbln can't be read */
  199. G_strchg(p->field[p->n_fields].name, ' ', '_');
  200. }
  201. else
  202. p->field[p->n_fields].name = NULL;
  203. if (table != NULL)
  204. p->field[p->n_fields].table = G_store(table);
  205. else
  206. p->field[p->n_fields].table = NULL;
  207. if (key != NULL)
  208. p->field[p->n_fields].key = G_store(key);
  209. else
  210. p->field[p->n_fields].key = NULL;
  211. if (db != NULL)
  212. p->field[p->n_fields].database = G_store(db);
  213. else
  214. p->field[p->n_fields].database = NULL;
  215. if (driver != NULL)
  216. p->field[p->n_fields].driver = G_store(driver);
  217. else
  218. p->field[p->n_fields].driver = NULL;
  219. p->n_fields++;
  220. return 0;
  221. }
  222. /*!
  223. \brief Get default information about link to database for new dblink
  224. \param Map pointer to Map_info structure
  225. \param field layer number
  226. \param field_name layer name
  227. \param type how many tables are linked to map: GV_1TABLE / GV_MTABLE
  228. \return pointer to new field_info structure
  229. */
  230. struct field_info *Vect_default_field_info(struct Map_info *Map,
  231. int field, const char *field_name, int type)
  232. {
  233. struct field_info *fi;
  234. char buf[1000], buf2[1000];
  235. const char *schema;
  236. const char *drv, *db;
  237. dbConnection connection;
  238. G_debug(1, "Vect_default_field_info(): map = %s field = %d", Map->name,
  239. field);
  240. db_get_connection(&connection);
  241. drv = G__getenv2("DB_DRIVER", G_VAR_MAPSET);
  242. db = G__getenv2("DB_DATABASE", G_VAR_MAPSET);
  243. G_debug(2, "drv = %s db = %s", drv, db);
  244. if (!connection.driverName && !connection.databaseName) {
  245. /* Set default values and create dbf db dir */
  246. db_set_default_connection();
  247. db_get_connection(&connection);
  248. G_important_message(_("Default driver / database set to:\n"
  249. "driver: %s\ndatabase: %s"), connection.driverName,
  250. connection.databaseName);
  251. }
  252. /* they must be a matched pair, so if one is set but not the other
  253. then give up and let the user figure it out */
  254. else if (!connection.driverName) {
  255. G_fatal_error(_("Default driver is not set"));
  256. }
  257. else if (!connection.databaseName) {
  258. G_fatal_error(_("Default database is not set"));
  259. }
  260. drv = connection.driverName;
  261. db = connection.databaseName;
  262. fi = (struct field_info *)G_malloc(sizeof(struct field_info));
  263. fi->number = field;
  264. if (field_name != NULL)
  265. fi->name = G_store(field_name);
  266. else
  267. fi->name = NULL;
  268. /* Table name */
  269. if (type == GV_1TABLE) {
  270. sprintf(buf, "%s", Map->name);
  271. }
  272. else {
  273. if (field_name != NULL && strlen(field_name) > 0)
  274. sprintf(buf, "%s_%s", Map->name, field_name);
  275. else
  276. sprintf(buf, "%s_%d", Map->name, field);
  277. }
  278. schema = connection.schemaName;
  279. if (schema && strlen(schema) > 0) {
  280. sprintf(buf2, "%s.%s", schema, buf);
  281. fi->table = G_store(buf2);
  282. }
  283. else {
  284. fi->table = G_store(buf);
  285. }
  286. fi->key = G_store("cat"); /* Should be: id/fid/gfid/... ? */
  287. fi->database = G_store(db);
  288. fi->driver = G_store(drv);
  289. return (fi);
  290. }
  291. /*!
  292. \brief Get information about link to database.
  293. Variables are substituted by values, link is index to array of
  294. dblinks.
  295. \param Map pointer to Map_info structure
  296. \param link link id
  297. \return pointer to new field_info structure
  298. */
  299. struct field_info *Vect_get_dblink(const struct Map_info *Map, int link)
  300. {
  301. struct field_info *fi;
  302. G_debug(1, "Vect_get_dblink(): link = %d", link);
  303. if (link >= Map->dblnk->n_fields) {
  304. G_warning(_("Requested dblink %d, maximum link number %d"), link,
  305. Map->dblnk->n_fields - 1);
  306. return NULL;
  307. }
  308. fi = (struct field_info *)malloc(sizeof(struct field_info));
  309. fi->number = Map->dblnk->field[link].number;
  310. if (Map->dblnk->field[link].name != NULL)
  311. fi->name = G_store(Map->dblnk->field[link].name);
  312. else
  313. fi->name = NULL;
  314. fi->table = G_store(Map->dblnk->field[link].table);
  315. fi->key = G_store(Map->dblnk->field[link].key);
  316. fi->database = Vect_subst_var(Map->dblnk->field[link].database, Map);
  317. fi->driver = G_store(Map->dblnk->field[link].driver);
  318. return fi;
  319. }
  320. /*!
  321. \brief Get information about link to database (by layer number)
  322. Variables are substituted by values, field is number of requested
  323. field.
  324. \param Map pointer to Map_info structure
  325. \param field layer number
  326. \return pointer to new field_info structure
  327. \return NULL if not found
  328. */
  329. struct field_info *Vect_get_field(const struct Map_info *Map, int field)
  330. {
  331. int i;
  332. struct field_info *fi = NULL;
  333. G_debug(1, "Vect_get_field(): field = %d", field);
  334. for (i = 0; i < Map->dblnk->n_fields; i++) {
  335. if (Map->dblnk->field[i].number == field) {
  336. fi = Vect_get_dblink(Map, i);
  337. break;
  338. }
  339. }
  340. return fi;
  341. }
  342. /*!
  343. \brief Get information about link to database (by layer name)
  344. \param Map pointer to Map_info structure
  345. \param field layer name
  346. \return pointer to new field_info structure
  347. \return NULL if not found
  348. */
  349. struct field_info *Vect_get_field_by_name(const struct Map_info *Map, const char *field)
  350. {
  351. int i;
  352. struct field_info *fi = NULL;
  353. G_debug(1, "Vect_get_field_by_name(): field = %s", field);
  354. for (i = 0; i < Map->dblnk->n_fields; i++) {
  355. if (strcmp(Map->dblnk->field[i].name, field) == 0) {
  356. fi = Vect_get_dblink(Map, i);
  357. break;
  358. }
  359. }
  360. return fi;
  361. }
  362. /*!
  363. \brief Get information about link to database (by layer number or layer name)
  364. \param Map pointer to Map_info structure
  365. \param field layer number or name
  366. \return pointer to new field_info structure
  367. \return NULL if not found
  368. */
  369. struct field_info *Vect_get_field2(const struct Map_info *Map, const char *field)
  370. {
  371. int ifield;
  372. struct field_info *fi;
  373. G_debug(1, "Vect_get_field2(): field = %s", field);
  374. fi = NULL;
  375. ifield = atoi(field);
  376. if (ifield > 0) {
  377. fi = Vect_get_field(Map, ifield);
  378. if (fi)
  379. return fi;
  380. }
  381. return Vect_get_field_by_name(Map, field);
  382. }
  383. /*!
  384. \brief Get field number of given field
  385. \param Map pointer to Map_info structure
  386. \param field layer name
  387. \return layer number
  388. \return -1 for all layers
  389. \return 0 if layer not found
  390. */
  391. int Vect_get_field_number(const struct Map_info *Map, const char *field)
  392. {
  393. struct field_info *fi;
  394. G_debug(1, "Vect_get_field_number(): field = %s", field);
  395. if (strcmp(field, "-1") == 0)
  396. return -1;
  397. if (Vect_get_num_dblinks(Map) == 0)
  398. return atoi(field);
  399. fi = Vect_get_field2(Map, field);
  400. if (fi)
  401. return fi->number;
  402. return atoi(field);
  403. }
  404. /*!
  405. \brief Read dblinks to existing structure.
  406. Variables are not substituted by values.
  407. \param Map pointer to Map_info structure
  408. \return number of links read
  409. \return -1 on error
  410. */
  411. int Vect_read_dblinks(struct Map_info *Map)
  412. {
  413. FILE *fd;
  414. char file[1024], buf[2001];
  415. char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname;
  416. int fld;
  417. char *c;
  418. int row, rule;
  419. struct dblinks *dbl;
  420. char **tokens;
  421. int ntok, i;
  422. G_debug(1, "Vect_read_dblinks(): map = %s, mapset = %s", Map->name,
  423. Map->mapset);
  424. dbl = Map->dblnk;
  425. Vect_reset_dblinks(dbl);
  426. G_debug(3, "Searching for FID column in OGR DB");
  427. if (Map->format & (GV_FORMAT_OGR | GV_FORMAT_OGR_DIRECT)) {
  428. #ifndef HAVE_GDAL
  429. G_fatal_error(_("The support for OGR vector maps wasn't"
  430. " compiled in"));
  431. #else
  432. #if GDAL_VERSION_NUM > 1320 && HAVE_OGR /* seems to be fixed after 1320 release */
  433. int layer, nLayers;
  434. char *ogr_fid_col;
  435. G_debug(3, "GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
  436. if (Map->fInfo.ogr.ds == NULL) {
  437. /* open the connection to fetch the FID column name */
  438. OGRRegisterAll();
  439. /* data source handle */
  440. Map->fInfo.ogr.ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
  441. if (Map->fInfo.ogr.ds == NULL)
  442. G_fatal_error(_("Unable to open OGR data source '%s'"),
  443. Map->fInfo.ogr.dsn);
  444. }
  445. if (Map->fInfo.ogr.layer == NULL) {
  446. /* get layer number */
  447. layer = -1;
  448. nLayers = OGR_DS_GetLayerCount(Map->fInfo.ogr.ds); /* Layers = Maps in OGR DB */
  449. G_debug(3, "%d layers (maps) found in data source", nLayers);
  450. G_debug(3, "Trying to open OGR layer: %s", Map->fInfo.ogr.layer_name);
  451. if (Map->fInfo.ogr.layer_name) {
  452. Map->fInfo.ogr.layer = OGR_DS_GetLayerByName(Map->fInfo.ogr.ds, Map->fInfo.ogr.layer_name);
  453. if (Map->fInfo.ogr.layer == NULL) {
  454. OGR_DS_Destroy(Map->fInfo.ogr.ds);
  455. Map->fInfo.ogr.ds = NULL;
  456. G_fatal_error(_("Unable to open OGR layer <%s>"),
  457. Map->fInfo.ogr.layer_name);
  458. }
  459. }
  460. }
  461. /* get fid column */
  462. ogr_fid_col = G_store(OGR_L_GetFIDColumn(Map->fInfo.ogr.layer));
  463. G_debug(3, "Using FID column <%s> in OGR DB", ogr_fid_col);
  464. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name,
  465. Map->fInfo.ogr.layer_name, ogr_fid_col,
  466. Map->fInfo.ogr.dsn, "ogr");
  467. #else
  468. dbDriver *driver;
  469. dbCursor cursor;
  470. dbString sql;
  471. int FID = 0, OGC_FID = 0, OGR_FID = 0, GID = 0;
  472. G_debug(3, "GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
  473. /* FID is not available for all OGR drivers */
  474. db_init_string(&sql);
  475. driver = db_start_driver_open_database("ogr", Map->fInfo.ogr.dsn);
  476. if (driver == NULL) {
  477. G_warning(_("Unable to open OGR DBMI driver"));
  478. return -1;
  479. }
  480. /* this is a bit stupid, but above FID auto-detection doesn't work yet...: */
  481. db_auto_print_errors(0);
  482. sprintf(buf, "select FID from %s where FID > 0",
  483. Map->fInfo.ogr.layer_name);
  484. db_set_string(&sql, buf);
  485. if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
  486. DB_OK) {
  487. /* FID not available, so we try ogc_fid */
  488. G_debug(3, "Failed. Now searching for ogc_fid column in OGR DB");
  489. sprintf(buf, "select ogc_fid from %s where ogc_fid > 0",
  490. Map->fInfo.ogr.layer_name);
  491. db_set_string(&sql, buf);
  492. if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
  493. DB_OK) {
  494. /* Neither FID nor ogc_fid available, so we try ogr_fid */
  495. G_debug(3,
  496. "Failed. Now searching for ogr_fid column in OGR DB");
  497. sprintf(buf, "select ogr_fid from %s where ogr_fid > 0",
  498. Map->fInfo.ogr.layer_name);
  499. db_set_string(&sql, buf);
  500. if (db_open_select_cursor
  501. (driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
  502. /* Neither FID nor ogc_fid available, so we try gid */
  503. G_debug(3,
  504. "Failed. Now searching for gid column in OGR DB");
  505. sprintf(buf, "select gid from %s where gid > 0",
  506. Map->fInfo.ogr.layer_name);
  507. db_set_string(&sql, buf);
  508. if (db_open_select_cursor
  509. (driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
  510. /* neither FID nor ogc_fid nor ogr_fid nor gid available */
  511. G_warning(_("All FID tests failed. Neither 'FID' nor 'ogc_fid' "
  512. "nor 'ogr_fid' nor 'gid' available in OGR DB table"));
  513. db_close_database_shutdown_driver(driver);
  514. return 0;
  515. }
  516. else
  517. GID = 1;
  518. }
  519. else
  520. OGR_FID = 1;
  521. }
  522. else
  523. OGC_FID = 1;
  524. }
  525. else
  526. FID = 1;
  527. G_debug(3, "FID: %d, OGC_FID: %d, OGR_FID: %d, GID: %d", FID, OGC_FID,
  528. OGR_FID, GID);
  529. db_close_cursor(&cursor);
  530. db_close_database_shutdown_driver(driver);
  531. db_auto_print_errors(1);
  532. if (FID) {
  533. G_debug(3, "Using FID column in OGR DB");
  534. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name, Map->fInfo.ogr.layer_name, "FID",
  535. Map->fInfo.ogr.dsn, "ogr");
  536. }
  537. else {
  538. if (OGC_FID) {
  539. G_debug(3, "Using ogc_fid column in OGR DB");
  540. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name, Map->fInfo.ogr.layer_name,
  541. "ogc_fid", Map->fInfo.ogr.dsn, "ogr");
  542. }
  543. else {
  544. if (OGR_FID) {
  545. G_debug(3, "Using ogr_fid column in OGR DB");
  546. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name, Map->fInfo.ogr.layer_name,
  547. "ogr_fid", Map->fInfo.ogr.dsn, "ogr");
  548. }
  549. else {
  550. if (GID) {
  551. G_debug(3, "Using gid column in OGR DB");
  552. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name,
  553. Map->fInfo.ogr.layer_name, "gid",
  554. Map->fInfo.ogr.dsn, "ogr");
  555. }
  556. }
  557. }
  558. }
  559. #endif /* GDAL_VERSION_NUM > 1320 && HAVE_OGR */
  560. return (1);
  561. #endif /* HAVE_GDAL */
  562. }
  563. else if (Map->format != GV_FORMAT_NATIVE) {
  564. G_fatal_error(_("Don't know how to read links for format %d"),
  565. Map->format);
  566. }
  567. sprintf(file, "%s/%s/%s/%s/%s/%s", Map->gisdbase, Map->location,
  568. Map->mapset, GV_DIRECTORY, Map->name,
  569. GV_DBLN_ELEMENT);
  570. G_debug(1, "dbln file: %s", file);
  571. fd = fopen(file, "r");
  572. if (fd == NULL) { /* This may be correct, no tables defined */
  573. G_debug(1, "Cannot open vector database definition file");
  574. return (-1);
  575. }
  576. row = 0;
  577. rule = 0;
  578. while (G_getl2(buf, 2000, fd)) {
  579. row++;
  580. G_chop(buf);
  581. G_debug(1, "dbln: %s", buf);
  582. c = (char *)strchr(buf, '#');
  583. if (c != NULL)
  584. *c = '\0';
  585. if (strlen(buf) == 0)
  586. continue;
  587. #ifdef NOT_ABLE_TO_READ_GRASS_6
  588. int ndef;
  589. ndef = sscanf(buf, "%s|%s|%s|%s|%s", fldstr, tab, col, db, drv);
  590. if (ndef < 2 || (ndef < 5 && rule < 1)) {
  591. G_warning(_("Error in rule on row %d in <%s>"), row, file);
  592. continue;
  593. }
  594. #else
  595. tokens = G_tokenize(buf, " |");
  596. ntok = G_number_of_tokens(tokens);
  597. if (ntok < 2 || (ntok < 5 && rule < 1)) {
  598. G_warning(_("Error in rule on row %d in <%s>"), row, file);
  599. continue;
  600. }
  601. strcpy(fldstr, tokens[0]);
  602. strcpy(tab, tokens[1]);
  603. if (ntok > 2) {
  604. strcpy(col, tokens[2]);
  605. if (ntok > 3) {
  606. strcpy(db, tokens[3]);
  607. /* allow for spaces in path names */
  608. for (i=4; i < ntok-1; i++) {
  609. strcat(db, " ");
  610. strcat(db, tokens[i]);
  611. }
  612. strcpy(drv, tokens[ntok-1]);
  613. }
  614. }
  615. G_free_tokens(tokens);
  616. #endif
  617. /* get field and field name */
  618. fldname = strchr(fldstr, '/');
  619. if (fldname != NULL) { /* field has name */
  620. fldname[0] = 0;
  621. fldname++;
  622. }
  623. fld = atoi(fldstr);
  624. Vect_add_dblink(dbl, fld, fldname, tab, col, db, drv);
  625. G_debug(1,
  626. "field = %d name = %s, table = %s, key = %s, database = %s, driver = %s",
  627. fld, fldname, tab, col, db, drv);
  628. rule++;
  629. }
  630. fclose(fd);
  631. G_debug(1, "Dblinks read");
  632. return (rule);
  633. }
  634. /*!
  635. \brief Write dblinks to file
  636. \param Map pointer to Map_info structure
  637. \return 0 on success
  638. \return -1 on error
  639. */
  640. int Vect_write_dblinks(struct Map_info *Map)
  641. {
  642. int i;
  643. FILE *fd;
  644. char file[GPATH_MAX], buf[GPATH_MAX];
  645. struct dblinks *dbl;
  646. G_debug(1, "Vect_write_dblinks(): map = %s, mapset = %s", Map->name,
  647. Map->mapset);
  648. dbl = Map->dblnk;
  649. sprintf(file, "%s/%s/%s/%s/%s/%s", Map->gisdbase, Map->location,
  650. Map->mapset, GV_DIRECTORY, Map->name,
  651. GV_DBLN_ELEMENT);
  652. G_debug(1, "dbln file: %s", file);
  653. fd = fopen(file, "w");
  654. if (fd == NULL) { /* This may be correct, no tables defined */
  655. G_warning(_("Unable to open vector database definition file '%s'"),
  656. file);
  657. return (-1);
  658. }
  659. for (i = 0; i < dbl->n_fields; i++) {
  660. if (dbl->field[i].name != NULL)
  661. sprintf(buf, "%d/%s", dbl->field[i].number, dbl->field[i].name);
  662. else
  663. sprintf(buf, "%d", dbl->field[i].number);
  664. fprintf(fd, "%s|%s|%s|%s|%s\n", buf, dbl->field[i].table,
  665. dbl->field[i].key, dbl->field[i].database,
  666. dbl->field[i].driver);
  667. G_debug(1, "%s|%s|%s|%s|%s", buf, dbl->field[i].table,
  668. dbl->field[i].key, dbl->field[i].database,
  669. dbl->field[i].driver);
  670. }
  671. fclose(fd);
  672. G_debug(1, "Dblinks written");
  673. return 0;
  674. }
  675. /*!
  676. \brief Substitute variable in string
  677. \param in current string
  678. \param Map pointer to Map_info structure
  679. \return pointer to new string
  680. */
  681. char *Vect_subst_var(const char *in, const struct Map_info *Map)
  682. {
  683. char *c;
  684. char buf[1000], str[1000];
  685. G_debug(3, "Vect_subst_var(): in = %s, map = %s, mapset = %s", in,
  686. Map->name, Map->mapset);
  687. strcpy(str, in);
  688. strcpy(buf, str);
  689. c = (char *)strstr(buf, "$GISDBASE");
  690. if (c != NULL) {
  691. *c = '\0';
  692. sprintf(str, "%s%s%s", buf, Map->gisdbase, c + 9);
  693. }
  694. strcpy(buf, str);
  695. c = (char *)strstr(buf, "$LOCATION_NAME");
  696. if (c != NULL) {
  697. *c = '\0';
  698. sprintf(str, "%s%s%s", buf, Map->location, c + 14);
  699. }
  700. strcpy(buf, str);
  701. c = (char *)strstr(buf, "$MAPSET");
  702. if (c != NULL) {
  703. *c = '\0';
  704. sprintf(str, "%s%s%s", buf, Map->mapset, c + 7);
  705. }
  706. strcpy(buf, str);
  707. c = (char *)strstr(buf, "$MAP");
  708. if (c != NULL) {
  709. *c = '\0';
  710. sprintf(str, "%s%s%s", buf, Map->name, c + 4);
  711. }
  712. G_debug(3, " -> %s", str);
  713. return (G_store(str));
  714. }
  715. /*!
  716. \brief Rewrite 'dbln' file
  717. Should be used by GRASS modules which update database tables, so
  718. that other applications know that tables were changed and can
  719. reload data.
  720. \param Map pointer to Map_info structure
  721. */
  722. void Vect_set_db_updated(struct Map_info *Map)
  723. {
  724. if (strcmp(Map->mapset, G_mapset()) != 0) {
  725. G_fatal_error(_("Bug: attempt to update map which is not in current mapset"));
  726. }
  727. Vect_write_dblinks(Map);
  728. }