create.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <unistd.h>
  2. #include <grass/gis.h>
  3. #include <grass/glocale.h>
  4. #include "local_proto.h"
  5. static void file_handler(void *);
  6. char *create_pgfile(const char *dsn, const char *schema, const char *olink,
  7. char **options, int topo,
  8. char **fid_column, char **geom_column)
  9. {
  10. int i;
  11. const char *epsg;
  12. char *filename, *conninfo;
  13. char buf[GPATH_MAX];
  14. FILE *fp;
  15. struct Key_Value *key_val;
  16. filename = NULL;
  17. G_asprintf(&filename, "PG_%d", (int) getpid());
  18. G_debug(1, "PG file: %s", filename);
  19. fp = G_fopen_new("", filename);
  20. if (!fp)
  21. G_fatal_error(_("Unable to create <%s> file"), filename);
  22. sprintf(buf, "GRASS_VECTOR_PGFILE=%s", filename);
  23. putenv(G_store(buf));
  24. G_add_error_handler(file_handler, filename);
  25. key_val = G_create_key_value();
  26. /* be friendly, ignored 'PG:' prefix for GRASS-PostGIS data driver */
  27. if (G_strncasecmp(dsn, "PG:", 3) == 0) {
  28. int length;
  29. length = strlen(dsn);
  30. conninfo = (char *) G_malloc(length - 2);
  31. for (i = 3; i < length; i++)
  32. conninfo[i-3] = dsn[i];
  33. conninfo[length-3] = '\0';
  34. }
  35. else {
  36. conninfo = G_store(dsn);
  37. }
  38. /* required options */
  39. G_set_key_value("conninfo", conninfo, key_val);
  40. if (schema)
  41. G_set_key_value("schema", schema, key_val);
  42. if (topo)
  43. G_set_key_value("topology", "yes", key_val);
  44. /* is EPSG defined */
  45. epsg = G_database_epsg_code();
  46. /* extra options */
  47. if (options) {
  48. char **tokens;
  49. for(i = 0; options[i]; i++) {
  50. tokens = G_tokenize(options[i], "=");
  51. if (G_number_of_tokens(tokens) != 2) {
  52. G_warning(_("Invalid option skipped: %s"), options[i]);
  53. continue;
  54. }
  55. G_debug(1, "option: %s=%s", tokens[0], tokens[1]);
  56. /* force lower case */
  57. G_str_to_lower(tokens[0]);
  58. /* strip whitespace for key/value */
  59. G_strip(tokens[0]);
  60. G_strip(tokens[1]);
  61. if (strcmp(tokens[0], "srid") == 0 && (epsg && strcmp(tokens[1], epsg) != 0))
  62. G_warning(_("EPSG code defined for current location (%s) is overridden by %s"),
  63. epsg, tokens[1]);
  64. G_set_key_value(tokens[0], tokens[1], key_val);
  65. if (strcmp(tokens[0], "fid") == 0)
  66. G_asprintf(fid_column, "%s", tokens[1]);
  67. if (strcmp(tokens[0], "geometry_name") == 0)
  68. G_asprintf(geom_column, "%s", tokens[1]);
  69. G_free_tokens(tokens);
  70. }
  71. }
  72. /* check EPSG code if defined as an option */
  73. if (epsg && !G_find_key_value("srid", key_val))
  74. G_set_key_value("srid", epsg, key_val);
  75. if (olink) {
  76. /* create a link for output feature table */
  77. G_set_key_value("link", "yes", key_val);
  78. G_set_key_value("link_name", olink, key_val);
  79. }
  80. else {
  81. G_set_key_value("link", "no", key_val);
  82. }
  83. if (G_fwrite_key_value(fp, key_val) < 0)
  84. G_fatal_error(_("Error writing <%s> file"), filename);
  85. fclose(fp);
  86. G_free(conninfo);
  87. return filename;
  88. }
  89. void file_handler(void *p) {
  90. const char *filename = (const char *) p;
  91. G_debug(1, "file_handler: %s", filename);
  92. G_remove("", filename);
  93. putenv("GRASS_VECTOR_PGFILE=");
  94. }