main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /****************************************************************
  2. *
  3. * MODULE: v.hull
  4. *
  5. * AUTHOR(S): Andrea Aime <aaime@libero.it>
  6. * Updated 19 Dec 2003, Markus Neteler to 5.7
  7. * Last updated 16 jan 2007, Benjamin Ducke to support 3D hull creation
  8. * OGR support by Martin Landa <landa.martin gmail.com> (2009)
  9. *
  10. * PURPOSE: Creates the convex hull surrounding a vector points.
  11. *
  12. * COPYRIGHT: (C) 2001-2010 by the GRASS Development Team
  13. *
  14. * This program is free software under the GNU General
  15. * Public License (>=v2). Read the file COPYING that
  16. * comes with GRASS for details.
  17. *
  18. ****************************************************************/
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <assert.h>
  22. #include <grass/gis.h>
  23. #include <grass/vector.h>
  24. #include <grass/glocale.h>
  25. #include "hull.h"
  26. int main(int argc, char **argv)
  27. {
  28. struct GModule *module;
  29. struct Option *input, *output, *field;
  30. struct Flag *all, *flat;
  31. struct Cell_head window;
  32. char *sitefile;
  33. struct Map_info Map;
  34. struct Point *points; /* point loaded from site file */
  35. int *hull; /* index of points located on the convex hull */
  36. int numSitePoints, numHullPoints;
  37. int MODE2D;
  38. G_gisinit(argv[0]);
  39. module = G_define_module();
  40. G_add_keyword(_("vector"));
  41. G_add_keyword(_("geometry"));
  42. module->description =
  43. _("Produces a convex hull for a given vector map.");
  44. input = G_define_standard_option(G_OPT_V_INPUT);
  45. field = G_define_standard_option(G_OPT_V_FIELD_ALL);
  46. output = G_define_standard_option(G_OPT_V_OUTPUT);
  47. all = G_define_flag();
  48. all->key = 'a';
  49. all->description =
  50. _("Use all vector points (do not limit to current region)");
  51. flat = G_define_flag();
  52. flat->key = 'f';
  53. flat->description =
  54. _("Create a 'flat' 2D hull even if the input is 3D points");
  55. if (G_parser(argc, argv))
  56. exit(EXIT_FAILURE);
  57. sitefile = input->answer;
  58. Vect_check_input_output_name(input->answer, output->answer,
  59. GV_FATAL_EXIT);
  60. Vect_set_open_level(1);
  61. if (Vect_open_old2(&Map, sitefile, "", field->answer) < 0)
  62. G_fatal_error(_("Unable to open vector map <%s>"), sitefile);
  63. /* load site coordinates */
  64. G_get_window(&window);
  65. numSitePoints = loadSiteCoordinates(&Map, &points, all->answer, &window,
  66. Vect_get_field_number(&Map, field->answer));
  67. if (numSitePoints < 0)
  68. G_fatal_error(_("Error loading vector points from <%s>"), sitefile);
  69. if (numSitePoints < 3)
  70. G_fatal_error(_("Convex hull calculation requires at least three points (%d found)"), numSitePoints);
  71. G_verbose_message(_("%d points read from vector map <%s>"), sitefile);
  72. /* create a 2D or a 3D hull? */
  73. MODE2D = 1;
  74. if (Vect_is_3d(&Map)) {
  75. MODE2D = 0;
  76. }
  77. if (flat->answer) {
  78. MODE2D = 1;
  79. }
  80. /* create vector map */
  81. if (0 > Vect_open_new(&Map, output->answer, MODE2D ? WITHOUT_Z : WITH_Z)) {
  82. G_fatal_error(_("Unable to create vector map <%s>"), output->answer);
  83. }
  84. Vect_hist_command(&Map);
  85. if (MODE2D) {
  86. /* compute convex hull */
  87. numHullPoints = convexHull(points, numSitePoints, &hull);
  88. /* output vector map */
  89. outputHull(&Map, points, hull, numHullPoints);
  90. }
  91. else {
  92. /* this does everything for the 3D hull including vector map creation */
  93. convexHull3d(points, numSitePoints, &Map);
  94. }
  95. /* clean up and bye bye */
  96. Vect_build(&Map);
  97. Vect_close(&Map);
  98. exit(EXIT_SUCCESS);
  99. }