/
gpx2kml.xsl
195 lines (178 loc) · 6.03 KB
/
gpx2kml.xsl
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
<?xml version="1.0" encoding="UTF-8"?>
<!-- gpx2kml.xsl: Copyright (C) 2013 anineco@nifty.com -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:gpx="http://www.topografix.com/GPX/1/1"
xmlns:kashmir3d="http://www.kashmir3d.com/namespace/kashmir3d"
xmlns:date="http://exslt.org/dates-and-times"
xmlns:exsl="http://exslt.org/common"
xmlns:str="http://exslt.org/strings"
xmlns:tmp="urn:tmp"
extension-element-prefixes="date exsl str"
exclude-result-prefixes="gpx kashmir3d tmp">
<xsl:output method="xml" encoding="UTF-8" indent="yes"
cdata-section-elements="description"/>
<xsl:param name="VERSION">GPX2KML Ver.1.5</xsl:param>
<xsl:param name="ALPHA">b3</xsl:param>
<xsl:param name="ICONLUT">iconlut.xml</xsl:param>
<xsl:param name="iconlut" select="document($ICONLUT,/)/iconlut"/>
<xsl:variable name="tmp">
<xsl:for-each select="//gpx:wpt//kashmir3d:icon|//gpx:rte/gpx:rtept//kashmir3d:icon[.!='903001']">
<xsl:sort select="."/>
<tmp:icon><xsl:value-of select="."/></tmp:icon>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="icons" select="exsl:node-set($tmp)"/>
<xsl:template match="/">
<xsl:comment><xsl:value-of select="concat($VERSION,' ',date:date-time())"/></xsl:comment>
<xsl:apply-templates select="gpx:gpx"/>
</xsl:template>
<xsl:template match="gpx:gpx">
<kml>
<Document>
<xsl:apply-templates select="gpx:metadata"/>
<xsl:apply-templates select="gpx:rte|gpx:trk" mode="LineStyle"/>
<xsl:for-each select="$icons/tmp:icon">
<xsl:if test="not(preceding-sibling::tmp:icon[.=current()])">
<xsl:call-template name="IconStyle">
<xsl:with-param name="icon" select="$iconlut/icon[@code=current()]"/>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
<Folder>
<name>Route</name>
<xsl:apply-templates select="gpx:rte" mode="LineString"/>
</Folder>
<Folder>
<name>Track</name>
<xsl:apply-templates select="gpx:trk" mode="LineString"/>
</Folder>
<Folder>
<name>Waypoint</name>
<xsl:apply-templates select="gpx:wpt|gpx:rte/gpx:rtept[.//kashmir3d:icon!='903001']"/>
</Folder>
</Document>
</kml>
</xsl:template>
<xsl:template name="IconStyle">
<xsl:param name="icon"/>
<xsl:param name="id" select="concat('N',$icon/@code)"/>
<Style id="{$id}">
<IconStyle>
<scale>1</scale>
<Icon>
<href>
<xsl:value-of select="$icon/@src"/>
<xsl:if test="starts-with($icon/@src,'http:')">
<xsl:value-of select="concat('#',$id,'.',$icon/@size,'.',$icon/@size)"/>
</xsl:if>
</href>
</Icon>
<hotSpot x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
</IconStyle>
<LabelStyle>
<color>ffffffff</color>
<scale>0.75</scale>
</LabelStyle>
</Style>
</xsl:template>
<xsl:template match="gpx:metadata">
<name><xsl:value-of select="gpx:name"/></name>
<Snippet><xsl:value-of select="concat('作成日 ',gpx:time)"/></Snippet>
<xsl:apply-templates select="gpx:author"/>
</xsl:template>
<xsl:template match="gpx:author">
<atom:author>
<atom:name><xsl:value-of select="gpx:name"/></atom:name>
</atom:author>
<atom:link href="{gpx:link/@href}"/>
</xsl:template>
<xsl:template match="gpx:rte|gpx:trk" mode="LineStyle">
<Style id="{generate-id()}">
<LineStyle>
<xsl:apply-templates select="gpx:extensions/kashmir3d:line_color"/>
<xsl:apply-templates select="gpx:extensions/kashmir3d:line_size"/>
</LineStyle>
</Style>
</xsl:template>
<xsl:template match="kashmir3d:line_color">
<color><xsl:value-of select="concat($ALPHA,.)"/></color>
</xsl:template>
<xsl:template match="kashmir3d:line_size">
<width><xsl:value-of select="."/></width>
</xsl:template>
<xsl:template match="gpx:rte" mode="LineString">
<Placemark>
<name><xsl:value-of select="gpx:name"/></name>
<styleUrl><xsl:value-of select="concat('#',generate-id())"/></styleUrl>
<xsl:call-template name="LineString"/>
</Placemark>
</xsl:template>
<xsl:template match="gpx:trk" mode="LineString">
<xsl:for-each select="gpx:trkseg">
<Placemark>
<name><xsl:value-of select="../gpx:name"/></name>
<styleUrl><xsl:value-of select="concat('#',generate-id(..))"/></styleUrl>
<xsl:call-template name="LineString"/>
</Placemark>
</xsl:for-each>
</xsl:template>
<xsl:template name="LineString">
<LineString>
<coordinates>
<xsl:for-each select="gpx:rtept|gpx:trkpt">
<xsl:value-of select="concat(
format-number(@lon,'#.000000'),',',
format-number(@lat,'#.000000'),',',
format-number(gpx:ele,'#.00'),' '
)"/>
</xsl:for-each>
</coordinates>
</LineString>
</xsl:template>
<xsl:template match="gpx:wpt|gpx:rtept">
<Placemark>
<name><xsl:value-of select="gpx:name"/></name>
<xsl:apply-templates select="gpx:cmt[not(.='' or .='*=' or .=' = ')]"/>
<xsl:apply-templates select="gpx:extensions/kashmir3d:icon"/>
<Point>
<coordinates>
<xsl:value-of select="concat(
format-number(@lon,'#.000000'),',',
format-number(@lat,'#.000000'),',',
format-number(gpx:ele,'#.00')
)"/>
</coordinates>
</Point>
</Placemark>
</xsl:template>
<xsl:template match="gpx:cmt">
<xsl:param name="c" select="str:split(.,',')"/>
<description>
<xsl:text><![CDATA[<table>]]></xsl:text>
<xsl:for-each select="$c[not(starts-with(.,'@'))]">
<xsl:text><![CDATA[<tr><td>]]></xsl:text>
<xsl:value-of select="substring-before(.,'=')"/>
<xsl:text><![CDATA[</td><td>]]></xsl:text>
<xsl:choose>
<xsl:when test="substring-before(.,'=')='URL'">
<xsl:value-of select="concat('<a href="',
substring-after(.,'='),'">',
substring-after(.,'='),'</a>'
)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-after(.,'=')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text><![CDATA[</td></tr>]]></xsl:text>
</xsl:for-each>
<xsl:text><![CDATA[</table>]]></xsl:text>
</description>
</xsl:template>
<xsl:template match="kashmir3d:icon">
<xsl:param name="icon" select="$iconlut/icon[@code=current()]"/>
<styleUrl><xsl:value-of select="concat('#N',.)"/></styleUrl>
</xsl:template>
</xsl:stylesheet>