4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "Waypoint/WaypointReader.hpp"
25 #include "Waypoint/Waypoints.hpp"
26 #include "Engine/Waypoint/WaypointVisitor.hpp"
27 #include "OS/ConvertPathName.hpp"
28 #include "OS/Args.hpp"
29 #include "Operation/Operation.hpp"
36 LoadWaypoints(const char *_path
, Waypoints
&waypoints
)
39 WaypointReader
parser(path
, 0);
41 fprintf(stderr
, "WayPointParser::SetFile() has failed\n");
45 NullOperationEnvironment operation
;
46 if (!parser
.Parse(waypoints
, operation
)) {
47 fprintf(stderr
, "WayPointParser::Parse() has failed\n");
56 ParseGeopoint(const char *line
, GeoPoint
&location
)
61 value
= strtod(line
, &endptr
);
65 location
.latitude
= Angle::Degrees(value
);
68 value
= strtod(line
, &endptr
);
72 location
.longitude
= Angle::Degrees(value
);
77 enum class WaypointType
: uint8_t {
84 AlwaysTrue(const Waypoint
&waypoint
)
90 IsLandable(const Waypoint
&waypoint
)
92 return waypoint
.IsLandable();
96 IsAirport(const Waypoint
&waypoint
)
98 return waypoint
.IsAirport();
101 static const Waypoint
*
102 GetNearestWaypoint(const GeoPoint
&location
, const Waypoints
&waypoints
,
103 fixed range
, WaypointType type
)
105 bool (*predicate
)(const Waypoint
&);
107 case WaypointType::AIRPORT
:
108 predicate
= IsAirport
;
110 case WaypointType::LANDABLE
:
111 predicate
= IsLandable
;
114 predicate
= AlwaysTrue
;
118 return waypoints
.GetNearestIf(location
, range
, predicate
);
122 PrintWaypoint(const Waypoint
*waypoint
)
127 _ftprintf(stdout
, _T("%f %f %.0f %s\n"),
128 (double)waypoint
->location
.latitude
.Degrees(),
129 (double)waypoint
->location
.longitude
.Degrees(),
130 (double)waypoint
->elevation
,
131 waypoint
->name
.c_str());
134 int main(int argc
, char **argv
)
136 WaypointType type
= WaypointType::ALL
;
137 fixed range
= fixed(100000);
139 Args
args(argc
, argv
,
140 "PATH\n\nPATH is expected to be any compatible waypoint file.\n"
141 "Stdin expects a list of coordinates at floating point values\n"
142 "in the format: LAT LON\n\ne.g.\n"
143 "-23.49858 123.45838\n"
145 "65.18234 -173.48307\n\n"
146 "Output is in the format: LAT LON ELEV (in m) NAME\n\ne.g.\n"
147 "50.823055 6.186384 189 Aachen Merzbruc");
150 while ((arg
= args
.PeekNext()) != NULL
&& *arg
== '-') {
154 if ((value
= StringAfterPrefix(arg
, "--range=")) != NULL
) {
155 double _range
= strtod(value
, NULL
);
157 range
= fixed(_range
);
158 } else if (StringStartsWith(arg
, "--airports-only")) {
159 type
= WaypointType::AIRPORT
;
160 } else if (StringStartsWith(arg
, "--landables-only")) {
161 type
= WaypointType::LANDABLE
;
167 const char *path
= args
.ExpectNext();
171 if (!LoadWaypoints(path
, waypoints
))
176 while ((line
= fgets(buffer
, sizeof(buffer
) - 3, stdin
)) != NULL
) {
178 if (!ParseGeopoint(line
, location
))
181 const Waypoint
*waypoint
= GetNearestWaypoint(location
, waypoints
,
183 PrintWaypoint(waypoint
);