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.

position.cc 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. Distance Between Points Filter(s)
  3. Copyright (C) 2002 Robert Lipe, robertlipe+source@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 "grtcirc.h"
  19. #include <math.h>
  20. #include <stdlib.h>
  21. #if FILTERS_ENABLED
  22. #ifndef M_PI
  23. # define M_PI 3.14159265358979323846
  24. #endif
  25. static route_head* cur_rte = NULL;
  26. static double pos_dist;
  27. static double max_diff_time;
  28. static char* distopt = NULL;
  29. static char* timeopt = NULL;
  30. static char* purge_duplicates = NULL;
  31. static int check_time;
  32. typedef struct {
  33. double distance;
  34. } extra_data;
  35. static
  36. arglist_t position_args[] = {
  37. {
  38. "distance", &distopt, "Maximum positional distance",
  39. NULL, ARGTYPE_FLOAT | ARGTYPE_REQUIRED, ARG_NOMINMAX
  40. },
  41. {
  42. "all", &purge_duplicates,
  43. "Suppress all points close to other points",
  44. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  45. },
  46. {
  47. "time", &timeopt, "Maximum time in seconds beetween two points",
  48. NULL, ARGTYPE_FLOAT | ARGTYPE_REQUIRED, ARG_NOMINMAX
  49. },
  50. ARG_TERMINATOR
  51. };
  52. static double
  53. gc_distance(double lat1, double lon1, double lat2, double lon2)
  54. {
  55. return gcdist(
  56. RAD(lat1),
  57. RAD(lon1),
  58. RAD(lat2),
  59. RAD(lon2)
  60. );
  61. }
  62. /* tear through a waypoint queue, processing points by distance */
  63. static void
  64. position_runqueue(queue* q, int nelems, int qtype)
  65. {
  66. queue* elem, * tmp;
  67. Waypoint** comp;
  68. int* qlist;
  69. double dist, diff_time;
  70. int i = 0, j, anyitem;
  71. comp = (Waypoint**) xcalloc(nelems, sizeof(*comp));
  72. qlist = (int*) xcalloc(nelems, sizeof(*qlist));
  73. #if NEWQ
  74. foreach(Waypoint* waypointp, waypt_list) {
  75. comp[i] = waypointp;
  76. #else
  77. QUEUE_FOR_EACH(q, elem, tmp) {
  78. comp[i] = (Waypoint*)elem;
  79. #endif
  80. qlist[i] = 0;
  81. i++;
  82. }
  83. for (i = 0 ; i < nelems ; i++) {
  84. anyitem = 0;
  85. if (!qlist[i]) {
  86. for (j = i + 1 ; j < nelems ; j++) {
  87. if (!qlist[j]) {
  88. dist = gc_distance(comp[j]->latitude,
  89. comp[j]->longitude,
  90. comp[i]->latitude,
  91. comp[i]->longitude);
  92. /* convert radians to integer feet */
  93. dist = (int)(5280*radtomiles(dist));
  94. diff_time = fabs(waypt_time(comp[i]) - waypt_time(comp[j]));
  95. if (dist <= pos_dist) {
  96. if (check_time && diff_time >= max_diff_time) {
  97. continue;
  98. }
  99. qlist[j] = 1;
  100. switch (qtype) {
  101. case wptdata:
  102. waypt_del(comp[j]);
  103. delete comp[j];
  104. break;
  105. case trkdata:
  106. track_del_wpt(cur_rte, comp[j]);
  107. break;
  108. case rtedata:
  109. route_del_wpt(cur_rte, comp[j]);
  110. break;
  111. default:
  112. break;
  113. }
  114. anyitem = 1;
  115. }
  116. }
  117. }
  118. if (anyitem && !!purge_duplicates) {
  119. switch (qtype) {
  120. case wptdata:
  121. waypt_del(comp[i]);
  122. break;
  123. case trkdata:
  124. track_del_wpt(cur_rte, comp[i]);
  125. break;
  126. case rtedata:
  127. route_del_wpt(cur_rte, comp[i]);
  128. break;
  129. default:
  130. break;
  131. }
  132. delete comp[i];
  133. }
  134. }
  135. }
  136. if (comp) {
  137. xfree(comp);
  138. }
  139. if (qlist) {
  140. xfree(qlist);
  141. }
  142. }
  143. static void
  144. position_process_route(const route_head* rh)
  145. {
  146. int i = rh->rte_waypt_ct;
  147. if (i) {
  148. cur_rte = (route_head*)rh;
  149. position_runqueue((queue*)&rh->waypoint_list, i, rtedata);
  150. cur_rte = NULL;
  151. }
  152. }
  153. static void
  154. position_noop_w(const Waypoint* w)
  155. {
  156. }
  157. static void
  158. position_noop_t(const route_head* h)
  159. {
  160. }
  161. void position_process(void)
  162. {
  163. int i = waypt_count();
  164. if (i) {
  165. position_runqueue(&waypt_head, i, wptdata);
  166. }
  167. route_disp_all(position_process_route, position_noop_t, position_noop_w);
  168. track_disp_all(position_process_route, position_noop_t, position_noop_w);
  169. }
  170. void
  171. position_init(const char* args)
  172. {
  173. char* fm;
  174. pos_dist = 0;
  175. max_diff_time = 0;
  176. check_time = 0;
  177. if (distopt) {
  178. pos_dist = strtod(distopt, &fm);
  179. if ((*fm == 'm') || (*fm == 'M')) {
  180. /* distance is meters */
  181. pos_dist *= 3.2802;
  182. }
  183. }
  184. if (timeopt) {
  185. check_time = 1;
  186. max_diff_time = strtod(timeopt, &fm);
  187. }
  188. }
  189. void
  190. position_deinit(void)
  191. {
  192. }
  193. filter_vecs_t position_vecs = {
  194. position_init,
  195. position_process,
  196. position_deinit,
  197. NULL,
  198. position_args
  199. };
  200. #endif // FILTERS_ENABLED