field.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 Copy DB links from input vector map to output vector map
  128. \param In pointer to Map_info structure (input)
  129. \param Out pointer to Map_info structure (output)
  130. \param first_only copy only first link
  131. */
  132. void Vect_copy_map_dblinks(const struct Map_info *In, struct Map_info *Out,
  133. int first_only)
  134. {
  135. int i, ndblinks;
  136. struct field_info *Fi;
  137. ndblinks = Vect_get_num_dblinks(In);
  138. for (i = 0; i < ndblinks; i++) {
  139. Fi = Vect_get_dblink(In, 0);
  140. if (!Fi) {
  141. G_warning(_("Database connection not defined. Skipping."));
  142. continue;
  143. }
  144. Vect_map_add_dblink(Out, Fi->number, Fi->name, Fi->table, Fi->key,
  145. Fi->database, Fi->driver);
  146. if (first_only && ndblinks > 1)
  147. G_warning(_("More DB links defined for input vector map. "
  148. "Using only first DB link for output."));
  149. }
  150. }
  151. /*!
  152. \brief Check if DB connection exists in dblinks structure
  153. \param Map pointer to Map_info structure
  154. \param field layer number
  155. \param name layer name
  156. \return 1 dblink for field exists
  157. \return 0 dblink does not exist for field
  158. */
  159. int Vect_map_check_dblink(const struct Map_info *Map, int field, const char *name)
  160. {
  161. return Vect_check_dblink(Map->dblnk, field, name);
  162. }
  163. /*!
  164. \brief Check if DB connection exists in dblinks structure
  165. \param p pointer to existing dblinks structure
  166. \param field layer number
  167. \return 1 dblink for field exists
  168. \return 0 dblink does not exist for field
  169. */
  170. int Vect_check_dblink(const struct dblinks *p, int field, const char *name)
  171. {
  172. int i;
  173. G_debug(3, "Vect_check_dblink: field %d, name %s", field, (name != NULL ? name : "not given"));
  174. for (i = 0; i < p->n_fields; i++) {
  175. if (p->field[i].number == field) {
  176. return 1;
  177. }
  178. if (name != NULL && p->field[i].name != NULL) {
  179. if (strcmp(p->field[i].name, name) == 0)
  180. return 1;
  181. }
  182. }
  183. return 0;
  184. }
  185. /*!
  186. \brief Add new DB connection to dblinks structure
  187. \param[in,out] p pointer to existing dblinks structure
  188. \param number layer number (1 for OGR)
  189. \param name layer name (layer for OGR) - if not given use table name
  190. \param table table name (layer for OGR)
  191. \param key key name
  192. \param db database name (datasource for OGR)
  193. \param driver driver name (dbf, postgresql, ogr, ...)
  194. \return 0 on success
  195. \return -1 error
  196. */
  197. int Vect_add_dblink(struct dblinks *p, int number, const char *name,
  198. const char *table, const char *key, const char *db,
  199. const char *driver)
  200. {
  201. int ret;
  202. G_debug(3, "Field number <%d>, name <%s>", number, name);
  203. if (!name) {
  204. /* if name is not given, use table name */
  205. name = table;
  206. }
  207. ret = Vect_check_dblink(p, number, name);
  208. if (ret == 1) {
  209. G_warning(_("Layer number %d or name <%s> already exists"), number,
  210. name);
  211. return -1;
  212. }
  213. if (p->n_fields == p->alloc_fields) {
  214. p->alloc_fields += 10;
  215. p->field = (struct field_info *)G_realloc((void *)p->field,
  216. p->alloc_fields *
  217. sizeof(struct field_info));
  218. }
  219. p->field[p->n_fields].number = number;
  220. if (name != NULL) {
  221. p->field[p->n_fields].name = G_store(name);
  222. /* replace all spaces with underscore, otherwise dbln can't be read */
  223. G_strchg(p->field[p->n_fields].name, ' ', '_');
  224. }
  225. else
  226. p->field[p->n_fields].name = NULL;
  227. if (table != NULL)
  228. p->field[p->n_fields].table = G_store(table);
  229. else
  230. p->field[p->n_fields].table = NULL;
  231. if (key != NULL)
  232. p->field[p->n_fields].key = G_store(key);
  233. else
  234. p->field[p->n_fields].key = NULL;
  235. if (db != NULL)
  236. p->field[p->n_fields].database = G_store(db);
  237. else
  238. p->field[p->n_fields].database = NULL;
  239. if (driver != NULL)
  240. p->field[p->n_fields].driver = G_store(driver);
  241. else
  242. p->field[p->n_fields].driver = NULL;
  243. p->n_fields++;
  244. return 0;
  245. }
  246. /*!
  247. \brief Get default information about link to database for new dblink
  248. \param Map pointer to Map_info structure
  249. \param field layer number
  250. \param field_name layer name
  251. \param type how many tables are linked to map: GV_1TABLE / GV_MTABLE
  252. \return pointer to allocated field_info structure
  253. */
  254. struct field_info *Vect_default_field_info(struct Map_info *Map,
  255. int field, const char *field_name, int type)
  256. {
  257. struct field_info *fi;
  258. char buf[GNAME_MAX], buf2[GNAME_MAX];
  259. const char *schema;
  260. dbConnection connection;
  261. G_debug(1, "Vect_default_field_info(): map = %s field = %d", Map->name,
  262. field);
  263. if (Map->format == GV_FORMAT_OGR_DIRECT) {
  264. G_zero(&connection, sizeof(dbConnection));
  265. connection.driverName = G_store("ogr");
  266. connection.databaseName = G_store(Map->fInfo.ogr.dsn);
  267. }
  268. else {
  269. db_get_connection(&connection);
  270. }
  271. G_debug(2, "drv = %s db = %s", connection.driverName, connection.databaseName);
  272. if (!connection.driverName && !connection.databaseName) {
  273. /* Set default values */
  274. db_set_default_connection();
  275. db_get_connection(&connection);
  276. G_important_message(_("Default driver / database set to:\n"
  277. "driver: %s\ndatabase: %s"), connection.driverName,
  278. connection.databaseName);
  279. }
  280. /* they must be a matched pair, so if one is set but not the other
  281. then give up and let the user figure it out */
  282. else if (!connection.driverName) {
  283. G_fatal_error(_("Default driver is not set"));
  284. }
  285. else if (!connection.databaseName) {
  286. G_fatal_error(_("Default database is not set"));
  287. }
  288. fi = (struct field_info *)G_malloc(sizeof(struct field_info));
  289. fi->number = field;
  290. /* Table name */
  291. if (type == GV_1TABLE) {
  292. sprintf(buf, "%s", Map->name);
  293. }
  294. else {
  295. if (field_name != NULL && strlen(field_name) > 0)
  296. sprintf(buf, "%s_%s", Map->name, field_name);
  297. else
  298. sprintf(buf, "%s_%d", Map->name, field);
  299. }
  300. schema = connection.schemaName;
  301. if (schema && strlen(schema) > 0) {
  302. sprintf(buf2, "%s.%s", schema, buf);
  303. fi->table = G_store(buf2);
  304. }
  305. else {
  306. fi->table = G_store(buf);
  307. }
  308. /* Field name */
  309. if (field_name)
  310. fi->name = G_store(field_name);
  311. else
  312. fi->name = G_store(buf);
  313. fi->key = G_store(GV_KEY_COLUMN); /* Should be: id/fid/gfid/... ? */
  314. fi->database = G_store(connection.databaseName);
  315. fi->driver = G_store(connection.driverName);
  316. return fi;
  317. }
  318. /*!
  319. \brief Get information about link to database.
  320. Variables are substituted by values, link is index to array of
  321. dblinks.
  322. \param Map pointer to Map_info structure
  323. \param link link id
  324. \return pointer to new field_info structure
  325. */
  326. struct field_info *Vect_get_dblink(const struct Map_info *Map, int link)
  327. {
  328. struct field_info *fi;
  329. G_debug(1, "Vect_get_dblink(): link = %d", link);
  330. if (link >= Map->dblnk->n_fields) {
  331. G_warning(_("Requested dblink %d, maximum link number %d"), link,
  332. Map->dblnk->n_fields - 1);
  333. return NULL;
  334. }
  335. fi = (struct field_info *)malloc(sizeof(struct field_info));
  336. fi->number = Map->dblnk->field[link].number;
  337. if (Map->dblnk->field[link].name != NULL)
  338. fi->name = G_store(Map->dblnk->field[link].name);
  339. else
  340. fi->name = NULL;
  341. fi->table = G_store(Map->dblnk->field[link].table);
  342. fi->key = G_store(Map->dblnk->field[link].key);
  343. fi->database = Vect_subst_var(Map->dblnk->field[link].database, Map);
  344. fi->driver = G_store(Map->dblnk->field[link].driver);
  345. return fi;
  346. }
  347. /*!
  348. \brief Get information about link to database (by layer number)
  349. Variables are substituted by values, field is number of requested
  350. field.
  351. \param Map pointer to Map_info structure
  352. \param field layer number
  353. \return pointer to new field_info structure
  354. \return NULL if not found
  355. */
  356. struct field_info *Vect_get_field(const struct Map_info *Map, int field)
  357. {
  358. int i;
  359. struct field_info *fi = NULL;
  360. G_debug(1, "Vect_get_field(): field = %d", field);
  361. for (i = 0; i < Map->dblnk->n_fields; i++) {
  362. if (Map->dblnk->field[i].number == field) {
  363. fi = Vect_get_dblink(Map, i);
  364. break;
  365. }
  366. }
  367. return fi;
  368. }
  369. /*!
  370. \brief Get information about link to database (by layer name)
  371. \param Map pointer to Map_info structure
  372. \param field layer name
  373. \return pointer to new field_info structure
  374. \return NULL if not found
  375. */
  376. struct field_info *Vect_get_field_by_name(const struct Map_info *Map, const char *field)
  377. {
  378. int i;
  379. struct field_info *fi = NULL;
  380. G_debug(1, "Vect_get_field_by_name(): field = %s", field);
  381. for (i = 0; i < Map->dblnk->n_fields; i++) {
  382. if (strcmp(Map->dblnk->field[i].name, field) == 0) {
  383. fi = Vect_get_dblink(Map, i);
  384. break;
  385. }
  386. }
  387. return fi;
  388. }
  389. /*!
  390. \brief Get information about link to database (by layer number or layer name)
  391. Note: if <em>field</em> is -1 then the function returns the first
  392. dblink or NULL
  393. \param Map pointer to Map_info structure
  394. \param field layer number or name
  395. \return pointer to new field_info structure
  396. \return NULL if not found
  397. */
  398. struct field_info *Vect_get_field2(const struct Map_info *Map, const char *field)
  399. {
  400. int ifield;
  401. struct field_info *fi;
  402. G_debug(1, "Vect_get_field2(): field = %s", field);
  403. fi = NULL;
  404. ifield = atoi(field);
  405. if (ifield > 0) {
  406. fi = Vect_get_field(Map, ifield);
  407. if (fi)
  408. return fi;
  409. }
  410. else if (ifield == -1) {
  411. if (Vect_get_num_dblinks(Map) > 0)
  412. return Vect_get_dblink(Map, 0); /* return first */
  413. else
  414. return NULL;
  415. }
  416. else if (ifield == 0)
  417. return Vect_get_field_by_name(Map, field);
  418. return NULL;
  419. }
  420. /*!
  421. \brief Get field number of given field
  422. \param Map pointer to Map_info structure
  423. \param field layer name
  424. \return layer number
  425. \return -1 for all layers
  426. \return 0 if layer not found
  427. */
  428. int Vect_get_field_number(const struct Map_info *Map, const char *field)
  429. {
  430. struct field_info *fi;
  431. G_debug(1, "Vect_get_field_number(): field = %s", field);
  432. if (strcmp(field, "-1") == 0)
  433. return -1;
  434. if (Vect_get_num_dblinks(Map) == 0)
  435. return atoi(field);
  436. fi = Vect_get_field2(Map, field);
  437. if (fi)
  438. return fi->number;
  439. return atoi(field);
  440. }
  441. /*!
  442. \brief Read dblinks to existing structure.
  443. Variables are not substituted by values.
  444. \param Map pointer to Map_info structure
  445. \return number of links read
  446. \return -1 on error
  447. */
  448. int Vect_read_dblinks(struct Map_info *Map)
  449. {
  450. FILE *fd;
  451. char file[1024], buf[2001];
  452. char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname;
  453. int fld;
  454. char *c;
  455. int row, rule;
  456. struct dblinks *dbl;
  457. char **tokens;
  458. int ntok, i;
  459. G_debug(1, "Vect_read_dblinks(): map = %s, mapset = %s", Map->name,
  460. Map->mapset);
  461. dbl = Map->dblnk;
  462. Vect_reset_dblinks(dbl);
  463. G_debug(3, "Searching for FID column in OGR DB");
  464. if (Map->format & (GV_FORMAT_OGR | GV_FORMAT_OGR_DIRECT)) {
  465. #ifndef HAVE_OGR
  466. G_warning(_("GRASS is not compiled with OGR support"));
  467. #else
  468. #if GDAL_VERSION_NUM > 1320 && HAVE_OGR /* seems to be fixed after 1320 release */
  469. int nLayers;
  470. char *ogr_fid_col;
  471. G_debug(3, "GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
  472. if (Map->fInfo.ogr.ds == NULL) {
  473. /* open the connection to fetch the FID column name */
  474. OGRRegisterAll();
  475. /* data source handle */
  476. Map->fInfo.ogr.ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
  477. if (Map->fInfo.ogr.ds == NULL)
  478. G_fatal_error(_("Unable to open OGR data source '%s'"),
  479. Map->fInfo.ogr.dsn);
  480. }
  481. if (Map->fInfo.ogr.layer == NULL) {
  482. /* get layer number */
  483. nLayers = OGR_DS_GetLayerCount(Map->fInfo.ogr.ds); /* Layers = Maps in OGR DB */
  484. G_debug(3, "%d layers (maps) found in data source", nLayers);
  485. G_debug(3, "Trying to open OGR layer: %s", Map->fInfo.ogr.layer_name);
  486. if (Map->fInfo.ogr.layer_name) {
  487. Map->fInfo.ogr.layer = OGR_DS_GetLayerByName(Map->fInfo.ogr.ds, Map->fInfo.ogr.layer_name);
  488. if (Map->fInfo.ogr.layer == NULL) {
  489. OGR_DS_Destroy(Map->fInfo.ogr.ds);
  490. Map->fInfo.ogr.ds = NULL;
  491. G_fatal_error(_("Unable to open OGR layer <%s>"),
  492. Map->fInfo.ogr.layer_name);
  493. }
  494. }
  495. }
  496. /* get fid column */
  497. ogr_fid_col = G_store(OGR_L_GetFIDColumn(Map->fInfo.ogr.layer));
  498. G_debug(3, "Using FID column <%s> in OGR DB", ogr_fid_col);
  499. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name,
  500. Map->fInfo.ogr.layer_name, ogr_fid_col,
  501. Map->fInfo.ogr.dsn, "ogr");
  502. #else
  503. dbDriver *driver;
  504. dbCursor cursor;
  505. dbString sql;
  506. int FID = 0, OGC_FID = 0, OGR_FID = 0, GID = 0;
  507. G_debug(3, "GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
  508. /* FID is not available for all OGR drivers */
  509. db_init_string(&sql);
  510. driver = db_start_driver_open_database("ogr", Map->fInfo.ogr.dsn);
  511. if (driver == NULL) {
  512. G_warning(_("Unable to open OGR DBMI driver"));
  513. return -1;
  514. }
  515. /* this is a bit stupid, but above FID auto-detection doesn't work yet...: */
  516. db_auto_print_errors(0);
  517. sprintf(buf, "select FID from %s where FID > 0",
  518. Map->fInfo.ogr.layer_name);
  519. db_set_string(&sql, buf);
  520. if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
  521. DB_OK) {
  522. /* FID not available, so we try ogc_fid */
  523. G_debug(3, "Failed. Now searching for ogc_fid column in OGR DB");
  524. sprintf(buf, "select ogc_fid from %s where ogc_fid > 0",
  525. Map->fInfo.ogr.layer_name);
  526. db_set_string(&sql, buf);
  527. if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
  528. DB_OK) {
  529. /* Neither FID nor ogc_fid available, so we try ogr_fid */
  530. G_debug(3,
  531. "Failed. Now searching for ogr_fid column in OGR DB");
  532. sprintf(buf, "select ogr_fid from %s where ogr_fid > 0",
  533. Map->fInfo.ogr.layer_name);
  534. db_set_string(&sql, buf);
  535. if (db_open_select_cursor
  536. (driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
  537. /* Neither FID nor ogc_fid available, so we try gid */
  538. G_debug(3,
  539. "Failed. Now searching for gid column in OGR DB");
  540. sprintf(buf, "select gid from %s where gid > 0",
  541. Map->fInfo.ogr.layer_name);
  542. db_set_string(&sql, buf);
  543. if (db_open_select_cursor
  544. (driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
  545. /* neither FID nor ogc_fid nor ogr_fid nor gid available */
  546. G_warning(_("All FID tests failed. Neither 'FID' nor 'ogc_fid' "
  547. "nor 'ogr_fid' nor 'gid' available in OGR DB table"));
  548. db_close_database_shutdown_driver(driver);
  549. return 0;
  550. }
  551. else
  552. GID = 1;
  553. }
  554. else
  555. OGR_FID = 1;
  556. }
  557. else
  558. OGC_FID = 1;
  559. }
  560. else
  561. FID = 1;
  562. G_debug(3, "FID: %d, OGC_FID: %d, OGR_FID: %d, GID: %d", FID, OGC_FID,
  563. OGR_FID, GID);
  564. db_close_cursor(&cursor);
  565. db_close_database_shutdown_driver(driver);
  566. db_auto_print_errors(1);
  567. if (FID) {
  568. G_debug(3, "Using FID column in OGR DB");
  569. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name, Map->fInfo.ogr.layer_name, "FID",
  570. Map->fInfo.ogr.dsn, "ogr");
  571. }
  572. else {
  573. if (OGC_FID) {
  574. G_debug(3, "Using ogc_fid column in OGR DB");
  575. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name, Map->fInfo.ogr.layer_name,
  576. "ogc_fid", Map->fInfo.ogr.dsn, "ogr");
  577. }
  578. else {
  579. if (OGR_FID) {
  580. G_debug(3, "Using ogr_fid column in OGR DB");
  581. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name, Map->fInfo.ogr.layer_name,
  582. "ogr_fid", Map->fInfo.ogr.dsn, "ogr");
  583. }
  584. else {
  585. if (GID) {
  586. G_debug(3, "Using gid column in OGR DB");
  587. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name,
  588. Map->fInfo.ogr.layer_name, "gid",
  589. Map->fInfo.ogr.dsn, "ogr");
  590. }
  591. }
  592. }
  593. }
  594. #endif /* GDAL_VERSION_NUM > 1320 && HAVE_OGR */
  595. return (1);
  596. #endif /* HAVE_GDAL */
  597. }
  598. else if (Map->format != GV_FORMAT_NATIVE) {
  599. G_fatal_error(_("Don't know how to read links for format %d"),
  600. Map->format);
  601. }
  602. sprintf(file, "%s/%s/%s/%s/%s/%s", Map->gisdbase, Map->location,
  603. Map->mapset, GV_DIRECTORY, Map->name,
  604. GV_DBLN_ELEMENT);
  605. G_debug(1, "dbln file: %s", file);
  606. fd = fopen(file, "r");
  607. if (fd == NULL) { /* This may be correct, no tables defined */
  608. G_debug(1, "Cannot open vector database definition file");
  609. return (-1);
  610. }
  611. row = 0;
  612. rule = 0;
  613. while (G_getl2(buf, 2000, fd)) {
  614. row++;
  615. G_chop(buf);
  616. G_debug(1, "dbln: %s", buf);
  617. c = (char *)strchr(buf, '#');
  618. if (c != NULL)
  619. *c = '\0';
  620. if (strlen(buf) == 0)
  621. continue;
  622. #ifdef NOT_ABLE_TO_READ_GRASS_6
  623. int ndef;
  624. ndef = sscanf(buf, "%s|%s|%s|%s|%s", fldstr, tab, col, db, drv);
  625. if (ndef < 2 || (ndef < 5 && rule < 1)) {
  626. G_warning(_("Error in rule on row %d in <%s>"), row, file);
  627. continue;
  628. }
  629. #else
  630. tokens = G_tokenize(buf, " |");
  631. ntok = G_number_of_tokens(tokens);
  632. if (ntok < 2 || (ntok < 5 && rule < 1)) {
  633. G_warning(_("Error in rule on row %d in <%s>"), row, file);
  634. continue;
  635. }
  636. strcpy(fldstr, tokens[0]);
  637. strcpy(tab, tokens[1]);
  638. if (ntok > 2) {
  639. strcpy(col, tokens[2]);
  640. if (ntok > 3) {
  641. strcpy(db, tokens[3]);
  642. /* allow for spaces in path names */
  643. for (i=4; i < ntok-1; i++) {
  644. strcat(db, " ");
  645. strcat(db, tokens[i]);
  646. }
  647. strcpy(drv, tokens[ntok-1]);
  648. }
  649. }
  650. G_free_tokens(tokens);
  651. #endif
  652. /* get field and field name */
  653. fldname = strchr(fldstr, '/');
  654. if (fldname != NULL) { /* field has name */
  655. fldname[0] = 0;
  656. fldname++;
  657. }
  658. fld = atoi(fldstr);
  659. Vect_add_dblink(dbl, fld, fldname, tab, col, db, drv);
  660. G_debug(1,
  661. "field = %d name = %s, table = %s, key = %s, database = %s, driver = %s",
  662. fld, fldname, tab, col, db, drv);
  663. rule++;
  664. }
  665. fclose(fd);
  666. G_debug(1, "Dblinks read");
  667. return (rule);
  668. }
  669. /*!
  670. \brief Write dblinks to file
  671. \param Map pointer to Map_info structure
  672. \return 0 on success
  673. \return -1 on error
  674. */
  675. int Vect_write_dblinks(struct Map_info *Map)
  676. {
  677. int i;
  678. FILE *fd;
  679. char file[GPATH_MAX], buf[GPATH_MAX];
  680. struct dblinks *dbl;
  681. if (Map->format != GV_FORMAT_NATIVE)
  682. /* nothing to write for non-native formats */
  683. return 0;
  684. G_debug(1, "Vect_write_dblinks(): map = %s, mapset = %s", Map->name,
  685. Map->mapset);
  686. dbl = Map->dblnk;
  687. sprintf(file, "%s/%s/%s/%s/%s/%s", Map->gisdbase, Map->location,
  688. Map->mapset, GV_DIRECTORY, Map->name,
  689. GV_DBLN_ELEMENT);
  690. G_debug(1, "dbln file: %s", file);
  691. fd = fopen(file, "w");
  692. if (fd == NULL) { /* This may be correct, no tables defined */
  693. G_warning(_("Unable to open vector database definition file '%s'"),
  694. file);
  695. return (-1);
  696. }
  697. for (i = 0; i < dbl->n_fields; i++) {
  698. if (dbl->field[i].name != NULL)
  699. sprintf(buf, "%d/%s", dbl->field[i].number, dbl->field[i].name);
  700. else
  701. sprintf(buf, "%d", dbl->field[i].number);
  702. fprintf(fd, "%s|%s|%s|%s|%s\n", buf, dbl->field[i].table,
  703. dbl->field[i].key, dbl->field[i].database,
  704. dbl->field[i].driver);
  705. G_debug(1, "%s|%s|%s|%s|%s", buf, dbl->field[i].table,
  706. dbl->field[i].key, dbl->field[i].database,
  707. dbl->field[i].driver);
  708. }
  709. fclose(fd);
  710. G_debug(1, "Dblinks written");
  711. return 0;
  712. }
  713. /*!
  714. \brief Substitute variable in string
  715. \param in current string
  716. \param Map pointer to Map_info structure
  717. \return pointer to new string
  718. */
  719. char *Vect_subst_var(const char *in, const struct Map_info *Map)
  720. {
  721. char *c;
  722. char buf[1000], str[1000];
  723. G_debug(3, "Vect_subst_var(): in = %s, map = %s, mapset = %s", in,
  724. Map->name, Map->mapset);
  725. #ifdef __MINGW32__
  726. char *cin;
  727. cin = G_str_replace(in, "/", "\\");
  728. strcpy(str, cin);
  729. G_free(cin);
  730. #else
  731. strcpy(str, in);
  732. #endif
  733. strcpy(buf, str);
  734. c = (char *)strstr(buf, "$GISDBASE");
  735. if (c != NULL) {
  736. *c = '\0';
  737. sprintf(str, "%s%s%s", buf, Map->gisdbase, c + 9);
  738. }
  739. strcpy(buf, str);
  740. c = (char *)strstr(buf, "$LOCATION_NAME");
  741. if (c != NULL) {
  742. *c = '\0';
  743. sprintf(str, "%s%s%s", buf, Map->location, c + 14);
  744. }
  745. strcpy(buf, str);
  746. c = (char *)strstr(buf, "$MAPSET");
  747. if (c != NULL) {
  748. *c = '\0';
  749. sprintf(str, "%s%s%s", buf, Map->mapset, c + 7);
  750. }
  751. strcpy(buf, str);
  752. c = (char *)strstr(buf, "$MAP");
  753. if (c != NULL) {
  754. *c = '\0';
  755. sprintf(str, "%s%s%s", buf, Map->name, c + 4);
  756. }
  757. G_debug(3, " -> %s", str);
  758. return (G_store(str));
  759. }
  760. /*!
  761. \brief Rewrite 'dbln' file
  762. Should be used by GRASS modules which update database tables, so
  763. that other applications know that tables were changed and can reload
  764. data.
  765. \param Map pointer to Map_info structure
  766. */
  767. void Vect_set_db_updated(struct Map_info *Map)
  768. {
  769. if (strcmp(Map->mapset, G_mapset()) != 0 &&
  770. G_strcasecmp(Map->mapset, "ogr") != 0) {
  771. G_fatal_error(_("Bug: attempt to update map which is not in current mapset"));
  772. }
  773. Vect_write_dblinks(Map);
  774. }