main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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-2017 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. #ifdef HAVE_OGR
  25. #include <ogr_api.h>
  26. #endif
  27. #include "local_proto.h"
  28. int main(int argc, char *argv[])
  29. {
  30. struct GModule *module;
  31. struct _options options;
  32. struct _flags flags;
  33. struct Map_info Map;
  34. FILE *fd;
  35. int ilayer, use_ogr;
  36. char buf[GPATH_MAX], *dsn, *layer;
  37. const char *output;
  38. struct Cell_head cellhd;
  39. ds_t Ogr_ds;
  40. G_gisinit(argv[0]);
  41. module = G_define_module();
  42. G_add_keyword(_("vector"));
  43. G_add_keyword(_("import"));
  44. G_add_keyword(_("external"));
  45. G_add_keyword("OGR");
  46. G_add_keyword("PostGIS");
  47. G_add_keyword(_("level1"));
  48. module->description = _("Creates a new pseudo-vector map as a link to an OGR-supported layer "
  49. "or a PostGIS feature table.");
  50. parse_args(argc, argv,
  51. &options, &flags);
  52. use_ogr = TRUE;
  53. G_debug(1, "GRASS_VECTOR_OGR defined? %s",
  54. getenv("GRASS_VECTOR_OGR") ? "yes" : "no");
  55. if (options.dsn->answer &&
  56. G_strncasecmp(options.dsn->answer, "PG:", 3) == 0) {
  57. /* -> PostgreSQL */
  58. #if defined HAVE_OGR && defined HAVE_POSTGRES
  59. if (getenv("GRASS_VECTOR_OGR"))
  60. use_ogr = TRUE;
  61. else
  62. use_ogr = FALSE;
  63. #else
  64. #ifdef HAVE_POSTGRES
  65. if (getenv("GRASS_VECTOR_OGR"))
  66. G_warning(_("Environment variable GRASS_VECTOR_OGR defined, "
  67. "but GRASS is compiled with OGR support. "
  68. "Using GRASS-PostGIS data driver instead."));
  69. use_ogr = FALSE;
  70. #else /* -> force using OGR */
  71. G_warning(_("GRASS is not compiled with PostgreSQL support. "
  72. "Using OGR-PostgreSQL driver instead of native "
  73. "GRASS-PostGIS data driver."));
  74. use_ogr = TRUE;
  75. #endif /* HAVE_POSTRES */
  76. #endif /* HAVE_OGR && HAVE_POSTGRES */
  77. }
  78. #ifdef HAVE_OGR
  79. /* GDAL drivers must be registered since check_projection()
  80. * depends on it (even use_ogr is false)*/
  81. OGRRegisterAll();
  82. #endif
  83. if (flags.format->answer) {
  84. /* list formats */
  85. list_formats(stdout);
  86. exit(EXIT_SUCCESS);
  87. }
  88. dsn = NULL;
  89. if (options.dsn->answer)
  90. dsn = get_datasource_name(options.dsn->answer, use_ogr);
  91. if (flags.list->answer || flags.tlist->answer) {
  92. /* list layers */
  93. if (!dsn)
  94. G_fatal_error(_("Required parameter <%s> not set"), options.dsn->key);
  95. list_layers(stdout, dsn, NULL,
  96. flags.tlist->answer ? TRUE : FALSE, use_ogr);
  97. exit(EXIT_SUCCESS);
  98. }
  99. /* get layer index/name */
  100. layer = NULL;
  101. if (options.layer->answer)
  102. layer = G_store(options.layer->answer);
  103. ilayer = list_layers(NULL, dsn, &layer,
  104. FALSE, use_ogr);
  105. if (ilayer == -1) {
  106. if (options.layer->answer)
  107. G_fatal_error(_("Layer <%s> not available"), options.layer->answer);
  108. else
  109. G_fatal_error(_("No layer defined"));
  110. }
  111. G_debug(2, "layer '%s' was found", layer);
  112. /* define name for output */
  113. if (!options.output->answer)
  114. output = layer;
  115. else
  116. output = options.output->answer;
  117. if (G_find_vector2(output, G_mapset()) && !G_check_overwrite(argc, argv)) {
  118. G_fatal_error(_("option <%s>: <%s> exists. To overwrite, use the --overwrite flag"),
  119. options.output->key, output);
  120. }
  121. /* open OGR DSN */
  122. Ogr_ds = NULL;
  123. if (strlen(options.dsn->answer) > 0) {
  124. #if GDAL_VERSION_NUM >= 2020000
  125. Ogr_ds = GDALOpenEx(options.dsn->answer, GDAL_OF_VECTOR, NULL, NULL, NULL);
  126. #else
  127. Ogr_ds = OGROpen(dsn, FALSE, NULL);
  128. #endif
  129. }
  130. if (Ogr_ds == NULL)
  131. G_fatal_error(_("Unable to open data source <%s>"), dsn);
  132. G_get_window(&cellhd);
  133. cellhd.north = 1.;
  134. cellhd.south = 0.;
  135. cellhd.west = 0.;
  136. cellhd.east = 1.;
  137. cellhd.top = 1.;
  138. cellhd.bottom = 0.;
  139. cellhd.rows = 1;
  140. cellhd.rows3 = 1;
  141. cellhd.cols = 1;
  142. cellhd.cols3 = 1;
  143. cellhd.depths = 1;
  144. cellhd.ns_res = 1.;
  145. cellhd.ns_res3 = 1.;
  146. cellhd.ew_res = 1.;
  147. cellhd.ew_res3 = 1.;
  148. cellhd.tb_res = 1.;
  149. /* check projection match */
  150. check_projection(&cellhd, Ogr_ds, ilayer, NULL, NULL, 0,
  151. flags.override->answer, flags.proj->answer);
  152. ds_close(Ogr_ds);
  153. /* create new vector map */
  154. putenv("GRASS_VECTOR_EXTERNAL_IGNORE=1");
  155. if (Vect_open_new(&Map, output, WITHOUT_Z) < 0) /* dimension is set later from data source */
  156. G_fatal_error(_("Unable to create vector map <%s>"), output);
  157. Vect_set_error_handler_io(NULL, &Map);
  158. Vect_hist_command(&Map);
  159. Vect_close(&Map);
  160. /* Vect_open_new created 'head', 'coor', 'hist'
  161. -> delete 'coor' and create 'frmt' */
  162. sprintf(buf, "%s/%s/%s/%s/coor", G_location_path(), G_mapset(),
  163. GV_DIRECTORY, output);
  164. G_debug(2, "Delete '%s'", buf);
  165. if (unlink(buf) == -1) {
  166. G_fatal_error(_("Unable to delete '%s'"), buf);
  167. }
  168. /* create frmt file */
  169. sprintf(buf, "%s/%s", GV_DIRECTORY, output);
  170. fd = G_fopen_new(buf, GV_FRMT_ELEMENT);
  171. if (fd == NULL)
  172. G_fatal_error(_("Unable to create file '%s/%s'"), buf, GV_FRMT_ELEMENT);
  173. if (!use_ogr) {
  174. char *table_name, *schema_name;
  175. get_table_name(layer, &table_name, &schema_name);
  176. fprintf(fd, "format: postgis\n");
  177. fprintf(fd, "conninfo: %s\n", dsn);
  178. if (schema_name)
  179. fprintf(fd, "schema: %s\n", schema_name);
  180. fprintf(fd, "table: %s\n", table_name);
  181. G_free(table_name);
  182. G_free(schema_name);
  183. }
  184. else {
  185. fprintf(fd, "format: ogr\n");
  186. fprintf(fd, "dsn: %s\n", dsn);
  187. fprintf(fd, "layer: %s\n", layer);
  188. }
  189. if (options.where->answer)
  190. fprintf(fd, "where: %s\n", options.where->answer);
  191. fclose(fd);
  192. if (!flags.topo->answer) {
  193. Vect_set_open_level(1);
  194. if (Vect_open_old(&Map, output, G_mapset()) < 0)
  195. G_fatal_error(_("Unable to open vector map <%s>"), output);
  196. Vect_build(&Map);
  197. Vect_close(&Map);
  198. }
  199. G_done_msg(_("Link to vector map <%s> created."), output);
  200. exit(EXIT_SUCCESS);
  201. }