main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /****************************************************************************
  2. *
  3. * MODULE: m.measure
  4. * AUTHOR(S): Created from d.measure by Glynn Clements, 2010
  5. * James Westervelt and Michael Shapiro
  6. * (CERL - original contributors)
  7. * Markus Neteler <neteler itc.it>,
  8. * Reinhard Brunzema <r.brunzema@web.de>,
  9. * Bernhard Reiter <bernhard intevation.de>,
  10. * Huidae Cho <grass4u gmail.com>,
  11. * Eric G. Miller <egm2 jps.net>,
  12. * Glynn Clements <glynn gclements.plus.com>,
  13. * Hamish Bowman <hamish_b yahoo.com>,
  14. * Jan-Oliver Wagner <jan intevation.de>
  15. * Martin Landa <landa.martin gmail.com>
  16. * PURPOSE: Distance and area measurement
  17. * COPYRIGHT: (C) 1999-2006, 2010 by the GRASS Development Team
  18. *
  19. * This program is free software under the GNU General Public
  20. * License (>=v2). Read the file COPYING that comes with GRASS
  21. * for details.
  22. *
  23. *****************************************************************************/
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <grass/gis.h>
  27. #include <grass/glocale.h>
  28. int main(int argc, char **argv)
  29. {
  30. struct GModule *module;
  31. struct Option *coords, *units;
  32. struct Flag *shell;
  33. double *x, *y;
  34. double length, area, f, sq_f;
  35. int i, npoints;
  36. const char *units_name, *sq_units_name;
  37. /* Initialize the GIS calls */
  38. G_gisinit(argv[0]);
  39. module = G_define_module();
  40. module->description = _("Measures the lengths and areas of features.");
  41. G_add_keyword(_("miscellaneous"));
  42. G_add_keyword(_("measurement"));
  43. G_add_keyword(_("distance"));
  44. G_add_keyword(_("area"));
  45. coords = G_define_standard_option(G_OPT_M_COORDS);
  46. coords->required = YES;
  47. coords->multiple = YES;
  48. units = G_define_standard_option(G_OPT_M_UNITS);
  49. units->label = _("Units");
  50. units->description = _("Default: location map units");
  51. shell = G_define_flag();
  52. shell->key = 'g';
  53. shell->description = _("Shell script style");
  54. if (G_parser(argc, argv))
  55. exit(EXIT_FAILURE);
  56. npoints = 0;
  57. for (i = 0; coords->answers[i]; i += 2)
  58. npoints++;
  59. x = G_malloc(npoints * sizeof(double));
  60. y = G_malloc(npoints * sizeof(double));
  61. for (i = 0; i < npoints; i++)
  62. {
  63. x[i] = atof(coords->answers[2*i+0]);
  64. y[i] = atof(coords->answers[2*i+1]);
  65. }
  66. /* determine units */
  67. if (G_projection() == PROJECTION_LL && !units->answer) {
  68. units_name = G_get_units_name(U_METERS, 1, 0);
  69. sq_units_name = G_get_units_name(U_METERS, 1, 1);
  70. }
  71. else {
  72. units_name = G_get_units_name(G_units(units->answer), 1, 0);
  73. sq_units_name = G_get_units_name(G_units(units->answer), 1, 1);
  74. }
  75. f = G_meters_to_units_factor(G_units(units->answer));
  76. sq_f = G_meters_to_units_factor_sq(G_units(units->answer));
  77. G_debug(1, "using '%s (%f) %s (%f)'",
  78. units_name, f, sq_units_name, sq_f);
  79. G_begin_distance_calculations();
  80. length = 0;
  81. for (i = 1; i < npoints; i++)
  82. length += G_distance(x[i-1], y[i-1], x[i], y[i]);
  83. if (shell->answer) {
  84. printf("units=%s,%s\n", units_name, sq_units_name);
  85. /* length */
  86. printf("length=%.6f\n", f * length);
  87. }
  88. else {
  89. printf("%-8s %10.6f %s\n", _("Length:"), f * length, units_name);
  90. }
  91. if (npoints > 3) {
  92. G_begin_polygon_area_calculations();
  93. area = G_area_of_polygon(x, y, npoints);
  94. if (shell->answer) {
  95. printf("area=%.6f\n", sq_f * area);
  96. }
  97. else {
  98. printf("%-8s %10.6f %s\n", _("Area:"), sq_f * area, sq_units_name);
  99. }
  100. }
  101. exit(EXIT_SUCCESS);
  102. }