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;
39 AltosState prev = null;
40 double gps_start_altitude;
42 static final String[] kml_state_colors = {
43 "FF000000", // startup
52 "FFFFFFFF", // invalid
53 "FFFF0000", // stateless
56 static String state_color(int state) {
57 if (state < 0 || kml_state_colors.length <= state)
58 return kml_state_colors[AltosLib.ao_flight_invalid];
59 return kml_state_colors[state];
62 static final String kml_header_start =
63 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
64 "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" +
66 " <name>AO Flight#%d S/N: %03d</name>\n" +
68 static final String kml_header_end =
72 static final String kml_style_start =
73 " <Style id=\"ao-flightstate-%s\">\n" +
74 " <LineStyle><color>%s</color><width>4</width></LineStyle>\n" +
78 static final String kml_style_end =
80 " </BalloonStyle>\n" +
83 static final String kml_placemark_start =
85 " <name>%s</name>\n" +
86 " <styleUrl>#ao-flightstate-%s</styleUrl>\n" +
88 " <tessellate>1</tessellate>\n" +
89 " <altitudeMode>absolute</altitudeMode>\n" +
92 static final String kml_coord_fmt =
93 " %.7f,%.7f,%.7f <!-- alt %12.7f time %12.7f sats %d -->\n";
95 static final String kml_placemark_end =
100 static final String kml_footer =
104 void start (AltosState record) {
105 out.printf(kml_header_start, record.flight, record.serial);
106 out.printf("Date: %04d-%02d-%02d\n",
107 record.gps.year, record.gps.month, record.gps.day);
108 out.printf("Time: %2d:%02d:%02d\n",
109 record.gps.hour, record.gps.minute, record.gps.second);
110 out.printf("%s", kml_header_end);
113 boolean started = false;
115 void state_start(AltosState state) {
116 String state_name = AltosLib.state_name(state.state());
117 String state_color = state_color(state.state());
118 out.printf(kml_style_start, state_name, state_color);
119 out.printf("\tState: %s\n", state_name);
120 out.printf("%s", kml_style_end);
121 out.printf(kml_placemark_start, state_name, state_name);
124 void state_end(AltosState state) {
125 out.printf("%s", kml_placemark_end);
128 void coord(AltosState state) {
129 AltosGPS gps = state.gps;
132 if (state.height() != AltosLib.MISSING)
133 altitude = state.height() + gps_start_altitude;
136 out.printf(kml_coord_fmt,
138 altitude, (double) gps.alt,
139 state.time, gps.nsat);
143 out.printf("%s", kml_footer);
146 public void close() {
158 public void write(AltosState state) {
159 AltosGPS gps = state.gps;
164 if (gps.lat == AltosLib.MISSING)
166 if (gps.lon == AltosLib.MISSING)
171 gps_start_altitude = gps.alt;
173 if (prev != null && prev.gps_sequence == state.gps_sequence)
175 if (state.state() != flight_state) {
176 flight_state = state.state();
187 public void write(AltosStateIterable states) {
188 for (AltosState state : states) {
189 if ((state.set & AltosState.set_gps) != 0)
194 public AltosKML(File in_name) throws FileNotFoundException {
196 out = new KMLWriter(name);
199 public AltosKML(String in_string) throws FileNotFoundException {
200 this(new File(in_string));