field.c 25 KB

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