altosuilib: Wait for product data while scanning
[fw/altos] / altoslib / AltosKML.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_5;
19
20 import java.io.*;
21
22 public class AltosKML implements AltosWriter {
23
24         File                    name;
25         PrintStream             out;
26         int                     flight_state = -1;
27         AltosState              prev = null;
28         double                  gps_start_altitude;
29
30         static final String[] kml_state_colors = {
31                 "FF000000",     // startup
32                 "FF000000",     // idle
33                 "FF000000",     // pad
34                 "FF0000FF",     // boost
35                 "FF4080FF",     // fast
36                 "FF00FFFF",     // coast
37                 "FFFF0000",     // drogue
38                 "FF00FF00",     // main
39                 "FF000000",     // landed
40                 "FFFFFFFF",     // invalid
41                 "FFFF0000",     // stateless
42         };
43
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];
48         }
49
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" +
53                 "<Document>\n" +
54                 "  <name>AO Flight#%d S/N: %03d</name>\n" +
55                 "  <description>\n";
56         static final String kml_header_end =
57                 "  </description>\n" +
58                 "  <open>0</open>\n";
59
60         static final String kml_style_start =
61                 "  <Style id=\"ao-flightstate-%s\">\n" +
62                 "    <LineStyle><color>%s</color><width>4</width></LineStyle>\n" +
63                 "    <BalloonStyle>\n" +
64                 "      <text>\n";
65
66         static final String kml_style_end =
67                 "      </text>\n" +
68                 "    </BalloonStyle>\n" +
69                 "  </Style>\n";
70
71         static final String kml_placemark_start =
72                 "  <Placemark>\n" +
73                 "    <name>%s</name>\n" +
74                 "    <styleUrl>#ao-flightstate-%s</styleUrl>\n" +
75                 "    <LineString>\n" +
76                 "      <tessellate>1</tessellate>\n" +
77                 "      <altitudeMode>absolute</altitudeMode>\n" +
78                 "      <coordinates>\n";
79
80         static final String kml_coord_fmt =
81         "        %.7f,%.7f,%.7f <!-- alt %12.7f time %12.7f sats %d -->\n";
82
83         static final String kml_placemark_end =
84                 "      </coordinates>\n" +
85                 "    </LineString>\n" +
86                 "  </Placemark>\n";
87
88         static final String kml_footer =
89                 "</Document>\n" +
90                 "</kml>\n";
91
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);
99         }
100
101         boolean started = false;
102
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);
110         }
111
112         void state_end(AltosState state) {
113                 out.printf("%s", kml_placemark_end);
114         }
115
116         void coord(AltosState state) {
117                 AltosGPS        gps = state.gps;
118                 double          altitude;
119
120                 if (state.height() != AltosLib.MISSING)
121                         altitude = state.height() + gps_start_altitude;
122                 else
123                         altitude = gps.alt;
124                 out.printf(kml_coord_fmt,
125                            gps.lon, gps.lat,
126                            altitude, (double) gps.alt,
127                            state.time, gps.nsat);
128         }
129
130         void end() {
131                 out.printf("%s", kml_footer);
132         }
133
134         public void close() {
135                 if (prev != null) {
136                         state_end(prev);
137                         end();
138                         prev = null;
139                 }
140         }
141
142         public void write(AltosState state) {
143                 AltosGPS        gps = state.gps;
144
145                 if (gps == null)
146                         return;
147
148                 if (gps.lat == AltosLib.MISSING)
149                         return;
150                 if (gps.lon == AltosLib.MISSING)
151                         return;
152                 if (!started) {
153                         start(state);
154                         started = true;
155                         gps_start_altitude = gps.alt;
156                 }
157                 if (prev != null && prev.gps_sequence == state.gps_sequence)
158                         return;
159                 if (state.state != flight_state) {
160                         flight_state = state.state;
161                         if (prev != null) {
162                                 coord(state);
163                                 state_end(prev);
164                         }
165                         state_start(state);
166                 }
167                 coord(state);
168                 prev = state;
169         }
170
171         public void write(AltosStateIterable states) {
172                 for (AltosState state : states) {
173                         if ((state.set & AltosState.set_gps) != 0)
174                                 write(state);
175                 }
176         }
177
178         public AltosKML(File in_name) throws FileNotFoundException {
179                 name = in_name;
180                 out = new PrintStream(name);
181         }
182
183         public AltosKML(String in_string) throws FileNotFoundException {
184                 this(new File(in_string));
185         }
186 }