field.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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], buf2[GNAME_MAX];
  291. const char *schema;
  292. dbConnection connection;
  293. G_debug(1, "Vect_default_field_info(): map = %s field = %d", Map->name,
  294. field);
  295. if (Map->format == GV_FORMAT_OGR_DIRECT) {
  296. G_zero(&connection, sizeof(dbConnection));
  297. connection.driverName = G_store("ogr");
  298. connection.databaseName = G_store(Map->fInfo.ogr.dsn);
  299. }
  300. else {
  301. db_get_connection(&connection);
  302. }
  303. G_debug(2, "drv = %s db = %s", connection.driverName, connection.databaseName);
  304. if (!connection.driverName && !connection.databaseName) {
  305. /* Set default values */
  306. db_set_default_connection();
  307. db_get_connection(&connection);
  308. G_important_message(_("Default driver / database set to:\n"
  309. "driver: %s\ndatabase: %s"), connection.driverName,
  310. connection.databaseName);
  311. }
  312. /* they must be a matched pair, so if one is set but not the other
  313. then give up and let the user figure it out */
  314. else if (!connection.driverName) {
  315. G_fatal_error(_("Default driver is not set"));
  316. }
  317. else if (!connection.databaseName) {
  318. G_fatal_error(_("Default database is not set"));
  319. }
  320. fi = (struct field_info *)G_malloc(sizeof(struct field_info));
  321. fi->number = field;
  322. /* Field name */
  323. fi->name = NULL;
  324. if (field_name && *field_name) {
  325. fi->name = G_store(field_name);
  326. if (!name2sql(fi->name)) {
  327. G_free(fi->name);
  328. fi->name = NULL;
  329. }
  330. }
  331. /* Table name */
  332. if (type == GV_1TABLE) {
  333. sprintf(buf, "%s", Map->name);
  334. }
  335. else {
  336. if (fi->name != NULL && strlen(fi->name) > 0) {
  337. sprintf(buf, "%s_%s", Map->name, fi->name);
  338. if (!name2sql(buf)) {
  339. sprintf(buf, "%s_%d", Map->name, field);
  340. }
  341. }
  342. else
  343. sprintf(buf, "%s_%d", Map->name, field);
  344. }
  345. schema = connection.schemaName;
  346. if (schema && strlen(schema) > 0) {
  347. sprintf(buf2, "%s.%s", schema, buf);
  348. fi->table = G_store(buf2);
  349. }
  350. else {
  351. fi->table = G_store(buf);
  352. }
  353. /* Field name still empty */
  354. if (!fi->name)
  355. fi->name = G_store(buf);
  356. fi->key = G_store(GV_KEY_COLUMN); /* Should be: id/fid/gfid/... ? */
  357. #ifdef TEMPORARY_MAP_DB
  358. if (Map->temporary) {
  359. Vect__get_element_path(buf, Map, NULL);
  360. if (strcmp(DB_DEFAULT_DRIVER, "sqlite") == 0)
  361. strcat(buf, "/sqlite.db");
  362. else
  363. strcat(buf, "/db.dbf");
  364. fi->database = G_store(buf);
  365. fi->driver = DB_DEFAULT_DRIVER;
  366. }
  367. else {
  368. fi->database = G_store(connection.databaseName);
  369. fi->driver = G_store(connection.driverName);
  370. }
  371. #else
  372. fi->database = G_store(connection.databaseName);
  373. fi->driver = G_store(connection.driverName);
  374. #endif
  375. return fi;
  376. }
  377. /*!
  378. \brief Get information about link to database.
  379. Variables are substituted by values, link is index to array of
  380. dblinks.
  381. \param Map pointer to Map_info structure
  382. \param link link id
  383. \return pointer to new field_info structure
  384. */
  385. struct field_info *Vect_get_dblink(const struct Map_info *Map, int link)
  386. {
  387. struct field_info *fi;
  388. G_debug(1, "Vect_get_dblink(): link = %d", link);
  389. if (link >= Map->dblnk->n_fields) {
  390. G_warning(_("Requested dblink %d, maximum link number %d"), link,
  391. Map->dblnk->n_fields - 1);
  392. return NULL;
  393. }
  394. fi = (struct field_info *)G_malloc(sizeof(struct field_info));
  395. fi->number = Map->dblnk->field[link].number;
  396. if (Map->dblnk->field[link].name != NULL)
  397. fi->name = G_store(Map->dblnk->field[link].name);
  398. else
  399. fi->name = NULL;
  400. fi->table = G_store(Map->dblnk->field[link].table);
  401. fi->key = G_store(Map->dblnk->field[link].key);
  402. fi->database = Vect_subst_var(Map->dblnk->field[link].database, Map);
  403. fi->driver = G_store(Map->dblnk->field[link].driver);
  404. return fi;
  405. }
  406. /*!
  407. \brief Get information about link to database (by layer number)
  408. Variables are substituted by values, field is number of requested
  409. field.
  410. \param Map pointer to Map_info structure
  411. \param field layer number
  412. \return pointer to new field_info structure
  413. \return NULL if not found
  414. */
  415. struct field_info *Vect_get_field(const struct Map_info *Map, int field)
  416. {
  417. int i;
  418. struct field_info *fi = NULL;
  419. G_debug(1, "Vect_get_field(): field = %d", field);
  420. for (i = 0; i < Map->dblnk->n_fields; i++) {
  421. if (Map->dblnk->field[i].number == field) {
  422. fi = Vect_get_dblink(Map, i);
  423. break;
  424. }
  425. }
  426. return fi;
  427. }
  428. /*!
  429. \brief Get information about link to database (by layer name)
  430. \param Map pointer to Map_info structure
  431. \param field layer name
  432. \return pointer to new field_info structure
  433. \return NULL if not found
  434. */
  435. struct field_info *Vect_get_field_by_name(const struct Map_info *Map, const char *field)
  436. {
  437. int i;
  438. struct field_info *fi = NULL;
  439. G_debug(1, "Vect_get_field_by_name(): field = %s", field);
  440. for (i = 0; i < Map->dblnk->n_fields; i++) {
  441. if (strcmp(Map->dblnk->field[i].name, field) == 0) {
  442. fi = Vect_get_dblink(Map, i);
  443. break;
  444. }
  445. }
  446. return fi;
  447. }
  448. /*!
  449. \brief Get information about link to database (by layer number or layer name)
  450. Note: if <em>field</em> is -1 then the function returns the first
  451. dblink or NULL
  452. \param Map pointer to Map_info structure
  453. \param field layer number or name
  454. \return pointer to new field_info structure
  455. \return NULL if not found
  456. */
  457. struct field_info *Vect_get_field2(const struct Map_info *Map, const char *field)
  458. {
  459. int ifield;
  460. struct field_info *fi;
  461. G_debug(1, "Vect_get_field2(): field = %s", field);
  462. fi = NULL;
  463. ifield = atoi(field);
  464. if (ifield > 0) {
  465. fi = Vect_get_field(Map, ifield);
  466. if (fi)
  467. return fi;
  468. }
  469. else if (ifield == -1) {
  470. if (Vect_get_num_dblinks(Map) > 0)
  471. return Vect_get_dblink(Map, 0); /* return first */
  472. else
  473. return NULL;
  474. }
  475. else if (ifield == 0)
  476. return Vect_get_field_by_name(Map, field);
  477. return NULL;
  478. }
  479. /*!
  480. \brief Get field number of given field
  481. \param Map pointer to Map_info structure
  482. \param field layer name
  483. \return layer number
  484. \return -1 for all layers
  485. \return 0 if layer not found
  486. */
  487. int Vect_get_field_number(const struct Map_info *Map, const char *field)
  488. {
  489. struct field_info *fi;
  490. G_debug(1, "Vect_get_field_number(): field = %s", field);
  491. if (strcmp(field, "-1") == 0)
  492. return -1;
  493. if (Vect_get_num_dblinks(Map) == 0)
  494. return atoi(field);
  495. fi = Vect_get_field2(Map, field);
  496. if (fi)
  497. return fi->number;
  498. return atoi(field);
  499. }
  500. static int read_dblinks_nat(struct Map_info *Map)
  501. {
  502. FILE *fd;
  503. char file[1024], buf[2001];
  504. char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname;
  505. int fld;
  506. char *c, path[GPATH_MAX];
  507. int row, rule;
  508. struct dblinks *dbl;
  509. char **tokens;
  510. int ntok, i;
  511. dbl = Map->dblnk;
  512. /* Read dblink for native format */
  513. Vect__get_path(path, Map);
  514. fd = G_fopen_old(path, GV_DBLN_ELEMENT, Map->mapset);
  515. if (fd == NULL) { /* This may be correct, no tables defined */
  516. G_debug(1, "Cannot open vector database definition file");
  517. return -1;
  518. }
  519. row = 0;
  520. rule = 0;
  521. while (G_getl2(buf, 2000, fd)) {
  522. row++;
  523. G_chop(buf);
  524. G_debug(1, "dbln: %s", buf);
  525. c = (char *)strchr(buf, '#');
  526. if (c != NULL)
  527. *c = '\0';
  528. if (strlen(buf) == 0)
  529. continue;
  530. #ifdef NOT_ABLE_TO_READ_GRASS_6
  531. int ndef;
  532. ndef = sscanf(buf, "%s|%s|%s|%s|%s", fldstr, tab, col, db, drv);
  533. if (ndef < 2 || (ndef < 5 && rule < 1)) {
  534. G_warning(_("Error in rule on row %d in <%s>"), row, file);
  535. continue;
  536. }
  537. #else
  538. tokens = G_tokenize(buf, " |");
  539. ntok = G_number_of_tokens(tokens);
  540. if (ntok < 2 || (ntok < 5 && rule < 1)) {
  541. G_warning(_("Error in rule on row %d in <%s>"), row, file);
  542. continue;
  543. }
  544. strcpy(fldstr, tokens[0]);
  545. strcpy(tab, tokens[1]);
  546. if (ntok > 2) {
  547. strcpy(col, tokens[2]);
  548. if (ntok > 3) {
  549. strcpy(db, tokens[3]);
  550. /* allow for spaces in path names */
  551. for (i=4; i < ntok-1; i++) {
  552. strcat(db, " ");
  553. strcat(db, tokens[i]);
  554. }
  555. strcpy(drv, tokens[ntok-1]);
  556. }
  557. }
  558. G_free_tokens(tokens);
  559. #endif
  560. /* get field and field name */
  561. fldname = strchr(fldstr, '/');
  562. if (fldname != NULL) { /* field has name */
  563. fldname[0] = 0;
  564. fldname++;
  565. }
  566. fld = atoi(fldstr);
  567. Vect_add_dblink(dbl, fld, fldname, tab, col, db, drv);
  568. G_debug(1,
  569. "field = %d name = %s, table = %s, key = %s, database = %s, driver = %s",
  570. fld, fldname, tab, col, db, drv);
  571. rule++;
  572. }
  573. fclose(fd);
  574. G_debug(1, "Dblinks read");
  575. return rule;
  576. }
  577. /* return -1 on error */
  578. static int read_dblinks_ogr(struct Map_info *Map)
  579. {
  580. struct dblinks *dbl;
  581. dbl = Map->dblnk;
  582. G_debug(3, "Searching for FID column in OGR DB");
  583. #ifndef HAVE_OGR
  584. G_warning(_("GRASS is not compiled with OGR support"));
  585. #else
  586. #if GDAL_VERSION_NUM > 1320 && HAVE_OGR /* seems to be fixed after 1320 release */
  587. int nLayers;
  588. char *ogr_fid_col;
  589. G_debug(3, "GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
  590. if (Map->fInfo.ogr.ds == NULL) {
  591. /* open the connection to fetch the FID column name */
  592. OGRRegisterAll();
  593. /* data source handle */
  594. Map->fInfo.ogr.ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
  595. if (Map->fInfo.ogr.ds == NULL) {
  596. G_warning(_("Unable to open OGR data source '%s'"),
  597. Map->fInfo.ogr.dsn);
  598. return -1;
  599. }
  600. }
  601. if (Map->fInfo.ogr.layer == NULL) {
  602. /* get layer number */
  603. nLayers = OGR_DS_GetLayerCount(Map->fInfo.ogr.ds); /* Layers = Maps in OGR DB */
  604. G_debug(3, "%d layers (maps) found in data source", nLayers);
  605. G_debug(3, "Trying to open OGR layer: %s", Map->fInfo.ogr.layer_name);
  606. if (Map->fInfo.ogr.layer_name) {
  607. Map->fInfo.ogr.layer = OGR_DS_GetLayerByName(Map->fInfo.ogr.ds, Map->fInfo.ogr.layer_name);
  608. if (Map->fInfo.ogr.layer == NULL) {
  609. OGR_DS_Destroy(Map->fInfo.ogr.ds);
  610. Map->fInfo.ogr.ds = NULL;
  611. G_warning(_("Unable to open OGR layer <%s>"),
  612. Map->fInfo.ogr.layer_name);
  613. return -1;
  614. }
  615. }
  616. }
  617. /* get fid column */
  618. ogr_fid_col = G_store(OGR_L_GetFIDColumn(Map->fInfo.ogr.layer));
  619. G_debug(3, "Using FID column <%s> in OGR DB", ogr_fid_col);
  620. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name,
  621. Map->fInfo.ogr.layer_name, ogr_fid_col,
  622. Map->fInfo.ogr.dsn, "ogr");
  623. #else
  624. dbDriver *driver;
  625. dbCursor cursor;
  626. dbString sql;
  627. int FID = 0, OGC_FID = 0, OGR_FID = 0, GID = 0;
  628. G_debug(3, "GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
  629. /* FID is not available for all OGR drivers */
  630. db_init_string(&sql);
  631. driver = db_start_driver_open_database("ogr", Map->fInfo.ogr.dsn);
  632. if (driver == NULL) {
  633. G_warning(_("Unable to open OGR DBMI driver"));
  634. return -1;
  635. }
  636. /* this is a bit stupid, but above FID auto-detection doesn't work yet...: */
  637. db_auto_print_errors(0);
  638. sprintf(buf, "select FID from %s where FID > 0",
  639. Map->fInfo.ogr.layer_name);
  640. db_set_string(&sql, buf);
  641. if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
  642. DB_OK) {
  643. /* FID not available, so we try ogc_fid */
  644. G_debug(3, "Failed. Now searching for ogc_fid column in OGR DB");
  645. sprintf(buf, "select ogc_fid from %s where ogc_fid > 0",
  646. Map->fInfo.ogr.layer_name);
  647. db_set_string(&sql, buf);
  648. if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
  649. DB_OK) {
  650. /* Neither FID nor ogc_fid available, so we try ogr_fid */
  651. G_debug(3,
  652. "Failed. Now searching for ogr_fid column in OGR DB");
  653. sprintf(buf, "select ogr_fid from %s where ogr_fid > 0",
  654. Map->fInfo.ogr.layer_name);
  655. db_set_string(&sql, buf);
  656. if (db_open_select_cursor
  657. (driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
  658. /* Neither FID nor ogc_fid available, so we try gid */
  659. G_debug(3,
  660. "Failed. Now searching for gid column in OGR DB");
  661. sprintf(buf, "select gid from %s where gid > 0",
  662. Map->fInfo.ogr.layer_name);
  663. db_set_string(&sql, buf);
  664. if (db_open_select_cursor
  665. (driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
  666. /* neither FID nor ogc_fid nor ogr_fid nor gid available */
  667. G_warning(_("All FID tests failed. Neither 'FID' nor 'ogc_fid' "
  668. "nor 'ogr_fid' nor 'gid' available in OGR DB table"));
  669. db_close_database_shutdown_driver(driver);
  670. return 0;
  671. }
  672. else
  673. GID = 1;
  674. }
  675. else
  676. OGR_FID = 1;
  677. }
  678. else
  679. OGC_FID = 1;
  680. }
  681. else
  682. FID = 1;
  683. G_debug(3, "FID: %d, OGC_FID: %d, OGR_FID: %d, GID: %d", FID, OGC_FID,
  684. OGR_FID, GID);
  685. db_close_cursor(&cursor);
  686. db_close_database_shutdown_driver(driver);
  687. db_auto_print_errors(1);
  688. if (FID) {
  689. G_debug(3, "Using FID column in OGR DB");
  690. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name, Map->fInfo.ogr.layer_name, "FID",
  691. Map->fInfo.ogr.dsn, "ogr");
  692. }
  693. else {
  694. if (OGC_FID) {
  695. G_debug(3, "Using ogc_fid column in OGR DB");
  696. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name, Map->fInfo.ogr.layer_name,
  697. "ogc_fid", Map->fInfo.ogr.dsn, "ogr");
  698. }
  699. else {
  700. if (OGR_FID) {
  701. G_debug(3, "Using ogr_fid column in OGR DB");
  702. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name, Map->fInfo.ogr.layer_name,
  703. "ogr_fid", Map->fInfo.ogr.dsn, "ogr");
  704. }
  705. else {
  706. if (GID) {
  707. G_debug(3, "Using gid column in OGR DB");
  708. Vect_add_dblink(dbl, 1, Map->fInfo.ogr.layer_name,
  709. Map->fInfo.ogr.layer_name, "gid",
  710. Map->fInfo.ogr.dsn, "ogr");
  711. }
  712. }
  713. }
  714. }
  715. #endif /* GDAL_VERSION_NUM > 1320 && HAVE_OGR */
  716. return 1;
  717. #endif /* HAVE_GDAL */
  718. }
  719. static int read_dblinks_pg(struct Map_info *Map)
  720. {
  721. #ifdef HAVE_POSTGRES
  722. char *name;
  723. struct dblinks *dbl;
  724. struct Format_info_pg *pg_info;
  725. dbl = Map->dblnk;
  726. pg_info = &(Map->fInfo.pg);
  727. if (!pg_info->fid_column) {
  728. G_warning(_("Feature table <%s> has no primary key defined. "
  729. "Unable to define DB links."), pg_info->table_name);
  730. return -1;
  731. }
  732. G_debug(3, "Using FID column <%s>", pg_info->fid_column);
  733. name = NULL;
  734. if (G_strcasecmp(pg_info->schema_name, "public") != 0)
  735. G_asprintf(&name, "%s.%s", pg_info->schema_name,
  736. pg_info->table_name);
  737. else
  738. name = pg_info->table_name;
  739. Vect_add_dblink(dbl, 1, name, name,
  740. pg_info->fid_column,
  741. pg_info->db_name, "pg");
  742. if (name != pg_info->table_name)
  743. G_free(name);
  744. return 1;
  745. #else
  746. G_warning(_("GRASS not compiled with PostgreSQL support"));
  747. return -1;
  748. #endif
  749. }
  750. /*!
  751. \brief Read dblinks to existing structure.
  752. Variables are not substituted by values.
  753. \param Map pointer to Map_info structure
  754. \return number of links read
  755. \return -1 on error
  756. */
  757. int Vect_read_dblinks(struct Map_info *Map)
  758. {
  759. G_debug(1, "Vect_read_dblinks(): map = %s, mapset = %s", Map->name,
  760. Map->mapset);
  761. Vect_reset_dblinks(Map->dblnk);
  762. if (Map->format == GV_FORMAT_NATIVE) {
  763. return read_dblinks_nat(Map);
  764. }
  765. else if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_OGR_DIRECT) {
  766. return read_dblinks_ogr(Map);
  767. }
  768. else if (Map->format == GV_FORMAT_POSTGIS) {
  769. return read_dblinks_pg(Map);
  770. }
  771. else {
  772. G_fatal_error(_("Unknown vector map format"));
  773. }
  774. return -1;
  775. }
  776. /*!
  777. \brief Write dblinks to file
  778. \param Map pointer to Map_info structure
  779. \return 0 on success
  780. \return -1 on error
  781. */
  782. int Vect_write_dblinks(struct Map_info *Map)
  783. {
  784. int i;
  785. FILE *fd;
  786. char path[GPATH_MAX], buf[1024];
  787. struct dblinks *dbl;
  788. if (Map->format != GV_FORMAT_NATIVE)
  789. /* nothing to write for non-native formats */
  790. return 0;
  791. G_debug(1, "Vect_write_dblinks(): map = %s, mapset = %s", Map->name,
  792. Map->mapset);
  793. dbl = Map->dblnk;
  794. Vect__get_path(path, Map);
  795. fd = G_fopen_new(path, GV_DBLN_ELEMENT);
  796. if (fd == NULL) { /* This may be correct, no tables defined */
  797. G_warning(_("Unable to create database definition file for vector map <%s>"),
  798. Vect_get_name(Map));
  799. return -1;
  800. }
  801. for (i = 0; i < dbl->n_fields; i++) {
  802. if (dbl->field[i].name != NULL)
  803. sprintf(buf, "%d/%s", dbl->field[i].number, dbl->field[i].name);
  804. else
  805. sprintf(buf, "%d", dbl->field[i].number);
  806. fprintf(fd, "%s|%s|%s|%s|%s\n", buf, dbl->field[i].table,
  807. dbl->field[i].key, dbl->field[i].database,
  808. dbl->field[i].driver);
  809. G_debug(1, "%s|%s|%s|%s|%s", buf, dbl->field[i].table,
  810. dbl->field[i].key, dbl->field[i].database,
  811. dbl->field[i].driver);
  812. }
  813. fclose(fd);
  814. G_debug(1, "Dblinks written");
  815. return 0;
  816. }
  817. /*!
  818. \brief Substitute variable in string
  819. \param in current string
  820. \param Map pointer to Map_info structure
  821. \return pointer to new string
  822. */
  823. char *Vect_subst_var(const char *in, const struct Map_info *Map)
  824. {
  825. char *c;
  826. char buf[1000], str[1000];
  827. G_debug(3, "Vect_subst_var(): in = %s, map = %s, mapset = %s", in,
  828. Map->name, Map->mapset);
  829. #ifdef __MINGW32__
  830. char *cin;
  831. cin = G_str_replace(in, "/", "\\");
  832. strcpy(str, cin);
  833. G_free(cin);
  834. #else
  835. strcpy(str, in);
  836. #endif
  837. strcpy(buf, str);
  838. c = (char *)strstr(buf, "$GISDBASE");
  839. if (c != NULL) {
  840. *c = '\0';
  841. sprintf(str, "%s%s%s", buf, Map->gisdbase, c + 9);
  842. }
  843. strcpy(buf, str);
  844. c = (char *)strstr(buf, "$LOCATION_NAME");
  845. if (c != NULL) {
  846. *c = '\0';
  847. sprintf(str, "%s%s%s", buf, Map->location, c + 14);
  848. }
  849. strcpy(buf, str);
  850. c = (char *)strstr(buf, "$MAPSET");
  851. if (c != NULL) {
  852. *c = '\0';
  853. sprintf(str, "%s%s%s", buf, Map->mapset, c + 7);
  854. }
  855. strcpy(buf, str);
  856. c = (char *)strstr(buf, "$MAP");
  857. if (c != NULL) {
  858. *c = '\0';
  859. sprintf(str, "%s%s%s", buf, Map->name, c + 4);
  860. }
  861. G_debug(3, " -> %s", str);
  862. return (G_store(str));
  863. }
  864. /*!
  865. \brief Rewrite 'dbln' file
  866. Should be used by GRASS modules which update database tables, so
  867. that other applications know that tables were changed and can reload
  868. data.
  869. \param Map pointer to Map_info structure
  870. */
  871. void Vect_set_db_updated(struct Map_info *Map)
  872. {
  873. if (strcmp(Map->mapset, G_mapset()) != 0 &&
  874. G_strcasecmp(Map->mapset, "ogr") != 0) {
  875. G_fatal_error(_("Bug: attempt to update map which is not in current mapset"));
  876. }
  877. Vect_write_dblinks(Map);
  878. }