Drop useless alignment specifiers in sizer Add()
[survex.git] / src / kml.cc
blob46d9ff26f3bbcf63560c8ee9ae0202019c8af59a
1 /* kml.cc
2 * Export from Aven as KML.
3 */
4 /* Copyright (C) 2012 Olaf Kähler
5 * Copyright (C) 2012,2013,2014,2015,2016 Olly Betts
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include "kml.h"
28 #include "export.h" // For LABELS, etc
30 #include <stdio.h>
31 #include <string>
32 #include <math.h>
34 #include "useful.h"
35 #include <proj_api.h>
37 #include "aven.h"
38 #include "message.h"
40 using namespace std;
42 #define WGS84_DATUM_STRING "+proj=longlat +ellps=WGS84 +datum=WGS84"
44 static void
45 html_escape(FILE *fh, const char *s)
47 while (*s) {
48 switch (*s) {
49 case '<':
50 fputs("&lt;", fh);
51 break;
52 case '>':
53 fputs("&gt;", fh);
54 break;
55 case '&':
56 fputs("&amp;", fh);
57 break;
58 default:
59 PUTC(*s, fh);
61 ++s;
65 KML::KML(const char * input_datum)
66 : pj_input(NULL), pj_output(NULL), in_linestring(false), in_wall(false)
68 if (!(pj_input = pj_init_plus(input_datum))) {
69 wxString m = wmsg(/*Failed to initialise input coordinate system “%s”*/287);
70 m = wxString::Format(m.c_str(), input_datum);
71 throw m;
73 if (!(pj_output = pj_init_plus(WGS84_DATUM_STRING))) {
74 wxString m = wmsg(/*Failed to initialise output coordinate system “%s”*/288);
75 m = wxString::Format(m.c_str(), WGS84_DATUM_STRING);
76 throw m;
80 KML::~KML()
82 if (pj_input)
83 pj_free(pj_input);
84 if (pj_output)
85 pj_free(pj_output);
88 const int *
89 KML::passes() const
91 static const int default_passes[] = {
92 PASG, XSECT, WALL1, WALL2, LEGS|SURF, LABELS|ENTS|FIXES|EXPORTS, 0
94 return default_passes;
97 /* Initialise KML routines. */
98 void KML::header(const char * title, const char *, time_t,
99 double, double, double, double, double, double)
101 fputs(
102 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
103 "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n", fh);
104 fputs("<Document><name>", fh);
105 html_escape(fh, title);
106 fputs("</name>\n", fh);
107 // Set up styles for the icons to reduce the file size.
108 fputs("<Style id=\"fix\"><IconStyle>"
109 "<Icon><href>http://maps.google.com/mapfiles/kml/paddle/red-blank.png</href></Icon>"
110 "</IconStyle></Style>\n", fh);
111 fputs("<Style id=\"exp\"><IconStyle>"
112 "<Icon><href>http://maps.google.com/mapfiles/kml/paddle/blu-blank.png</href></Icon>"
113 "</IconStyle></Style>\n", fh);
114 fputs("<Style id=\"ent\"><IconStyle>"
115 "<Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-blank.png</href></Icon>"
116 "</IconStyle></Style>\n", fh);
117 // FIXME: does KML allow bounds?
118 // NB Lat+long bounds are not necessarily the same as the bounds in survex
119 // coords translated to WGS84 lat+long...
122 void
123 KML::line(const img_point *p1, const img_point *p, unsigned /*flags*/, bool fPendingMove)
125 if (fPendingMove) {
126 if (!in_linestring) {
127 in_linestring = true;
128 fputs("<Placemark><MultiGeometry>\n", fh);
129 } else {
130 fputs("</coordinates></LineString>\n", fh);
132 fputs("<LineString><altitudeMode>absolute</altitudeMode><coordinates>\n", fh);
133 double X = p1->x, Y = p1->y, Z = p1->z;
134 pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z);
135 X = deg(X);
136 Y = deg(Y);
137 // %.8f is at worst just over 1mm.
138 fprintf(fh, "%.8f,%.8f,%.2f\n", X, Y, Z);
140 double X = p->x, Y = p->y, Z = p->z;
141 pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z);
142 X = deg(X);
143 Y = deg(Y);
144 // %.8f is at worst just over 1mm.
145 fprintf(fh, "%.8f,%.8f,%.2f\n", X, Y, Z);
148 void
149 KML::xsect(const img_point *p, double angle, double d1, double d2)
151 double s = sin(rad(angle));
152 double c = cos(rad(angle));
154 double x1 = p->x + c * d1;
155 double y1 = p->y + s * d1;
156 double z1 = p->z;
157 pj_transform(pj_input, pj_output, 1, 1, &x1, &y1, &z1);
158 x1 = deg(x1);
159 y1 = deg(y1);
161 double x2 = p->x - c * d2;
162 double y2 = p->y - s * d2;
163 double z2 = p->z;
164 pj_transform(pj_input, pj_output, 1, 1, &x2, &y2, &z2);
165 x2 = deg(x2);
166 y2 = deg(y2);
168 fputs("<Placemark><name></name><LineString><altitudeMode>absolute</altitudeMode><coordinates>", fh);
169 fprintf(fh, "%.8f,%.8f,%.8f %.8f,%.8f,%.8f", x1, y1, z1, x2, y2, z2);
170 fputs("</coordinates></LineString></Placemark>\n", fh);
173 void
174 KML::wall(const img_point *p, double angle, double d)
176 double s = sin(rad(angle));
177 double c = cos(rad(angle));
179 double x = p->x + c * d;
180 double y = p->y + s * d;
181 double z = p->z;
182 pj_transform(pj_input, pj_output, 1, 1, &x, &y, &z);
183 x = deg(x);
184 y = deg(y);
186 if (!in_wall) {
187 fputs("<Placemark><name></name><LineString><altitudeMode>absolute</altitudeMode><coordinates>", fh);
188 in_wall = true;
190 fprintf(fh, "%.8f,%.8f,%.8f\n", x, y, z);
193 void
194 KML::passage(const img_point *p, double angle, double d1, double d2)
196 double s = sin(rad(angle));
197 double c = cos(rad(angle));
199 // Draw along one side and push the other onto a stack, then at the end pop
200 // the stack and write out those points to give one polygon, fewer points,
201 // and a smaller file.
202 double x1 = p->x + c * d1;
203 double y1 = p->y + s * d1;
204 double z1 = p->z;
205 pj_transform(pj_input, pj_output, 1, 1, &x1, &y1, &z1);
206 x1 = deg(x1);
207 y1 = deg(y1);
209 double x2 = p->x - c * d2;
210 double y2 = p->y - s * d2;
211 double z2 = p->z;
212 pj_transform(pj_input, pj_output, 1, 1, &x2, &y2, &z2);
213 x2 = deg(x2);
214 y2 = deg(y2);
216 if (psg.empty()) {
217 fprintf(fh, "<Placemark><name></name><Polygon><altitudeMode>absolute</altitudeMode>"
218 "<outerBoundaryIs><LinearRing><coordinates>\n");
220 // NB - order of vertices should be anti-clockwise in a KML file, so go
221 // along the right wall now, and put the left wall points on a stack to
222 // come back along at the end.
223 fprintf(fh, "%.8f,%.8f,%.8f\n", x2, y2, z2);
224 psg.push_back(Vector3(x1, y1, z1));
227 void
228 KML::tube_end()
230 if (!psg.empty()) {
231 vector<Vector3>::const_reverse_iterator i;
232 for (i = psg.rbegin(); i != psg.rend(); ++i) {
233 fprintf(fh, "%.8f,%.8f,%.8f\n", i->GetX(), i->GetY(), i->GetZ());
235 psg.clear();
236 fputs("</coordinates></LinearRing></outerBoundaryIs>"
237 "</Polygon></Placemark>\n", fh);
239 if (in_wall) {
240 fputs("</coordinates></LineString></Placemark>\n", fh);
241 in_wall = false;
245 void
246 KML::label(const img_point *p, const char *s, bool /*fSurface*/, int type)
248 double X = p->x, Y = p->y, Z = p->z;
249 pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z);
250 X = deg(X);
251 Y = deg(Y);
252 // %.8f is at worst just over 1mm.
253 fprintf(fh, "<Placemark><Point><coordinates>%.8f,%.8f,%.2f</coordinates></Point><name>", X, Y, Z);
254 html_escape(fh, s);
255 fputs("</name>", fh);
256 // Add a "pin" symbol with colour matching what aven shows.
257 switch (type) {
258 case FIXES:
259 fputs("<styleUrl>#fix</styleUrl>", fh);
260 break;
261 case EXPORTS:
262 fputs("<styleUrl>#exp</styleUrl>", fh);
263 break;
264 case ENTS:
265 fputs("<styleUrl>#ent</styleUrl>", fh);
266 break;
268 fputs("</Placemark>\n", fh);
271 void
272 KML::footer()
274 if (in_linestring)
275 fputs("</coordinates></LineString></MultiGeometry></Placemark>\n", fh);
276 fputs("</Document></kml>\n", fh);