1
2 /****************************************************************************
3 *
4 * MODULE: i.photo.elev
5 * AUTHOR(S): Mike Baba (original contributor)
6 * Markus Neteler <neteler itc.it>,
7 * Roberto Flor <flor itc.it>,
8 * Bernhard Reiter <bernhard intevation.de>,
9 * Glynn Clements <glynn gclements.plus.com>
10 * Hamish Bowman
11 * Markus Metz
12 *
13 * PURPOSE: Select the elevation model
14 * COPYRIGHT: (C) 1999-2012 by the GRASS Development Team
15 *
16 * This program is free software under the GNU General Public
17 * License (>=v2). Read the file COPYING that comes with GRASS
18 * for details.
19 *
20 *****************************************************************************/
21
22 /* read the target for the block and cast it into the alternate GRASS env */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <grass/gis.h>
29 #include <grass/imagery.h>
30 #include <grass/glocale.h>
31 #include "orthophoto.h"
32
33 static int which_env;
34
35 int select_current_env(void);
36 int select_target_env(void);
37
main(int argc,char * argv[])38 int main(int argc, char *argv[])
39 {
40
41 struct GModule *module;
42 struct Option *group_opt, *loc_opt, *mapset_opt, *elev_opt;
43 struct Option *math_opt, *unit_opt, *nd_opt;
44 struct Flag *list_flag, *print_flag;
45
46 char location[GMAPSET_MAX];
47 char mapset[GMAPSET_MAX];
48 char group[GNAME_MAX];
49
50 char *elev_layer;
51 char *mapset_elev, *mapset_elev_old;
52 char *location_elev, *location_elev_old;
53 char *math_exp;
54 char *units;
55 char *nd;
56
57 char buf[GPATH_MAX];
58 int stat;
59 int overwrite;
60
61 G_gisinit(argv[0]);
62
63 module = G_define_module();
64 G_add_keyword(_("imagery"));
65 G_add_keyword(_("orthorectify"));
66 module->description =
67 _("Select or modify the target elevation model.");
68
69 group_opt = G_define_standard_option(G_OPT_I_GROUP);
70 group_opt->description =
71 _("Name of imagery group for ortho-rectification");
72
73 loc_opt = G_define_standard_option(G_OPT_M_LOCATION);
74 loc_opt->required = NO;
75 loc_opt->description =
76 _("Name of the target location");
77
78 mapset_opt = G_define_standard_option(G_OPT_M_MAPSET);
79 mapset_opt->required = NO;
80 mapset_opt->description =
81 _("Name of the target mapset");
82
83 elev_opt = G_define_standard_option(G_OPT_R_ELEV);
84 elev_opt->required = NO;
85 elev_opt->description =
86 _("Name of elevation map to use for ortho-rectification");
87
88 math_opt = G_define_standard_option(G_OPT_M_NULL_VALUE);
89 math_opt->key = "math_expression";
90 math_opt->required = NO;
91 math_opt->description =
92 _("Math expression to convert to real elevation");
93
94 unit_opt = G_define_standard_option(G_OPT_M_UNITS);
95 unit_opt->required = NO;
96 unit_opt->description =
97 _("Unit of the elevation map");
98
99 nd_opt = G_define_standard_option(G_OPT_M_NULL_VALUE);
100 nd_opt->required = NO;
101 nd_opt->description =
102 _("No data value");
103
104 print_flag = G_define_flag();
105 print_flag->key = 'p';
106 print_flag->description =
107 _("Print currently selected elevation map and exit");
108
109 if (G_parser(argc, argv))
110 exit(EXIT_FAILURE);
111
112 elev_layer = (char *)G_malloc(GNAME_MAX * sizeof(char));
113 mapset_elev = (char *)G_malloc(GMAPSET_MAX * sizeof(char));
114 mapset_elev_old = (char *)G_malloc(GMAPSET_MAX * sizeof(char));
115 location_elev = (char *)G_malloc(80 * sizeof(char));
116 location_elev_old = (char *)G_malloc(80 * sizeof(char));
117 math_exp = (char *)G_malloc(80 * sizeof(char));
118 units = (char *)G_malloc(80 * sizeof(char));
119 nd = (char *)G_malloc(80 * sizeof(char));
120
121 *elev_layer = 0;
122 *mapset_elev = 0;
123 *mapset_elev_old = 0;
124 *location_elev = 0;
125 *location_elev_old = 0;
126 *math_exp = 0;
127 *units = 0;
128 *nd = 0;
129
130 strcpy(group, group_opt->answer);
131 if (loc_opt->answer)
132 strcpy(location_elev, loc_opt->answer);
133 if (mapset_opt->answer)
134 strcpy(mapset_elev, mapset_opt->answer);
135 /*if(elev_opt->answer)
136 strcpy(elev_layer, elev_opt->answer);*/
137 if (math_opt->answer)
138 strcpy(math_exp, math_opt->answer);
139 if (unit_opt->answer)
140 strcpy(units, unit_opt->answer);
141 if (nd_opt->answer)
142 strcpy(nd, nd_opt->answer);
143
144 if (!I_get_target(group, location, mapset)) {
145 G_fatal_error(_("Please select a target for group [%s] first"), group);
146 }
147
148 sprintf(buf, "%s/%s", G_gisdbase(), location);
149 if (access(buf, 0) != 0) {
150 G_fatal_error(_("Target location [%s] not found\n"), location);
151 }
152
153 /*Report the contents of the ELEVATION file as in the GROUP */
154 if (print_flag->answer) {
155 /*If the content is empty report an error */
156 if (!I_get_group_elev(group, elev_layer, mapset_elev, location_elev, math_exp, units, nd)) {
157 G_fatal_error(_("Cannot find default elevation map for target in group [%s]"),group);
158 }
159 /*If there is a content, print it */
160 else {
161 fprintf(stdout, "map:\t\t\t%s\n",elev_layer);
162 fprintf(stdout, "mapset:\t\t\t%s\n",mapset_elev);
163 fprintf(stdout, "location:\t\t%s\n",location_elev);
164 fprintf(stdout, "math expression:\t%s\n",math_exp);
165 fprintf(stdout, "units:\t\t\t%s\n",units);
166 fprintf(stdout, "nodata value:\t\t%s\n",nd);
167 exit(EXIT_SUCCESS);
168 }
169 }
170
171 /*Creating a Target environment*/
172 G_create_alt_env();
173 /*Set an alternate Location */
174 G_setenv_nogisrc("LOCATION_NAME", location);
175 /*Check for permissions in mapset */
176 stat = G_mapset_permissions(mapset);
177 /*We have permissions on the mapset */
178 if (stat > 0) {
179 /*Set the alternate mapset */
180 G_setenv_nogisrc("MAPSET", mapset);
181 /*Make an alternate search path */
182 G_create_alt_search_path();
183 /*Switch from current to alternate environment */
184 G_switch_env();
185 /*Switch to alternate search path */
186 G_switch_search_path();
187 /*Source Env = 0 ; Target Env = 1 */
188 which_env = 0;
189 /*Select the target environment */
190 select_target_env();
191 /*Start working on the Target mapset */
192 if (!elev_opt->answer) {
193 /* select current location */
194 select_current_env();
195 G_fatal_error(_("Elevation map name is missing. Please set '%s' option"),
196 elev_opt->key);
197 }
198
199 /* return to current Location/mapset to write in the group file */
200 select_current_env();
201
202 /* load information from the ELEVATION file in the GROUP */
203 if (I_find_group_elev_file(group)) {
204 I_get_group_elev(group, elev_layer, mapset_elev_old, location_elev_old,
205 math_exp, units, nd);
206 if (*location_elev == 0)
207 strcpy(location_elev, location_elev_old);
208 if (*mapset_elev == 0)
209 strcpy(mapset_elev, mapset_elev_old);
210 }
211 /* if location and/or mapset of elevation are not set, use target */
212 if (*mapset_elev == 0)
213 strcpy(mapset_elev, mapset);
214 if (*location_elev == 0)
215 strcpy(location_elev, location);
216
217 /* select target location */
218 select_target_env();
219 /* elevation map exists in target ? */
220 if (G_find_raster2(elev_opt->answer, mapset_elev) == NULL) {
221 /* select current location */
222 select_current_env();
223 G_fatal_error(_("Raster map <%s> not found"), elev_opt->answer);
224 }
225 /* select current location */
226 select_current_env();
227
228 /* Modify ELEVATION file in source GROUP */
229 I_put_group_elev(group, elev_opt->answer, mapset_elev, location_elev,
230 math_exp, units, nd);
231
232 G_message(_("Group [%s] in location [%s] mapset [%s] now uses elevation map [%s]"),
233 group, G_location(), G_mapset(), elev_opt->answer);
234 }
235 else {
236 G_fatal_error(_("Mapset [%s] in target location [%s] - %s "),
237 mapset, location,
238 stat == 0 ? _("permission denied\n") : _("not found\n"));
239 }
240
241 return(EXIT_SUCCESS);
242 }
243
244
select_current_env(void)245 int select_current_env(void)
246 {
247 if (which_env != 0) {
248 G_switch_env();
249 G_switch_search_path();
250 which_env = 0;
251 }
252
253 return 0;
254 }
255
select_target_env(void)256 int select_target_env(void)
257 {
258 if (which_env != 1) {
259 G_switch_env();
260 G_switch_search_path();
261 which_env = 1;
262 }
263
264 return 0;
265 }
266