source: git/src/kml.cc @ 4e6f6ae

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

src/: Fix exporting to GPX, KML and HPGL, which all failed to write
the header to the exported file in 1.2.18.

  • Property mode set to 100644
File size: 4.4 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 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    : 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
80KML::~KML()
81{
82    if (pj_input)
83        pj_free(pj_input);
84    if (pj_output)
85        pj_free(pj_output);
86}
87
88const int *
89KML::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. */
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    // FIXME: does KML allow bounds?
107    // NB Lat+long bounds are not necessarily the same as the bounds in survex
108    // coords translated to WGS84 lat+long...
109}
110
111void
112KML::line(const img_point *p1, const img_point *p, bool /*fSurface*/, bool fPendingMove)
113{
114    (void)p1;
115    (void)p;
116    (void)fPendingMove;
117#if 0
118    if (fPendingMove) {
119        if (in_trkseg) {
120            fputs("</trkseg><trkseg>\n", fh);
121        } else {
122            fputs("<trk><trkseg>\n", fh);
123            in_trkseg = true;
124        }
125        double X = p1->x, Y = p1->y, Z = p1->z;
126        pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z);
127        X = deg(X);
128        Y = deg(Y);
129        // %.8f is at worst just over 1mm.
130        fprintf(fh, "<trkpt lon=\"%.8f\" lat=\"%.8f\"><ele>%.2f</ele></trkpt>\n", X, Y, Z);
131    }
132    double X = p->x, Y = p->y, Z = p->z;
133    pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z);
134    X = deg(X);
135    Y = deg(Y);
136    // %.8f is at worst just over 1mm.
137    fprintf(fh, "<trkpt lon=\"%.8f\" lat=\"%.8f\"><ele>%.2f</ele></trkpt>\n", X, Y, Z);
138#endif
139}
140
141void
142KML::label(const img_point *p, const char *s, bool /*fSurface*/, int type)
143{
144    (void)type;
145    double X = p->x, Y = p->y, Z = p->z;
146    pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z);
147    X = deg(X);
148    Y = deg(Y);
149    // %.8f is at worst just over 1mm.
150    fprintf(fh, "<Placemark><Point><coordinates>%.8f,%.8f,%.2f</coordinates></Point><name>", X, Y, Z);
151    html_escape(fh, s);
152    fputs("</name>", fh);
153#if 0
154    // Add a "pin" symbol with colour matching what aven shows.
155    switch (type) {
156        case FIXES:
157            fputs("<sym>Pin, Red</sym>", fh);
158            break;
159        case EXPORTS:
160            fputs("<sym>Pin, Blue</sym>", fh);
161            break;
162        case ENTS:
163            fputs("<sym>Pin, Green</sym>", fh);
164            break;
165    }
166#endif
167    fputs("</Placemark>\n", fh);
168}
169
170void
171KML::footer()
172{
173#if 0
174    if (in_trkseg)
175        fputs("</trkseg></trk>\n", fh);
176#endif
177    fputs("</Document></kml>\n", fh);
178}
Note: See TracBrowser for help on using the repository browser.