projection.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * lidar-related projection functions
  3. *
  4. * Authors:
  5. * Markus Metz (r.in.lidar)
  6. * Vaclav Petras (refactoring and various additions)
  7. *
  8. * Copyright 2011-2016 by Markus Metz, and The GRASS Development Team
  9. *
  10. * This program is free software licensed under the GPL (>=v2).
  11. * Read the COPYING file that comes with GRASS for details.
  12. *
  13. */
  14. #include <string.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. #include <grass/gprojects.h>
  18. #include "projection.h"
  19. void projection_mismatch_report(struct Cell_head cellhd,
  20. struct Cell_head loc_wind,
  21. struct Key_Value *loc_proj_info,
  22. struct Key_Value *loc_proj_units,
  23. struct Key_Value *proj_info,
  24. struct Key_Value *proj_units, int err)
  25. {
  26. int i_value;
  27. char error_msg[8192];
  28. strcpy(error_msg,
  29. _("Projection of dataset does not"
  30. " appear to match current location.\n\n"));
  31. /* TODO: output this info sorted by key: */
  32. if (loc_wind.proj != cellhd.proj || err != -2) {
  33. if (loc_proj_info != NULL) {
  34. strcat(error_msg, _("GRASS LOCATION PROJ_INFO is:\n"));
  35. for (i_value = 0; i_value < loc_proj_info->nitems; i_value++)
  36. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  37. loc_proj_info->key[i_value],
  38. loc_proj_info->value[i_value]);
  39. strcat(error_msg, "\n");
  40. }
  41. if (proj_info != NULL) {
  42. strcat(error_msg, _("Import dataset PROJ_INFO is:\n"));
  43. for (i_value = 0; i_value < proj_info->nitems; i_value++)
  44. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  45. proj_info->key[i_value], proj_info->value[i_value]);
  46. }
  47. else {
  48. strcat(error_msg, _("Import dataset PROJ_INFO is:\n"));
  49. if (cellhd.proj == PROJECTION_XY)
  50. sprintf(error_msg + strlen(error_msg),
  51. "Dataset proj = %d (unreferenced/unknown)\n",
  52. cellhd.proj);
  53. else if (cellhd.proj == PROJECTION_LL)
  54. sprintf(error_msg + strlen(error_msg),
  55. "Dataset proj = %d (lat/long)\n", cellhd.proj);
  56. else if (cellhd.proj == PROJECTION_UTM)
  57. sprintf(error_msg + strlen(error_msg),
  58. "Dataset proj = %d (UTM), zone = %d\n",
  59. cellhd.proj, cellhd.zone);
  60. else
  61. sprintf(error_msg + strlen(error_msg),
  62. "Dataset proj = %d (unknown), zone = %d\n",
  63. cellhd.proj, cellhd.zone);
  64. }
  65. }
  66. else {
  67. if (loc_proj_units != NULL) {
  68. strcat(error_msg, "GRASS LOCATION PROJ_UNITS is:\n");
  69. for (i_value = 0; i_value < loc_proj_units->nitems; i_value++)
  70. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  71. loc_proj_units->key[i_value],
  72. loc_proj_units->value[i_value]);
  73. strcat(error_msg, "\n");
  74. }
  75. if (proj_units != NULL) {
  76. strcat(error_msg, "Import dataset PROJ_UNITS is:\n");
  77. for (i_value = 0; i_value < proj_units->nitems; i_value++)
  78. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  79. proj_units->key[i_value], proj_units->value[i_value]);
  80. }
  81. }
  82. sprintf(error_msg + strlen(error_msg),
  83. _("\nIn case of no significant differences in the projection definitions,"
  84. " use the -o flag to ignore them and use"
  85. " current location definition.\n"));
  86. strcat(error_msg,
  87. _("Consider generating a new location with 'location' parameter"
  88. " from input data set.\n"));
  89. G_fatal_error("%s", error_msg);
  90. }
  91. void projection_check_wkt(struct Cell_head cellhd,
  92. struct Cell_head loc_wind,
  93. const char *projstr, int override, int verbose)
  94. {
  95. struct Key_Value *loc_proj_info = NULL, *loc_proj_units = NULL;
  96. struct Key_Value *proj_info, *proj_units;
  97. int err = 0;
  98. proj_info = NULL;
  99. proj_units = NULL;
  100. /* Projection only required for checking so convert non-interactively */
  101. if (GPJ_wkt_to_grass(&cellhd, &proj_info, &proj_units, projstr, 0) < 0)
  102. G_warning(_("Unable to convert input map projection information to "
  103. "GRASS format for checking"));
  104. /* Does the projection of the current location match the dataset? */
  105. /* fetch LOCATION PROJ info */
  106. if (loc_wind.proj != PROJECTION_XY) {
  107. loc_proj_info = G_get_projinfo();
  108. loc_proj_units = G_get_projunits();
  109. }
  110. if (override) {
  111. cellhd.proj = loc_wind.proj;
  112. cellhd.zone = loc_wind.zone;
  113. if (verbose)
  114. G_message(_("Over-riding projection check"));
  115. }
  116. else if (loc_wind.proj != cellhd.proj
  117. || (err =
  118. G_compare_projections(loc_proj_info, loc_proj_units,
  119. proj_info, proj_units)) != TRUE) {
  120. projection_mismatch_report(cellhd, loc_wind, loc_proj_info,
  121. loc_proj_units,
  122. proj_info, proj_units, err);
  123. }
  124. else if (verbose) {
  125. G_message(_("Projection of input dataset and current location "
  126. "appear to match"));
  127. }
  128. }