trans_digit.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <math.h>
  2. #include <grass/vector.h>
  3. #include <grass/gis.h>
  4. #include <grass/dbmi.h>
  5. #include <grass/glocale.h>
  6. #include "trans.h"
  7. #define PI M_PI
  8. int transform_digit_file(struct Map_info *Old, struct Map_info *New,
  9. double ztozero, int swap_xy, int swap_xz,
  10. int swap_yz, int swap_after,
  11. double *trans_params_def, char **columns, int field)
  12. {
  13. int i, type, cat, line, ret;
  14. int verbose, format;
  15. unsigned int j;
  16. double *trans_params;
  17. double ang, x, y, tmp;
  18. static struct line_pnts *Points;
  19. static struct line_cats *Cats;
  20. /* db */
  21. struct field_info *fi;
  22. int ctype;
  23. dbDriver *driver;
  24. dbValue val;
  25. cat = -1; /* dummy value for debugging */
  26. Points = Vect_new_line_struct();
  27. Cats = Vect_new_cats_struct();
  28. driver = NULL;
  29. fi = NULL;
  30. if (field > 0) {
  31. fi = Vect_get_field(Old, field);
  32. driver = db_start_driver_open_database(fi->driver, fi->database);
  33. if (!driver)
  34. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  35. fi->database, fi->driver);
  36. trans_params = (double *)G_calloc(IDX_ZROT + 1, sizeof(double));
  37. }
  38. else {
  39. trans_params = trans_params_def;
  40. ang = PI * trans_params[IDX_ZROT] / 180;
  41. }
  42. line = 0;
  43. ret = 1;
  44. format = G_info_format();
  45. verbose = G_verbose() > G_verbose_min();
  46. while (TRUE) {
  47. type = Vect_read_next_line(Old, Points, Cats);
  48. if (type == -1) { /* error */
  49. ret = 0;
  50. break;
  51. }
  52. if (type == -2) { /* EOF */
  53. ret = 1;
  54. break;
  55. }
  56. if (field != -1 && !Vect_cat_get(Cats, field, NULL))
  57. continue;
  58. if (verbose && line % 1000 == 0) {
  59. if (format == G_INFO_FORMAT_PLAIN)
  60. fprintf(stderr, "%d..", line);
  61. else
  62. fprintf(stderr, "%11d\b\b\b\b\b\b\b\b\b\b\b", line);
  63. }
  64. if (swap_xy && !swap_after) {
  65. for (i = 0; i < Points->n_points; i++) {
  66. x = Points->x[i];
  67. Points->x[i] = Points->y[i];
  68. Points->y[i] = x;
  69. }
  70. }
  71. if (swap_xz && !swap_after) {
  72. for (i = 0; i < Points->n_points; i++) {
  73. tmp = Points->z[i];
  74. Points->z[i] = Points->x[i];
  75. Points->x[i] = tmp;
  76. }
  77. }
  78. if (swap_yz && !swap_after) {
  79. for (i = 0; i < Points->n_points; i++) {
  80. tmp = Points->z[i];
  81. Points->z[i] = Points->y[i];
  82. Points->y[i] = tmp;
  83. }
  84. }
  85. /* get transformation parameters */
  86. if (field > 0) {
  87. Vect_cat_get(Cats, field, &cat); /* get first category */
  88. if (cat > -1) {
  89. for (j = 0; j <= IDX_ZROT; j++) {
  90. if (columns[j] == NULL) {
  91. trans_params[j] = trans_params_def[j];
  92. continue;
  93. }
  94. ctype = db_column_Ctype(driver, fi->table, columns[j]);
  95. switch (ctype) {
  96. case DB_C_TYPE_INT:
  97. case DB_C_TYPE_DOUBLE:
  98. case DB_C_TYPE_STRING:
  99. break;
  100. case -1:
  101. G_fatal_error(_("Column <%s> not found in table <%s>"),
  102. columns[j], fi->table);
  103. default:
  104. G_fatal_error(_("Unsupported column type of <%s>"),
  105. columns[j]);
  106. }
  107. if (db_select_value
  108. (driver, fi->table, fi->key, cat, columns[j], &val) != 1
  109. || db_test_value_isnull(&val)) {
  110. trans_params[j] = trans_params_def[j];
  111. G_warning(_("Unable to select value for category %d from table <%s>, column <%s>. "
  112. "For category %d using default transformation parameter %.3f."),
  113. cat, fi->table, columns[j], cat,
  114. trans_params[j]);
  115. }
  116. else {
  117. trans_params[j] = db_get_value_as_double(&val, ctype);
  118. }
  119. }
  120. }
  121. else {
  122. G_warning(_("No category number defined. Using default transformation parameters."));
  123. for (j = 0; j <= IDX_ZROT; j++) {
  124. trans_params[j] = trans_params_def[j];
  125. }
  126. }
  127. ang = PI * trans_params[IDX_ZROT] / 180;
  128. }
  129. /* transform points */
  130. for (i = 0; i < Points->n_points; i++) {
  131. G_debug(3, "idx=%d, cat=%d, xshift=%g, yshift=%g, zshift=%g, "
  132. "xscale=%g, yscale=%g, zscale=%g, zrot=%g",
  133. i, cat, trans_params[IDX_XSHIFT],
  134. trans_params[IDX_YSHIFT], trans_params[IDX_ZSHIFT],
  135. trans_params[IDX_XSCALE], trans_params[IDX_YSCALE],
  136. trans_params[IDX_ZSCALE], trans_params[IDX_ZROT]);
  137. /* transform point */
  138. x = trans_params[IDX_XSHIFT] +
  139. trans_params[IDX_XSCALE] * Points->x[i] * cos(ang)
  140. - trans_params[IDX_YSCALE] * Points->y[i] * sin(ang);
  141. y = trans_params[IDX_YSHIFT] +
  142. trans_params[IDX_XSCALE] * Points->x[i] * sin(ang)
  143. + trans_params[IDX_YSCALE] * Points->y[i] * cos(ang);
  144. Points->x[i] = x;
  145. Points->y[i] = y;
  146. /* ztozero shifts oldmap z to zero, zshift shifts rescaled object
  147. * to target elevation: */
  148. Points->z[i] =
  149. ((Points->z[i] + ztozero) * trans_params[IDX_ZSCALE]) +
  150. trans_params[IDX_ZSHIFT];
  151. if (swap_after) {
  152. if (swap_xy) {
  153. tmp = Points->x[i];
  154. Points->x[i] = Points->y[i];
  155. Points->y[i] = tmp;
  156. }
  157. if (swap_xz) {
  158. tmp = Points->z[i];
  159. Points->z[i] = Points->x[i];
  160. Points->x[i] = tmp;
  161. }
  162. if (swap_yz) {
  163. tmp = Points->z[i];
  164. Points->z[i] = Points->y[i];
  165. Points->y[i] = tmp;
  166. }
  167. }
  168. }
  169. Vect_write_line(New, type, Points, Cats);
  170. line++;
  171. }
  172. if (verbose && format != G_INFO_FORMAT_PLAIN)
  173. fprintf(stderr, "\r");
  174. if (field > 0) {
  175. db_close_database_shutdown_driver(driver);
  176. G_free((void *)trans_params);
  177. }
  178. return ret;
  179. }