main.c 9.1 KB

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