Debian Codegraph @

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

raw symbols SCIP
1 /****************************************************************************
2 *
3 * MODULE: i.ortho.camera (former i.photo.camera)
4 * AUTHOR(S): Mike Baba, DBA Systems, Inc. (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 *
11 * PURPOSE: Creates or modifies entries in a camera reference file
12 * COPYRIGHT: (C) 1999-2008 by the GRASS Development Team
13 *
14 * This program is free software under the GNU General Public
15 * License (>=v2). Read the file COPYING that comes with GRASS
16 * for details.
17 *
18 *****************************************************************************/
19
20 /* create/edit a camera reference file
21 * optionally set the camera for a given imagery group */
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <grass/gis.h>
27 #include <grass/glocale.h>
28 #include "orthophoto.h"
29
30 int main(int argc, char *argv[])
31 {
32 struct GModule *module;
33 struct Option *group_opt, /* group */
34 *camera_opt, /* name of camera file camera */
35 *cname_opt, /* camera name */
36 *cid_opt, /* camera id */
37 *cfl_opt, /* calibrated focal length */
38 *pp_opt, /* principal point of symmetry x,y */
39 *fid_opt; /* coordinates of fiducials */
40
41 /* flag to print camera info */
42 /* flag to print camera info in shell script style */
43
44 const char *location;
45 const char *mapset;
46 char *group;
47 char *camera, *cam_name, *cam_id;
48 double ppx, ppy, cfl;
49 struct Ortho_Camera_File_Ref cam_info;
50 int put_cam_info = 0;
51
52 G_gisinit(argv[0]);
53
54 module = G_define_module();
55 G_add_keyword(_("imagery"));
56 G_add_keyword(_("orthorectify"));
57 module->description =
58 _("Select and modify the imagery group camera reference file.");
59
60 group_opt = G_define_standard_option(G_OPT_I_GROUP);
61 group_opt->required = NO;
62 group_opt->description = _("Name of imagery group for ortho-rectification");
63
64 camera_opt = G_define_standard_option(G_OPT_F_INPUT);
65 camera_opt->key = "camera";
66 camera_opt->required = YES;
67 camera_opt->gisprompt = "old,camera,file";
68 camera_opt->label = _("Name of camera reference file");
69
70 cname_opt = G_define_option();
71 cname_opt->type = TYPE_STRING;
72 cname_opt->key = "name";
73 cname_opt->label = _("Camera name");
74
75 cid_opt = G_define_option();
76 cid_opt->type = TYPE_STRING;
77 cid_opt->key = "id";
78 cid_opt->label = _("Camera id");
79
80 cfl_opt = G_define_option();
81 cfl_opt->type = TYPE_DOUBLE;
82 cfl_opt->key = "clf";
83 cfl_opt->label = _("Calibrated focal length");
84
85 pp_opt = G_define_standard_option(G_OPT_M_COORDS);
86 pp_opt->key = "pp";
87 pp_opt->label = _("Principal point coordinates");
88
89 fid_opt = G_define_standard_option(G_OPT_M_COORDS);
90 fid_opt->key = "fid";
91 fid_opt->multiple = YES;
92 fid_opt->label = _("Fiducial coordinates");
93
94 if (G_parser(argc, argv))
95 exit(EXIT_FAILURE);
96
97 location = G_location();
98 mapset = G_mapset();
99
100 group = group_opt->answer;
101 camera = camera_opt->answer;
102 cam_name = cname_opt->answer;
103 cam_id = cid_opt->answer;
104
105 if (G_legal_filename(camera) < 0)
106 G_fatal_error(_("<%s> is an illegal file name"), camera);
107
108 if (cam_name || cam_id || cfl_opt->answer || pp_opt->answers ||
109 fid_opt->answers)
110 put_cam_info = 1;
111
112 ppx = ppy = .0;
113 if (pp_opt->answers) {
114 sscanf(pp_opt->answers[0], "%lf", &ppx);
115 sscanf(pp_opt->answers[1], "%lf", &ppy);
116 }
117
118 if (G_find_file2("camera", camera, G_mapset())) {
119 /* use existing camera file */
120 if (!I_get_cam_info(camera, &cam_info))
121 G_fatal_error(_("Can not read camera file '%s'"), camera);
122
123 if (cam_name && strcmp(cam_name, cam_info.cam_name)) {
124 G_message(_("Replacing camera name '%s' with '%s'"),
125 cam_info.cam_name, cam_name);
126 strcpy(cam_info.cam_name, cam_name);
127 }
128
129 if (cam_id && strcmp(cam_id, cam_info.cam_id)) {
130 G_message(_("Replacing camera cam_id '%s' with '%s'"),
131 cam_info.cam_id, cam_id);
132 strcpy(cam_info.cam_id, cam_id);
133 }
134
135 if (cfl_opt->answer) {
136 cfl = atof(cfl_opt->answer);
137 if (cfl != cam_info.CFL) {
138 G_message(_("Replacing calibrated focal length '%g' with '%g'"),
139 cam_info.CFL, cfl);
140 cam_info.CFL = cfl;
141 }
142 }
143
144 if (pp_opt->answers) {
145 G_message(_("Replacing coordinates of principal point '%.17g, "
146 "%.17g' with '%.17g, %.17g'"),
147 cam_info.Xp, cam_info.Yp, ppx, ppy);
148 cam_info.Xp = ppx;
149 cam_info.Yp = ppy;
150 }
151 }
152 else {
153 /* create new camera file */
154
155 if (!cam_name)
156 G_fatal_error(
157 _("Please provide a camera name for a new camera definition"));
158 strcpy(cam_info.cam_name, cam_name);
159
160 if (!cam_id)
161 G_fatal_error(
162 _("Please provide a camera ID for a new camera definition"));
163 strcpy(cam_info.cam_id, cam_id);
164
165 if (!cfl_opt->answer)
166 G_fatal_error(_("Please provide calibrated focal length for a new "
167 "camera definition"));
168 cam_info.CFL = atof(cfl_opt->answer);
169
170 if (!pp_opt->answers)
171 G_message(_(
172 "Using default coordinates 0.0, 0.0 for the principal point"));
173 cam_info.Xp = ppx;
174 cam_info.Yp = ppy;
175 }
176
177 /* fiducials */
178 cam_info.num_fid = 0;
179 if (fid_opt->answers) {
180 int i, fid_no;
181 double Xf, Yf;
182
183 for (i = 0, fid_no = 0; fid_opt->answers[i] != NULL; i += 2, fid_no++) {
184 /* cam_info can hold max 20 fiducials */
185 if (fid_no > 19) {
186 G_warning(_("Too many fiducials!"));
187 break;
188 }
189 sscanf(fid_opt->answers[i], "%lf", &Xf);
190 cam_info.fiducials[fid_no].Xf = Xf;
191 sscanf(fid_opt->answers[i + 1], "%lf", &Yf);
192 cam_info.fiducials[fid_no].Yf = Yf;
193
194 snprintf(cam_info.fiducials[fid_no].fid_id,
195 sizeof(cam_info.fiducials[fid_no].fid_id), "%d", fid_no);
196 }
197 cam_info.num_fid = fid_no;
198 }
199
200 if (put_cam_info) {
201 /* create/modify camera file */
202 I_put_cam_info(camera, &cam_info);
203 }
204
205 /* set group camera */
206 if (group) {
207 /* group must be in current mapset */
208 if (!I_find_group(group))
209 G_fatal_error(_("No group '%s' in current mapset"), group);
210
211 I_put_group_camera(group, camera);
212
213 G_message(_("Group [%s] in project (location) [%s] mapset [%s] now "
214 "uses camera "
215 "file [%s]"),
216 group, location, mapset, camera);
217 }
218
219 exit(EXIT_SUCCESS);
220 }