proj.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <grass/gis.h>
  2. #include <grass/gprojects.h>
  3. #include <grass/glocale.h>
  4. #include <gdal.h>
  5. void check_projection(struct Cell_head *cellhd, GDALDatasetH hDS, int override)
  6. {
  7. struct Cell_head loc_wind;
  8. struct Key_Value *proj_info = NULL, *proj_units = NULL;
  9. struct Key_Value *loc_proj_info = NULL, *loc_proj_units = NULL;
  10. int projcomp_error = 0;
  11. char error_msg[8096];
  12. /* Projection only required for checking so convert non-interactively */
  13. if (GPJ_wkt_to_grass(cellhd, &proj_info,
  14. &proj_units, GDALGetProjectionRef(hDS), 0) < 0)
  15. G_warning(_("Unable to convert input raster map projection information to "
  16. "GRASS format for checking"));
  17. else {
  18. /* -------------------------------------------------------------------- */
  19. /* Does the projection of the current location match the */
  20. /* dataset? */
  21. /* -------------------------------------------------------------------- */
  22. G_get_default_window(&loc_wind);
  23. if (loc_wind.proj != PROJECTION_XY) {
  24. loc_proj_info = G_get_projinfo();
  25. loc_proj_units = G_get_projunits();
  26. }
  27. if (override) {
  28. cellhd->proj = loc_wind.proj;
  29. cellhd->zone = loc_wind.zone;
  30. G_warning(_("Over-riding projection check"));
  31. }
  32. else if (loc_wind.proj != cellhd->proj ||
  33. (projcomp_error = G_compare_projections(
  34. loc_proj_info, loc_proj_units, proj_info, proj_units)) < 0) {
  35. int i_value;
  36. strcpy(error_msg,
  37. _("Projection of dataset does not"
  38. " appear to match current location.\n\n"));
  39. /* TODO: output this info sorted by key: */
  40. if (loc_proj_info != NULL) {
  41. strcat(error_msg, _("Location PROJ_INFO is:\n"));
  42. for (i_value = 0;
  43. loc_proj_info != NULL &&
  44. i_value < loc_proj_info->nitems; i_value++)
  45. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  46. loc_proj_info->key[i_value],
  47. loc_proj_info->value[i_value]);
  48. strcat(error_msg, "\n");
  49. }
  50. if (proj_info != NULL) {
  51. strcat(error_msg, _("Dataset PROJ_INFO is:\n"));
  52. for (i_value = 0;
  53. proj_info != NULL && i_value < proj_info->nitems;
  54. i_value++)
  55. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  56. proj_info->key[i_value],
  57. proj_info->value[i_value]);
  58. strcat(error_msg, "\nERROR: ");
  59. switch (projcomp_error) {
  60. case -1:
  61. strcat(error_msg, "proj\n");
  62. break;
  63. case -2:
  64. strcat(error_msg, "units\n");
  65. break;
  66. case -3:
  67. strcat(error_msg, "datum\n");
  68. break;
  69. case -4:
  70. strcat(error_msg, "ellps\n");
  71. break;
  72. case -5:
  73. strcat(error_msg, "zone\n");
  74. break;
  75. }
  76. }
  77. else {
  78. strcat(error_msg, _("Import dataset PROJ_INFO is:\n"));
  79. if (cellhd->proj == PROJECTION_XY)
  80. sprintf(error_msg + strlen(error_msg),
  81. "cellhd.proj = %d (unreferenced/unknown)\n",
  82. cellhd->proj);
  83. else if (cellhd->proj == PROJECTION_LL)
  84. sprintf(error_msg + strlen(error_msg),
  85. "cellhd.proj = %d (lat/long)\n", cellhd->proj);
  86. else if (cellhd->proj == PROJECTION_UTM)
  87. sprintf(error_msg + strlen(error_msg),
  88. "cellhd.proj = %d (UTM), zone = %d\n",
  89. cellhd->proj, cellhd->zone);
  90. else if (cellhd->proj == PROJECTION_SP)
  91. sprintf(error_msg + strlen(error_msg),
  92. "cellhd.proj = %d (State Plane), zone = %d\n",
  93. cellhd->proj, cellhd->zone);
  94. else
  95. sprintf(error_msg + strlen(error_msg),
  96. "cellhd.proj = %d (unknown), zone = %d\n",
  97. cellhd->proj, cellhd->zone);
  98. }
  99. strcat(error_msg,
  100. _("\nIn case of no significant differences in the projection definitions,"
  101. " use the -o flag to ignore them and use"
  102. " current location definition.\n"));
  103. strcat(error_msg,
  104. _("Consider generating a new location from the input dataset using "
  105. "the 'location' parameter.\n"));
  106. G_fatal_error("%s", error_msg);
  107. }
  108. else {
  109. G_message(_("Projection of input dataset and current location "
  110. "appear to match"));
  111. }
  112. }
  113. }