main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. coords = G_define_standard_option(G_OPT_M_COORDS);
  44. coords->required = YES;
  45. coords->multiple = YES;
  46. units = G_define_standard_option(G_OPT_M_UNITS);
  47. units->label = _("Units");
  48. units->description = _("Default: location map units");
  49. shell = G_define_flag();
  50. shell->key = 'g';
  51. shell->description = _("Shell script style");
  52. if (G_parser(argc, argv))
  53. exit(EXIT_FAILURE);
  54. npoints = 0;
  55. for (i = 0; coords->answers[i]; i += 2)
  56. npoints++;
  57. x = G_malloc(npoints * sizeof(double));
  58. y = G_malloc(npoints * sizeof(double));
  59. for (i = 0; i < npoints; i++)
  60. {
  61. x[i] = atof(coords->answers[2*i+0]);
  62. y[i] = atof(coords->answers[2*i+1]);
  63. }
  64. /* determine units */
  65. if (G_projection() == PROJECTION_LL && !units->answer) {
  66. units_name = G_get_units_name(U_METERS, 1, 0);
  67. sq_units_name = G_get_units_name(U_METERS, 1, 1);
  68. }
  69. else {
  70. units_name = G_get_units_name(G_units(units->answer), 1, 0);
  71. sq_units_name = G_get_units_name(G_units(units->answer), 1, 1);
  72. }
  73. f = G_meters_to_units_factor(G_units(units->answer));
  74. sq_f = G_meters_to_units_factor_sq(G_units(units->answer));
  75. G_debug(1, "using '%s (%f) %s (%f)'",
  76. units_name, f, sq_units_name, sq_f);
  77. G_begin_distance_calculations();
  78. length = 0;
  79. for (i = 1; i < npoints; i++)
  80. length += G_distance(x[i-1], y[i-1], x[i], y[i]);
  81. if (shell->answer) {
  82. printf("units=%s,%s\n", units_name, sq_units_name);
  83. /* length */
  84. printf("length=%.6f\n", f * length);
  85. }
  86. else {
  87. printf("%-8s %10.6f %s\n", _("Length:"), f * length, units_name);
  88. }
  89. if (npoints > 3) {
  90. G_begin_polygon_area_calculations();
  91. area = G_area_of_polygon(x, y, npoints);
  92. if (shell->answer) {
  93. printf("area=%.6f\n", sq_f * area);
  94. }
  95. else {
  96. printf("%-8s %10.6f %s\n", _("Area:"), sq_f * area, sq_units_name);
  97. }
  98. }
  99. exit(EXIT_SUCCESS);
  100. }