[1534ed9] | 1 | /* kml.cc |
---|
| 2 | * Export from Aven as KML. |
---|
| 3 | */ |
---|
| 4 | /* Copyright (C) 2012 Olaf Kähler |
---|
[a2c29c1] | 5 | * Copyright (C) 2012,2013,2014,2015,2016 Olly Betts |
---|
[1534ed9] | 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) |
---|
[ae6a68c] | 66 | : pj_input(NULL), pj_output(NULL), in_linestring(false) |
---|
[1534ed9] | 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 | { |
---|
[ae6a68c] | 91 | static const int default_passes[] = { LABELS|ENTS|FIXES|EXPORTS, LEGS|SURF, 0 }; |
---|
[1534ed9] | 92 | return default_passes; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /* Initialise KML routines. */ |
---|
[55a861a] | 96 | void KML::header(const char * title, const char *, time_t, |
---|
| 97 | double, double, double, double, double, double) |
---|
[1534ed9] | 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); |
---|
[7aed359] | 105 | // Set up styles for the icons to reduce the file size. |
---|
| 106 | fputs("<Style id=\"fix\"><IconStyle>" |
---|
[72e6593] | 107 | "<Icon><href>http://maps.google.com/mapfiles/kml/paddle/red-blank.png</href></Icon>" |
---|
[7aed359] | 108 | "</IconStyle></Style>\n", fh); |
---|
| 109 | fputs("<Style id=\"exp\"><IconStyle>" |
---|
[72e6593] | 110 | "<Icon><href>http://maps.google.com/mapfiles/kml/paddle/blu-blank.png</href></Icon>" |
---|
[7aed359] | 111 | "</IconStyle></Style>\n", fh); |
---|
| 112 | fputs("<Style id=\"ent\"><IconStyle>" |
---|
[72e6593] | 113 | "<Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-blank.png</href></Icon>" |
---|
[7aed359] | 114 | "</IconStyle></Style>\n", fh); |
---|
[1534ed9] | 115 | // FIXME: does KML allow bounds? |
---|
| 116 | // NB Lat+long bounds are not necessarily the same as the bounds in survex |
---|
| 117 | // coords translated to WGS84 lat+long... |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void |
---|
[a2c29c1] | 121 | KML::line(const img_point *p1, const img_point *p, unsigned /*flags*/, bool fPendingMove) |
---|
[1534ed9] | 122 | { |
---|
| 123 | if (fPendingMove) { |
---|
[3b23819] | 124 | if (!in_linestring) { |
---|
| 125 | in_linestring = true; |
---|
| 126 | fputs("<Placemark><MultiGeometry>\n", fh); |
---|
| 127 | } else { |
---|
| 128 | fputs("</coordinates></LineString>\n", fh); |
---|
| 129 | } |
---|
| 130 | fputs("<LineString><altitudeMode>absolute</altitudeMode><coordinates>\n", fh); |
---|
[1534ed9] | 131 | double X = p1->x, Y = p1->y, Z = p1->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. |
---|
[ae6a68c] | 136 | fprintf(fh, "%.8f,%.8f,%.2f\n", X, Y, Z); |
---|
[1534ed9] | 137 | } |
---|
| 138 | double X = p->x, Y = p->y, Z = p->z; |
---|
| 139 | pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z); |
---|
| 140 | X = deg(X); |
---|
| 141 | Y = deg(Y); |
---|
| 142 | // %.8f is at worst just over 1mm. |
---|
[ae6a68c] | 143 | fprintf(fh, "%.8f,%.8f,%.2f\n", X, Y, Z); |
---|
[1534ed9] | 144 | } |
---|
| 145 | |
---|
| 146 | void |
---|
| 147 | KML::label(const img_point *p, const char *s, bool /*fSurface*/, int type) |
---|
| 148 | { |
---|
| 149 | double X = p->x, Y = p->y, Z = p->z; |
---|
| 150 | pj_transform(pj_input, pj_output, 1, 1, &X, &Y, &Z); |
---|
| 151 | X = deg(X); |
---|
| 152 | Y = deg(Y); |
---|
| 153 | // %.8f is at worst just over 1mm. |
---|
| 154 | fprintf(fh, "<Placemark><Point><coordinates>%.8f,%.8f,%.2f</coordinates></Point><name>", X, Y, Z); |
---|
| 155 | html_escape(fh, s); |
---|
| 156 | fputs("</name>", fh); |
---|
| 157 | // Add a "pin" symbol with colour matching what aven shows. |
---|
| 158 | switch (type) { |
---|
| 159 | case FIXES: |
---|
[7aed359] | 160 | fputs("<styleUrl>#fix</styleUrl>", fh); |
---|
[1534ed9] | 161 | break; |
---|
| 162 | case EXPORTS: |
---|
[7aed359] | 163 | fputs("<styleUrl>#exp</styleUrl>", fh); |
---|
[1534ed9] | 164 | break; |
---|
| 165 | case ENTS: |
---|
[7aed359] | 166 | fputs("<styleUrl>#ent</styleUrl>", fh); |
---|
[1534ed9] | 167 | break; |
---|
| 168 | } |
---|
| 169 | fputs("</Placemark>\n", fh); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | void |
---|
| 173 | KML::footer() |
---|
| 174 | { |
---|
[ae6a68c] | 175 | if (in_linestring) |
---|
[3b23819] | 176 | fputs("</coordinates></LineString></MultiGeometry></Placemark>\n", fh); |
---|
[1534ed9] | 177 | fputs("</Document></kml>\n", fh); |
---|
| 178 | } |
---|