where.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <stdio.h>
  2. #include <grass/gis.h>
  3. #include <grass/glocale.h>
  4. #include <grass/gprojects.h>
  5. #include <grass/display.h>
  6. #include "local_proto.h"
  7. extern struct pj_info iproj, oproj;
  8. int where_am_i(char **coords, FILE *fp, int have_spheroid, int decimal, int dcoord)
  9. {
  10. char buf1[50], buf2[50];
  11. int screen_x, screen_y;
  12. double east, north;
  13. int projection;
  14. projection = G_projection();
  15. for (;;) {
  16. if (coords) {
  17. const char *x, *y;
  18. x = *coords++;
  19. if (!x)
  20. return 0;
  21. y = *coords++;
  22. if (!y)
  23. return 0;
  24. if (sscanf(x, "%d", &screen_x) != 1 || sscanf(y, "%d", &screen_y) != 1)
  25. G_fatal_error(_("Invalid coordinates <%s,%s>"), x, y);
  26. }
  27. else
  28. if (fscanf(fp, "%d %d", &screen_x, &screen_y) != 2)
  29. return 0;
  30. east = D_d_to_u_col((double)screen_x);
  31. north = D_d_to_u_row((double)screen_y);
  32. if (decimal) {
  33. G_format_easting(east, buf1, 0);
  34. G_format_northing(north, buf2, 0);
  35. }
  36. else {
  37. G_format_easting(east, buf1, projection);
  38. G_format_northing(north, buf2, projection);
  39. }
  40. fprintf(stdout, " %s %s", buf1, buf2);
  41. if (dcoord) {
  42. fprintf(stdout, " %.1f %.1f",
  43. 100 * (east - D_get_u_west()) / (D_get_u_east() -
  44. D_get_u_west()),
  45. 100 * (north - D_get_u_south()) / (D_get_u_north() -
  46. D_get_u_south()));
  47. }
  48. if (have_spheroid) {
  49. double lat = north;
  50. double lon = east;
  51. if (pj_do_proj(&lon, &lat, &iproj, &oproj) < 0)
  52. G_fatal_error("Error in pj_do_proj()");
  53. if (decimal) {
  54. G_format_easting(lon, buf1, 0);
  55. G_format_northing(lat, buf2, 0);
  56. }
  57. else {
  58. G_lon_format(lon, buf1);
  59. G_lat_format(lat, buf2);
  60. }
  61. fprintf(stdout, " %s %s", buf1, buf2);
  62. }
  63. fprintf(stdout, "\n");
  64. }
  65. return 0;
  66. }