File indexing completed on 2025-03-09 03:50:40

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2006-05-16
0007  * Description : a tool to export GPS data to KML file.
0008  *
0009  * SPDX-FileCopyrightText: 2006-2007 by Stephane Pontier <shadow dot walker at free dot fr>
0010  * SPDX-FileCopyrightText: 2008-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "kmlgpsdataparser.h"
0017 
0018 // KDE includes
0019 
0020 #include <klocalizedstring.h>
0021 
0022 namespace DigikamGenericGeolocationEditPlugin
0023 {
0024 
0025 KMLGeoDataParser::KMLGeoDataParser()
0026     : GeoDataParser(),
0027       kmlDocument  (nullptr)
0028 {
0029 }
0030 
0031 KMLGeoDataParser::~KMLGeoDataParser()
0032 {
0033 }
0034 
0035 QString KMLGeoDataParser::lineString()
0036 {
0037     QString line = QLatin1String("");
0038 
0039     // cache the end to not recalculate it with large number of points
0040 
0041     GeoDataMap::ConstIterator end (m_GeoDataMap.constEnd());
0042 
0043     for (GeoDataMap::ConstIterator it = m_GeoDataMap.constBegin() ; it != end ; ++it)
0044     {
0045         line += QString::fromUtf8("%1,%2,%3 ").arg(it.value().longitude()).arg(it.value().latitude()).arg(it.value().altitude());
0046     }
0047 
0048     return line;
0049 }
0050 
0051 void KMLGeoDataParser::CreateTrackLine(QDomElement& parent, QDomDocument& root, int altitudeMode)
0052 {
0053     kmlDocument = &root;
0054 
0055     // add the linetrack
0056 
0057     QDomElement kmlPlacemark  = addKmlElement(parent, QLatin1String("Placemark"));
0058     addKmlTextElement(kmlPlacemark, QLatin1String("name"), i18nc("@item: linetrack over the map", "Track"));
0059     QDomElement kmlLineString = addKmlElement(kmlPlacemark, QLatin1String("LineString"));
0060     addKmlTextElement(kmlLineString, QLatin1String("coordinates"), lineString());
0061     addKmlTextElement(kmlPlacemark, QLatin1String("styleUrl"), QLatin1String("#linetrack"));
0062 
0063     if      (altitudeMode == 2)
0064     {
0065         addKmlTextElement(kmlLineString, QLatin1String("altitudeMode"), QLatin1String("absolute"));
0066     }
0067     else if (altitudeMode == 1)
0068     {
0069         addKmlTextElement(kmlLineString, QLatin1String("altitudeMode"), QLatin1String("relativeToGround"));
0070     }
0071     else
0072     {
0073         addKmlTextElement(kmlLineString, QLatin1String("altitudeMode"), QLatin1String("clampToGround"));
0074     }
0075 }
0076 
0077 void KMLGeoDataParser::CreateTrackPoints(QDomElement& parent, QDomDocument& root, int timeZone, int altitudeMode)
0078 {
0079     kmlDocument = &root;
0080 
0081     // create the points
0082 
0083     QDomElement kmlPointsFolder = addKmlElement(parent, QLatin1String("Folder"));
0084     addKmlTextElement(kmlPointsFolder, QLatin1String("name"),       i18nc("@item: points over the map", "Points"));
0085     addKmlTextElement(kmlPointsFolder, QLatin1String("visibility"), QLatin1String("0"));
0086     addKmlTextElement(kmlPointsFolder, QLatin1String("open"),       QLatin1String("0"));
0087     int i                       = 0;
0088 
0089     // cache the end to not recalculate it with large number of points
0090 
0091     GeoDataMap::ConstIterator end (m_GeoDataMap.constEnd());
0092 
0093     for (GeoDataMap::ConstIterator it = m_GeoDataMap.constBegin() ; it != end ; ++it, ++i)
0094     {
0095         QDomElement kmlPointPlacemark = addKmlElement(kmlPointsFolder, QLatin1String("Placemark"));
0096         addKmlTextElement(kmlPointPlacemark, QLatin1String("name"), QString::fromUtf8("%1 %2 ")
0097                                                                     .arg(i18nc("@item: point coordinates", "Point"))
0098                                                                     .arg(i));
0099         addKmlTextElement(kmlPointPlacemark, QLatin1String("styleUrl"), QLatin1String("#track"));
0100         QDomElement kmlTimeStamp      = addKmlElement(kmlPointPlacemark, QLatin1String("TimeStamp"));
0101 
0102         // GPS device are sync in time by satellite using GMT time.
0103         // If the camera time is different than GMT time, we want to
0104         // convert the GPS time to localtime of the picture to be display
0105         // in the same timeframe
0106 
0107         QDateTime GPSLocalizedTime    = it.key().addSecs(timeZone*3600);
0108 
0109         addKmlTextElement(kmlTimeStamp, QLatin1String("when"), GPSLocalizedTime.toString(QLatin1String("yyyy-MM-ddThh:mm:ssZ")));
0110         QDomElement kmlGeometry       = addKmlElement(kmlPointPlacemark, QLatin1String("Point"));
0111         addKmlTextElement(kmlPointPlacemark, QLatin1String("visibility"), QLatin1String("0"));
0112 
0113         if (it.value().latitude())
0114         {
0115             addKmlTextElement(kmlGeometry, QLatin1String("coordinates"),
0116                               QString::fromUtf8("%1,%2,%3 ")
0117                               .arg(it.value().longitude()).arg(it.value().latitude()).arg(it.value().altitude()));
0118         }
0119         else
0120         {
0121             addKmlTextElement(kmlGeometry, QLatin1String("coordinates"), QString::fromUtf8("%1,%2 ").arg(it.value().longitude()).arg(it.value().latitude()));
0122         }
0123 
0124         if      (altitudeMode == 2 )
0125         {
0126             addKmlTextElement(kmlGeometry, QLatin1String("altitudeMode"), QLatin1String("absolute"));
0127         }
0128         else if (altitudeMode == 1 )
0129         {
0130             addKmlTextElement(kmlGeometry, QLatin1String("altitudeMode"), QLatin1String("relativeToGround"));
0131         }
0132         else
0133         {
0134             addKmlTextElement(kmlGeometry, QLatin1String("altitudeMode"), QLatin1String("clampToGround"));
0135         }
0136     }
0137 }
0138 
0139 } // namespace DigikamGenericGeolocationEditPlugin