summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/gpssync/gpsdataparser.cpp
blob: 66d7bd0d6950553ec40f363d16cf32c3f05c47ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* ============================================================
 *
 * This file is a part of kipi-plugins project
 * http://www.kipi-plugins.org
 *
 * Date        : 2006-09-19
 * Description : GPS data file parser.
 *               (GPX format http://www.topografix.com/gpx.asp).
 *
 * Copyright (C) 2006-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * ============================================================ */

// C++ includes.

#include <cmath>
#include <cstdlib>

// TQt includes.

#include <tqstring.h>
#include <tqstringlist.h>
#include <tqfile.h>
#include <tqdom.h>
#include <tqtextstream.h>

// KDE includes.

#include <kdebug.h>

// Local includes.

#include "gpsdataparser.h"

namespace KIPIGPSSyncPlugin
{

GPSDataParser::GPSDataParser()
{
    clear();
}

void GPSDataParser::clear()
{
    m_GPSDataMap.clear();
}

int GPSDataParser::numPoints()
{
    return m_GPSDataMap.count();
}

bool GPSDataParser::matchDate(const TQDateTime& photoDateTime, int maxGapTime, int timeZone,
                              bool interpolate, int interpolationDstTime,
                              GPSDataContainer& gpsData)
{
    // GPS device are sync in time by satelite using GMT time.
    // If the camera time is different than GMT time, we need to convert it to GMT time
    // Using the time zone.
    TQDateTime cameraGMTDateTime = photoDateTime.addSecs(timeZone*(-1));

    kdDebug() << "cameraGMTDateTime: " << cameraGMTDateTime << endl;

    // We trying to find the right date in the GPS points list.
    bool findItem = false;
    int nbSecItem = maxGapTime;
    int nbSecs;

    for (GPSDataMap::Iterator it = m_GPSDataMap.begin();
         it != m_GPSDataMap.end(); ++it )
    {
        // Here we check a possible accuracy in seconds between the
        // Camera GMT time and the GPS device GMT time.

        nbSecs = abs(cameraGMTDateTime.secsTo( it.key() ));

        // We tring to find the minimal accuracy.
        if( nbSecs < maxGapTime && nbSecs < nbSecItem)
        {
            gpsData   = m_GPSDataMap[it.key()];
            findItem  = true;
            nbSecItem = nbSecs;
        }
    }

    if (findItem) return true;

    // If we can't find it, we will trying to interpolate the GPS point.

    if (interpolate)
    {
        // The interpolate GPS point will be separate by at the maximum of 'interpolationDstTime'
        // seconds before and after the next and previous real GPS point found.

        TQDateTime prevDateTime = findPrevDate(cameraGMTDateTime, interpolationDstTime);
        TQDateTime nextDateTime = findNextDate(cameraGMTDateTime, interpolationDstTime);

        if (!nextDateTime.isNull() && !prevDateTime.isNull())
        {
            GPSDataContainer prevGPSPoint = m_GPSDataMap[prevDateTime];
            GPSDataContainer nextGPSPoint = m_GPSDataMap[nextDateTime];

            double alt1 = prevGPSPoint.altitude();
            double lon1 = prevGPSPoint.longitude();
            double lat1 = prevGPSPoint.latitude();
            uint   t1   = prevDateTime.toTime_t();
            double alt2 = nextGPSPoint.altitude();
            double lon2 = nextGPSPoint.longitude();
            double lat2 = nextGPSPoint.latitude();
            uint   t2   = nextDateTime.toTime_t();
            uint   tCor = cameraGMTDateTime.toTime_t();

            if (tCor-t1 != 0)
            {
                gpsData.setAltitude(alt1  + (alt2-alt1) * (tCor-t1)/(t2-t1));
                gpsData.setLatitude(lat1  + (lat2-lat1) * (tCor-t1)/(t2-t1));
                gpsData.setLongitude(lon1 + (lon2-lon1) * (tCor-t1)/(t2-t1));
                gpsData.setInterpolated(true);
                return true;
            }
        }
    }

    return false;
}

TQDateTime GPSDataParser::findNextDate(const TQDateTime& dateTime, int secs)
{
    // We will find the item in GPS data list where the time is
    // at the maximum bigger than 'secs' mn of the value to match.
    TQDateTime itemFound = dateTime.addSecs(secs);
    bool found = false;

    for (GPSDataMap::Iterator it = m_GPSDataMap.begin();
        it != m_GPSDataMap.end(); ++it )
    {
        if (it.key() > dateTime)
        {
            if (it.key() < itemFound)
            {
                itemFound = it.key();
                found = true;
            }
        }
    }

    if (found)
        return itemFound;

    return TQDateTime();
}

TQDateTime GPSDataParser::findPrevDate(const TQDateTime& dateTime, int secs)
{
    // We will find the item in GPS data list where the time is
    // at the maximum smaller than 'secs' mn of the value to match.
    TQDateTime itemFound = dateTime.addSecs((-1)*secs);
    bool found = false;

    for (GPSDataMap::Iterator it = m_GPSDataMap.begin();
        it != m_GPSDataMap.end(); ++it )
    {
        if (it.key() < dateTime)
        {
            if (it.key() > itemFound)
            {
                itemFound = it.key();
                found = true;
            }
        }
    }

    if (found)
        return itemFound;

    return TQDateTime();
}

bool GPSDataParser::loadGPXFile(const KURL& url)
{
    TQFile gpxfile(url.path());

    if (!gpxfile.open(IO_ReadOnly))
        return false;

    TQDomDocument gpxDoc("gpx");
    if (!gpxDoc.setContent(&gpxfile))
        return false;

    TQDomElement gpxDocElem = gpxDoc.documentElement();
    if (gpxDocElem.tagName()!="gpx")
        return false;

    for (TQDomNode nTrk = gpxDocElem.firstChild();
         !nTrk.isNull(); nTrk = nTrk.nextSibling())
    {
        TQDomElement trkElem = nTrk.toElement();
        if (trkElem.isNull()) continue;
        if (trkElem.tagName() != "trk") continue;

        for (TQDomNode nTrkseg = trkElem.firstChild();
            !nTrkseg.isNull(); nTrkseg = nTrkseg.nextSibling())
        {
            TQDomElement trksegElem = nTrkseg.toElement();
            if (trksegElem.isNull()) continue;
            if (trksegElem.tagName() != "trkseg") continue;

            for (TQDomNode nTrkpt = trksegElem.firstChild();
                !nTrkpt.isNull(); nTrkpt = nTrkpt.nextSibling())
            {
                TQDomElement trkptElem = nTrkpt.toElement();
                if (trkptElem.isNull()) continue;
                if (trkptElem.tagName() != "trkpt") continue;

                TQDateTime ptDateTime;
                double    ptAltitude  = 0.0;
                double    ptLatitude  = 0.0;
                double    ptLongitude = 0.0;

                // Get GPS position. If not available continue to next point.
                TQString lat = trkptElem.attribute("lat");
                TQString lon = trkptElem.attribute("lon");
                if (lat.isEmpty() || lon.isEmpty()) continue;

                ptLatitude  = lat.toDouble();
                ptLongitude = lon.toDouble();

                // Get metadata of track point (altitude and time stamp)
                for (TQDomNode nTrkptMeta = trkptElem.firstChild();
                    !nTrkptMeta.isNull(); nTrkptMeta = nTrkptMeta.nextSibling())
                {
                    TQDomElement trkptMetaElem = nTrkptMeta.toElement();
                    if (trkptMetaElem.isNull()) continue;
                    if (trkptMetaElem.tagName() == TQString("time"))
                    {
                        // Get GPS point time stamp. If not available continue to next point.
                        TQString time = trkptMetaElem.text();
                        if (time.isEmpty()) continue;
                        ptDateTime = TQDateTime::fromString(time, Qt::ISODate);
                    }
                    if (trkptMetaElem.tagName() == TQString("ele"))
                    {
                        // Get GPS point altitude. If not available continue to next point.
                        TQString ele = trkptMetaElem.text();
                        if (!ele.isEmpty())
                            ptAltitude  = ele.toDouble();
                    }
                }

                if (ptDateTime.isNull())
                    continue;

                GPSDataContainer gpsData(ptAltitude, ptLatitude, ptLongitude, false);
                m_GPSDataMap.insert( ptDateTime, gpsData );
            }
        }
    }

    kdDebug( 51001 ) << "GPX File " << url.fileName()
                     << " parsed with " << numPoints()
                     << " points extracted" << endl;
    return true;
}

} // NameSpace KIPIGPSSyncPlugin