You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

transform.cc 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. Transformation filter for GPS data.
  3. Copyright (C) 2006 Olaf Klein, o.b.klein@gpsbabel.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #include "defs.h"
  17. #include "filterdefs.h"
  18. #include <stdlib.h>
  19. #if FILTERS_ENABLED
  20. #include <ctype.h>
  21. #define MYNAME "transform"
  22. static char current_target;
  23. static route_head* current_trk;
  24. static route_head* current_rte;
  25. static char* opt_routes, *opt_tracks, *opt_waypts, *opt_delete, *rpt_name_digits, *opt_rpt_name;
  26. static QString current_namepart;
  27. static int name_digits, use_src_name;
  28. static char RPT[] = "RPT";
  29. static
  30. arglist_t transform_args[] = {
  31. {
  32. "wpt", &opt_waypts, "Transform track(s) or route(s) into waypoint(s) [R/T]", NULL,
  33. ARGTYPE_STRING, ARG_NOMINMAX
  34. },
  35. {
  36. "rte", &opt_routes, "Transform waypoint(s) or track(s) into route(s) [W/T]", NULL,
  37. ARGTYPE_STRING, ARG_NOMINMAX
  38. },
  39. {
  40. "trk", &opt_tracks, "Transform waypoint(s) or route(s) into tracks(s) [W/R]", NULL,
  41. ARGTYPE_STRING, ARG_NOMINMAX
  42. },
  43. {
  44. "rptdigits", &rpt_name_digits, "Number of digits in generated names", NULL,
  45. ARGTYPE_INT, "2", NULL
  46. },
  47. {
  48. "rptname", &opt_rpt_name, "Use source name for route point names", "N",
  49. ARGTYPE_BOOL, ARG_NOMINMAX
  50. },
  51. {
  52. "del", &opt_delete, "Delete source data after transformation", "N",
  53. ARGTYPE_BOOL, ARG_NOMINMAX
  54. },
  55. ARG_TERMINATOR
  56. };
  57. static void
  58. transform_waypoints(void)
  59. {
  60. route_head* rte;
  61. rte = route_head_alloc();
  62. switch (current_target) {
  63. case 'R':
  64. route_add_head(rte);
  65. break;
  66. case 'T':
  67. track_add_head(rte);
  68. break;
  69. }
  70. #if NEWQ
  71. foreach(Waypoint* wpt, waypt_list) {
  72. #else
  73. queue* elem, *tmp;
  74. QUEUE_FOR_EACH(&waypt_head, elem, tmp) {
  75. Waypoint* wpt = (Waypoint*) elem;
  76. #endif
  77. wpt = new Waypoint(*wpt);
  78. switch (current_target) {
  79. case 'R':
  80. route_add_wpt_named(rte, wpt, RPT, name_digits);
  81. break;
  82. case 'T':
  83. track_add_wpt_named(rte, wpt, RPT, name_digits);
  84. break;
  85. }
  86. }
  87. }
  88. static void
  89. transform_rte_disp_hdr_cb(const route_head* rte)
  90. {
  91. current_namepart = RPT;
  92. if (!rte->rte_name.isEmpty() && use_src_name) {
  93. current_namepart = rte->rte_name;
  94. }
  95. if (current_target == 'T') {
  96. current_trk = route_head_alloc();
  97. track_add_head(current_trk);
  98. if (!rte->rte_name.isEmpty()) {
  99. current_trk->rte_desc = QString("Generated from route %1").arg(rte->rte_name);
  100. current_trk->rte_name = rte->rte_name; /* name the new trk */
  101. }
  102. }
  103. }
  104. static void
  105. transform_trk_disp_hdr_cb(const route_head* trk)
  106. {
  107. current_namepart = RPT;
  108. if (!trk->rte_name.isEmpty() && use_src_name) {
  109. current_namepart = trk->rte_name;
  110. }
  111. if (current_target == 'R') {
  112. current_rte = route_head_alloc();
  113. route_add_head(current_rte);
  114. if (!trk->rte_name.isEmpty()) {
  115. current_rte->rte_desc = "Generated from track ";
  116. current_rte->rte_desc += trk->rte_name;
  117. current_rte->rte_name = trk->rte_name; /* name the new rte */
  118. }
  119. }
  120. }
  121. static void
  122. transform_any_disp_wpt_cb(const Waypoint* wpt)
  123. {
  124. Waypoint* temp = new Waypoint(*wpt);
  125. if (current_target == 'R') {
  126. route_add_wpt_named(current_rte, temp, current_namepart, name_digits);
  127. } else if (current_target == 'T') {
  128. track_add_wpt_named(current_trk, temp, current_namepart, name_digits);
  129. } else {
  130. waypt_add(temp);
  131. }
  132. }
  133. static void
  134. transform_routes(void)
  135. {
  136. route_disp_all(transform_rte_disp_hdr_cb, NULL, transform_any_disp_wpt_cb);
  137. }
  138. static void
  139. transform_tracks(void)
  140. {
  141. track_disp_all(transform_trk_disp_hdr_cb, NULL, transform_any_disp_wpt_cb);
  142. }
  143. /*******************************************************************************
  144. * %%% global callbacks called by gpsbabel main process %%% *
  145. *******************************************************************************/
  146. static void
  147. transform_init(const char* args)
  148. {
  149. }
  150. static void
  151. transform_deinit(void)
  152. {
  153. }
  154. static void
  155. transform_process(void)
  156. {
  157. int delete_after = (opt_delete && (*opt_delete == '1')) ? 1 : 0;
  158. use_src_name = (opt_rpt_name && (*opt_rpt_name == '1')) ? 1 : 0;
  159. name_digits = 3;
  160. if (rpt_name_digits && *rpt_name_digits) {
  161. name_digits = atoi(rpt_name_digits);
  162. }
  163. if (opt_waypts != NULL) {
  164. current_target = 'W';
  165. switch (toupper(*opt_waypts)) {
  166. case 'R':
  167. transform_routes();
  168. if (delete_after) {
  169. route_flush_all_routes();
  170. }
  171. break;
  172. case 'T':
  173. transform_tracks();
  174. if (delete_after) {
  175. route_flush_all_tracks();
  176. }
  177. break;
  178. default:
  179. fatal(MYNAME ": Invalid option value (%s)!\n", opt_waypts);
  180. }
  181. }
  182. if (opt_routes != NULL) {
  183. current_target = 'R';
  184. switch (toupper(*opt_routes)) {
  185. case 'W':
  186. transform_waypoints();
  187. if (delete_after) {
  188. waypt_flush_all();
  189. }
  190. break;
  191. case 'T':
  192. transform_tracks();
  193. if (delete_after) {
  194. route_flush_all_tracks();
  195. }
  196. break;
  197. default:
  198. fatal(MYNAME ": Invalid option value (%s)!\n", opt_routes);
  199. }
  200. }
  201. if (opt_tracks != NULL) {
  202. current_target = 'T';
  203. switch (toupper(*opt_tracks)) {
  204. case 'W':
  205. transform_waypoints();
  206. if (delete_after) {
  207. waypt_flush_all();
  208. }
  209. break;
  210. case 'R':
  211. transform_routes();
  212. if (delete_after) {
  213. route_flush_all_routes();
  214. }
  215. break;
  216. default:
  217. fatal(MYNAME ": Invalid option value (%s)!\n", opt_tracks);
  218. }
  219. }
  220. }
  221. /*******************************************************************************/
  222. filter_vecs_t transform_vecs = {
  223. transform_init,
  224. transform_process,
  225. transform_deinit,
  226. NULL,
  227. transform_args
  228. };
  229. /*******************************************************************************/
  230. #endif // FILTERS_ENABLED