Debian Codegraph @

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

raw symbols SCIP
1 /****************************************************************************
2 *
3 * MODULE: i.ortho.transform (cloned from m.transform nee g.transform)
4 * AUTHOR(S): Brian J. Buckley
5 * Glynn Clements
6 * Hamish Bowman
7 * Markus Metz
8 * PURPOSE: Utility to compute transformation based upon GCPs and
9 * output error measurements
10 * COPYRIGHT: (C) 2006-2013 by the GRASS Development Team
11 *
12 * This program is free software under the GNU General Public
13 * License (>=v2). Read the file COPYING that comes with GRASS
14 * for details.
15 *
16 *****************************************************************************/
17
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>
23
24 #include <grass/gis.h>
25 #include <grass/glocale.h>
26 #include <grass/imagery.h>
27 #include <grass/glocale.h>
28 #include "orthophoto.h"
29
30 struct Max {
31 int idx;
32 double val;
33 };
34
35 struct Stats {
36 struct Max x, y, g;
37 double sum2, rms;
38 };
39
40 static int summary;
41 static int forward;
42 static char **columns;
43 static int need_fwd;
44 static int need_rev;
45 static int need_fd;
46 static int need_rd;
47 static char *coord_file;
48
49 struct Ortho_Image_Group group;
50
51 static struct Ortho_Control_Points *points;
52
53 static int count;
54 static struct Stats fwd, rev;
55
56 /* target fns taken from i.ortho.rectify */
57 static int which_env = -1; /* 0 = cur, 1 = target */
58
59 int select_current_env(void)
60 {
61 if (which_env < 0) {
62 G_create_alt_env();
63 which_env = 0;
64 }
65 if (which_env != 0) {
66 G_switch_env();
67 which_env = 0;
68 }
69
70 return 0;
71 }
72
73 int select_target_env(void)
74 {
75 if (which_env < 0) {
76 G_create_alt_env();
77 which_env = 1;
78 }
79 if (which_env != 1) {
80 G_switch_env();
81 which_env = 1;
82 }
83
84 return 0;
85 }
86
87 static int get_target(void)
88 {
89 char location[GMAPSET_MAX];
90 char mapset[GMAPSET_MAX];
91 char buf[1024];
92 int stat;
93
94 if (!I_get_target(group.name, location, mapset)) {
95 snprintf(buf, sizeof(buf),
96 _("Target information for group <%s> missing"), group.name);
97 goto error;
98 }
99
100 snprintf(buf, sizeof(buf), "%s/%s", G_gisdbase(), location);
101 if (access(buf, 0) != 0) {
102 snprintf(buf, sizeof(buf),
103 _("Target project (location) <%s> not found"), location);
104 goto error;
105 }
106 select_target_env();
107 G_setenv_nogisrc("LOCATION_NAME", location);
108 stat = G_mapset_permissions(mapset);
109 if (stat > 0) {
110 G_setenv_nogisrc("MAPSET", mapset);
111 select_current_env();
112 return 1;
113 }
114 snprintf(buf, sizeof(buf),
115 _("Mapset <%s> in target project (location) <%s> - "), mapset,
116 location);
117 strcat(buf, stat == 0 ? _("permission denied") : _("not found"));
118 error:
119 strcat(buf, "\n");
120 strcat(buf, _("Please run i.target for group "));
121 strcat(buf, group.name);
122 G_fatal_error("%s", buf);
123 return 1; /* never reached */
124 }
125
126 static void update_max(struct Max *m, int n, double k)
127 {
128 if (k > m->val) {
129 m->idx = n;
130 m->val = k;
131 }
132 }
133
134 static void update_stats(struct Stats *st, int n, double dx, double dy,
135 double dg, double d2)
136 {
137 update_max(&st->x, n, dx);
138 update_max(&st->y, n, dy);
139 update_max(&st->g, n, dg);
140 st->sum2 += d2;
141 }
142
143 static void diagonal(double *dg, double *d2, double dx, double dy)
144 {
145 *d2 = dx * dx + dy * dy;
146 *dg = sqrt(*d2);
147 }
148
149 static void compute_transformation(void)
150 {
151 int n, i, status;
152 double e0, e1, e2, n0, n1, n2, z1, z2;
153 struct Ortho_Control_Points temp_points;
154
155 /* compute photo <-> image equations */
156 group.ref_equation_stat = I_compute_ref_equations(
157 &group.photo_points, group.E12, group.N12, group.E21, group.N21);
158
159 if (group.ref_equation_stat <= 0)
160 G_fatal_error(_("Error conducting transform (%d)"),
161 group.ref_equation_stat);
162
163 /* compute target <-> photo equations */
164
165 /* alloc and fill temp control points */
166 temp_points.count = 0;
167 temp_points.status = NULL;
168 temp_points.e1 = NULL;
169 temp_points.n1 = NULL;
170 temp_points.z1 = NULL;
171 temp_points.e2 = NULL;
172 temp_points.n2 = NULL;
173 temp_points.z2 = NULL;
174
175 /* e0, n0, equal photo coordinates not image coords */
176 for (i = 0; i < group.control_points.count; i++) {
177 status = group.control_points.status[i];
178 e1 = group.control_points.e1[i];
179 n1 = group.control_points.n1[i];
180 z1 = group.control_points.z1[i];
181 e2 = group.control_points.e2[i];
182 n2 = group.control_points.n2[i];
183 z2 = group.control_points.z2[i];
184
185 /* image to photo transformation */
186 I_georef(e1, n1, &e0, &n0, group.E12, group.N12, 1);
187 I_new_con_point(&temp_points, e0, n0, z1, e2, n2, z2, status);
188 }
189
190 group.con_equation_stat = I_compute_ortho_equations(
191 &temp_points, &group.camera_ref, &group.camera_exp, &group.XC,
192 &group.YC, &group.ZC, &group.omega, &group.phi, &group.kappa, &group.M,
193 &group.MI);
194
195 if (group.con_equation_stat <= 0)
196 G_fatal_error(_("Error conducting transform (%d)"),
197 group.con_equation_stat);
198
199 count = 0;
200
201 for (n = 0; n < points->count; n++) {
202 double etmp, ntmp;
203 double fx, fy, fd, fd2;
204 double rx, ry, rd, rd2;
205
206 if (points->status[n] <= 0)
207 continue;
208
209 count++;
210
211 fd = fd2 = rd = rd2 = 0;
212
213 if (need_fwd) {
214 /* image -> photo -> target */
215
216 /* image coordinates ex, nx to photo coordinates ex1, nx1 */
217 I_georef(points->e1[n], points->n1[n], &etmp, &ntmp, group.E12,
218 group.N12, 1);
219
220 /* photo coordinates ex1, nx1 to target coordinates e1, n1 */
221 I_inverse_ortho_ref(etmp, ntmp, points->z1[n], &e2, &n2, &z2,
222 &group.camera_ref, group.XC, group.YC, group.ZC,
223 group.MI);
224
225 fx = fabs(e2 - points->e2[n]);
226 fy = fabs(n2 - points->n2[n]);
227
228 if (need_fd)
229 diagonal(&fd, &fd2, fx, fy);
230
231 if (summary)
232 update_stats(&fwd, n, fx, fy, fd, fd2);
233 }
234
235 if (need_rev) {
236 /* target -> photo -> image */
237
238 /* target coordinates e1, n1 to photo coordinates ex1, nx1 */
239 I_ortho_ref(points->e2[n], points->n2[n], points->z2[n], &etmp,
240 &ntmp, &z2, &group.camera_ref, group.XC, group.YC,
241 group.ZC, group.M);
242
243 /* photo coordinates ex1, nx1 to image coordinates ex, nx */
244 I_georef(etmp, ntmp, &e1, &n1, group.E21, group.N21, 1);
245
246 rx = fabs(e1 - points->e1[n]);
247 ry = fabs(n1 - points->n1[n]);
248
249 if (need_rd)
250 diagonal(&rd, &rd2, rx, ry);
251
252 if (summary)
253 update_stats(&rev, n, rx, ry, rd, rd2);
254 }
255
256 if (!columns[0])
257 continue;
258
259 if (coord_file)
260 continue;
261
262 for (i = 0;; i++) {
263 const char *col = columns[i];
264
265 if (!col)
266 break;
267
268 if (strcmp("idx", col) == 0)
269 printf(" %d", n);
270 if (strcmp("src", col) == 0)
271 printf(" %f %f", points->e1[n], points->n1[n]);
272 if (strcmp("dst", col) == 0)
273 printf(" %f %f", points->e2[n], points->n2[n]);
274 if (strcmp("fwd", col) == 0)
275 printf(" %f %f", e2, n2);
276 if (strcmp("rev", col) == 0)
277 printf(" %f %f", e1, n1);
278 if (strcmp("fxy", col) == 0)
279 printf(" %f %f", fx, fy);
280 if (strcmp("rxy", col) == 0)
281 printf(" %f %f", rx, ry);
282 if (strcmp("fd", col) == 0)
283 printf(" %f", fd);
284 if (strcmp("rd", col) == 0)
285 printf(" %f", rd);
286 }
287
288 printf("\n");
289 }
290
291 if (summary && count > 0) {
292 fwd.rms = sqrt(fwd.sum2 / count);
293 rev.rms = sqrt(rev.sum2 / count);
294 }
295 }
296
297 static void do_max(char name, const struct Max *m)
298 {
299 printf("%c[%d] = %.2f\n", name, m->idx, m->val);
300 }
301
302 static void do_stats(const char *name, const struct Stats *st)
303 {
304 printf("%s:\n", name);
305 do_max('x', &st->x);
306 do_max('y', &st->y);
307 do_max('g', &st->g);
308 printf("RMS = %.2f\n", st->rms);
309 }
310
311 static void analyze(void)
312 {
313 if (group.ref_equation_stat == -1)
314 G_warning(_("Poorly placed image to photo control points"));
315 else if (group.con_equation_stat == -1)
316 G_warning(_("Poorly placed image to target control points"));
317 else if (group.ref_equation_stat == -2 || group.con_equation_stat == -2)
318 G_fatal_error(_("Insufficient memory"));
319 else if (group.ref_equation_stat < 0 || group.con_equation_stat < 0)
320 G_fatal_error(_("Parameter error"));
321 else if (group.ref_equation_stat == 0 || group.con_equation_stat == 0)
322 G_fatal_error(_("No active control points"));
323 else if (summary) {
324 printf("Number of active points: %d\n", count);
325 do_stats("Forward", &fwd);
326 do_stats("Reverse", &rev);
327 }
328 }
329
330 static void parse_format(void)
331 {
332 int i;
333
334 if (summary) {
335 need_fwd = need_rev = need_fd = need_rd = 1;
336 return;
337 }
338
339 if (!columns)
340 return;
341
342 for (i = 0;; i++) {
343 const char *col = columns[i];
344
345 if (!col)
346 break;
347
348 if (strcmp("fwd", col) == 0)
349 need_fwd = 1;
350 if (strcmp("fxy", col) == 0)
351 need_fwd = 1;
352 if (strcmp("fd", col) == 0)
353 need_fwd = need_fd = 1;
354 if (strcmp("rev", col) == 0)
355 need_rev = 1;
356 if (strcmp("rxy", col) == 0)
357 need_rev = 1;
358 if (strcmp("rd", col) == 0)
359 need_rev = need_rd = 1;
360 }
361 }
362
363 static void dump_cooefs(void)
364 {
365 int i;
366
367 for (i = 0; i < 3; i++)
368 fprintf(stdout, "E%d=%.15g\n", i,
369 forward ? group.E12[i] : group.E21[i]);
370
371 for (i = 0; i < 3; i++)
372 fprintf(stdout, "N%d=%.15g\n", i,
373 forward ? group.N12[i] : group.N21[i]);
374
375 /* print ortho transformation matrix ? */
376 }
377
378 static void xform_value(double east, double north, double height)
379 {
380 double e1, n1, z1, xe, xn, xz;
381
382 if (forward) {
383 /* image -> photo -> target */
384 I_georef(east, north, &e1, &n1, group.E12, group.N12, 1);
385 z1 = height;
386 I_inverse_ortho_ref(e1, n1, z1, &xe, &xn, &xz, &group.camera_ref,
387 group.XC, group.YC, group.ZC, group.MI);
388 xz = z1;
389 }
390 else {
391 /* target -> photo -> image */
392 I_ortho_ref(east, north, height, &e1, &n1, &z1, &group.camera_ref,
393 group.XC, group.YC, group.ZC, group.M);
394
395 I_georef(e1, n1, &xe, &xn, group.E21, group.N21, 1);
396 xz = 0.;
397 }
398
399 fprintf(stdout, "%.15g %.15g %.15g\n", xe, xn, xz);
400 }
401
402 static void do_pt_xforms(void)
403 {
404 double easting, northing, height;
405 int ret;
406 FILE *fp;
407
408 if (strcmp(coord_file, "-") == 0)
409 fp = stdin;
410 else {
411 fp = fopen(coord_file, "r");
412 if (!fp)
413 G_fatal_error(_("Unable to open file <%s>"), coord_file);
414 }
415
416 for (;;) {
417 char buf[1024];
418
419 if (!G_getl2(buf, sizeof(buf), fp))
420 break;
421
422 if ((buf[0] == '#') || (buf[0] == '\0'))
423 continue;
424
425 /* ? sscanf(buf, "%s %s", &east_str, &north_str)
426 ? G_scan_easting(,,-1)
427 ? G_scan_northing(,,-1) */
428 /* ? multiple delims with sscanf(buf, "%[ ,|\t]", &dummy) ? */
429
430 ret = sscanf(buf, "%lf %lf %lf", &easting, &northing, &height);
431 if (ret != 3)
432 G_fatal_error(_("Invalid coordinates: [%s]"), buf);
433
434 xform_value(easting, northing, height);
435 }
436
437 if (fp != stdin)
438 fclose(fp);
439 }
440
441 int main(int argc, char **argv)
442 {
443 struct Option *grp, *fmt, *xfm_pts;
444 struct Flag *sum, *rev_flag, *dump_flag, *pan_flag;
445 struct GModule *module;
446 char *desc;
447 char *camera;
448
449 G_gisinit(argv[0]);
450
451 /* Get Args */
452 module = G_define_module();
453 G_add_keyword(_("imagery"));
454 G_add_keyword(_("orthorectify"));
455 G_add_keyword(_("transformation"));
456 G_add_keyword(_("GCP"));
457 module->description =
458 _("Computes a coordinate transformation based on the control points.");
459
460 grp = G_define_standard_option(G_OPT_I_GROUP);
461
462 fmt = G_define_option();
463 fmt->key = "format";
464 fmt->type = TYPE_STRING;
465 fmt->required = NO;
466 fmt->multiple = YES;
467 fmt->options = "idx,src,dst,fwd,rev,fxy,rxy,fd,rd";
468 desc = NULL;
469 G_asprintf(
470 &desc, "idx;%s;src;%s;dst;%s;fwd;%s;rev;%s;fxy;%s;rxy;%s;fd;%s;rd;%s",
471 _("point index"), _("source coordinates"), _("destination coordinates"),
472 _("forward coordinates (destination)"),
473 _("reverse coordinates (source)"),
474 _("forward coordinates difference (destination)"),
475 _("reverse coordinates difference (source)"),
476 _("forward error (destination)"), _("reverse error (source)"));
477 fmt->descriptions = desc;
478 fmt->answer = "fd,rd";
479 fmt->description = _("Output format");
480
481 sum = G_define_flag();
482 sum->key = 's';
483 sum->description = _("Display summary information");
484
485 xfm_pts = G_define_standard_option(G_OPT_F_INPUT);
486 xfm_pts->key = "coords";
487 xfm_pts->required = NO;
488 xfm_pts->label = _(
489 "File containing coordinates to transform (\"-\" to read from stdin)");
490 xfm_pts->description =
491 _("Local x,y,z coordinates to target east,north,height");
492
493 rev_flag = G_define_flag();
494 rev_flag->key = 'r';
495 rev_flag->label = _("Reverse transform of coords file or coeff. dump");
496 rev_flag->description =
497 _("Target east,north,height coordinates to local x,y,z");
498
499 dump_flag = G_define_flag();
500 dump_flag->key = 'x';
501 dump_flag->description = _("Display transform matrix coefficients");
502
503 pan_flag = G_define_flag();
504 pan_flag->key = 'p';
505 pan_flag->description = _("Enable panorama camera correction");
506
507 if (G_parser(argc, argv))
508 exit(EXIT_FAILURE);
509
510 G_strip(grp->answer);
511 strcpy(group.name, grp->answer);
512
513 summary = !!sum->answer;
514 columns = fmt->answers;
515 forward = !rev_flag->answer;
516 coord_file = xfm_pts->answer;
517
518 if (pan_flag->answer)
519 I_ortho_panorama();
520
521 if (!I_get_ref_points(group.name, &group.photo_points)) {
522 G_fatal_error(_("Can not read reference points for group <%s>"),
523 group.name);
524 }
525 if (!I_get_con_points(group.name, &group.control_points)) {
526 group.control_points.count = 0;
527 }
528
529 camera = (char *)G_malloc(GNAME_MAX * sizeof(char));
530 if (!I_get_group_camera(group.name, camera))
531 G_fatal_error(_("No camera reference file selected for group <%s>"),
532 group.name);
533
534 if (!I_get_cam_info(camera, &group.camera_ref))
535 G_fatal_error(_("Bad format in camera file for group <%s>"),
536 group.name);
537
538 /* get initial camera exposure station, if any */
539 if (I_find_initial(group.name)) {
540 if (!I_get_init_info(group.name, &group.camera_exp))
541 G_warning(
542 _("Bad format in initial exposure station file for group <%s>"),
543 group.name);
544 }
545
546 /* get the target */
547 get_target();
548
549 points = &group.control_points;
550
551 parse_format();
552
553 /* I_compute_ortho_equations() must be run in the target location */
554 select_target_env();
555 compute_transformation();
556 select_current_env();
557
558 analyze();
559
560 if (dump_flag->answer)
561 dump_cooefs();
562
563 if (coord_file)
564 do_pt_xforms();
565
566 return 0;
567 }