|
| 1 | +/* |
| 2 | + * Copyright 2020 Kai Pastor |
| 3 | + * |
| 4 | + * This file is part of OpenOrienteering. |
| 5 | + * |
| 6 | + * OpenOrienteering is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * OpenOrienteering is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "kml_course_export.h" |
| 21 | + |
| 22 | +#include <QByteArray> |
| 23 | +#include <QIODevice> |
| 24 | +#include <QSaveFile> |
| 25 | + |
| 26 | +#include "mapper_config.h" |
| 27 | +#include "core/georeferencing.h" |
| 28 | +#include "core/latlon.h" |
| 29 | +#include "core/map.h" |
| 30 | +#include "core/map_coord.h" |
| 31 | +#include "core/map_part.h" |
| 32 | +#include "core/objects/object.h" |
| 33 | + |
| 34 | + |
| 35 | +namespace OpenOrienteering { |
| 36 | + |
| 37 | +KmlCourseExport::~KmlCourseExport() = default; |
| 38 | + |
| 39 | +KmlCourseExport::KmlCourseExport(const Map& map) |
| 40 | +: map(map) |
| 41 | +{} |
| 42 | + |
| 43 | + |
| 44 | +bool KmlCourseExport::canExport(const PathObject* object) |
| 45 | +{ |
| 46 | + if (!object) |
| 47 | + { |
| 48 | + error_string = tr("For KML course export, a single path object must be selected."); |
| 49 | + return false; |
| 50 | + } |
| 51 | + return true; |
| 52 | +} |
| 53 | + |
| 54 | +bool KmlCourseExport::canExport() |
| 55 | +{ |
| 56 | + return canExport(findObjectForExport()); |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +bool KmlCourseExport::doExport(const QString& filepath) |
| 61 | +{ |
| 62 | + auto const* const object = findObjectForExport(); |
| 63 | + if (!canExport(object)) |
| 64 | + { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + device = std::make_unique<QSaveFile>(filepath); |
| 69 | + if (!device->open(QIODevice::WriteOnly)) |
| 70 | + { |
| 71 | + error_string = device->errorString(); |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + writeKml(*object); |
| 76 | + if (!device->commit()) |
| 77 | + { |
| 78 | + error_string = device->errorString(); |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + return true; |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +QString KmlCourseExport::errorString() const |
| 87 | +{ |
| 88 | + return error_string; |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +const PathObject* KmlCourseExport::findObjectForExport() const |
| 93 | +{ |
| 94 | + const PathObject* path_object = nullptr; |
| 95 | + if (map.getNumSelectedObjects() == 1 |
| 96 | + && map.getFirstSelectedObject()->getType() == Object::Path) |
| 97 | + { |
| 98 | + path_object = static_cast<PathObject const*>(map.getFirstSelectedObject()); |
| 99 | + } |
| 100 | + else if (map.getNumParts() == 1 |
| 101 | + && map.getPart(0)->getNumObjects() == 1 |
| 102 | + && map.getPart(0)->getObject(0)->getType() == Object::Path) |
| 103 | + { |
| 104 | + path_object = static_cast<PathObject const*>(map.getPart(0)->getObject(0)); |
| 105 | + } |
| 106 | + return (path_object && path_object->parts().size() == 1) ? path_object : nullptr; |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +void KmlCourseExport::writeKml(const PathObject& object) |
| 111 | +{ |
| 112 | + device->write( |
| 113 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 114 | + "<!-- Generator: OpenOrienteering Mapper " APP_VERSION " -->\n" |
| 115 | + "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" |
| 116 | + "<Document>\n" |
| 117 | + " <name>Course</name>\n" |
| 118 | + " <Folder>\n" |
| 119 | + " <name>Course 1</name>\n" |
| 120 | + " <open>1</open>\n" |
| 121 | + ); |
| 122 | + writeKmlPlacemarks(object.getRawCoordinateVector()); |
| 123 | + device->write( |
| 124 | + " </Folder>\n" |
| 125 | + "</Document>\n" |
| 126 | + "</kml>\n" |
| 127 | + ); |
| 128 | + |
| 129 | +} |
| 130 | + |
| 131 | +void KmlCourseExport::writeKmlPlacemarks(const std::vector<MapCoord>& coords) |
| 132 | +{ |
| 133 | + auto next = [](auto current) { |
| 134 | + return current + (current->isCurveStart() ? 3 : 1); |
| 135 | + }; |
| 136 | + |
| 137 | + writeKmlPlacemark(coords.front(), "S1", "Start"); |
| 138 | + auto code_number = 1; |
| 139 | + for (auto current = next(coords.begin()); current != coords.end() - 1; current = next(current)) |
| 140 | + { |
| 141 | + auto const name = QByteArray::number(code_number); |
| 142 | + writeKmlPlacemark(*current, name, QByteArray("Control " + name)); |
| 143 | + ++code_number; |
| 144 | + } |
| 145 | + writeKmlPlacemark(coords.back(), "F1", "Finish"); |
| 146 | +} |
| 147 | + |
| 148 | +void KmlCourseExport::writeKmlPlacemark(const MapCoord& coord, const char* name, const char* description) |
| 149 | +{ |
| 150 | + device->write( |
| 151 | + " <Placemark>\n" |
| 152 | + " <name>" |
| 153 | + ); |
| 154 | + device->write(name); |
| 155 | + device->write("</name>\n" |
| 156 | + " <description>" |
| 157 | + ); |
| 158 | + device->write(description); |
| 159 | + device->write("</description>\n" |
| 160 | + " <Point>\n" |
| 161 | + " <coordinates>" |
| 162 | + ); |
| 163 | + writeCoordinates(map.getGeoreferencing().toGeographicCoords(MapCoordF(coord))); |
| 164 | + device->write( |
| 165 | + "</coordinates>\n" |
| 166 | + " </Point>\n" |
| 167 | + " </Placemark>\n" |
| 168 | + ); |
| 169 | +} |
| 170 | + |
| 171 | +void KmlCourseExport::writeCoordinates(const LatLon& latlon) |
| 172 | +{ |
| 173 | + device->write(QByteArray::number(latlon.longitude(), 'f', 7)); |
| 174 | + device->write(","); |
| 175 | + device->write(QByteArray::number(latlon.latitude(), 'f', 7)); |
| 176 | + device->write(",0"); |
| 177 | +} |
| 178 | + |
| 179 | + |
| 180 | +} // namespace OpenOrienteering |
0 commit comments