main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /****************************************************************************
  2. *
  3. * MODULE: g.transform
  4. * AUTHOR(S): Brian J. Buckley<br>
  5. * Glynn Clements
  6. * PURPOSE: Utility to compute transformation based upon GCPs and
  7. * output error measurements
  8. * COPYRIGHT: (C) 2006-2008 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public
  11. * License (>=v2). Read the file COPYING that comes with GRASS
  12. * for details.
  13. *
  14. *****************************************************************************/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include <grass/gis.h>
  20. #include <grass/glocale.h>
  21. #include <grass/imagery.h>
  22. #include <grass/glocale.h>
  23. extern int CRS_compute_georef_equations(struct Control_Points *, double *,
  24. double *, double *, double *, int);
  25. extern int CRS_georef(double, double, double *, double *, double *, double *,
  26. int);
  27. struct Max
  28. {
  29. int idx;
  30. double val;
  31. };
  32. struct Stats
  33. {
  34. struct Max x, y, g;
  35. double sum2, rms;
  36. };
  37. static char *name;
  38. static int order;
  39. static int summary;
  40. static char **columns;
  41. static int need_fwd;
  42. static int need_rev;
  43. static int need_fd;
  44. static int need_rd;
  45. static struct Control_Points points;
  46. static int equation_stat;
  47. static int count;
  48. static struct Stats fwd, rev;
  49. static void update_max(struct Max *m, int n, double k)
  50. {
  51. if (k > m->val) {
  52. m->idx = n;
  53. m->val = k;
  54. }
  55. }
  56. static void update_stats(struct Stats *st, int n, double dx, double dy,
  57. double dg, double d2)
  58. {
  59. update_max(&st->x, n, dx);
  60. update_max(&st->y, n, dy);
  61. update_max(&st->g, n, dg);
  62. st->sum2 += d2;
  63. }
  64. static void diagonal(double *dg, double *d2, double dx, double dy)
  65. {
  66. *d2 = dx * dx + dy * dy;
  67. *dg = sqrt(*d2);
  68. }
  69. static void compute_transformation(void)
  70. {
  71. static const int order_pnts[3] = { 3, 6, 10 };
  72. double E12[10], N12[10], E21[10], N21[10];
  73. int n, i;
  74. equation_stat =
  75. CRS_compute_georef_equations(&points, E12, N12, E21, N21, order);
  76. if (equation_stat == 0)
  77. G_fatal_error(_("Not enough points, %d are required"),
  78. order_pnts[order - 1]);
  79. if (equation_stat <= 0)
  80. return;
  81. count = 0;
  82. for (n = 0; n < points.count; n++) {
  83. double e1, n1, e2, n2;
  84. double fx, fy, fd, fd2;
  85. double rx, ry, rd, rd2;
  86. if (points.status[n] <= 0)
  87. continue;
  88. count++;
  89. if (need_fwd) {
  90. CRS_georef(points.e1[n], points.n1[n], &e2, &n2, E12, N12, order);
  91. fx = fabs(e2 - points.e2[n]);
  92. fy = fabs(n2 - points.n2[n]);
  93. if (need_fd)
  94. diagonal(&fd, &fd2, fx, fy);
  95. if (summary)
  96. update_stats(&fwd, n, fx, fy, fd, fd2);
  97. }
  98. if (need_rev) {
  99. CRS_georef(points.e2[n], points.n2[n], &e1, &n1, E21, N21, order);
  100. rx = fabs(e1 - points.e1[n]);
  101. ry = fabs(n1 - points.n1[n]);
  102. if (need_rd)
  103. diagonal(&rd, &rd2, rx, ry);
  104. if (summary)
  105. update_stats(&rev, n, rx, ry, rd, rd2);
  106. }
  107. if (!columns)
  108. continue;
  109. for (i = 0;; i++) {
  110. const char *col = columns[i];
  111. if (!col)
  112. break;
  113. if (strcmp("idx", col) == 0)
  114. printf(" %d", n);
  115. if (strcmp("src", col) == 0)
  116. printf(" %f %f", points.e1[n], points.n1[n]);
  117. if (strcmp("dst", col) == 0)
  118. printf(" %f %f", points.e2[n], points.n2[n]);
  119. if (strcmp("fwd", col) == 0)
  120. printf(" %f %f", e2, n2);
  121. if (strcmp("rev", col) == 0)
  122. printf(" %f %f", e1, n1);
  123. if (strcmp("fxy", col) == 0)
  124. printf(" %f %f", fx, fy);
  125. if (strcmp("rxy", col) == 0)
  126. printf(" %f %f", rx, ry);
  127. if (strcmp("fd", col) == 0)
  128. printf(" %f", fd);
  129. if (strcmp("rd", col) == 0)
  130. printf(" %f", rd);
  131. }
  132. printf("\n");
  133. }
  134. if (summary && count > 0) {
  135. fwd.rms = sqrt(fwd.sum2 / count);
  136. rev.rms = sqrt(rev.sum2 / count);
  137. }
  138. }
  139. static void do_max(char name, const struct Max *m)
  140. {
  141. printf("%c[%d] = %.2f\n", name, m->idx, m->val);
  142. }
  143. static void do_stats(const char *name, const struct Stats *st)
  144. {
  145. printf("%s:\n", name);
  146. do_max('x', &st->x);
  147. do_max('y', &st->y);
  148. do_max('g', &st->g);
  149. printf("RMS = %.2f\n", st->rms);
  150. }
  151. static void analyze(void)
  152. {
  153. if (equation_stat == -1)
  154. G_warning(_("Poorly placed control points"));
  155. else if (equation_stat == -2)
  156. G_fatal_error(_("Insufficient memory"));
  157. else if (equation_stat < 0)
  158. G_fatal_error(_("Parameter error"));
  159. else if (equation_stat == 0)
  160. G_fatal_error(_("No active control points"));
  161. else if (summary) {
  162. printf("Number of active points: %d\n", count);
  163. do_stats("Forward", &fwd);
  164. do_stats("Reverse", &rev);
  165. }
  166. }
  167. static void parse_format(void)
  168. {
  169. int i;
  170. if (summary) {
  171. need_fwd = need_rev = need_fd = need_rd = 1;
  172. return;
  173. }
  174. if (!columns)
  175. return;
  176. for (i = 0;; i++) {
  177. const char *col = columns[i];
  178. if (!col)
  179. break;
  180. if (strcmp("fwd", col) == 0)
  181. need_fwd = 1;
  182. if (strcmp("fxy", col) == 0)
  183. need_fwd = 1;
  184. if (strcmp("fd", col) == 0)
  185. need_fwd = need_fd = 1;
  186. if (strcmp("rev", col) == 0)
  187. need_rev = 1;
  188. if (strcmp("rxy", col) == 0)
  189. need_rev = 1;
  190. if (strcmp("rd", col) == 0)
  191. need_rev = need_rd = 1;
  192. }
  193. }
  194. int main(int argc, char **argv)
  195. {
  196. struct Option *grp, *val, *fmt;
  197. struct Flag *sum;
  198. struct GModule *module;
  199. G_gisinit(argv[0]);
  200. /* Get Args */
  201. module = G_define_module();
  202. G_add_keyword(_("general"));
  203. G_add_keyword(_("transformation"));
  204. G_add_keyword(_("GCP"));
  205. module->description =
  206. _("Computes a coordinate transformation based on the control points.");
  207. grp = G_define_standard_option(G_OPT_I_GROUP);
  208. val = G_define_option();
  209. val->key = "order";
  210. val->type = TYPE_INTEGER;
  211. val->required = YES;
  212. val->options = "1-3";
  213. val->description = _("Rectification polynomial order");
  214. fmt = G_define_option();
  215. fmt->key = "format";
  216. fmt->type = TYPE_STRING;
  217. fmt->required = NO;
  218. fmt->multiple = YES;
  219. fmt->options = "idx,src,dst,fwd,rev,fxy,rxy,fd,rd";
  220. fmt->descriptions = _("idx;point index;"
  221. "src;source coordinates;"
  222. "dst;destination coordinates;"
  223. "fwd;forward coordinates (destination);"
  224. "rev;reverse coordinates (source);"
  225. "fxy;forward coordinates difference (destination);"
  226. "rxy;reverse coordinates difference (source);"
  227. "fd;forward error (destination);"
  228. "rd;reverse error (source)");
  229. fmt->answer = "fd,rd";
  230. fmt->description = _("Output format");
  231. sum = G_define_flag();
  232. sum->key = 's';
  233. sum->description = _("Display summary information");
  234. if (G_parser(argc, argv))
  235. exit(EXIT_FAILURE);
  236. name = grp->answer;
  237. order = atoi(val->answer);
  238. summary = !!sum->answer;
  239. columns = fmt->answers;
  240. I_get_control_points(name, &points);
  241. parse_format();
  242. compute_transformation();
  243. I_put_control_points(name, &points);
  244. analyze();
  245. return 0;
  246. }