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