altoslib: Move CSV/KML output code to altoslib
[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_3;
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",
32                 "FF000000",
33                 "FF000000",
34                 "FF0000FF",
35                 "FF4080FF",
36                 "FF00FFFF",
37                 "FFFF0000",
38                 "FF00FF00",
39                 "FF000000",
40                 "FFFFFFFF"
41         };
42
43         static final String kml_header_start =
44                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
45                 "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" +
46                 "<Document>\n" +
47                 "  <name>AO Flight#%d S/N: %03d</name>\n" +
48                 "  <description>\n";
49         static final String kml_header_end =
50                 "  </description>\n" +
51                 "  <open>0</open>\n";
52
53         static final String kml_style_start =
54                 "  <Style id=\"ao-flightstate-%s\">\n" +
55                 "    <LineStyle><color>%s</color><width>4</width></LineStyle>\n" +
56                 "    <BalloonStyle>\n" +
57                 "      <text>\n";
58
59         static final String kml_style_end =
60                 "      </text>\n" +
61                 "    </BalloonStyle>\n" +
62                 "  </Style>\n";
63
64         static final String kml_placemark_start =
65                 "  <Placemark>\n" +
66                 "    <name>%s</name>\n" +
67                 "    <styleUrl>#ao-flightstate-%s</styleUrl>\n" +
68                 "    <LineString>\n" +
69                 "      <tessellate>1</tessellate>\n" +
70                 "      <altitudeMode>absolute</altitudeMode>\n" +
71                 "      <coordinates>\n";
72
73         static final String kml_coord_fmt =
74         "        %.7f,%.7f,%.7f <!-- alt %12.7f time %12.7f sats %d -->\n";
75
76         static final String kml_placemark_end =
77                 "      </coordinates>\n" +
78                 "    </LineString>\n" +
79                 "  </Placemark>\n";
80
81         static final String kml_footer =
82                 "</Document>\n" +
83                 "</kml>\n";
84
85         void start (AltosState record) {
86                 out.printf(kml_header_start, record.flight, record.serial);
87                 out.printf("Date:   %04d-%02d-%02d\n",
88                            record.gps.year, record.gps.month, record.gps.day);
89                 out.printf("Time:     %2d:%02d:%02d\n",
90                            record.gps.hour, record.gps.minute, record.gps.second);
91                 out.printf("%s", kml_header_end);
92         }
93
94         boolean started = false;
95
96         void state_start(AltosState state) {
97                 String  state_name = AltosLib.state_name(state.state);
98                 out.printf(kml_style_start, state_name, kml_state_colors[state.state]);
99                 out.printf("\tState: %s\n", state_name);
100                 out.printf("%s", kml_style_end);
101                 out.printf(kml_placemark_start, state_name, state_name);
102         }
103
104         void state_end(AltosState state) {
105                 out.printf("%s", kml_placemark_end);
106         }
107
108         void coord(AltosState state) {
109                 AltosGPS        gps = state.gps;
110                 double          altitude;
111
112                 if (state.height() != AltosLib.MISSING)
113                         altitude = state.height() + gps_start_altitude;
114                 else
115                         altitude = gps.alt;
116                 out.printf(kml_coord_fmt,
117                            gps.lon, gps.lat,
118                            altitude, (double) gps.alt,
119                            state.time, gps.nsat);
120         }
121
122         void end() {
123                 out.printf("%s", kml_footer);
124         }
125
126         public void close() {
127                 if (prev != null) {
128                         state_end(prev);
129                         end();
130                         prev = null;
131                 }
132         }
133
134         public void write(AltosState state) {
135                 AltosGPS        gps = state.gps;
136
137                 if (gps == null)
138                         return;
139
140                 if (gps.lat == AltosLib.MISSING)
141                         return;
142                 if (gps.lon == AltosLib.MISSING)
143                         return;
144                 if (!started) {
145                         start(state);
146                         started = true;
147                         gps_start_altitude = gps.alt;
148                 }
149                 if (prev != null && prev.gps_sequence == state.gps_sequence)
150                         return;
151                 if (state.state != flight_state) {
152                         flight_state = state.state;
153                         if (prev != null) {
154                                 coord(state);
155                                 state_end(prev);
156                         }
157                         state_start(state);
158                 }
159                 coord(state);
160                 prev = state;
161         }
162
163         public void write(AltosStateIterable states) {
164                 for (AltosState state : states) {
165                         if ((state.set & AltosState.set_gps) != 0)
166                                 write(state);
167                 }
168         }
169
170         public AltosKML(File in_name) throws FileNotFoundException {
171                 name = in_name;
172                 out = new PrintStream(name);
173         }
174
175         public AltosKML(String in_string) throws FileNotFoundException {
176                 this(new File(in_string));
177         }
178 }