main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /****************************************************************************
  2. *
  3. * MODULE: d.where
  4. * AUTHOR(S): James Westervelt and Michael Shapiro (CERL)
  5. (original contributors)
  6. * Bernhard Reiter <bernhard intevation.de>,
  7. * Markus Neteler <neteler itc.it>,
  8. * Eric G. Miller <egm2 jps.net>,
  9. * Glynn Clements <glynn gclements.plus.com>,
  10. * Hamish Bowman <hamish_b yahoo.com>,
  11. * Jan-Oliver Wagner <jan intevation.de>,
  12. * Paul Kelly <paul-grass stjohnspoint.co.uk>
  13. * PURPOSE: interactive query of location in active display
  14. * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
  15. *
  16. * This program is free software under the GNU General Public
  17. * License (>=v2). Read the file COPYING that comes with GRASS
  18. * for details.
  19. *
  20. *****************************************************************************/
  21. #include <string.h>
  22. #include <grass/gis.h>
  23. #include <grass/gprojects.h>
  24. #include <grass/display.h>
  25. #include "local_proto.h"
  26. #include <grass/glocale.h>
  27. struct pj_info iproj, oproj;
  28. int main(int argc, char **argv)
  29. {
  30. struct GModule *module;
  31. struct Option *coords, *file;
  32. struct Flag *decimal, *latlong, *wgs84, *dcoord;
  33. int have_spheroid = 0;
  34. FILE *fp;
  35. /* Initialize the GIS calls */
  36. G_gisinit(argv[0]);
  37. module = G_define_module();
  38. G_add_keyword(_("display"));
  39. G_add_keyword(_("sampling"));
  40. G_add_keyword(_("position"));
  41. G_add_keyword(_("querying"));
  42. module->description =
  43. _("Identifies the geographic coordinates associated with "
  44. "point locations given in display coordinates.");
  45. coords = G_define_option();
  46. coords->key = "at";
  47. coords->key_desc = "x,y";
  48. coords->type = TYPE_DOUBLE;
  49. coords->required = NO;
  50. coords->multiple = YES;
  51. coords->description =
  52. _("Display coordinates to convert");
  53. file = G_define_standard_option(G_OPT_F_INPUT);
  54. file->required = NO;
  55. file->description =
  56. _("File from which to read coordinates (\"-\" to read from stdin)");
  57. decimal = G_define_flag();
  58. decimal->key = 'd';
  59. decimal->description = _("Output lat/long in decimal degree");
  60. latlong = G_define_flag();
  61. latlong->key = 'l';
  62. latlong->description =
  63. _("Output lat/long referenced to current ellipsoid");
  64. wgs84 = G_define_flag();
  65. wgs84->key = 'w';
  66. wgs84->description =
  67. _("Output lat/long referenced to WGS84 ellipsoid using datum "
  68. "transformation parameters defined in current location (if available)");
  69. dcoord = G_define_flag();
  70. dcoord->key = 'f';
  71. dcoord->description =
  72. _("Output frame coordinates of current display monitor (percentage)");
  73. /* if (G_parser(argc,argv)) */
  74. if (G_parser(argc, argv))
  75. exit(EXIT_FAILURE);
  76. if (latlong->answer && wgs84->answer)
  77. G_fatal_error(_("Ambiguous request for lat/long ellipsoids"));
  78. if (decimal->answer && !(latlong->answer || wgs84->answer))
  79. G_fatal_error(_("Please specify a lat/long ellipsoid with -l or -w"));
  80. if (((G_projection() == PROJECTION_LL) && wgs84->answer) ||
  81. ((G_projection() != PROJECTION_LL) &&
  82. (latlong->answer || wgs84->answer)))
  83. have_spheroid = 1;
  84. if (have_spheroid == 1) {
  85. struct Key_Value *in_proj_info, *in_unit_info;
  86. char buff[100], dum[100];
  87. /* read current projection info */
  88. if ((in_proj_info = G_get_projinfo()) == NULL)
  89. G_fatal_error(_("Can't get projection info of current location"));
  90. if ((in_unit_info = G_get_projunits()) == NULL)
  91. G_fatal_error(_("Can't get projection units of current location"));
  92. if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
  93. G_fatal_error(_("Can't get projection key values of current location"));
  94. if (!wgs84->answer) {
  95. /* Set output to same ellipsoid as input if we're not looking
  96. * for the WGS84 values */
  97. if (GPJ_get_equivalent_latlong(&oproj, &iproj) < 0)
  98. G_fatal_error(_("Unable to set up lat/long projection parameters"));
  99. }
  100. else {
  101. struct Key_Value *out_proj_info, *out_unit_info;
  102. out_proj_info = G_create_key_value();
  103. out_unit_info = G_create_key_value();
  104. /* set output projection to lat/long */
  105. G_set_key_value("proj", "ll", out_proj_info);
  106. /* Check that datumparams are defined for this location (otherwise
  107. * the WGS84 values would be meaningless), and if they are set the
  108. * output datum to WGS84 */
  109. if (G_get_datumparams_from_projinfo(in_proj_info, buff, dum) < 0)
  110. G_fatal_error(_("WGS84 output not possible as this location does not contain\n"
  111. "datum transformation parameters. Try running g.setproj."));
  112. else
  113. G_set_key_value("datum", "wgs84", out_proj_info);
  114. G_set_key_value("unit", "degree", out_unit_info);
  115. G_set_key_value("units", "degrees", out_unit_info);
  116. G_set_key_value("meters", "1.0", out_unit_info);
  117. if (pj_get_kv(&oproj, out_proj_info, out_unit_info) < 0)
  118. G_fatal_error(_("Unable to set up lat/long projection parameters"));
  119. G_free_key_value(out_proj_info);
  120. G_free_key_value(out_unit_info);
  121. }
  122. G_free_key_value(in_proj_info);
  123. G_free_key_value(in_unit_info);
  124. }
  125. if (file->answer) {
  126. if (strcmp(file->answer, "-") == 0)
  127. fp = stdin;
  128. else {
  129. fp = fopen(file->answer, "r");
  130. if (!fp)
  131. G_fatal_error(_("Unable to open input file <%s>"), file->answer);
  132. }
  133. }
  134. else
  135. fp = stdin;
  136. D_open_driver();
  137. D_setup(0);
  138. where_am_i(coords->answers, fp, have_spheroid, decimal->answer, dcoord->answer);
  139. D_close_driver();
  140. exit(EXIT_SUCCESS);
  141. }