altoslib: Do data analysis on raw values rather than AltosState
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altoslib_11;
20
21 import java.io.*;
22 import java.util.*;
23
24 class KMLWriter extends PrintWriter {
25         public PrintWriter printf(String format, Object ... arguments) {
26                 return printf(Locale.ROOT, format, arguments);
27         }
28
29         public KMLWriter(File name) throws FileNotFoundException {
30                 super(name);
31         }
32 }
33
34 public class AltosKML implements AltosWriter {
35
36         File                    name;
37         PrintWriter             out;
38         int                     flight_state = -1;
39         AltosGPS                prev = null;
40         double                  gps_start_altitude;
41
42         static final String[] kml_state_colors = {
43                 "FF000000",     // startup
44                 "FF000000",     // idle
45                 "FF000000",     // pad
46                 "FF0000FF",     // boost
47                 "FF4080FF",     // fast
48                 "FF00FFFF",     // coast
49                 "FFFF0000",     // drogue
50                 "FF00FF00",     // main
51                 "FF000000",     // landed
52                 "FFFFFFFF",     // invalid
53                 "FFFF0000",     // stateless
54         };
55
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];
60         }
61
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" +
65                 "<Document>\n" +
66                 "  <name>AO Flight#%d S/N: %03d</name>\n" +
67                 "  <description>\n";
68         static final String kml_header_end =
69                 "  </description>\n" +
70                 "  <open>0</open>\n";
71
72         static final String kml_style_start =
73                 "  <Style id=\"ao-flightstate-%s\">\n" +
74                 "    <LineStyle><color>%s</color><width>4</width></LineStyle>\n" +
75                 "    <BalloonStyle>\n" +
76                 "      <text>\n";
77
78         static final String kml_style_end =
79                 "      </text>\n" +
80                 "    </BalloonStyle>\n" +
81                 "  </Style>\n";
82
83         static final String kml_placemark_start =
84                 "  <Placemark>\n" +
85                 "    <name>%s</name>\n" +
86                 "    <styleUrl>#ao-flightstate-%s</styleUrl>\n" +
87                 "    <LineString>\n" +
88                 "      <tessellate>1</tessellate>\n" +
89                 "      <altitudeMode>absolute</altitudeMode>\n" +
90                 "      <coordinates>\n";
91
92         static final String kml_coord_fmt =
93         "        %.7f,%.7f,%.7f <!-- alt %12.7f time %12.7f sats %d -->\n";
94
95         static final String kml_placemark_end =
96                 "      </coordinates>\n" +
97                 "    </LineString>\n" +
98                 "  </Placemark>\n";
99
100         static final String kml_footer =
101                 "</Document>\n" +
102                 "</kml>\n";
103
104         void start (int state) {
105 /*
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);
112 */
113         }
114
115         boolean started = false;
116
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);
124         }
125
126         void state_end() {
127                 out.printf("%s", kml_placemark_end);
128         }
129
130         void coord(double time, AltosGPS gps, int state, double height) {
131                 double          altitude;
132
133                 if (height != AltosLib.MISSING)
134                         altitude = height + gps_start_altitude;
135                 else
136                         altitude = gps.alt;
137                 out.printf(kml_coord_fmt,
138                            gps.lon, gps.lat,
139                            altitude, (double) gps.alt,
140                            time, gps.nsat);
141         }
142
143         void end() {
144                 out.printf("%s", kml_footer);
145         }
146
147         public void close() {
148                 if (prev != null) {
149                         state_end();
150                         end();
151                         prev = null;
152                 }
153                 if (out != null) {
154                         out.close();
155                         out = null;
156                 }
157         }
158
159         public void write(AltosGPS gps, int state, double height) {
160                 if (gps.lat == AltosLib.MISSING)
161                         return;
162                 if (gps.lon == AltosLib.MISSING)
163                         return;
164                 if (!started) {
165                         start(state);
166                         started = true;
167                         gps_start_altitude = gps.alt;
168                 }
169                 if (state != flight_state) {
170                         flight_state = state;
171                         if (prev != null) {
172 //                              coord(gps, state, height);
173                                 state_end();
174                         }
175                         state_start(state);
176                 }
177 //              coord(0, gps, state, height);
178                 prev = gps;
179         }
180
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)
185                                 break;
186                         s = (int) state.value;
187                 }
188                 return s;
189         }
190
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)
195                                 break;
196                         h = height.value;
197                 }
198                 return h;
199         }
200
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));
204                 }
205         }
206
207         public AltosKML(File in_name) throws FileNotFoundException {
208                 name = in_name;
209                 out = new KMLWriter(name);
210         }
211
212         public AltosKML(String in_string) throws FileNotFoundException {
213                 this(new File(in_string));
214         }
215 }