main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /****************************************************************
  2. *
  3. * MODULE: v.external
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. * Updated by Martin Landa <landa.martin gmail.com> (2009)
  7. *
  8. * PURPOSE: Create a new vector as a link to OGR layer (read-only)
  9. *
  10. * COPYRIGHT: (C) 2003-2010 by the GRASS Development Team
  11. *
  12. * This program is free software under the
  13. * GNU General Public License (>=v2).
  14. * Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. **************************************************************/
  18. #include <grass/config.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <grass/gis.h>
  23. #include <grass/dbmi.h>
  24. #include <grass/vector.h>
  25. #include <grass/glocale.h>
  26. #include "ogr_api.h"
  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, is3D;
  36. char buf[GPATH_MAX];
  37. G_gisinit(argv[0]);
  38. module = G_define_module();
  39. G_add_keyword(_("vector"));
  40. G_add_keyword(_("external"));
  41. G_add_keyword(_("ogr"));
  42. module->description = _("Creates a new pseudo-vector map as a link to an OGR-supported layer.");
  43. parse_args(argc, argv,
  44. &options, &flags);
  45. OGRRegisterAll();
  46. if (flags.format->answer) {
  47. list_formats(stdout);
  48. exit(EXIT_SUCCESS);
  49. }
  50. if (flags.layer->answer) {
  51. if (!options.dsn->answer)
  52. G_fatal_error(_("Required parameter <%s> not set"), options.dsn->key);
  53. list_layers(stdout, options.dsn->answer, NULL, NULL);
  54. exit(EXIT_SUCCESS);
  55. }
  56. if (!options.output->answer)
  57. G_fatal_error(_("Required parameter <%s> not set"), options.output->key);
  58. if (!options.layer->answer)
  59. G_fatal_error(_("Required parameter <%s> not set"), options.layer->key);
  60. ilayer = list_layers(NULL, options.dsn->answer, options.layer->answer, &is3D);
  61. if (ilayer == -1) {
  62. G_fatal_error(_("Layer <%s> not available"), options.layer->answer);
  63. }
  64. G_debug(2, "layer '%s' was found", options.layer->answer);
  65. Vect_open_new(&Map, options.output->answer, is3D);
  66. Vect_hist_command(&Map);
  67. Vect_close(&Map);
  68. /* Vect_open_new created 'head', 'coor', 'hist' -> delete 'coor' and create 'frmt' */
  69. sprintf(buf, "%s/%s/vector/%s/coor", G_location_path(), G_mapset(),
  70. options.output->answer);
  71. G_debug(2, "Delete '%s'", buf);
  72. if (unlink(buf) == -1) {
  73. Vect_delete(options.output->answer);
  74. G_fatal_error(_("Unable to delete '%s'"), buf);
  75. }
  76. /* Create frmt */
  77. sprintf(buf, "%s/%s", GV_DIRECTORY, options.output->answer);
  78. fd = G_fopen_new(buf, GV_FRMT_ELEMENT);
  79. if (fd == NULL) {
  80. Vect_delete(options.output->answer);
  81. G_fatal_error("Unable to open file '%s'", buf);
  82. }
  83. fprintf(fd, "FORMAT: ogr\n");
  84. fprintf(fd, "DSN: %s\n", options.dsn->answer);
  85. fprintf(fd, "LAYER: %s\n", options.layer->answer);
  86. fclose(fd);
  87. if (!flags.topo->answer) {
  88. Vect_open_old(&Map, options.output->answer, G_mapset());
  89. Vect_build(&Map);
  90. Vect_close(&Map);
  91. }
  92. G_done_msg(_("Link to vector map <%s> created."), options.output->answer);
  93. exit(EXIT_SUCCESS);
  94. }