proj.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. struct Key_Value **proj_epsg,
  14. char *geom_col, int verbose)
  15. {
  16. OGRSpatialReferenceH hSRS;
  17. char *pszProj4 = NULL;
  18. hSRS = NULL;
  19. *proj_info = NULL;
  20. *proj_units = NULL;
  21. *proj_epsg = 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 (OSRIsProjected(hSRS))
  87. authkey = "PROJCS";
  88. else /* is geographic */
  89. authkey = "GEOGCS";
  90. authname = OSRGetAuthorityName(hSRS, authkey);
  91. if (authname && *authname && strcmp(authname, "EPSG") == 0) {
  92. authcode = OSRGetAuthorityCode(hSRS, authkey);
  93. if (authcode && *authcode) {
  94. *proj_epsg = G_create_key_value();
  95. G_set_key_value("epsg", authcode, *proj_epsg);
  96. }
  97. }
  98. }
  99. if (OSRExportToProj4(hSRS, &pszProj4) != OGRERR_NONE) {
  100. G_important_message(_("Projection for layer <%s> can not be converted to proj4"),
  101. OGR_L_GetName(Ogr_layer));
  102. if (verbose) {
  103. char *wkt = NULL;
  104. if (OSRExportToPrettyWkt(hSRS, &wkt, FALSE) != OGRERR_NONE) {
  105. G_important_message(_("Can't get WKT-style parameter string"));
  106. }
  107. else if (wkt) {
  108. G_important_message(_("WKT-style definition:\n%s"), wkt);
  109. }
  110. }
  111. return 2;
  112. }
  113. return 0;
  114. }
  115. /* keep in sync with r.in.gdal, r.external, v.in.ogr */
  116. void check_projection(struct Cell_head *cellhd, ds_t hDS, int layer, char *geom_col,
  117. char *outloc, int create_only, int override,
  118. int check_only)
  119. {
  120. struct Cell_head loc_wind;
  121. struct Key_Value *proj_info, *proj_units, *proj_epsg;
  122. struct Key_Value *loc_proj_info, *loc_proj_units;
  123. char error_msg[8096];
  124. int proj_trouble;
  125. OGRLayerH Ogr_layer;
  126. /* Get first layer to be imported to use for projection check */
  127. Ogr_layer = ds_getlayerbyindex(hDS, layer);
  128. /* -------------------------------------------------------------------- */
  129. /* Fetch the projection in GRASS form. */
  130. /* -------------------------------------------------------------------- */
  131. proj_info = NULL;
  132. proj_units = NULL;
  133. proj_epsg = NULL;
  134. loc_proj_info = NULL;
  135. loc_proj_units = NULL;
  136. /* proj_trouble:
  137. * 0: valid srs
  138. * 1: no srs, default to xy
  139. * 2: unreadable srs, default to xy
  140. */
  141. /* Projection only required for checking so convert non-interactively */
  142. proj_trouble = get_layer_proj(Ogr_layer, cellhd, &proj_info, &proj_units,
  143. &proj_epsg, geom_col, 1);
  144. /* -------------------------------------------------------------------- */
  145. /* Do we need to create a new location? */
  146. /* -------------------------------------------------------------------- */
  147. if (outloc != NULL) {
  148. /* do not create a xy location because this can mean that the
  149. * real SRS has not been recognized or is missing */
  150. if (proj_trouble) {
  151. G_fatal_error(_("Unable to convert input map projection to GRASS "
  152. "format; cannot create new location."));
  153. }
  154. else {
  155. if (0 != G_make_location_epsg(outloc, cellhd, proj_info,
  156. proj_units, proj_epsg)) {
  157. G_fatal_error(_("Unable to create new location <%s>"),
  158. outloc);
  159. }
  160. G_message(_("Location <%s> created"), outloc);
  161. G_unset_window(); /* new location, projection, and window */
  162. G_get_window(cellhd);
  163. }
  164. /* If create only, clean up and exit here */
  165. if (create_only) {
  166. ds_close(hDS);
  167. exit(EXIT_SUCCESS);
  168. }
  169. }
  170. else {
  171. int err = 0;
  172. void (*msg_fn)(const char *, ...);
  173. if (check_only && override) {
  174. /* can't check when over-riding check */
  175. override = 0;
  176. }
  177. if (proj_trouble == 2) {
  178. strcpy(error_msg, _("Unable to convert input map projection information "
  179. "to GRASS format."));
  180. if (override) {
  181. msg_fn = G_warning;
  182. }
  183. else {
  184. msg_fn = G_fatal_error;
  185. ds_close(hDS);
  186. }
  187. msg_fn(error_msg);
  188. if (!override) {
  189. exit(EXIT_FAILURE);
  190. }
  191. }
  192. /* -------------------------------------------------------------------- */
  193. /* Does the projection of the current location match the */
  194. /* dataset? */
  195. /* -------------------------------------------------------------------- */
  196. G_get_default_window(&loc_wind);
  197. /* fetch LOCATION PROJ info */
  198. if (loc_wind.proj != PROJECTION_XY) {
  199. loc_proj_info = G_get_projinfo();
  200. loc_proj_units = G_get_projunits();
  201. }
  202. if (override) {
  203. cellhd->proj = loc_wind.proj;
  204. cellhd->zone = loc_wind.zone;
  205. G_message(_("Over-riding projection check"));
  206. }
  207. else if (loc_wind.proj != cellhd->proj
  208. || (err =
  209. G_compare_projections(loc_proj_info, loc_proj_units,
  210. proj_info, proj_units)) != 1) {
  211. int i_value;
  212. strcpy(error_msg,
  213. _("Projection of dataset does not"
  214. " appear to match current location.\n\n"));
  215. /* TODO: output this info sorted by key: */
  216. if (loc_wind.proj != cellhd->proj || err != -2) {
  217. /* error in proj_info */
  218. if (loc_proj_info != NULL) {
  219. strcat(error_msg, _("Location PROJ_INFO is:\n"));
  220. for (i_value = 0; i_value < loc_proj_info->nitems;
  221. i_value++)
  222. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  223. loc_proj_info->key[i_value],
  224. loc_proj_info->value[i_value]);
  225. strcat(error_msg, "\n");
  226. }
  227. if (proj_info != NULL) {
  228. strcat(error_msg, _("Dataset PROJ_INFO is:\n"));
  229. for (i_value = 0; i_value < proj_info->nitems; i_value++)
  230. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  231. proj_info->key[i_value],
  232. proj_info->value[i_value]);
  233. }
  234. else {
  235. strcat(error_msg, _("Dataset PROJ_INFO is:\n"));
  236. if (cellhd->proj == PROJECTION_XY)
  237. sprintf(error_msg + strlen(error_msg),
  238. "Dataset proj = %d (unreferenced/unknown)\n",
  239. cellhd->proj);
  240. else if (cellhd->proj == PROJECTION_LL)
  241. sprintf(error_msg + strlen(error_msg),
  242. "Dataset proj = %d (lat/long)\n",
  243. cellhd->proj);
  244. else if (cellhd->proj == PROJECTION_UTM)
  245. sprintf(error_msg + strlen(error_msg),
  246. "Dataset proj = %d (UTM), zone = %d\n",
  247. cellhd->proj, cellhd->zone);
  248. else
  249. sprintf(error_msg + strlen(error_msg),
  250. "Dataset proj = %d (unknown), zone = %d\n",
  251. cellhd->proj, cellhd->zone);
  252. }
  253. if (loc_wind.proj != cellhd->proj) {
  254. strcat(error_msg, "\nERROR: proj\n");
  255. }
  256. else {
  257. strcat(error_msg, "\nERROR: ");
  258. switch (err) {
  259. case -1:
  260. strcat(error_msg, "proj\n");
  261. break;
  262. case -2:
  263. strcat(error_msg, "units\n");
  264. break;
  265. case -3:
  266. strcat(error_msg, "datum\n");
  267. break;
  268. case -4:
  269. strcat(error_msg, "ellps, a, es\n");
  270. break;
  271. case -5:
  272. strcat(error_msg, "zone\n");
  273. break;
  274. case -6:
  275. strcat(error_msg, "south\n");
  276. break;
  277. case -7:
  278. strcat(error_msg, "x_0\n");
  279. break;
  280. case -8:
  281. strcat(error_msg, "y_0\n");
  282. break;
  283. case -9:
  284. strcat(error_msg, "lon_0\n");
  285. break;
  286. case -10:
  287. strcat(error_msg, "lat_0\n");
  288. break;
  289. case -11:
  290. strcat(error_msg, "lat_1, lat2\n");
  291. break;
  292. }
  293. }
  294. }
  295. else {
  296. /* error in proj_units */
  297. if (loc_proj_units != NULL) {
  298. strcat(error_msg, "Location PROJ_UNITS is:\n");
  299. for (i_value = 0; i_value < loc_proj_units->nitems;
  300. i_value++)
  301. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  302. loc_proj_units->key[i_value],
  303. loc_proj_units->value[i_value]);
  304. strcat(error_msg, "\n");
  305. }
  306. if (proj_units != NULL) {
  307. strcat(error_msg, "Dataset PROJ_UNITS is:\n");
  308. for (i_value = 0; i_value < proj_units->nitems; i_value++)
  309. sprintf(error_msg + strlen(error_msg), "%s: %s\n",
  310. proj_units->key[i_value],
  311. proj_units->value[i_value]);
  312. }
  313. }
  314. if (!check_only) {
  315. strcat(error_msg,
  316. _("\nIn case of no significant differences in the projection definitions,"
  317. " use the -o flag to ignore them and use"
  318. " current location definition.\n"));
  319. strcat(error_msg,
  320. _("Consider generating a new location from the input dataset using "
  321. "the 'location' parameter.\n"));
  322. }
  323. if (check_only)
  324. msg_fn = G_warning;
  325. else
  326. msg_fn = G_fatal_error;
  327. msg_fn("%s", error_msg);
  328. if (check_only) {
  329. ds_close(hDS);
  330. exit(EXIT_FAILURE);
  331. }
  332. }
  333. else {
  334. if (check_only)
  335. msg_fn = G_message;
  336. else
  337. msg_fn = G_verbose_message;
  338. msg_fn(_("Projection of input dataset and current location "
  339. "appear to match"));
  340. if (check_only) {
  341. ds_close(hDS);
  342. exit(EXIT_SUCCESS);
  343. }
  344. }
  345. }
  346. }