main.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /****************************************************************************
  2. *
  3. * MODULE: v.transform
  4. * AUTHOR(S): See below also.
  5. * Eric G. Miller <egm2@jps.net>
  6. * Upgrade to 5.7 Radim Blazek
  7. * Column support & OGR support added by Martin Landa (09/2007)
  8. *
  9. * PURPOSE: To transform a vector map's coordinates via a set of tie
  10. * points.
  11. *
  12. * COPYRIGHT: (C) 2002-2011 by the GRASS Development Team
  13. *
  14. * This program is free software under the GNU General Public
  15. * License (>=v2). Read the file COPYING that comes with GRASS
  16. * for details.
  17. *
  18. *****************************************************************************/
  19. /*
  20. *History:
  21. *- This takes an ascii digit file in one coordinate system and converts
  22. * the map to another coordinate system.
  23. * Uses the transform library: $(TRANSLIB)
  24. *
  25. * Written during the ice age of Illinois, 02/16/90, by the GRASS team, -mh.
  26. *
  27. *- Modified by Dave Gerdes 1/90 for dig_head stuff
  28. *- Modified by Radim Blazek to work on binary files 2002
  29. *- Interactive functionality disabled, 2007
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <grass/gis.h>
  35. #include <grass/vector.h>
  36. #include <grass/dbmi.h>
  37. #include <grass/glocale.h>
  38. #include "trans.h"
  39. #include "local_proto.h"
  40. int main(int argc, char *argv[])
  41. {
  42. struct file_info Current, Trans;
  43. struct GModule *module;
  44. struct Option *vold, *vnew, *xshift, *yshift, *zshift,
  45. *xscale, *yscale, *zscale, *zrot, *columns, *field_opt;
  46. struct Flag *tozero_flag, *swap_flag, *no_topo;
  47. char mon[4], date[40], buf[1000];
  48. struct Map_info Old, New;
  49. int day, yr;
  50. struct bound_box box;
  51. double ztozero;
  52. double trans_params[7]; /* xshift, ..., xscale, ..., zrot */
  53. /* columns */
  54. unsigned int i;
  55. int idx, out3d;
  56. char **tokens;
  57. char *columns_name[7]; /* xshift, yshift, zshift, xscale, yscale, zscale, zrot */
  58. int field;
  59. G_gisinit(argv[0]);
  60. module = G_define_module();
  61. G_add_keyword(_("vector"));
  62. G_add_keyword(_("transformation"));
  63. G_add_keyword("GCP");
  64. module->description =
  65. _("Performs an affine transformation (shift, scale and rotate) "
  66. "on vector map.");
  67. tozero_flag = G_define_flag();
  68. tozero_flag->key = 't';
  69. tozero_flag->description = _("Shift all z values to bottom=0");
  70. swap_flag = G_define_flag();
  71. swap_flag->key = 'w';
  72. swap_flag->description =
  73. _("Swap coordinates x, y and then apply other parameters");
  74. no_topo = G_define_flag();
  75. no_topo->key = 'b';
  76. no_topo->description = _("Do not build topology");
  77. vold = G_define_standard_option(G_OPT_V_INPUT);
  78. field_opt = G_define_standard_option(G_OPT_V_FIELD_ALL);
  79. field_opt->guisection = _("Custom");
  80. vnew = G_define_standard_option(G_OPT_V_OUTPUT);
  81. xshift = G_define_option();
  82. xshift->key = "xshift";
  83. xshift->type = TYPE_DOUBLE;
  84. xshift->required = NO;
  85. xshift->multiple = NO;
  86. xshift->description = _("Shifting value for x coordinates");
  87. xshift->answer = "0.0";
  88. xshift->guisection = _("Custom");
  89. yshift = G_define_option();
  90. yshift->key = "yshift";
  91. yshift->type = TYPE_DOUBLE;
  92. yshift->required = NO;
  93. yshift->multiple = NO;
  94. yshift->description = _("Shifting value for y coordinates");
  95. yshift->answer = "0.0";
  96. yshift->guisection = _("Custom");
  97. zshift = G_define_option();
  98. zshift->key = "zshift";
  99. zshift->type = TYPE_DOUBLE;
  100. zshift->required = NO;
  101. zshift->multiple = NO;
  102. zshift->description = _("Shifting value for z coordinates");
  103. zshift->answer = "0.0";
  104. zshift->guisection = _("Custom");
  105. xscale = G_define_option();
  106. xscale->key = "xscale";
  107. xscale->type = TYPE_DOUBLE;
  108. xscale->required = NO;
  109. xscale->multiple = NO;
  110. xscale->description = _("Scaling factor for x coordinates");
  111. xscale->answer = "1.0";
  112. xscale->guisection = _("Custom");
  113. yscale = G_define_option();
  114. yscale->key = "yscale";
  115. yscale->type = TYPE_DOUBLE;
  116. yscale->required = NO;
  117. yscale->multiple = NO;
  118. yscale->description = _("Scaling factor for y coordinates");
  119. yscale->answer = "1.0";
  120. yscale->guisection = _("Custom");
  121. zscale = G_define_option();
  122. zscale->key = "zscale";
  123. zscale->type = TYPE_DOUBLE;
  124. zscale->required = NO;
  125. zscale->multiple = NO;
  126. zscale->description = _("Scaling factor for z coordinates");
  127. zscale->answer = "1.0";
  128. zscale->guisection = _("Custom");
  129. zrot = G_define_option();
  130. zrot->key = "zrot";
  131. zrot->type = TYPE_DOUBLE;
  132. zrot->required = NO;
  133. zrot->multiple = NO;
  134. zrot->description =
  135. _("Rotation around z axis in degrees (counter-clockwise)");
  136. zrot->answer = "0.0";
  137. zrot->guisection = _("Custom");
  138. columns = G_define_standard_option(G_OPT_DB_COLUMNS);
  139. columns->label =
  140. _("Name of attribute column(s) used as transformation parameters");
  141. columns->description =
  142. _("Format: parameter:column, e.g. xshift:xs,yshift:ys,zrot:zr");
  143. columns->guisection = _("Custom");
  144. if (G_parser(argc, argv))
  145. exit(EXIT_FAILURE);
  146. strcpy(Current.name, vold->answer);
  147. strcpy(Trans.name, vnew->answer);
  148. Vect_check_input_output_name(vold->answer, vnew->answer, G_FATAL_EXIT);
  149. /* open input vector */
  150. if (Vect_open_old2(&Old, vold->answer, "", field_opt->answer) < 0)
  151. G_fatal_error(_("Unable to open vector map <%s>"), vold->answer);
  152. field = Vect_get_field_number(&Old, field_opt->answer);
  153. if (field < 1 && columns->answer) {
  154. G_fatal_error(_("Columns require a valid layer. Please use '%s' parameter."),
  155. field_opt->key);
  156. }
  157. out3d = Vect_is_3d(&Old);
  158. /* tokenize columns names */
  159. for (i = 0; i <= IDX_ZROT; i++) {
  160. columns_name[i] = NULL;
  161. }
  162. i = 0;
  163. if (columns->answer) {
  164. while (columns->answers[i]) {
  165. tokens = G_tokenize(columns->answers[i], ":");
  166. if (G_number_of_tokens(tokens) == 2) {
  167. if (strcmp(tokens[0], xshift->key) == 0)
  168. idx = IDX_XSHIFT;
  169. else if (strcmp(tokens[0], yshift->key) == 0)
  170. idx = IDX_YSHIFT;
  171. else if (strcmp(tokens[0], zshift->key) == 0)
  172. idx = IDX_ZSHIFT;
  173. else if (strcmp(tokens[0], xscale->key) == 0)
  174. idx = IDX_XSCALE;
  175. else if (strcmp(tokens[0], yscale->key) == 0)
  176. idx = IDX_YSCALE;
  177. else if (strcmp(tokens[0], zscale->key) == 0)
  178. idx = IDX_ZSCALE;
  179. else if (strcmp(tokens[0], zrot->key) == 0)
  180. idx = IDX_ZROT;
  181. else {
  182. G_warning(_("Unknown column parameter '%s'"),
  183. tokens[0]);
  184. idx = -1;
  185. }
  186. if (idx != -1)
  187. columns_name[idx] = G_store(tokens[1]);
  188. G_free_tokens(tokens);
  189. }
  190. else {
  191. G_fatal_error(_("Unable to tokenize column string: [%s]"),
  192. columns->answers[i]);
  193. }
  194. i++;
  195. }
  196. }
  197. /* determine transformation parameters */
  198. trans_params[IDX_XSHIFT] = atof(xshift->answer);
  199. trans_params[IDX_YSHIFT] = atof(yshift->answer);
  200. trans_params[IDX_ZSHIFT] = atof(zshift->answer);
  201. trans_params[IDX_XSCALE] = atof(xscale->answer);
  202. trans_params[IDX_YSCALE] = atof(yscale->answer);
  203. trans_params[IDX_ZSCALE] = atof(zscale->answer);
  204. trans_params[IDX_ZROT] = atof(zrot->answer);
  205. /* should output be 3D ?
  206. * note that z-scale and ztozero have no effect with input 2D */
  207. if (trans_params[IDX_ZSHIFT] != 0. || columns_name[IDX_ZSHIFT])
  208. out3d = WITH_Z;
  209. /* open output vector */
  210. if (Vect_open_new(&New, vnew->answer, out3d) < 0)
  211. G_fatal_error(_("Unable to create vector map <%s>"), vnew->answer);
  212. /* copy and set header */
  213. Vect_copy_head_data(&Old, &New);
  214. Vect_hist_copy(&Old, &New);
  215. Vect_hist_command(&New);
  216. sprintf(date, "%s", G_date());
  217. sscanf(date, "%*s%s%d%*s%d", mon, &day, &yr);
  218. sprintf(date, "%s %d %d", mon, day, yr);
  219. Vect_set_date(&New, date);
  220. Vect_set_person(&New, G_whoami());
  221. sprintf(buf, "transformed from %s", vold->answer);
  222. Vect_set_map_name(&New, buf);
  223. Vect_set_scale(&New, 1);
  224. Vect_set_zone(&New, 0);
  225. Vect_set_thresh(&New, 0.0);
  226. Vect_get_map_box(&Old, &box);
  227. /* z to zero */
  228. if (tozero_flag->answer)
  229. ztozero = 0 - box.B;
  230. else
  231. ztozero = 0;
  232. /* do the transformation */
  233. G_important_message(_("Tranforming features..."));
  234. transform_digit_file(&Old, &New,
  235. ztozero, swap_flag->answer, trans_params,
  236. columns_name, field);
  237. G_important_message(_("Copying attributes..."));
  238. if (Vect_copy_tables(&Old, &New, 0))
  239. G_warning(_("Failed to copy attribute table to output map"));
  240. Vect_close(&Old);
  241. if (!no_topo->answer)
  242. Vect_build(&New);
  243. Vect_get_map_box(&New, &box);
  244. G_verbose_message(_("New vector map <%s> boundary coordinates:"),
  245. vnew->answer);
  246. G_verbose_message(_(" N: %-10.3f S: %-10.3f"), box.N, box.S);
  247. G_verbose_message(_(" E: %-10.3f W: %-10.3f"), box.E, box.W);
  248. G_verbose_message(_(" B: %6.3f T: %6.3f"), box.B, box.T);
  249. Vect_close(&New);
  250. G_done_msg(" ");
  251. exit(EXIT_SUCCESS);
  252. }