main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /****************************************************************************
  2. *
  3. * MODULE: m.transform (nee g.transform)
  4. * AUTHOR(S): Brian J. Buckley
  5. * Glynn Clements
  6. * Hamish Bowman
  7. * PURPOSE: Utility to compute transformation based upon GCPs and
  8. * output error measurements
  9. * COPYRIGHT: (C) 2006-2010 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <math.h>
  20. #include <grass/gis.h>
  21. #include <grass/glocale.h>
  22. #include <grass/imagery.h>
  23. #include <grass/glocale.h>
  24. struct Max
  25. {
  26. int idx;
  27. double val;
  28. };
  29. struct Stats
  30. {
  31. struct Max x, y, g;
  32. double sum2, rms;
  33. };
  34. static char *name;
  35. static int order;
  36. static int summary;
  37. static int forward;
  38. static char **columns;
  39. static int need_fwd;
  40. static int need_rev;
  41. static int need_fd;
  42. static int need_rd;
  43. static char *coord_file;
  44. static double E12[10], N12[10], E21[10], N21[10];
  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. int n, i;
  73. equation_stat =
  74. I_compute_georef_equations(&points, E12, N12, E21, N21, order);
  75. if (equation_stat == 0)
  76. G_fatal_error(_("Not enough points, %d are required"),
  77. order_pnts[order - 1]);
  78. if (equation_stat <= 0)
  79. G_fatal_error(_("Error conducting transform (%d)"), equation_stat);
  80. count = 0;
  81. for (n = 0; n < points.count; n++) {
  82. double e1, n1, e2, n2;
  83. double fx, fy, fd, fd2;
  84. double rx, ry, rd, rd2;
  85. if (points.status[n] <= 0)
  86. continue;
  87. count++;
  88. if (need_fwd) {
  89. I_georef(points.e1[n], points.n1[n], &e2, &n2, E12, N12, order);
  90. fx = fabs(e2 - points.e2[n]);
  91. fy = fabs(n2 - points.n2[n]);
  92. if (need_fd)
  93. diagonal(&fd, &fd2, fx, fy);
  94. if (summary)
  95. update_stats(&fwd, n, fx, fy, fd, fd2);
  96. }
  97. if (need_rev) {
  98. I_georef(points.e2[n], points.n2[n], &e1, &n1, E21, N21, order);
  99. rx = fabs(e1 - points.e1[n]);
  100. ry = fabs(n1 - points.n1[n]);
  101. if (need_rd)
  102. diagonal(&rd, &rd2, rx, ry);
  103. if (summary)
  104. update_stats(&rev, n, rx, ry, rd, rd2);
  105. }
  106. if (!columns[0])
  107. continue;
  108. if (coord_file)
  109. continue;
  110. for (i = 0;; i++) {
  111. const char *col = columns[i];
  112. if (!col)
  113. break;
  114. if (strcmp("idx", col) == 0)
  115. printf(" %d", n);
  116. if (strcmp("src", col) == 0)
  117. printf(" %f %f", points.e1[n], points.n1[n]);
  118. if (strcmp("dst", col) == 0)
  119. printf(" %f %f", points.e2[n], points.n2[n]);
  120. if (strcmp("fwd", col) == 0)
  121. printf(" %f %f", e2, n2);
  122. if (strcmp("rev", col) == 0)
  123. printf(" %f %f", e1, n1);
  124. if (strcmp("fxy", col) == 0)
  125. printf(" %f %f", fx, fy);
  126. if (strcmp("rxy", col) == 0)
  127. printf(" %f %f", rx, ry);
  128. if (strcmp("fd", col) == 0)
  129. printf(" %f", fd);
  130. if (strcmp("rd", col) == 0)
  131. printf(" %f", rd);
  132. }
  133. printf("\n");
  134. }
  135. if (summary && count > 0) {
  136. fwd.rms = sqrt(fwd.sum2 / count);
  137. rev.rms = sqrt(rev.sum2 / count);
  138. }
  139. }
  140. static void do_max(char name, const struct Max *m)
  141. {
  142. printf("%c[%d] = %.2f\n", name, m->idx, m->val);
  143. }
  144. static void do_stats(const char *name, const struct Stats *st)
  145. {
  146. printf("%s:\n", name);
  147. do_max('x', &st->x);
  148. do_max('y', &st->y);
  149. do_max('g', &st->g);
  150. printf("RMS = %.2f\n", st->rms);
  151. }
  152. static void analyze(void)
  153. {
  154. if (equation_stat == -1)
  155. G_warning(_("Poorly placed control points"));
  156. else if (equation_stat == -2)
  157. G_fatal_error(_("Insufficient memory"));
  158. else if (equation_stat < 0)
  159. G_fatal_error(_("Parameter error"));
  160. else if (equation_stat == 0)
  161. G_fatal_error(_("No active control points"));
  162. else if (summary) {
  163. printf("Number of active points: %d\n", count);
  164. do_stats("Forward", &fwd);
  165. do_stats("Reverse", &rev);
  166. }
  167. }
  168. static void parse_format(void)
  169. {
  170. int i;
  171. if (summary) {
  172. need_fwd = need_rev = need_fd = need_rd = 1;
  173. return;
  174. }
  175. if (!columns)
  176. return;
  177. for (i = 0;; i++) {
  178. const char *col = columns[i];
  179. if (!col)
  180. break;
  181. if (strcmp("fwd", col) == 0)
  182. need_fwd = 1;
  183. if (strcmp("fxy", col) == 0)
  184. need_fwd = 1;
  185. if (strcmp("fd", col) == 0)
  186. need_fwd = need_fd = 1;
  187. if (strcmp("rev", col) == 0)
  188. need_rev = 1;
  189. if (strcmp("rxy", col) == 0)
  190. need_rev = 1;
  191. if (strcmp("rd", col) == 0)
  192. need_rev = need_rd = 1;
  193. }
  194. }
  195. static void dump_cooefs(void)
  196. {
  197. int i;
  198. static const int order_pnts[3] = { 3, 6, 10 };
  199. for (i = 0; i < order_pnts[order - 1]; i++)
  200. fprintf(stdout, "E%d=%.15g\n", i, forward ? E12[i] : E21[i]);
  201. for (i = 0; i < order_pnts[order - 1]; i++)
  202. fprintf(stdout, "N%d=%.15g\n", i, forward ? N12[i] : N21[i]);
  203. }
  204. static void xform_value(double east, double north)
  205. {
  206. double xe, xn;
  207. if(forward)
  208. I_georef(east, north, &xe, &xn, E12, N12, order);
  209. else
  210. I_georef(east, north, &xe, &xn, E21, N21, order);
  211. fprintf(stdout, "%.15g %.15g\n", xe, xn);
  212. }
  213. static void do_pt_xforms(void)
  214. {
  215. double easting, northing;
  216. int ret;
  217. FILE *fp;
  218. if (strcmp(coord_file, "-") == 0)
  219. fp = stdin;
  220. else {
  221. fp = fopen(coord_file, "r");
  222. if (!fp)
  223. G_fatal_error(_("Unable to open file <%s>"), coord_file);
  224. }
  225. for (;;) {
  226. char buf[64];
  227. if (!G_getl2(buf, sizeof(buf), fp))
  228. break;
  229. if ((buf[0] == '#') || (buf[0] == '\0'))
  230. continue;
  231. /* ? sscanf(buf, "%s %s", &east_str, &north_str)
  232. ? G_scan_easting(,,-1)
  233. ? G_scan_northing(,,-1) */
  234. /* ? muliple delims with sscanf(buf, "%[ ,|\t]", &dummy) ? */
  235. ret = sscanf(buf, "%lf %lf", &easting, &northing);
  236. if (ret != 2)
  237. G_fatal_error(_("Invalid coordinates: [%s]"), buf);
  238. xform_value(easting, northing);
  239. }
  240. if (fp != stdin)
  241. fclose(fp);
  242. }
  243. int main(int argc, char **argv)
  244. {
  245. struct Option *grp, *val, *fmt, *xfm_pts;
  246. struct Flag *sum, *rev_flag, *dump_flag;
  247. struct GModule *module;
  248. char *desc;
  249. G_gisinit(argv[0]);
  250. /* Get Args */
  251. module = G_define_module();
  252. G_add_keyword(_("miscellaneous"));
  253. G_add_keyword(_("transformation"));
  254. G_add_keyword("GCP");
  255. module->description =
  256. _("Computes a coordinate transformation based on the control points.");
  257. grp = G_define_standard_option(G_OPT_I_GROUP);
  258. val = G_define_option();
  259. val->key = "order";
  260. val->type = TYPE_INTEGER;
  261. val->required = YES;
  262. val->options = "1-3";
  263. val->answer = "1";
  264. val->description = _("Rectification polynomial order");
  265. fmt = G_define_option();
  266. fmt->key = "format";
  267. fmt->type = TYPE_STRING;
  268. fmt->required = NO;
  269. fmt->multiple = YES;
  270. fmt->options = "idx,src,dst,fwd,rev,fxy,rxy,fd,rd";
  271. desc = NULL;
  272. G_asprintf(&desc,
  273. "idx;%s;src;%s;dst;%s;fwd;%s;rev;%s;fxy;%s;rxy;%s;fd;%s;rd;%s",
  274. _("point index"),
  275. _("source coordinates"),
  276. _("destination coordinates"),
  277. _("forward coordinates (destination)"),
  278. _("reverse coordinates (source)"),
  279. _("forward coordinates difference (destination)"),
  280. _("reverse coordinates difference (source)"),
  281. _("forward error (destination)"),
  282. _("reverse error (source)"));
  283. fmt->descriptions = desc;
  284. fmt->answer = "fd,rd";
  285. fmt->description = _("Output format");
  286. sum = G_define_flag();
  287. sum->key = 's';
  288. sum->description = _("Display summary information");
  289. xfm_pts = G_define_standard_option(G_OPT_F_INPUT);
  290. xfm_pts->required = NO;
  291. xfm_pts->label =
  292. _("File containing coordinates to transform (\"-\" to read from stdin)");
  293. xfm_pts->description = _("Local x,y coordinates to target east,north");
  294. rev_flag = G_define_flag();
  295. rev_flag->key = 'r';
  296. rev_flag->label = _("Reverse transform of coords file or coeff. dump");
  297. rev_flag->description = _("Target east,north coordinates to local x,y");
  298. dump_flag = G_define_flag();
  299. dump_flag->key = 'x';
  300. dump_flag->description = _("Display transform matrix coefficients");
  301. if (G_parser(argc, argv))
  302. exit(EXIT_FAILURE);
  303. name = grp->answer;
  304. order = atoi(val->answer);
  305. summary = !!sum->answer;
  306. columns = fmt->answers;
  307. forward = !rev_flag->answer;
  308. coord_file = xfm_pts->answer;
  309. I_get_control_points(name, &points);
  310. parse_format();
  311. compute_transformation();
  312. I_put_control_points(name, &points);
  313. analyze();
  314. if(dump_flag->answer)
  315. dump_cooefs();
  316. if(coord_file)
  317. do_pt_xforms();
  318. return 0;
  319. }