main.c 3.7 KB

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