field.c 19 KB

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