1 | /* kml.cc |
---|
2 | * Export from Aven as KML. |
---|
3 | */ |
---|
4 | /* Copyright (C) 2012 Olaf Kähler |
---|
5 | * Copyright (C) 2012,2013,2014 Olly Betts |
---|
6 | * |
---|
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. |
---|
11 | * |
---|
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. |
---|
16 | * |
---|
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 |
---|
20 | */ |
---|
21 | |
---|
22 | #ifdef HAVE_CONFIG_H |
---|
23 | # include <config.h> |
---|
24 | #endif |
---|
25 | |
---|
26 | #include "kml.h" |
---|
27 | |
---|
28 | #include "export.h" // For LABELS, etc |
---|
29 | |
---|
30 | #include <stdio.h> |
---|
31 | #include <string> |
---|
32 | #include <math.h> |
---|
33 | |
---|
34 | #include "useful.h" |
---|
35 | #include <proj_api.h> |
---|
36 | |
---|
37 | #include "aven.h" |
---|
38 | #include "message.h" |
---|
39 | |
---|
40 | using namespace std; |
---|
41 | |
---|
42 | #define WGS84_DATUM_STRING "+proj=longlat +ellps=WGS84 +datum=WGS84" |
---|
43 | |
---|
44 | static void |
---|
45 | html_escape(FILE *fh, const char *s) |
---|
46 | { |
---|
47 | while (*s) { |
---|
48 | switch (*s) { |
---|
49 | case '<': |
---|
50 | fputs("<", fh); |
---|
51 | break; |
---|
52 | case '>': |
---|
53 | fputs(">", fh); |
---|
54 | break; |
---|
55 | case '&': |
---|
56 | fputs("&", fh); |
---|
57 | break; |
---|
58 | default: |
---|
59 | PUTC(*s, fh); |
---|
60 | } |
---|
61 | ++s; |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | KML::KML(const char * input_datum) |
---|
66 | : pj_input(NULL), pj_output(NULL), in_trkseg(false) |
---|
67 | { |
---|
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; |
---|
72 | } |
---|
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; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | KML::~KML() |
---|
81 | { |
---|
82 | if (pj_input) |
---|
83 | pj_free(pj_input); |
---|
84 | if (pj_output) |
---|
85 | pj_free(pj_output); |
---|
86 | } |
---|
87 | |
---|
88 | const int * |
---|
89 | KML::passes() const |
---|
90 | { |
---|
91 | // static const int default_passes[] = { LABELS|ENTS|FIXES|EXPORTS, LEGS|SURF, 0 }; |
---|
92 | static const int default_passes[] = { LABELS|ENTS|FIXES|EXPORTS, 0 }; |
---|
93 | return default_passes; |
---|
94 | } |
---|
95 | |
---|
96 | /* Initialise KML routines. */ |
---|
97 | void KML::header(const char * title, const char *, time_t) |
---|
98 | { |
---|
99 | fputs( |
---|
100 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
---|
101 | "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n", fh); |
---|
102 | fputs("<Document><name>", fh); |
---|
103 | html_escape(fh, title); |
---|
104 | fputs("</name>\n", fh); |
---|
105 | // FIXME: does KML allow bounds? |
---|
106 | // NB Lat+long bounds are not necessarily the same as the bounds in survex |
---|
107 | // coords translated to WGS84 lat+long... |
---|
108 | } |
---|
109 | |
---|
110 | void |
---|
111 | KML::line(const img_point *p1, const img_point *p, bool /*fSurface*/, bool fPendingMove) |
---|
112 | { |
---|
113 | (void)p1; |
---|
114 | (void)p; |
---|
115 | (void)fPendingMove; |
---|
116 | #if 0 |
---|
117 | if (fPendingMove) { |
---|
118 | if (in_trkseg) { |
---|
119 | fputs("</trkseg><trkseg>\n", fh); |
---|
120 | } else { |
---|
121 | fputs("<trk><trkseg>\n", fh); |
---|
122 | in_trkseg = true; |
---|
123 | } |
---|
124 | double X = p1->x, Y = p1->y, Z = p1->z; |
---|
125 | pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z); |
---|
126 | X = deg(X); |
---|
127 | Y = deg(Y); |
---|
128 | // %.8f is at worst just over 1mm. |
---|
129 | fprintf(fh, "<trkpt lon=\"%.8f\" lat=\"%.8f\"><ele>%.2f</ele></trkpt>\n", X, Y, Z); |
---|
130 | } |
---|
131 | double X = p->x, Y = p->y, Z = p->z; |
---|
132 | pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z); |
---|
133 | X = deg(X); |
---|
134 | Y = deg(Y); |
---|
135 | // %.8f is at worst just over 1mm. |
---|
136 | fprintf(fh, "<trkpt lon=\"%.8f\" lat=\"%.8f\"><ele>%.2f</ele></trkpt>\n", X, Y, Z); |
---|
137 | #endif |
---|
138 | } |
---|
139 | |
---|
140 | void |
---|
141 | KML::label(const img_point *p, const char *s, bool /*fSurface*/, int type) |
---|
142 | { |
---|
143 | (void)type; |
---|
144 | double X = p->x, Y = p->y, Z = p->z; |
---|
145 | pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z); |
---|
146 | X = deg(X); |
---|
147 | Y = deg(Y); |
---|
148 | // %.8f is at worst just over 1mm. |
---|
149 | fprintf(fh, "<Placemark><Point><coordinates>%.8f,%.8f,%.2f</coordinates></Point><name>", X, Y, Z); |
---|
150 | html_escape(fh, s); |
---|
151 | fputs("</name>", fh); |
---|
152 | #if 0 |
---|
153 | // Add a "pin" symbol with colour matching what aven shows. |
---|
154 | switch (type) { |
---|
155 | case FIXES: |
---|
156 | fputs("<sym>Pin, Red</sym>", fh); |
---|
157 | break; |
---|
158 | case EXPORTS: |
---|
159 | fputs("<sym>Pin, Blue</sym>", fh); |
---|
160 | break; |
---|
161 | case ENTS: |
---|
162 | fputs("<sym>Pin, Green</sym>", fh); |
---|
163 | break; |
---|
164 | } |
---|
165 | #endif |
---|
166 | fputs("</Placemark>\n", fh); |
---|
167 | } |
---|
168 | |
---|
169 | void |
---|
170 | KML::footer() |
---|
171 | { |
---|
172 | #if 0 |
---|
173 | if (in_trkseg) |
---|
174 | fputs("</trkseg></trk>\n", fh); |
---|
175 | #endif |
---|
176 | fputs("</Document></kml>\n", fh); |
---|
177 | } |
---|