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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.altoslib_5;
22 public class AltosKML implements AltosWriter {
26 int flight_state = -1;
27 AltosState prev = null;
28 double gps_start_altitude;
30 static final String[] kml_state_colors = {
31 "FF000000", // startup
40 "FFFFFFFF", // invalid
41 "FFFF0000", // stateless
44 static String state_color(int state) {
45 if (state < 0 || kml_state_colors.length <= state)
46 return kml_state_colors[AltosLib.ao_flight_invalid];
47 return kml_state_colors[state];
50 static final String kml_header_start =
51 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
52 "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" +
54 " <name>AO Flight#%d S/N: %03d</name>\n" +
56 static final String kml_header_end =
60 static final String kml_style_start =
61 " <Style id=\"ao-flightstate-%s\">\n" +
62 " <LineStyle><color>%s</color><width>4</width></LineStyle>\n" +
66 static final String kml_style_end =
68 " </BalloonStyle>\n" +
71 static final String kml_placemark_start =
73 " <name>%s</name>\n" +
74 " <styleUrl>#ao-flightstate-%s</styleUrl>\n" +
76 " <tessellate>1</tessellate>\n" +
77 " <altitudeMode>absolute</altitudeMode>\n" +
80 static final String kml_coord_fmt =
81 " %.7f,%.7f,%.7f <!-- alt %12.7f time %12.7f sats %d -->\n";
83 static final String kml_placemark_end =
88 static final String kml_footer =
92 void start (AltosState record) {
93 out.printf(kml_header_start, record.flight, record.serial);
94 out.printf("Date: %04d-%02d-%02d\n",
95 record.gps.year, record.gps.month, record.gps.day);
96 out.printf("Time: %2d:%02d:%02d\n",
97 record.gps.hour, record.gps.minute, record.gps.second);
98 out.printf("%s", kml_header_end);
101 boolean started = false;
103 void state_start(AltosState state) {
104 String state_name = AltosLib.state_name(state.state);
105 String state_color = state_color(state.state);
106 out.printf(kml_style_start, state_name, state_color);
107 out.printf("\tState: %s\n", state_name);
108 out.printf("%s", kml_style_end);
109 out.printf(kml_placemark_start, state_name, state_name);
112 void state_end(AltosState state) {
113 out.printf("%s", kml_placemark_end);
116 void coord(AltosState state) {
117 AltosGPS gps = state.gps;
120 if (state.height() != AltosLib.MISSING)
121 altitude = state.height() + gps_start_altitude;
124 out.printf(kml_coord_fmt,
126 altitude, (double) gps.alt,
127 state.time, gps.nsat);
131 out.printf("%s", kml_footer);
134 public void close() {
142 public void write(AltosState state) {
143 AltosGPS gps = state.gps;
148 if (gps.lat == AltosLib.MISSING)
150 if (gps.lon == AltosLib.MISSING)
155 gps_start_altitude = gps.alt;
157 if (prev != null && prev.gps_sequence == state.gps_sequence)
159 if (state.state != flight_state) {
160 flight_state = state.state;
171 public void write(AltosStateIterable states) {
172 for (AltosState state : states) {
173 if ((state.set & AltosState.set_gps) != 0)
178 public AltosKML(File in_name) throws FileNotFoundException {
180 out = new PrintStream(name);
183 public AltosKML(String in_string) throws FileNotFoundException {
184 this(new File(in_string));