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;
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 (int state) {
106 out.printf(kml_header_start, record.flight, record.serial);
107 out.printf("Date: %04d-%02d-%02d\n",
108 record.gps.year, record.gps.month, record.gps.day);
109 out.printf("Time: %2d:%02d:%02d\n",
110 record.gps.hour, record.gps.minute, record.gps.second);
111 out.printf("%s", kml_header_end);
115 boolean started = false;
117 void state_start(int state) {
118 String state_name = AltosLib.state_name(state);
119 String state_color = state_color(state);
120 out.printf(kml_style_start, state_name, state_color);
121 out.printf("\tState: %s\n", state_name);
122 out.printf("%s", kml_style_end);
123 out.printf(kml_placemark_start, state_name, state_name);
127 out.printf("%s", kml_placemark_end);
130 void coord(double time, AltosGPS gps, int state, double height) {
133 if (height != AltosLib.MISSING)
134 altitude = height + gps_start_altitude;
137 out.printf(kml_coord_fmt,
139 altitude, (double) gps.alt,
144 out.printf("%s", kml_footer);
147 public void close() {
159 public void write(AltosGPS gps, int state, double height) {
160 if (gps.lat == AltosLib.MISSING)
162 if (gps.lon == AltosLib.MISSING)
167 gps_start_altitude = gps.alt;
169 if (state != flight_state) {
170 flight_state = state;
172 // coord(gps, state, height);
177 // coord(0, gps, state, height);
181 private int state(AltosFlightSeries series, double time) {
182 int s = AltosLib.MISSING;
183 for (AltosTimeValue state : series.state_series) {
184 if (state.time > time)
186 s = (int) state.value;
191 private double height(AltosFlightSeries series, double time) {
192 double h = AltosLib.MISSING;
193 for (AltosTimeValue height : series.height_series) {
194 if (height.time > time)
201 public void write(AltosFlightSeries series) {
202 for (AltosGPSTimeValue gps : series.gps_series) {
203 write(gps.gps, state(series, gps.time), height(series, gps.time));
207 public AltosKML(File in_name) throws FileNotFoundException {
209 out = new KMLWriter(name);
212 public AltosKML(String in_string) throws FileNotFoundException {
213 this(new File(in_string));