2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_11;
24 class KMLWriter extends PrintWriter {
25 public PrintWriter printf(String format, Object ... arguments) {
26 return printf(Locale.ROOT, format, arguments);
29 public KMLWriter(File name) throws FileNotFoundException {
34 public class AltosKML implements AltosWriter {
38 int flight_state = -1;
40 double gps_start_altitude = AltosLib.MISSING;
41 AltosFlightStats stats;
43 static final String[] kml_state_colors = {
44 "FF000000", // startup
53 "FFFFFFFF", // invalid
54 "FFFF0000", // stateless
57 static String state_color(int state) {
58 if (state < 0 || kml_state_colors.length <= state)
59 return kml_state_colors[AltosLib.ao_flight_invalid];
60 return kml_state_colors[state];
63 static final String kml_header_start =
64 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
65 "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" +
67 " <name>AO Flight#%d S/N: %03d</name>\n" +
69 static final String kml_header_end =
73 static final String kml_style_start =
74 " <Style id=\"ao-flightstate-%s\">\n" +
75 " <LineStyle><color>%s</color><width>4</width></LineStyle>\n" +
79 static final String kml_style_end =
81 " </BalloonStyle>\n" +
84 static final String kml_placemark_start =
86 " <name>%s</name>\n" +
87 " <styleUrl>#ao-flightstate-%s</styleUrl>\n" +
89 " <tessellate>1</tessellate>\n" +
90 " <altitudeMode>absolute</altitudeMode>\n" +
93 static final String kml_coord_fmt =
94 " %.7f,%.7f,%.7f <!-- alt %12.7f time %12.7f sats %d -->\n";
96 static final String kml_placemark_end =
101 static final String kml_footer =
105 void start (AltosCalData cal_data) {
106 AltosGPS gps = cal_data.gps_pad;
108 gps_start_altitude = cal_data.gps_pad_altitude;
109 out.printf(kml_header_start, cal_data.flight, cal_data.serial);
110 out.printf("Date: %04d-%02d-%02d\n",
111 gps.year, gps.month, gps.day);
112 out.printf("Time: %2d:%02d:%02d\n",
113 gps.hour, gps.minute, gps.second);
114 out.printf("%s", kml_header_end);
117 boolean started = false;
119 void state_start(int state) {
120 String state_name = AltosLib.state_name(state);
121 String state_color = state_color(state);
122 out.printf(kml_style_start, state_name, state_color);
123 out.printf("State: %s\n", state_name);
124 out.printf("Time: %6.2f s\n", stats.state_end[state] - stats.state_start[state]);
125 out.printf("Average speed: %s\n", AltosConvert.speed.show(6, stats.state_speed[state]));
126 out.printf("Average accel: %s\n", AltosConvert.accel.show(6, stats.state_accel[state]));
127 out.printf("%s", kml_style_end);
128 out.printf(kml_placemark_start, state_name, state_name);
132 out.printf("%s", kml_placemark_end);
135 void coord(double time, AltosGPS gps, int state, double height) {
138 if (height != AltosLib.MISSING)
139 altitude = height + gps_start_altitude;
142 out.printf(kml_coord_fmt,
144 altitude, (double) gps.alt,
149 out.printf("%s", kml_footer);
152 public void close() {
164 public void write(AltosGPSTimeValue gtv, AltosCalData cal_data, int state, double height) {
165 AltosGPS gps = gtv.gps;
166 if (gps.lat == AltosLib.MISSING)
168 if (gps.lon == AltosLib.MISSING)
170 if (state != flight_state) {
171 flight_state = state;
173 coord(gtv.time, gps, state, height);
178 coord(0, gps, state, height);
182 private int state(AltosFlightSeries series, double time) {
183 return (int) series.value_before(AltosFlightSeries.state_name, time);
186 private double height(AltosFlightSeries series, double time) {
187 return series.value(AltosFlightSeries.height_name, time);
190 public void write(AltosFlightSeries series) {
191 stats = new AltosFlightStats(series);
192 start(series.cal_data);
193 for (AltosGPSTimeValue gtv : series.gps_series)
194 write(gtv, series.cal_data, state(series, gtv.time), height(series, gtv.time));
197 public AltosKML(File in_name) throws FileNotFoundException {
199 out = new KMLWriter(name);
202 public AltosKML(String in_string) throws FileNotFoundException {
203 this(new File(in_string));