Debian Codegraph @

imagery/i.ortho.photo/i.ortho.elev/main.c

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