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