source: git/src/kml.cc @ d01b55a

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since d01b55a was d01b55a, checked in by Olly Betts <olly@…>, 7 years ago

Use C++11 member initialisation

  • Property mode set to 100644
File size: 7.7 KB
Line 
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,2017,2018 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
40using namespace std;
41
42#define WGS84_DATUM_STRING "+proj=longlat +ellps=WGS84 +datum=WGS84"
43
44static void
45html_escape(FILE *fh, const char *s)
46{
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);
60        }
61        ++s;
62    }
63}
64
65KML::KML(const char * input_datum)
66{
67    if (!(pj_input = pj_init_plus(input_datum))) {
68        wxString m = wmsg(/*Failed to initialise input coordinate system “%s”*/287);
69        m = wxString::Format(m.c_str(), input_datum);
70        throw m;
71    }
72    if (!(pj_output = pj_init_plus(WGS84_DATUM_STRING))) {
73        wxString m = wmsg(/*Failed to initialise output coordinate system “%s”*/288);
74        m = wxString::Format(m.c_str(), WGS84_DATUM_STRING);
75        throw m;
76    }
77}
78
79KML::~KML()
80{
81    if (pj_input)
82        pj_free(pj_input);
83    if (pj_output)
84        pj_free(pj_output);
85}
86
87const int *
88KML::passes() const
89{
90    static const int default_passes[] = {
91        PASG, XSECT, WALL1, WALL2, LEGS|SURF, LABELS|ENTS|FIXES|EXPORTS, 0
92    };
93    return default_passes;
94}
95
96/* Initialise KML routines. */
97void KML::header(const char * title, const char *, time_t,
98                 double, double, double, double, double, double)
99{
100    fputs(
101"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
102"<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n", fh);
103    fputs("<Document><name>", fh);
104    html_escape(fh, title);
105    fputs("</name>\n", fh);
106    // Set up styles for the icons to reduce the file size.
107    fputs("<Style id=\"fix\"><IconStyle>"
108          "<Icon><href>http://maps.google.com/mapfiles/kml/paddle/red-blank.png</href></Icon>"
109          "</IconStyle></Style>\n", fh);
110    fputs("<Style id=\"exp\"><IconStyle>"
111          "<Icon><href>http://maps.google.com/mapfiles/kml/paddle/blu-blank.png</href></Icon>"
112          "</IconStyle></Style>\n", fh);
113    fputs("<Style id=\"ent\"><IconStyle>"
114          "<Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-blank.png</href></Icon>"
115          "</IconStyle></Style>\n", fh);
116    // FIXME: does KML allow bounds?
117    // NB Lat+long bounds are not necessarily the same as the bounds in survex
118    // coords translated to WGS84 lat+long...
119}
120
121void
122KML::start_pass(int)
123{
124    if (in_linestring) {
125        fputs("</coordinates></LineString></MultiGeometry></Placemark>\n", fh);
126        in_linestring = false;
127    }
128}
129
130void
131KML::line(const img_point *p1, const img_point *p, unsigned /*flags*/, bool fPendingMove)
132{
133    if (fPendingMove) {
134        if (!in_linestring) {
135            in_linestring = true;
136            fputs("<Placemark><MultiGeometry>\n", fh);
137        } else {
138            fputs("</coordinates></LineString>\n", fh);
139        }
140        fputs("<LineString><altitudeMode>absolute</altitudeMode><coordinates>\n", fh);
141        double X = p1->x, Y = p1->y, Z = p1->z;
142        pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z);
143        X = deg(X);
144        Y = deg(Y);
145        // %.8f is at worst just over 1mm.
146        fprintf(fh, "%.8f,%.8f,%.2f\n", X, Y, Z);
147    }
148    double X = p->x, Y = p->y, Z = p->z;
149    pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z);
150    X = deg(X);
151    Y = deg(Y);
152    // %.8f is at worst just over 1mm.
153    fprintf(fh, "%.8f,%.8f,%.2f\n", X, Y, Z);
154}
155
156void
157KML::xsect(const img_point *p, double angle, double d1, double d2)
158{
159    double s = sin(rad(angle));
160    double c = cos(rad(angle));
161
162    double x1 = p->x + c * d1;
163    double y1 = p->y + s * d1;
164    double z1 = p->z;
165    pj_transform(pj_input, pj_output, 1, 1, &x1, &y1, &z1);
166    x1 = deg(x1);
167    y1 = deg(y1);
168
169    double x2 = p->x - c * d2;
170    double y2 = p->y - s * d2;
171    double z2 = p->z;
172    pj_transform(pj_input, pj_output, 1, 1, &x2, &y2, &z2);
173    x2 = deg(x2);
174    y2 = deg(y2);
175
176    fputs("<Placemark><name></name><LineString><altitudeMode>absolute</altitudeMode><coordinates>", fh);
177    fprintf(fh, "%.8f,%.8f,%.2f %.8f,%.8f,%.2f", x1, y1, z1, x2, y2, z2);
178    fputs("</coordinates></LineString></Placemark>\n", fh);
179}
180
181void
182KML::wall(const img_point *p, double angle, double d)
183{
184    double s = sin(rad(angle));
185    double c = cos(rad(angle));
186
187    double x = p->x + c * d;
188    double y = p->y + s * d;
189    double z = p->z;
190    pj_transform(pj_input, pj_output, 1, 1, &x, &y, &z);
191    x = deg(x);
192    y = deg(y);
193
194    if (!in_wall) {
195        fputs("<Placemark><name></name><LineString><altitudeMode>absolute</altitudeMode><coordinates>", fh);
196        in_wall = true;
197    }
198    fprintf(fh, "%.8f,%.8f,%.2f\n", x, y, z);
199}
200
201void
202KML::passage(const img_point *p, double angle, double d1, double d2)
203{
204    double s = sin(rad(angle));
205    double c = cos(rad(angle));
206
207    // Draw along one side and push the other onto a stack, then at the end pop
208    // the stack and write out those points to give one polygon, fewer points,
209    // and a smaller file.
210    double x1 = p->x + c * d1;
211    double y1 = p->y + s * d1;
212    double z1 = p->z;
213    pj_transform(pj_input, pj_output, 1, 1, &x1, &y1, &z1);
214    x1 = deg(x1);
215    y1 = deg(y1);
216
217    double x2 = p->x - c * d2;
218    double y2 = p->y - s * d2;
219    double z2 = p->z;
220    pj_transform(pj_input, pj_output, 1, 1, &x2, &y2, &z2);
221    x2 = deg(x2);
222    y2 = deg(y2);
223
224    if (psg.empty()) {
225        fprintf(fh, "<Placemark><name></name><Polygon><altitudeMode>absolute</altitudeMode>"
226                    "<outerBoundaryIs><LinearRing><coordinates>\n");
227    }
228    // NB - order of vertices should be anti-clockwise in a KML file, so go
229    // along the right wall now, and put the left wall points on a stack to
230    // come back along at the end.
231    fprintf(fh, "%.8f,%.8f,%.2f\n", x2, y2, z2);
232    psg.push_back(Vector3(x1, y1, z1));
233}
234
235void
236KML::tube_end()
237{
238    if (!psg.empty()) {
239        vector<Vector3>::const_reverse_iterator i;
240        for (i = psg.rbegin(); i != psg.rend(); ++i) {
241            fprintf(fh, "%.8f,%.8f,%.2f\n", i->GetX(), i->GetY(), i->GetZ());
242        }
243        psg.clear();
244        fputs("</coordinates></LinearRing></outerBoundaryIs>"
245              "</Polygon></Placemark>\n", fh);
246    }
247    if (in_wall) {
248        fputs("</coordinates></LineString></Placemark>\n", fh);
249        in_wall = false;
250    }
251}
252
253void
254KML::label(const img_point *p, const char *s, bool /*fSurface*/, int type)
255{
256    double X = p->x, Y = p->y, Z = p->z;
257    pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z);
258    X = deg(X);
259    Y = deg(Y);
260    // %.8f is at worst just over 1mm.
261    fprintf(fh, "<Placemark><Point><coordinates>%.8f,%.8f,%.2f</coordinates></Point><name>", X, Y, Z);
262    html_escape(fh, s);
263    fputs("</name>", fh);
264    // Add a "pin" symbol with colour matching what aven shows.
265    switch (type) {
266        case FIXES:
267            fputs("<styleUrl>#fix</styleUrl>", fh);
268            break;
269        case EXPORTS:
270            fputs("<styleUrl>#exp</styleUrl>", fh);
271            break;
272        case ENTS:
273            fputs("<styleUrl>#ent</styleUrl>", fh);
274            break;
275    }
276    fputs("</Placemark>\n", fh);
277}
278
279void
280KML::footer()
281{
282    if (in_linestring)
283        fputs("</coordinates></LineString></MultiGeometry></Placemark>\n", fh);
284    fputs("</Document></kml>\n", fh);
285}
Note: See TracBrowser for help on using the repository browser.