field.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*!
  2. \file lib/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, 2011 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-2011
  14. */
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <grass/glocale.h>
  19. #include <grass/dbmi.h>
  20. #include <grass/vector.h>
  21. #ifdef HAVE_GDAL
  22. #include <gdal_version.h> /* needed for FID detection */
  23. #endif /* HAVE_GDAL */
  24. #ifdef HAVE_OGR
  25. #include <ogr_api.h>
  26. #endif
  27. /*!
  28. \brief Create and init new dblinks structure
  29. \return pointer to new dblinks structure
  30. */
  31. struct dblinks *Vect_new_dblinks_struct(void)
  32. {
  33. struct dblinks *p;
  34. p = (struct dblinks *)G_malloc(sizeof(struct dblinks));
  35. if (p) {
  36. p->alloc_fields = p->n_fields = 0;
  37. p->field = NULL;
  38. }
  39. return p;
  40. }
  41. /*!
  42. \brief Reset dblinks structure
  43. \param p pointer to existing dblinks structure
  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 pointer to Map_info structure
  52. \param number layer number
  53. \param name layer name (if not given use table name)
  54. \param table table name
  55. \param key key name
  56. \param db database name
  57. \param driver driver name
  58. \return 0 on success
  59. \return -1 on failure
  60. */
  61. int Vect_map_add_dblink(struct Map_info *Map, int number, const char *name,
  62. const char *table, const char *key, const char *db,
  63. const char *driver)
  64. {
  65. int ret;
  66. if (number < 1) {
  67. G_warning(_("Layer number must be 1 or greater"));
  68. return -1;
  69. }
  70. if (Map->mode != GV_MODE_WRITE && Map->mode != GV_MODE_RW) {
  71. G_warning(_("Unable to add attribute link, vector map is "
  72. "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 attribute 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 attribute links"));
  84. return -1;
  85. }
  86. return 0;
  87. }
  88. /*!
  89. \brief Delete db connection from Map_info structure
  90. \param Map pointer to Map_info structure
  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 pointer to Map_info structure
  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, const char *name)
  134. {
  135. return Vect_check_dblink(Map->dblnk, field, name);
  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, const char *name)
  145. {
  146. int i;
  147. G_debug(3, "Vect_check_dblink: field %d, name %s", field, (name != NULL ? name : "not given"));
  148. for (i = 0; i < p->n_fields; i++) {
  149. if (p->field[i].number == field) {
  150. return 1;
  151. }
  152. if (name != NULL && p->field[i].name != NULL) {
  153. if (strcmp(p->field[i].name, name) == 0)
  154. return 1;
  155. }
  156. }
  157. return 0;
  158. }
  159. /*!
  160. \brief Add new DB connection to dblinks structure
  161. \param[in,out] p pointer to existing dblinks structure
  162. \param number layer number (1 for OGR)
  163. \param name layer name (layer for OGR) - if not given use table name
  164. \param table table name (layer for OGR)
  165. \param key key name
  166. \param db database name (datasource for OGR)
  167. \param driver driver name (dbf, postgresql, ogr, ...)
  168. \return 0 on success
  169. \return -1 error
  170. */
  171. int Vect_add_dblink(struct dblinks *p, int number, const char *name,
  172. const char *table, const char *key, const char *db,
  173. const char *driver)
  174. {
  175. int ret;
  176. G_debug(3, "Field number <%d>, name <%s>", number, name);
  177. if (!name) {
  178. /* if name is not given, use table name */
  179. name = table;
  180. }
  181. ret = Vect_check_dblink(p, number, name);
  182. if (ret == 1) {
  183. G_warning(_("Layer number %d or name <%s> already exists"), number,
  184. name);
  185. return -1;
  186. }
  187. if (p->n_fields == p->alloc_fields) {
  188. p->alloc_fields += 10;
  189. p->field = (struct field_info *)G_realloc((void *)p->field,
  190. p->alloc_fields *
  191. sizeof(struct field_info));
  192. }
  193. p->field[p->n_fields].number = number;
  194. if (name != NULL) {
  195. p->field[p->n_fields].name = G_store(name);
  196. /* replace all spaces with underscore, otherwise dbln can't be read */
  197. G_strchg(p->field[p->n_fields].name, ' ', '_');
  198. }
  199. else
  200. p->field[p->n_fields].name = NULL;
  201. if (table != NULL)
  202. p->field[p->n_fields].table = G_store(table);
  203. else
  204. p->field[p->n_fields].table = NULL;
  205. if (key != NULL)
  206. p->field[p->n_fields].key = G_store(key);
  207. else
  208. p->field[p->n_fields].key = NULL;
  209. if (db != NULL)
  210. p->field[p->n_fields].database = G_store(db);
  211. else
  212. p->field[p->n_fields].database = NULL;
  213. if (driver != NULL)
  214. p->field[p->n_fields].driver = G_store(driver);
  215. else
  216. p->field[p->n_fields].driver = NULL;
  217. p->n_fields++;
  218. return 0;
  219. }
  220. /*!
  221. \brief Get default information about link to database for new dblink
  222. \param Map pointer to Map_info structure
  223. \param field layer number
  224. \param field_name layer name
  225. \param type how many tables are linked to map: GV_1TABLE / GV_MTABLE
  226. \return pointer to allocated field_info structure
  227. */
  228. struct field_info *Vect_default_field_info(struct Map_info *Map,
  229. int field, const char *field_name, int type)
  230. {
  231. struct field_info *fi;
  232. char buf[GNAME_MAX], buf2[GNAME_MAX];
  233. const char *schema;
  234. dbConnection connection;
  235. G_debug(1, "Vect_default_field_info(): map = %s field = %d", Map->name,
  236. field);
  237. if (Map->format == GV_FORMAT_OGR_DIRECT) {
  238. G_zero(&connection, sizeof(dbConnection));
  239. connection.driverName = G_store("ogr");
  240. connection.databaseName = G_store(Map->fInfo.ogr.dsn);
  241. }
  242. else {
  243. db_get_connection(&connection);
  244. }
  245. G_debug(2, "drv = %s db = %s", connection.driverName, connection.databaseName);
  246. if (!connection.driverName && !connection.databaseName) {
  247. /* Set default values */
  248. db_set_default_connection();
  249. db_get_connection(&connection);
  250. G_important_message(_("Default driver / database set to:\n"
  251. "driver: %s\ndatabase: %s"), connection.driverName,
  252. connection.databaseName);
  253. }
  254. /* they must be a matched pair, so if one is set but not the other
  255. then give up and let the user figure it out */
  256. else if (!connection.driverName) {
  257. G_fatal_error(_("Default driver is not set"));
  258. }
  259. else if (!connection.databaseName) {
  260. G_fatal_error(_("Default database is not set"));
  261. }
  262. fi = (struct field_info *)G_malloc(sizeof(struct field_info));
  263. fi->number = field;
  264. /* Table name */
  265. if (type == GV_1TABLE) {
  266. sprintf(buf, "%s", Map->name);
  267. }
  268. else {
  269. if (field_name != NULL && strlen(field_name) > 0)
  270. sprintf(buf, "%s_%s", Map->name, field_name);
  271. else
  272. sprintf(buf, "%s_%d", Map->name, field);
  273. }
  274. schema = connection.schemaName;
  275. if (schema && strlen(schema) > 0) {
  276. sprintf(buf2, "%s.%s", schema, buf);
  277. fi->table = G_store(buf2);
  278. }
  279. else {
  280. fi->table = G_store(buf);
  281. }
  282. /* Field name */
  283. if (field_name)
  284. fi->name = G_store(field_name);
  285. else
  286. fi->name = G_store(buf);
  287. fi->key = G_store(GV_KEY_COLUMN); /* Should be: id/fid/gfid/... ? */
  288. fi->database = G_store(connection.databaseName);
  289. fi->driver = G_store(connection.driverName);
  290. return fi;
  291. }
  292. /*!
  293. \brief Get information about link to database.
  294. Variables are substituted by values, link is index to array of
  295. dblinks.
  296. \param Map pointer to Map_info structure
  297. \param link link id
  298. \return pointer to new field_info structure
  299. */
  300. struct field_info *Vect_get_dblink(const struct Map_info *Map, int link)
  301. {
  302. struct field_info *fi;
  303. G_debug(1, "Vect_get_dblink(): link = %d", link);
  304. if (link >= Map->dblnk->n_fields) {
  305. G_warning(_("Requested dblink %d, maximum link number %d"), link,
  306. Map->dblnk->n_fields - 1);
  307. return NULL;
  308. }
  309. fi = (struct field_info *)malloc(sizeof(struct field_info));
  310. fi->number = Map->dblnk->field[link].number;
  311. if (Map->dblnk->field[link].name != NULL)
  312. fi->name = G_store(Map->dblnk->field[link].name);
  313. else
  314. fi->name = NULL;
  315. fi->table = G_store(Map->dblnk->field[link].table);
  316. fi->key = G_store(Map->dblnk->field[link].key);
  317. fi->database = Vect_subst_var(Map->dblnk->field[link].database, Map);
  318. fi->driver = G_store(Map->dblnk->field[link].driver);
  319. return fi;
  320. }
  321. /*!
  322. \brief Get information about link to database (by layer number)
  323. Variables are substituted by values, field is number of requested
  324. field.
  325. \param Map pointer to Map_info structure
  326. \param field layer number
  327. \return pointer to new field_info structure
  328. \return NULL if not found
  329. */
  330. struct field_info *Vect_get_field(const struct Map_info *Map, int field)
  331. {
  332. int i;
  333. struct field_info *fi = NULL;
  334. G_debug(1, "Vect_get_field(): field = %d", field);
  335. for (i = 0; i < Map->dblnk->n_fields; i++) {
  336. if (Map->dblnk->field[i].number == field) {
  337. fi = Vect_get_dblink(Map, i);
  338. break;
  339. }
  340. }
  341. return fi;
  342. }
  343. /*!
  344. \brief Get information about link to database (by layer name)
  345. \param Map pointer to Map_info structure
  346. \param field layer name
  347. \return pointer to new field_info structure
  348. \return NULL if not found
  349. */
  350. struct field_info *Vect_get_field_by_name(const struct Map_info *Map, const char *field)
  351. {
  352. int i;
  353. struct field_info *fi = NULL;
  354. G_debug(1, "Vect_get_field_by_name(): field = %s", field);
  355. for (i = 0; i < Map->dblnk->n_fields; i++) {
  356. if (strcmp(Map->dblnk->field[i].name, field) == 0) {
  357. fi = Vect_get_dblink(Map, i);
  358. break;
  359. }
  360. }
  361. return fi;
  362. }
  363. /*!
  364. \brief Get information about link to database (by layer number or layer name)
  365. \param Map pointer to Map_info structure
  366. \param field layer number or name
  367. \return pointer to new field_info structure
  368. \return NULL if not found
  369. */
  370. struct field_info *Vect_get_field2(const struct Map_info *Map, const char *field)
  371. {
  372. int ifield;
  373. struct field_info *fi;
  374. G_debug(1, "Vect_get_field2(): field = %s", field);
  375. fi = NULL;
  376. ifield = atoi(field);
  377. if (ifield > 0) {
  378. fi = Vect_get_field(Map, ifield);
  379. if (fi)
  380. return fi;
  381. }
  382. return Vect_get_field_by_name(Map, field);
  383. }
  384. /*!
  385. \brief Get field number of given field
  386. \param Map pointer to Map_info structure
  387. \param field layer name
  388. \return layer number
  389. \return -1 for all layers
  390. \return 0 if layer not found
  391. */
  392. int Vect_get_field_number(const struct Map_info *Map, const char *field)
  393. {
  394. struct field_info *fi;
  395. G_debug(1, "Vect_get_field_number(): field = %s", field);
  396. if (strcmp(field, "-1") == 0)
  397. return -1;
  398. if (Vect_get_num_dblinks(Map) == 0)
  399. return atoi(field);
  400. fi = Vect_get_field2(Map, field);
  401. if (fi)
  402. return fi->number;
  403. return atoi(field);
  404. }
  405. /*!
  406. \brief Read dblinks to existing structure.
  407. Variables are not substituted by values.
  408. \param Map pointer to Map_info structure
  409. \return number of links read
  410. \return -1 on error
  411. */
  412. int Vect_read_dblinks(struct Map_info *Map)
  413. {
  414. FILE *fd;
  415. char file[1024], buf[2001];
  416. char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname;
  417. int fld;
  418. char *c;
  419. int row, rule;
  420. struct dblinks *dbl;
  421. char **tokens;
  422. int ntok, i;
  423. G_debug(1, "Vect_read_dblinks(): map = %s, mapset = %s", Map->name,
  424. Map->mapset);
  425. dbl = Map->dblnk;
  426. Vect_reset_dblinks(dbl);
  427. G_debug(3, "Searching for FID column in OGR DB");
  428. if (Map->format & (GV_FORMAT_OGR | GV_FORMAT_OGR_DIRECT)) {
  429. #ifndef HAVE_GDAL
  430. G_fatal_error(_("The support for OGR vector maps wasn't"
  431. " compiled in"));
  432. #else
  433. #if GDAL_VERSION_NUM > 1320 && HAVE_OGR /* seems to be fixed after 1320 release */
  434. int nLayers;
  435. char *ogr_fid_col;
  436. G_debug(3, "GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
  437. if (Map->fInfo.ogr.ds == NULL) {
  438. /* open the connection to fetch the FID column name */
  439. OGRRegisterAll();
  440. /* data source handle */
  441. Map->fInfo.ogr.ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
  442. if (Map->fInfo.ogr.ds == NULL)
  443. G_fatal_error(_("Unable to open OGR data source '%s'"),
  444. Map->fInfo.ogr.dsn);
  445. }
  446. if (Map->fInfo.ogr.layer == NULL) {
  447. /* get layer number */
  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. #ifdef __MINGW32__
  688. char *cin;
  689. cin = G_str_replace(in, "/", "\\");
  690. strcpy(str, cin);
  691. G_free(cin);
  692. #else
  693. strcpy(str, in);
  694. #endif
  695. strcpy(buf, str);
  696. c = (char *)strstr(buf, "$GISDBASE");
  697. if (c != NULL) {
  698. *c = '\0';
  699. sprintf(str, "%s%s%s", buf, Map->gisdbase, c + 9);
  700. }
  701. strcpy(buf, str);
  702. c = (char *)strstr(buf, "$LOCATION_NAME");
  703. if (c != NULL) {
  704. *c = '\0';
  705. sprintf(str, "%s%s%s", buf, Map->location, c + 14);
  706. }
  707. strcpy(buf, str);
  708. c = (char *)strstr(buf, "$MAPSET");
  709. if (c != NULL) {
  710. *c = '\0';
  711. sprintf(str, "%s%s%s", buf, Map->mapset, c + 7);
  712. }
  713. strcpy(buf, str);
  714. c = (char *)strstr(buf, "$MAP");
  715. if (c != NULL) {
  716. *c = '\0';
  717. sprintf(str, "%s%s%s", buf, Map->name, c + 4);
  718. }
  719. G_debug(3, " -> %s", str);
  720. return (G_store(str));
  721. }
  722. /*!
  723. \brief Rewrite 'dbln' file
  724. Should be used by GRASS modules which update database tables, so
  725. that other applications know that tables were changed and can
  726. reload data.
  727. \param Map pointer to Map_info structure
  728. */
  729. void Vect_set_db_updated(struct Map_info *Map)
  730. {
  731. if (strcmp(Map->mapset, G_mapset()) != 0) {
  732. G_fatal_error(_("Bug: attempt to update map which is not in current mapset"));
  733. }
  734. Vect_write_dblinks(Map);
  735. }