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