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