proj.c 12 KB

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