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.
24 import org.altusmetrum.AltosLib.*;
26 public class AltosKML implements AltosWriter {
31 AltosRecord prev = null;
33 static final String[] kml_state_colors = {
46 static final String kml_header_start =
47 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
48 "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" +
50 " <name>AO Flight#%d S/N: %03d</name>\n" +
52 static final String kml_header_end =
56 static final String kml_style_start =
57 " <Style id=\"ao-flightstate-%s\">\n" +
58 " <LineStyle><color>%s</color><width>4</width></LineStyle>\n" +
62 static final String kml_style_end =
64 " </BalloonStyle>\n" +
67 static final String kml_placemark_start =
69 " <name>%s</name>\n" +
70 " <styleUrl>#ao-flightstate-%s</styleUrl>\n" +
72 " <tessellate>1</tessellate>\n" +
73 " <altitudeMode>absolute</altitudeMode>\n" +
76 static final String kml_coord_fmt =
77 " %.7f,%.7f,%.7f <!-- alt %12.7f time %12.7f sats %d -->\n";
79 static final String kml_placemark_end =
84 static final String kml_footer =
88 void start (AltosRecord record) {
89 out.printf(kml_header_start, record.flight, record.serial);
90 out.printf("Date: %04d-%02d-%02d\n",
91 record.gps.year, record.gps.month, record.gps.day);
92 out.printf("Time: %2d:%02d:%02d\n",
93 record.gps.hour, record.gps.minute, record.gps.second);
94 out.printf("%s", kml_header_end);
97 boolean started = false;
99 void state_start(AltosRecord record) {
100 String state_name = Altos.state_name(record.state);
101 out.printf(kml_style_start, state_name, kml_state_colors[record.state]);
102 out.printf("\tState: %s\n", state_name);
103 out.printf("%s", kml_style_end);
104 out.printf(kml_placemark_start, state_name, state_name);
107 void state_end(AltosRecord record) {
108 out.printf("%s", kml_placemark_end);
111 void coord(AltosRecord record) {
112 AltosGPS gps = record.gps;
113 out.printf(kml_coord_fmt,
115 record.filtered_altitude(), (double) gps.alt,
116 record.time, gps.nsat);
120 out.printf("%s", kml_footer);
123 public void close() {
131 public void write(AltosRecord record) {
132 AltosGPS gps = record.gps;
136 if ((record.seen & (AltosRecord.seen_flight)) == 0)
138 if ((record.seen & (AltosRecord.seen_gps_lat)) == 0)
140 if ((record.seen & (AltosRecord.seen_gps_lon)) == 0)
147 prev.gps.second == record.gps.second &&
148 prev.gps.minute == record.gps.minute &&
149 prev.gps.hour == record.gps.hour)
151 if (record.state != state) {
152 state = record.state;
163 public void write(AltosRecordIterable iterable) {
164 for (AltosRecord record : iterable)
168 public AltosKML(File in_name) throws FileNotFoundException {
170 out = new PrintStream(name);
173 public AltosKML(String in_string) throws FileNotFoundException {
174 this(new File(in_string));