proj.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include <grass/gis.h>
  2. #include <grass/gprojects.h>
  3. #include <grass/glocale.h>
  4. #include <gdal.h>
  5. #include <ogr_srs_api.h>
  6. /* keep in sync with r.in.gdal, v.in.ogr, v.external */
  7. void check_projection(struct Cell_head *cellhd, GDALDatasetH hDS,
  8. char *outloc, int create_only, int override,
  9. int check_only)
  10. {
  11. struct Cell_head loc_wind;
  12. struct Key_Value *proj_info = NULL,
  13. *proj_units = NULL;
  14. struct Key_Value *loc_proj_info = NULL,
  15. *loc_proj_units = NULL;
  16. char *wkt = NULL, *srid = NULL;
  17. char error_msg[8096];
  18. int proj_trouble;
  19. /* -------------------------------------------------------------------- */
  20. /* Fetch the projection in GRASS form, SRID, and WKT. */
  21. /* -------------------------------------------------------------------- */
  22. #if GDAL_VERSION_NUM >= 3000000
  23. OGRSpatialReferenceH hSRS;
  24. hSRS = GDALGetSpatialRef(hDS);
  25. if (hSRS) {
  26. /* get WKT2 definition */
  27. char **papszOptions;
  28. papszOptions = G_calloc(3, sizeof(char *));
  29. papszOptions[0] = G_store("MULTILINE=YES");
  30. papszOptions[1] = G_store("FORMAT=WKT2");
  31. OSRExportToWktEx(hSRS, &wkt, (const char **)papszOptions);
  32. G_free(papszOptions[0]);
  33. G_free(papszOptions[1]);
  34. G_free(papszOptions);
  35. }
  36. /* proj_trouble:
  37. * 0: valid srs
  38. * 1: no srs, default to xy
  39. * 2: unreadable srs, default to xy
  40. */
  41. /* Projection only required for checking so convert non-interactively */
  42. proj_trouble = 0;
  43. if (wkt && *wkt) {
  44. if (hSRS != NULL)
  45. GPJ_osr_to_grass(cellhd, &proj_info, &proj_units, hSRS, 0);
  46. if (!hSRS || (!OSRIsProjected(hSRS) && !OSRIsGeographic(hSRS))) {
  47. G_important_message(_("Input contains an invalid SRS. "
  48. "WKT definition:\n%s"), wkt);
  49. proj_trouble = 2;
  50. }
  51. else{
  52. const char *authkey, *authname, *authcode;
  53. if (OSRIsProjected(hSRS))
  54. authkey = "PROJCS";
  55. else /* is geographic */
  56. authkey = "GEOGCS";
  57. authname = OSRGetAuthorityName(hSRS, authkey);
  58. if (authname && *authname) {
  59. authcode = OSRGetAuthorityCode(hSRS, authkey);
  60. if (authcode && *authcode) {
  61. G_asprintf(&srid, "%s:%s", authname, authcode);
  62. }
  63. }
  64. }
  65. }
  66. #else
  67. wkt = G_store(GDALGetProjectionRef(hDS));
  68. /* proj_trouble:
  69. * 0: valid srs
  70. * 1: no srs, default to xy
  71. * 2: unreadable srs, default to xy
  72. */
  73. /* Projection only required for checking so convert non-interactively */
  74. proj_trouble = 0;
  75. if (wkt && *wkt) {
  76. OGRSpatialReferenceH hSRS;
  77. hSRS = OSRNewSpatialReference(wkt);
  78. if (hSRS != NULL)
  79. GPJ_osr_to_grass(cellhd, &proj_info, &proj_units, hSRS, 0);
  80. if (!hSRS || (!OSRIsProjected(hSRS) && !OSRIsGeographic(hSRS))) {
  81. G_important_message(_("Input contains an invalid SRS. "
  82. "WKT definition:\n%s"), wkt);
  83. proj_trouble = 2;
  84. }
  85. else{
  86. const char *authkey, *authname, *authcode;
  87. if (OSRIsProjected(hSRS))
  88. authkey = "PROJCS";
  89. else /* is geographic */
  90. authkey = "GEOGCS";
  91. authname = OSRGetAuthorityName(hSRS, authkey);
  92. if (authname && *authname) {
  93. authcode = OSRGetAuthorityCode(hSRS, authkey);
  94. if (authcode && *authcode) {
  95. G_asprintf(&srid, "%s:%s", authname, authcode);
  96. }
  97. }
  98. }
  99. if (hSRS)
  100. OSRDestroySpatialReference(hSRS);
  101. }
  102. #endif
  103. else {
  104. G_important_message(_("No projection information available"));
  105. cellhd->proj = PROJECTION_XY;
  106. cellhd->zone = 0;
  107. proj_trouble = 1;
  108. }
  109. /* -------------------------------------------------------------------- */
  110. /* Do we need to create a new location? */
  111. /* -------------------------------------------------------------------- */
  112. if (outloc != NULL) {
  113. /* do not create a xy location if an existing SRS was unreadable */
  114. if (proj_trouble == 2) {
  115. G_fatal_error(_("Unable to convert input map projection to GRASS "
  116. "format; cannot create new location."));
  117. }
  118. else {
  119. if (0 != G_make_location_crs(outloc, cellhd, proj_info,
  120. proj_units, srid, wkt)) {
  121. G_fatal_error(_("Unable to create new location <%s>"),
  122. outloc);
  123. }
  124. G_message(_("Location <%s> created"), outloc);
  125. G_unset_window(); /* new location, projection, and window */
  126. G_get_window(cellhd);
  127. }
  128. /* If create only, clean up and exit here */
  129. if (create_only) {
  130. GDALClose(hDS);
  131. exit(EXIT_SUCCESS);
  132. }
  133. }
  134. else {
  135. int err = 0;
  136. void (*msg_fn)(const char *, ...);
  137. if (check_only && override) {
  138. /* can't check when over-riding check */
  139. override = 0;
  140. }
  141. if (proj_trouble == 2) {
  142. strcpy(error_msg, _("Unable to convert input map projection information "
  143. "to GRASS format."));
  144. if (override) {
  145. msg_fn = G_warning;
  146. }
  147. else {
  148. msg_fn = G_fatal_error;
  149. }
  150. msg_fn(error_msg);
  151. if (!override) {
  152. exit(EXIT_FAILURE);
  153. }
  154. }
  155. /* -------------------------------------------------------------------- */
  156. /* Does the projection of the current location match the */
  157. /* dataset? */
  158. /* -------------------------------------------------------------------- */
  159. G_get_default_window(&loc_wind);
  160. /* fetch LOCATION PROJ info */
  161. if (loc_wind.proj != PROJECTION_XY) {
  162. loc_proj_info = G_get_projinfo();
  163. loc_proj_units = G_get_projunits();
  164. }
  165. if (override) {
  166. cellhd->proj = loc_wind.proj;
  167. cellhd->zone = loc_wind.zone;
  168. G_message(_("Over-riding projection check"));
  169. }
  170. else if (loc_wind.proj != cellhd->proj
  171. || (err =
  172. G_compare_projections(loc_proj_info, loc_proj_units,
  173. proj_info, proj_units)) != 1) {
  174. int i_value;
  175. strcpy(error_msg,
  176. _("Projection of dataset does not"
  177. " appear to match current location.\n\n"));
  178. /* TODO: output this info sorted by key: */
  179. if (loc_wind.proj != cellhd->proj || err != -2) {
  180. /* error in proj_info */
  181. if (loc_proj_info != NULL) {
  182. strcat(error_msg, _("Location PROJ_INFO is:\n"));
  183. for (i_value = 0; i_value < loc_proj_info->nitems;
  184. i_value++)
  185. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  186. loc_proj_info->key[i_value],
  187. loc_proj_info->value[i_value]);
  188. strcat(error_msg, "\n");
  189. }
  190. else {
  191. strcat(error_msg, _("Location PROJ_INFO is:\n"));
  192. if (loc_wind.proj == PROJECTION_XY)
  193. sprintf(error_msg + strlen(error_msg),
  194. "Location proj = %d (unreferenced/unknown)\n",
  195. loc_wind.proj);
  196. else if (loc_wind.proj == PROJECTION_LL)
  197. sprintf(error_msg + strlen(error_msg),
  198. "Location proj = %d (lat/long)\n",
  199. loc_wind.proj);
  200. else if (loc_wind.proj == PROJECTION_UTM)
  201. sprintf(error_msg + strlen(error_msg),
  202. "Location proj = %d (UTM), zone = %d\n",
  203. loc_wind.proj, cellhd->zone);
  204. else
  205. sprintf(error_msg + strlen(error_msg),
  206. "Location proj = %d (unknown), zone = %d\n",
  207. loc_wind.proj, cellhd->zone);
  208. }
  209. if (proj_info != NULL) {
  210. strcat(error_msg, _("Dataset PROJ_INFO is:\n"));
  211. for (i_value = 0; i_value < proj_info->nitems; i_value++)
  212. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  213. proj_info->key[i_value],
  214. proj_info->value[i_value]);
  215. }
  216. else {
  217. strcat(error_msg, _("Dataset PROJ_INFO is:\n"));
  218. if (cellhd->proj == PROJECTION_XY)
  219. sprintf(error_msg + strlen(error_msg),
  220. "Dataset proj = %d (unreferenced/unknown)\n",
  221. cellhd->proj);
  222. else if (cellhd->proj == PROJECTION_LL)
  223. sprintf(error_msg + strlen(error_msg),
  224. "Dataset proj = %d (lat/long)\n",
  225. cellhd->proj);
  226. else if (cellhd->proj == PROJECTION_UTM)
  227. sprintf(error_msg + strlen(error_msg),
  228. "Dataset proj = %d (UTM), zone = %d\n",
  229. cellhd->proj, cellhd->zone);
  230. else
  231. sprintf(error_msg + strlen(error_msg),
  232. "Dataset proj = %d (unknown), zone = %d\n",
  233. cellhd->proj, cellhd->zone);
  234. }
  235. if (loc_wind.proj != cellhd->proj) {
  236. strcat(error_msg, "\nDifference in: proj\n");
  237. }
  238. else {
  239. strcat(error_msg, "\nDifference in: ");
  240. switch (err) {
  241. case -1:
  242. strcat(error_msg, "proj\n");
  243. break;
  244. case -2:
  245. strcat(error_msg, "units\n");
  246. break;
  247. case -3:
  248. strcat(error_msg, "datum\n");
  249. break;
  250. case -4:
  251. strcat(error_msg, "ellps, a, es\n");
  252. break;
  253. case -5:
  254. strcat(error_msg, "zone\n");
  255. break;
  256. case -6:
  257. strcat(error_msg, "south\n");
  258. break;
  259. case -7:
  260. strcat(error_msg, "x_0\n");
  261. break;
  262. case -8:
  263. strcat(error_msg, "y_0\n");
  264. break;
  265. case -9:
  266. strcat(error_msg, "lon_0\n");
  267. break;
  268. case -10:
  269. strcat(error_msg, "lat_0\n");
  270. break;
  271. case -11:
  272. strcat(error_msg, "lat_1, lat2\n");
  273. break;
  274. }
  275. }
  276. }
  277. else {
  278. /* error in proj_units */
  279. if (loc_proj_units != NULL) {
  280. strcat(error_msg, "Location PROJ_UNITS is:\n");
  281. for (i_value = 0; i_value < loc_proj_units->nitems;
  282. i_value++)
  283. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  284. loc_proj_units->key[i_value],
  285. loc_proj_units->value[i_value]);
  286. strcat(error_msg, "\n");
  287. }
  288. if (proj_units != NULL) {
  289. strcat(error_msg, "Dataset PROJ_UNITS is:\n");
  290. for (i_value = 0; i_value < proj_units->nitems; i_value++)
  291. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  292. proj_units->key[i_value],
  293. proj_units->value[i_value]);
  294. }
  295. }
  296. if (!check_only) {
  297. strcat(error_msg,
  298. _("\nIn case of no significant differences in the projection definitions,"
  299. " use the -o flag to ignore them and use"
  300. " current location definition.\n"));
  301. strcat(error_msg,
  302. _("Consider generating a new location from the input dataset using "
  303. "the 'location' parameter.\n"));
  304. }
  305. if (check_only)
  306. msg_fn = G_message;
  307. else
  308. msg_fn = G_fatal_error;
  309. msg_fn("%s", error_msg);
  310. if (check_only) {
  311. GDALClose(hDS);
  312. exit(EXIT_FAILURE);
  313. }
  314. }
  315. else {
  316. if (check_only)
  317. msg_fn = G_message;
  318. else
  319. msg_fn = G_verbose_message;
  320. msg_fn(_("Projection of input dataset and current location "
  321. "appear to match"));
  322. if (check_only) {
  323. GDALClose(hDS);
  324. exit(EXIT_SUCCESS);
  325. }
  326. }
  327. }
  328. }