altosui: Fix Landed tab units
[fw/altos] / altosui / 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 altosui;
19
20 import java.lang.*;
21 import java.io.*;
22 import java.text.*;
23 import java.util.*;
24 import org.altusmetrum.AltosLib.*;
25
26 public class AltosKML implements AltosWriter {
27
28         File                    name;
29         PrintStream             out;
30         int                     state = -1;
31         AltosRecord             prev = null;
32
33         static final String[] kml_state_colors = {
34                 "FF000000",
35                 "FF000000",
36                 "FF000000",
37                 "FF0000FF",
38                 "FF4080FF",
39                 "FF00FFFF",
40                 "FFFF0000",
41                 "FF00FF00",
42                 "FF000000",
43                 "FFFFFFFF"
44         };
45
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" +
49                 "<Document>\n" +
50                 "  <name>AO Flight#%d S/N: %03d</name>\n" +
51                 "  <description>\n";
52         static final String kml_header_end =
53                 "  </description>\n" +
54                 "  <open>0</open>\n";
55
56         static final String kml_style_start =
57                 "  <Style id=\"ao-flightstate-%s\">\n" +
58                 "    <LineStyle><color>%s</color><width>4</width></LineStyle>\n" +
59                 "    <BalloonStyle>\n" +
60                 "      <text>\n";
61
62         static final String kml_style_end =
63                 "      </text>\n" +
64                 "    </BalloonStyle>\n" +
65                 "  </Style>\n";
66
67         static final String kml_placemark_start =
68                 "  <Placemark>\n" +
69                 "    <name>%s</name>\n" +
70                 "    <styleUrl>#ao-flightstate-%s</styleUrl>\n" +
71                 "    <LineString>\n" +
72                 "      <tessellate>1</tessellate>\n" +
73                 "      <altitudeMode>absolute</altitudeMode>\n" +
74                 "      <coordinates>\n";
75
76         static final String kml_coord_fmt =
77         "        %.7f,%.7f,%.7f <!-- alt %12.7f time %12.7f sats %d -->\n";
78
79         static final String kml_placemark_end =
80                 "      </coordinates>\n" +
81                 "    </LineString>\n" +
82                 "  </Placemark>\n";
83
84         static final String kml_footer =
85                 "</Document>\n" +
86                 "</kml>\n";
87
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);
95         }
96
97         boolean started = false;
98
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);
105         }
106
107         void state_end(AltosRecord record) {
108                 out.printf("%s", kml_placemark_end);
109         }
110
111         void coord(AltosRecord record) {
112                 AltosGPS        gps = record.gps;
113                 out.printf(kml_coord_fmt,
114                            gps.lon, gps.lat,
115                            record.filtered_altitude(), (double) gps.alt,
116                            record.time, gps.nsat);
117         }
118
119         void end() {
120                 out.printf("%s", kml_footer);
121         }
122
123         public void close() {
124                 if (prev != null) {
125                         state_end(prev);
126                         end();
127                         prev = null;
128                 }
129         }
130
131         public void write(AltosRecord record) {
132                 AltosGPS        gps = record.gps;
133
134                 if (gps == null) {
135                         System.out.printf ("no gps\n");
136                         return;
137                 }
138
139                 System.out.printf ("seen %x\n", record.seen);
140                 if ((record.seen & (AltosRecord.seen_flight)) == 0)
141                         return;
142                 if ((record.seen & (AltosRecord.seen_gps_lat)) == 0)
143                         return;
144                 if ((record.seen & (AltosRecord.seen_gps_lon)) == 0)
145                         return;
146                 if (!started) {
147                         start(record);
148                         started = true;
149                 }
150                 if (prev != null &&
151                     prev.gps.second == record.gps.second &&
152                     prev.gps.minute == record.gps.minute &&
153                     prev.gps.hour == record.gps.hour)
154                         return;
155                 if (record.state != state) {
156                         state = record.state;
157                         if (prev != null) {
158                                 coord(record);
159                                 state_end(prev);
160                         }
161                         state_start(record);
162                 }
163                 coord(record);
164                 prev = record;
165         }
166
167         public void write(AltosRecordIterable iterable) {
168                 for (AltosRecord record : iterable)
169                         write(record);
170         }
171
172         public AltosKML(File in_name) throws FileNotFoundException {
173                 name = in_name;
174                 out = new PrintStream(name);
175         }
176
177         public AltosKML(String in_string) throws FileNotFoundException {
178                 this(new File(in_string));
179         }
180 }