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.
21 import org.altusmetrum.altoslib_1.*;
23 public class AltosKML implements AltosWriter {
28 AltosRecord prev = null;
29 double gps_start_altitude;
31 static final String[] kml_state_colors = {
44 static final String kml_header_start =
45 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
46 "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" +
48 " <name>AO Flight#%d S/N: %03d</name>\n" +
50 static final String kml_header_end =
54 static final String kml_style_start =
55 " <Style id=\"ao-flightstate-%s\">\n" +
56 " <LineStyle><color>%s</color><width>4</width></LineStyle>\n" +
60 static final String kml_style_end =
62 " </BalloonStyle>\n" +
65 static final String kml_placemark_start =
67 " <name>%s</name>\n" +
68 " <styleUrl>#ao-flightstate-%s</styleUrl>\n" +
70 " <tessellate>1</tessellate>\n" +
71 " <altitudeMode>absolute</altitudeMode>\n" +
74 static final String kml_coord_fmt =
75 " %.7f,%.7f,%.7f <!-- alt %12.7f time %12.7f sats %d -->\n";
77 static final String kml_placemark_end =
82 static final String kml_footer =
86 void start (AltosRecord record) {
87 out.printf(kml_header_start, record.flight, record.serial);
88 out.printf("Date: %04d-%02d-%02d\n",
89 record.gps.year, record.gps.month, record.gps.day);
90 out.printf("Time: %2d:%02d:%02d\n",
91 record.gps.hour, record.gps.minute, record.gps.second);
92 out.printf("%s", kml_header_end);
95 boolean started = false;
97 void state_start(AltosRecord record) {
98 String state_name = Altos.state_name(record.state);
99 out.printf(kml_style_start, state_name, kml_state_colors[record.state]);
100 out.printf("\tState: %s\n", state_name);
101 out.printf("%s", kml_style_end);
102 out.printf(kml_placemark_start, state_name, state_name);
105 void state_end(AltosRecord record) {
106 out.printf("%s", kml_placemark_end);
109 void coord(AltosRecord record) {
110 AltosGPS gps = record.gps;
113 if (record.height() != AltosRecord.MISSING)
114 altitude = record.height() + gps_start_altitude;
117 out.printf(kml_coord_fmt,
119 altitude, (double) gps.alt,
120 record.time, gps.nsat);
124 out.printf("%s", kml_footer);
127 public void close() {
135 public void write(AltosRecord record) {
136 AltosGPS gps = record.gps;
141 if ((record.seen & (AltosRecord.seen_gps_lat)) == 0)
143 if ((record.seen & (AltosRecord.seen_gps_lon)) == 0)
148 gps_start_altitude = gps.alt;
150 if (prev != null && prev.gps_sequence == record.gps_sequence)
152 if (record.state != state) {
153 state = record.state;
164 public void write(AltosRecordIterable iterable) {
165 for (AltosRecord record : iterable)
169 public AltosKML(File in_name) throws FileNotFoundException {
171 out = new PrintStream(name);
174 public AltosKML(String in_string) throws FileNotFoundException {
175 this(new File(in_string));