1 /*
2     Discard points based on high Degree of Precision (DOP) values.
3 
4     Copyright (C) 2005-2014 Robert Lipe, robertlipe+source@gpsbabel.org
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 
20  */
21 
22 #include "defs.h"
23 #include "discard.h"
24 #include <cstdlib>
25 // Can't use QRegularExpression because Linux won't get Qt 5 for years.
26 #include <QtCore/QRegExp>
27 #include <cstdio>
28 #include <cstdlib>
29 
30 #if FILTERS_ENABLED
31 
32 /*
33  * Decide whether to keep or toss this point.
34  */
fix_process_wpt(const Waypoint * wpt)35 void DiscardFilter::fix_process_wpt(const Waypoint* wpt)
36 {
37   int del = 0;
38   int delh = 0;
39   int delv = 0;
40 
41   auto* waypointp = const_cast<Waypoint*>(wpt);
42 
43   if ((hdopf >= 0.0) && (waypointp->hdop > hdopf)) {
44     delh = 1;
45   }
46   if ((vdopf >= 0.0) && (waypointp->vdop > vdopf)) {
47     delv = 1;
48   }
49 
50   if (andopt) {
51     del = delh && delv;
52   } else {
53     del = delh || delv;
54   }
55 
56   if ((satpf >= 0) && (waypointp->sat < satpf)) {
57     del = 1;
58   }
59 
60   if ((fixnoneopt) && (waypointp->fix == fix_none)) {
61     del = 1;
62   }
63 
64   if ((fixunknownopt) && (waypointp->fix == fix_unknown)) {
65     del = 1;
66   }
67 
68   if ((eleminopt) && (waypointp->altitude < eleminpf)) {
69     del = 1;
70   }
71 
72   if ((elemaxopt) && (waypointp->altitude > elemaxpf)) {
73     del = 1;
74   }
75 
76   if (nameopt && name_regex.indexIn(waypointp->shortname) >= 0) {
77     del = 1;
78   }
79   if (descopt && desc_regex.indexIn(waypointp->description) >= 0) {
80     del = 1;
81   }
82   if (cmtopt && cmt_regex.indexIn(waypointp->notes) >= 0) {
83     del = 1;
84   }
85   if (iconopt && icon_regex.indexIn(waypointp->icon_descr) >= 0) {
86     del = 1;
87   }
88 
89   if (del) {
90     switch (what) {
91     case wptdata:
92       waypt_del(waypointp);
93       delete waypointp;
94       break;
95     case trkdata:
96       track_del_wpt(head, waypointp);
97       delete waypointp;
98       break;
99     case rtedata:
100       route_del_wpt(head, waypointp);
101       delete waypointp;
102       break;
103     default:
104       return;
105     }
106   }
107 }
108 
fix_process_head(const route_head * trk)109 void DiscardFilter::fix_process_head(const route_head* trk)
110 {
111   head = const_cast<route_head*>(trk);
112 }
113 
process()114 void DiscardFilter::process()
115 {
116   WayptFunctor<DiscardFilter> fix_process_wpt_f(this, &DiscardFilter::fix_process_wpt);
117   RteHdFunctor<DiscardFilter> fix_process_head_f(this, &DiscardFilter::fix_process_head);
118 
119   // Filter waypoints.
120   what = wptdata;
121   waypt_disp_all(fix_process_wpt_f);
122 
123   // Filter tracks
124   what = trkdata;
125   track_disp_all(fix_process_head_f, nullptr, fix_process_wpt_f);
126 
127   // And routes
128   what = rtedata;
129   route_disp_all(fix_process_head_f, nullptr, fix_process_wpt_f);
130 
131 }
132 
init()133 void DiscardFilter::init()
134 {
135   if (hdopopt) {
136     hdopf = atof(hdopopt);
137   } else {
138     hdopf = -1.0;
139   }
140 
141   if (vdopopt) {
142     vdopf = atof(vdopopt);
143   } else {
144     vdopf = -1.0;
145   }
146 
147   if (satopt) {
148     satpf = atoi(satopt);
149   } else {
150     satpf = -1;
151   }
152 
153   if (eleminopt) {
154     eleminpf = atoi(eleminopt);
155   }
156 
157   if (elemaxopt) {
158     elemaxpf = atoi(elemaxopt);
159   }
160 
161   if (nameopt) {
162     name_regex.setCaseSensitivity(Qt::CaseInsensitive);
163     name_regex.setPatternSyntax(QRegExp::WildcardUnix);
164     name_regex.setPattern(nameopt);
165   }
166   if (descopt) {
167     desc_regex.setCaseSensitivity(Qt::CaseInsensitive);
168     desc_regex.setPatternSyntax(QRegExp::WildcardUnix);
169     desc_regex.setPattern(descopt);
170   }
171   if (cmtopt) {
172     cmt_regex.setCaseSensitivity(Qt::CaseInsensitive);
173     cmt_regex.setPatternSyntax(QRegExp::WildcardUnix);
174     cmt_regex.setPattern(cmtopt);
175   }
176   if (iconopt) {
177     icon_regex.setCaseSensitivity(Qt::CaseInsensitive);
178     icon_regex.setPatternSyntax(QRegExp::WildcardUnix);
179     icon_regex.setPattern(iconopt);
180   }
181 }
182 
183 #endif
184