main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /****************************************************************
  2. *
  3. * MODULE: v.external
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. * Updated to GRASS 7 by Martin Landa <landa.martin gmail.com>
  7. *
  8. * PURPOSE: Create a new vector as a link to OGR layer
  9. *
  10. * COPYRIGHT: (C) 2003-2011 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General
  13. * Public License (>=v2). Read the file COPYING that
  14. * comes with GRASS for details.
  15. *
  16. **************************************************************/
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <grass/gis.h>
  21. #include <grass/dbmi.h>
  22. #include <grass/vector.h>
  23. #include <grass/glocale.h>
  24. #include <ogr_api.h>
  25. #include "local_proto.h"
  26. int main(int argc, char *argv[])
  27. {
  28. struct GModule *module;
  29. struct _options options;
  30. struct _flags flags;
  31. struct Map_info Map;
  32. FILE *fd;
  33. int ilayer, is3D, use_ogr;
  34. char buf[GPATH_MAX], *dsn;
  35. const char *output;
  36. G_gisinit(argv[0]);
  37. module = G_define_module();
  38. G_add_keyword(_("vector"));
  39. G_add_keyword(_("import"));
  40. G_add_keyword(_("input"));
  41. G_add_keyword(_("external"));
  42. G_add_keyword("OGR");
  43. G_add_keyword("PostGIS");
  44. module->description = _("Creates a new pseudo-vector map as a link to an OGR-supported layer "
  45. "or a PostGIS feature table.");
  46. parse_args(argc, argv,
  47. &options, &flags);
  48. use_ogr = TRUE;
  49. if(options.dsn->answer &&
  50. G_strncasecmp(options.dsn->answer, "PG:", 3) == 0) {
  51. /* -> PostgreSQL */
  52. #if defined HAVE_OGR && defined HAVE_POSTGRES
  53. if (getenv("GRASS_VECTOR_OGR"))
  54. use_ogr = TRUE;
  55. else
  56. use_ogr = FALSE;
  57. #else
  58. #ifdef HAVE_POSTGRES
  59. if (getenv("GRASS_VECTOR_OGR"))
  60. G_warning(_("Environment variable GRASS_VECTOR_OGR defined, "
  61. "but GRASS is compiled with OGR support. "
  62. "Using GRASS-PostGIS data driver instead."));
  63. use_ogr = FALSE;
  64. #else /* -> force using OGR */
  65. G_warning(_("GRASS is not compiled with PostgreSQL support. "
  66. "Using OGR-PostgreSQL driver instead of native "
  67. "GRASS-PostGIS data driver."));
  68. use_ogr = TRUE;
  69. #endif /* HAVE_POSTRES */
  70. #endif /* HAVE_OGR && HAVE_POSTGRES */
  71. }
  72. #ifdef HAVE_OGR
  73. if (use_ogr)
  74. OGRRegisterAll();
  75. #endif
  76. if (flags.format->answer) {
  77. /* list formats */
  78. list_formats(stdout);
  79. exit(EXIT_SUCCESS);
  80. }
  81. /* be friendly, ignored 'PG:' prefix for PostGIS links */
  82. dsn = NULL;
  83. if (options.dsn->answer) {
  84. if (!use_ogr) {
  85. int i, length;
  86. length = strlen(options.dsn->answer);
  87. dsn = (char *) G_malloc(length - 3);
  88. for (i = 3; i < length; i++)
  89. dsn[i-3] = options.dsn->answer[i];
  90. dsn[length-3] = '\0';
  91. }
  92. else {
  93. dsn = G_store(options.dsn->answer);
  94. }
  95. }
  96. if (flags.list->answer || flags.tlist->answer) {
  97. /* list layers */
  98. if (!dsn)
  99. G_fatal_error(_("Required parameter <%s> not set"), options.dsn->key);
  100. list_layers(stdout, dsn, NULL,
  101. flags.tlist->answer ? TRUE : FALSE,
  102. use_ogr, NULL);
  103. exit(EXIT_SUCCESS);
  104. }
  105. /* define name for output */
  106. if (!options.output->answer)
  107. output = options.layer->answer;
  108. else
  109. output = options.output->answer;
  110. /* get layer index */
  111. ilayer = list_layers(NULL, dsn, options.layer->answer,
  112. FALSE, use_ogr, &is3D);
  113. if (ilayer == -1) {
  114. G_fatal_error(_("Layer <%s> not available"), options.layer->answer);
  115. }
  116. G_debug(2, "layer '%s' was found", options.layer->answer);
  117. if (G_find_vector2(output, G_mapset()) && !G_check_overwrite(argc, argv)) {
  118. G_fatal_error(_("option <%s>: <%s> exists."),
  119. options.output->key, output);
  120. }
  121. /* create new vector map */
  122. Vect_open_new(&Map, output, is3D);
  123. Vect_set_error_handler_io(NULL, &Map);
  124. Vect_hist_command(&Map);
  125. Vect_close(&Map);
  126. /* Vect_open_new created 'head', 'coor', 'hist'
  127. -> delete 'coor' and create 'frmt' */
  128. sprintf(buf, "%s/%s/%s/%s/coor", G_location_path(), G_mapset(),
  129. GV_DIRECTORY, output);
  130. G_debug(2, "Delete '%s'", buf);
  131. if (unlink(buf) == -1) {
  132. Vect_delete(output);
  133. G_fatal_error(_("Unable to delete '%s'"), buf);
  134. }
  135. /* create frmt file */
  136. sprintf(buf, "%s/%s", GV_DIRECTORY, output);
  137. fd = G_fopen_new(buf, GV_FRMT_ELEMENT);
  138. if (fd == NULL) {
  139. Vect_delete(output);
  140. G_fatal_error("Unable to create file '%s'", buf);
  141. }
  142. if (!use_ogr) {
  143. char *table_name, *schema_name;
  144. get_table_name(options.layer->answer, &table_name, &schema_name);
  145. fprintf(fd, "FORMAT: postgis\n");
  146. fprintf(fd, "CONNINFO: %s\n", dsn);
  147. if (schema_name)
  148. fprintf(fd, "SCHEMA: %s\n", schema_name);
  149. fprintf(fd, "TABLE: %s\n", table_name);
  150. G_free(table_name);
  151. G_free(schema_name);
  152. }
  153. else {
  154. fprintf(fd, "FORMAT: ogr\n");
  155. fprintf(fd, "DSN: %s\n", dsn);
  156. fprintf(fd, "LAYER: %s\n", options.layer->answer);
  157. }
  158. fclose(fd);
  159. if (!flags.topo->answer) {
  160. Vect_open_old(&Map, output, G_mapset());
  161. Vect_build(&Map);
  162. Vect_close(&Map);
  163. }
  164. G_done_msg(_("Link to vector map <%s> created."), output);
  165. exit(EXIT_SUCCESS);
  166. }