How to summarize, group 4000+ GPX Trackpoints as Tracks named for by each days date?

余生长醉 提交于 2019-12-14 02:58:23

问题


the XSLT below extracts all Trackpoints (trkpt) only from a Garmin GPX-file (omiting any Waypoints) and exports the result as one (1) track into a new GPX-file ready for MapSource or Basecamp. Currently the name of that one track is the date of the first Trackpoint.

How can I achieve to have one track per day named for by each date (YYYY-MM-DD), where each track includes all its corresponding trackpoints?

(I believe that the date change in trkpt/time must be taken into account, but I am lost here).

The GPX-sourcefile can be any larger GPX-file created by Garmin (don't know about others). My source-file spans over 3 months of travel with trackpoint records taken every four minutes (total of 7633 trkpt during 75 days). Of course, the device was turned of while sleeping. Below please see the XSLT-code of the file named as: 'extract-TRKPTs.xsl'

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:gpx="http://www.topografix.com/GPX/1/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<xsl:output method="text" indent="yes" encoding="utf-8" />
<!-- command: msxsl.exe 20101008.gpx extract-TRKPTs.xsl > 20101008-trkpts.gpx -->
<!-- extracts all trackpoints only from a given Garmin GPX file -->
<!-- output is piped into a new .gpx file -->
<xsl:template match="/">&#60;<xsl:text>?xml version="1.0" encoding="utf-8" standalone="no" ?&#62;</xsl:text>
&#60;<xsl:text>gpx xmlns="http://www.topografix.com/GPX/1/1" creator="MapSource 6.16.2" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"</xsl:text>&#62;
<gpx>
<trk>&#60;trk&#62;
&#60;name&#62;Trackpoints: <xsl:value-of select="substring(/gpx:gpx/gpx:trk/gpx:trkseg/gpx:trkpt/gpx:time,1,10)"/>&#60;/name&#62;
<trkseg>&#60;trkseg&#62;
    <xsl:for-each select="/gpx:gpx/gpx:trk/gpx:trkseg/gpx:trkpt">
    <xsl:value-of select="gpx:trkpt"/><trkpt>
        &#60;<xsl:text>trkpt lat="</xsl:text><xsl:value-of select="@lat"/><xsl:text>" lon="</xsl:text><xsl:value-of select="@lon"/><xsl:text>"</xsl:text>&#62;
        &#60;ele&#62;<xsl:value-of select="gpx:ele"/>&#60;/ele&#62;
        &#60;time&#62;<xsl:value-of select="gpx:time"/>&#60;/time&#62;
        &#60;/trkpt&#62;</trkpt>
    </xsl:for-each>
</trkseg>&#60;/trkseg&#62;
</trk>&#60;/trk&#62;
</gpx>&#60;/gpx&#62;
</xsl:template>
</xsl:stylesheet>

The code looks ugly because it's set to output 'text' hence I needed to use special-character-entities. But hey, so far it works. Sorry, I am an XSLT-novice. Thanks for feedback.

Current output is as follows (all trackpoints in one single track):

<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="MapSource 6.16.2" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<trk>
<name>Trackpoints: 2010-09-21</name>
<trkseg>
    <trkpt lat="34.004124635830522" lon="-118.47464606165886">
    <ele>55.4130859375</ele>
    <time>2010-09-21T19:51:12Z</time>
    </trkpt>
    <trkpt lat="34.004120444878936" lon="-118.47464254125953">
    <ele>55.4130859375</ele>
    <time>2010-09-21T19:51:26Z</time>
    </trkpt>
    <trkpt lat="34.004129162058234" lon="-118.47465343773365">
    <ele>56.8551025390625</ele>
    <time>2010-09-22T21:02:55Z</time>
    </trkpt>
...
...
    <trkpt lat="37.042149296030402" lon="-112.52694656141102">
    <ele>1506.523193359375</ele>
    <time>2010-12-01T07:01:06Z</time>
    </trkpt>
    <trkpt lat="37.042256919667125" lon="-112.52645093947649">
    <ele>1504.1201171875</ele>
    <time>2010-12-01T07:01:23Z</time>
    </trkpt>
</trkseg>
</trk>
</gpx>

The expected output is as follows (please note: one track per day).

<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="MapSource 6.16.2" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<trk>
<name>Trackpoints: 2010-09-21</name>
<trkseg>
    <trkpt lat="34.004124635830522" lon="-118.47464606165886">
    <ele>55.4130859375</ele>
    <time>2010-09-21T19:51:12Z</time>
    </trkpt>
    <trkpt lat="34.004120444878936" lon="-118.47464254125953">
    <ele>55.4130859375</ele>
    <time>2010-09-21T19:51:26Z</time>
    </trkpt>
    <trkpt lat="34.004129162058234" lon="-118.47465343773365">
    <ele>56.8551025390625</ele>
    <time>2010-09-22T21:02:55Z</time>
    </trkpt>
</trkseg>
</trk>
...
...
<trk>
<name>Trackpoints: 2010-12-01</name>
<trkseg>

    <trkpt lat="37.042149296030402" lon="-112.52694656141102">
    <ele>1506.523193359375</ele>
    <time>2010-12-01T07:01:06Z</time>
    </trkpt>
    <trkpt lat="37.042256919667125" lon="-112.52645093947649">
    <ele>1504.1201171875</ele>
    <time>2010-12-01T07:01:23Z</time>
    </trkpt>
</trkseg>
</trk>
</gpx>

回答1:


You should be able to do this using Muenchian Grouping.

Since you didn't supply example input, I made my own (modified version of the example here).

XML Input

<gpx xmlns="http://www.topografix.com/GPX/1/1" 
    xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
    xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" 
    creator="Oregon 400t" 
    version="1.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd">
    <metadata>
        <link href="http://www.garmin.com">
            <text>Garmin International</text>
        </link>
        <time>2009-10-17T22:58:43Z</time>
    </metadata>
    <trk>
        <name>Example 1</name>
        <trkseg>
            <trkpt lat="47.644548" lon="-122.326897">
                <ele>4.46</ele>
                <time>2009-10-17T18:37:26Z</time>
            </trkpt>
            <trkpt lat="47.644548" lon="-122.326898">
                <ele>4.47</ele>
                <time>2009-10-17T18:38:26Z</time>
            </trkpt>
            <trkpt lat="47.644548" lon="-122.326898">
                <ele>4.48</ele>
                <time>2009-10-18T18:39:26Z</time>
            </trkpt>
        </trkseg>
    </trk>
    <trk>
        <name>Example 2</name>
        <trkseg>
            <trkpt lat="47.644548" lon="-122.326899">
                <ele>4.49</ele>
                <time>2009-10-18T18:37:26Z</time>
            </trkpt>
            <trkpt lat="47.644548" lon="-122.326900">
                <ele>4.50</ele>
                <time>2009-10-18T18:38:26Z</time>
            </trkpt>
            <trkpt lat="47.644548" lon="-122.326901">
                <ele>4.51</ele>
                <time>2009-10-19T18:39:26Z</time>
            </trkpt>
        </trkseg>    
    </trk>
</gpx>

XSLT 1.0

<xsl:stylesheet version="1.0" 
    xmlns="http://www.topografix.com/GPX/1/1" 
    xmlns:gpx="http://www.topografix.com/GPX/1/1" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="gpx">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="date" match="gpx:trkpt" use="substring(gpx:time,1,10)"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/*">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each select="//gpx:trkpt[generate-id(key('date',substring(gpx:time,1,10))[1])=generate-id()]">
                <trk>
                    <name>Trackpoints: <xsl:value-of select="substring(gpx:time,1,10)"/></name>
                    <trkseg>
                        <xsl:for-each select="key('date',substring(gpx:time,1,10))">
                            <xsl:copy-of select="."/>
                        </xsl:for-each>
                    </trkseg>
                </trk>
            </xsl:for-each>
        </xsl:copy>        
    </xsl:template>

</xsl:stylesheet>

XML Output

<gpx xmlns="http://www.topografix.com/GPX/1/1"
     xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
     xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     creator="Oregon 400t"
     version="1.1"
     xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd">
   <trk>
      <name>Trackpoints: 2009-10-17</name>
      <trkseg>
         <trkpt lat="47.644548" lon="-122.326897">
            <ele>4.46</ele>
            <time>2009-10-17T18:37:26Z</time>
         </trkpt>
         <trkpt lat="47.644548" lon="-122.326898">
            <ele>4.47</ele>
            <time>2009-10-17T18:38:26Z</time>
         </trkpt>
      </trkseg>
   </trk>
   <trk>
      <name>Trackpoints: 2009-10-18</name>
      <trkseg>
         <trkpt lat="47.644548" lon="-122.326898">
            <ele>4.48</ele>
            <time>2009-10-18T18:39:26Z</time>
         </trkpt>
         <trkpt lat="47.644548" lon="-122.326899">
            <ele>4.49</ele>
            <time>2009-10-18T18:37:26Z</time>
         </trkpt>
         <trkpt lat="47.644548" lon="-122.326900">
            <ele>4.50</ele>
            <time>2009-10-18T18:38:26Z</time>
         </trkpt>
      </trkseg>
   </trk>
   <trk>
      <name>Trackpoints: 2009-10-19</name>
      <trkseg>
         <trkpt lat="47.644548" lon="-122.326901">
            <ele>4.51</ele>
            <time>2009-10-19T18:39:26Z</time>
         </trkpt>
      </trkseg>
   </trk>
</gpx>


来源:https://stackoverflow.com/questions/25510509/how-to-summarize-group-4000-gpx-trackpoints-as-tracks-named-for-by-each-days-d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!