main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <stdlib.h>
  22. #include <string.h>
  23. #include <grass/gis.h>
  24. #include <grass/gprojects.h>
  25. #include <grass/display.h>
  26. #include "local_proto.h"
  27. #include <grass/glocale.h>
  28. struct pj_info iproj, oproj, tproj;
  29. int main(int argc, char **argv)
  30. {
  31. struct GModule *module;
  32. struct Option *coords, *file;
  33. struct Flag *decimal, *latlong, *wgs84, *dcoord;
  34. int have_spheroid = 0;
  35. FILE *fp;
  36. /* Initialize the GIS calls */
  37. G_gisinit(argv[0]);
  38. module = G_define_module();
  39. G_add_keyword(_("display"));
  40. G_add_keyword(_("sampling"));
  41. G_add_keyword(_("position"));
  42. G_add_keyword(_("querying"));
  43. module->description =
  44. _("Identifies the geographic coordinates associated with "
  45. "point locations given in display coordinates.");
  46. coords = G_define_option();
  47. coords->key = "at";
  48. coords->key_desc = "x,y";
  49. coords->type = TYPE_DOUBLE;
  50. coords->required = NO;
  51. coords->multiple = YES;
  52. coords->description =
  53. _("Display coordinates to convert");
  54. file = G_define_standard_option(G_OPT_F_INPUT);
  55. file->required = NO;
  56. file->description =
  57. _("File from which to read coordinates (\"-\" to read from stdin)");
  58. decimal = G_define_flag();
  59. decimal->key = 'd';
  60. decimal->description = _("Output lat/long in decimal degree");
  61. latlong = G_define_flag();
  62. latlong->key = 'l';
  63. latlong->description =
  64. _("Output lat/long referenced to current ellipsoid");
  65. wgs84 = G_define_flag();
  66. wgs84->key = 'w';
  67. wgs84->description =
  68. _("Output lat/long referenced to WGS84 ellipsoid using datum "
  69. "transformation parameters defined in current location (if available)");
  70. dcoord = G_define_flag();
  71. dcoord->key = 'f';
  72. dcoord->description =
  73. _("Output frame coordinates of current display monitor (percentage)");
  74. /* if (G_parser(argc,argv)) */
  75. if (G_parser(argc, argv))
  76. exit(EXIT_FAILURE);
  77. if (latlong->answer && wgs84->answer)
  78. G_fatal_error(_("Ambiguous request for lat/long ellipsoids"));
  79. if (decimal->answer && !(latlong->answer || wgs84->answer))
  80. G_fatal_error(_("Please specify a lat/long ellipsoid with -l or -w"));
  81. if (((G_projection() == PROJECTION_LL) && wgs84->answer) ||
  82. ((G_projection() != PROJECTION_LL) &&
  83. (latlong->answer || wgs84->answer)))
  84. have_spheroid = 1;
  85. if (have_spheroid == 1) {
  86. struct Key_Value *in_proj_info, *in_unit_info;
  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. oproj.pj = NULL;
  95. if (wgs84->answer) {
  96. struct Key_Value *out_proj_info, *out_unit_info;
  97. out_proj_info = G_create_key_value();
  98. out_unit_info = G_create_key_value();
  99. /* set output projection to lat/long */
  100. G_set_key_value("proj", "ll", out_proj_info);
  101. /* Check that datumparams are defined for this location (otherwise
  102. * the WGS84 values would be meaningless), and if they are set the
  103. * output datum to WGS84 */
  104. #if PROJ_VERSION_MAJOR < 6
  105. char buff[100], dum[100];
  106. /* PROJ6+ has its own datum transformation parameters */
  107. if (G_get_datumparams_from_projinfo(in_proj_info, buff, dum) < 0)
  108. G_fatal_error(_("WGS84 output not possible as this location does not contain\n"
  109. "datum transformation parameters. Try running g.setproj."));
  110. else
  111. #endif
  112. G_set_key_value("datum", "wgs84", out_proj_info);
  113. G_set_key_value("unit", "degree", out_unit_info);
  114. G_set_key_value("units", "degrees", out_unit_info);
  115. G_set_key_value("meters", "1.0", out_unit_info);
  116. if (pj_get_kv(&oproj, out_proj_info, out_unit_info) < 0)
  117. G_fatal_error(_("Unable to set up lat/long projection parameters"));
  118. G_free_key_value(out_proj_info);
  119. G_free_key_value(out_unit_info);
  120. }
  121. /* else the latlong equivalent is generated by GPJ_init_transform() */
  122. G_free_key_value(in_proj_info);
  123. G_free_key_value(in_unit_info);
  124. tproj.def = NULL;
  125. if (GPJ_init_transform(&iproj, &oproj, &tproj) < 0)
  126. G_fatal_error(_("Unable to initialize coordinate transformation"));
  127. }
  128. if (file->answer) {
  129. if (strcmp(file->answer, "-") == 0)
  130. fp = stdin;
  131. else {
  132. fp = fopen(file->answer, "r");
  133. if (!fp)
  134. G_fatal_error(_("Unable to open input file <%s>"), file->answer);
  135. }
  136. }
  137. else
  138. fp = stdin;
  139. D_open_driver();
  140. D_setup(0);
  141. where_am_i(coords->answers, fp, have_spheroid, decimal->answer,
  142. dcoord->answer);
  143. D_close_driver();
  144. exit(EXIT_SUCCESS);
  145. }