field.c 27 KB

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