Project

General

Profile

1
<?xml version="1.0" encoding="UTF-8"?>
2
<xsl:stylesheet version="1.0" 
3
    xmlns:wmc="http://www.opengis.net/context" 
4
    xmlns:wms="http://www.opengis.net/wms"
5
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
6
    xmlns:xlink="http://www.w3.org/1999/xlink">
7
<!--
8
Description: Convert a WMS Capabilities document to a Web Map Context
9
Author:      Nedjo Rogers nedjo AT gworks.ca
10
Licence:     LGPL as per: http://www.gnu.org/copyleft/lesser.html
11

    
12
$Id$
13
$Name$
14

    
15
-->
16
  <xsl:output method="xml" indent="yes"/>
17

    
18
  <xsl:param name="width">400</xsl:param>
19
  <xsl:param name="selectedLayer"/>
20
  <xsl:param name="styleName">composite</xsl:param>
21
  
22

    
23
  <xsl:template match="/WMT_MS_Capabilities">
24
    <xsl:param name="wmsVersion" select="@version"/>    
25
    <xsl:param name="wmsTitle" select="Service/Title"/>    
26
    <xsl:param name="wmsOnlineResource" select="Capability/Request/GetMap/DCPType/HTTP/Get/OnlineResource/@xlink:href"/>    
27
    <ViewContext version="1.0.0"
28
			    id="autoGeneratedContext_"
29
          xmlns="http://www.opengis.net/context">
30
      <General>
31
        <Title><xsl:value-of select="$wmsTitle"/></Title>
32
        <!-- TBD: The following should be extracted -->
33
        <xsl:variable name="minx" select="number(Capability/Layer/LatLonBoundingBox/@minx)"/>
34
        <xsl:variable name="maxx" select="number(Capability/Layer/LatLonBoundingBox/@maxx)"/>
35
        <xsl:variable name="miny" select="number(Capability/Layer/LatLonBoundingBox/@miny)"/>
36
        <xsl:variable name="maxy" select="number(Capability/Layer/LatLonBoundingBox/@maxy)"/>
37
        <xsl:variable name="dy" select=" $maxy - $miny "/>
38
        <xsl:variable name="dx" select=" $maxx - $minx "/>
39
        <xsl:variable name="height" select="round(($dy div $dx)*$width)"/>
40
        <Window width="{$width}" height="{$height}"/>
41
        <BoundingBox SRS="EPSG:4326" minx="{$minx}" miny="{$miny}" maxx="{$maxx}" maxy="{$maxy}"/>
42
      </General>
43
      <LayerList>
44
        <xsl:choose><xsl:when test="string-length($selectedLayer)>0">
45
          <xsl:apply-templates select="Capability/Layer/Layer[Name=$selectedLayer]">
46
            <xsl:with-param name="version" select="$wmsVersion"/>
47
            <xsl:with-param name="title" select="$wmsTitle"/>
48
            <xsl:with-param name="url" select="$wmsOnlineResource"/>
49
          </xsl:apply-templates>
50
        </xsl:when><xsl:otherwise>
51
          <xsl:apply-templates select="Capability/Layer/Layer">
52
            <xsl:with-param name="version" select="$wmsVersion"/>
53
            <xsl:with-param name="title" select="$wmsTitle"/>
54
            <xsl:with-param name="url" select="$wmsOnlineResource"/>
55
          </xsl:apply-templates>
56
        </xsl:otherwise></xsl:choose>
57
      </LayerList>
58
    </ViewContext>
59
  </xsl:template>
60

    
61
  <!-- Layer -->
62
  <xsl:template match="Layer">
63
    <xsl:param name="version"/>
64
    <xsl:param name="title"/>
65
    <xsl:param name="url"/>
66
    <xsl:element name="Layer" namespace="http://www.opengis.net/context">
67
      <xsl:attribute name="queryable">
68
        <xsl:value-of select="@queryable"/>
69
      </xsl:attribute>
70
      <xsl:attribute name="hidden">
71
        <xsl:text>0</xsl:text>
72
      </xsl:attribute>
73
      <xsl:element name="Server" namespace="http://www.opengis.net/context">
74
        <xsl:attribute name="service">
75
          <xsl:text>OGC:WMS</xsl:text>
76
        </xsl:attribute>
77
        <xsl:attribute name="version">
78
          <xsl:value-of select="$version"/>
79
        </xsl:attribute>
80
        <xsl:attribute name="title">
81
          <xsl:value-of select="$title"/>
82
        </xsl:attribute>
83
        <xsl:element name="OnlineResource" namespace="http://www.opengis.net/context">
84
          <xsl:attribute name="type">
85
            <xsl:text>simple</xsl:text>
86
          </xsl:attribute>
87
          <xsl:attribute name="xlink:href">
88
            <xsl:value-of select="$url"/>
89
          </xsl:attribute>
90
        </xsl:element>
91
      </xsl:element>
92
      <Name xmlns="http://www.opengis.net/context"><xsl:value-of select="Name"/></Name>
93
      <Title xmlns="http://www.opengis.net/context"><xsl:value-of select="Title"/></Title>
94
      <Abstract xmlns="http://www.opengis.net/context"><xsl:value-of select="Abstract"/></Abstract>
95
      <FormatList xmlns="http://www.opengis.net/context">
96
        <Format current="1" xmlns="http://www.opengis.net/context">image/png</Format>
97
      </FormatList>
98
      <StyleList xmlns="http://www.opengis.net/context">
99
        <Style xmlns="http://www.opengis.net/context" current="1">
100
          <xsl:copy-of select="Style[Name=$styleName]/*"/>
101
        </Style>
102
      </StyleList>
103
      <xsl:if test="Dimension">
104
        <DimensionList xmlns="http://www.opengis.net/context">
105
          <xsl:copy-of select="Dimension"/>
106
        </DimensionList>
107
      </xsl:if>
108
    </xsl:element>
109
  </xsl:template>
110
  
111
  <!-- duplicate everything with namespace prefix for WMS 1.3.x -->
112
  <xsl:template match="/wms:WMS_Capabilities">
113
    <xsl:param name="wmsVersion" select="/wms:WMS_Capabilities/@version"/>    
114
    <xsl:param name="wmsTitle" select="/wms:WMS_Capabilities/wms:Service/wms:Title"/>    
115
    <xsl:param name="wmsOnlineResource" select="/wms:WMS_Capabilities/wms:Capability/wms:Request/wms:GetMap/wms:DCPType/wms:HTTP/wms:Get/wms:OnlineResource/@xlink:href"/>    
116
  
117
    <xsl:param name="defLayers" select="wms:Capability//wms:Layer[wms:Name=$selectedLayer]"/>    
118
    <xsl:param name="defLayer" select="$defLayers[1]"/>
119
    <xsl:variable name="abstract" select="$defLayer/wms:Abstract"/>
120
    <xsl:variable name="title" select="$defLayer/wms:Title"/>
121
    <xsl:variable name="infoLink" select="$defLayer/wms:MetadataURL/wms:OnlineResource/@xlink:href"/>
122
    <xsl:variable name="logoLink" select="$defLayer/wms:Attribution/wms:LogoURL/wms:OnlineResource/@xlink:href"/>
123
    <xsl:variable name="providerName" select="wms:Service/wms:Title"/>
124
    <ViewContext version="1.0.0"
125
			    id="autoGeneratedContext_"
126
          xmlns="http://www.opengis.net/context">
127
      <General>
128
        <Title><xsl:value-of select="$title"/></Title>
129
        <Abstract><xsl:value-of select="$abstract"/></Abstract>
130
        <xsl:variable name="bbox" select="$defLayer/wms:BoundingBox"/>
131
        <xsl:variable name="minx" select="number($bbox/@minx)"/>
132
        <xsl:variable name="maxx" select="number($bbox/@maxx)"/>
133
        <xsl:variable name="miny" select="number($bbox/@miny)"/>
134
        <xsl:variable name="maxy" select="number($bbox/@maxy)"/>
135
        <xsl:variable name="srs" select="$bbox/@CRS"/>
136
        <xsl:choose><xsl:when test="$defLayer/@fixedWidth">
137
          <xsl:variable name="fWidth" select="$defLayer/@fixedWidth"/>
138
          <xsl:variable name="fHeight" select="$defLayer/@fixedHeight"/>
139
          <Window width="{$fWidth}" height="{$fHeight}"/>
140
        </xsl:when><xsl:otherwise>
141
          <xsl:variable name="dy" select=" $maxy - $miny "/>
142
          <xsl:variable name="dx" select=" $maxx - $minx "/>
143
          <xsl:variable name="height" select="round(($dy div $dx)*$width)"/>
144
          <Window width="{$width}" height="{$height}"/>
145
        </xsl:otherwise></xsl:choose>
146
        <BoundingBox SRS="{$srs}" minx="{$minx}" miny="{$miny}" maxx="{$maxx}" maxy="{$maxy}"/>
147
        <DescriptionURL format="text/html">
148
            <OnlineResource xlink:href="{$infoLink}" xlink:type="simple"/>
149
        </DescriptionURL>
150
        <LogoURL format="text/html">
151
            <OnlineResource xlink:href="{$logoLink}" xlink:type="simple"/>
152
        </LogoURL>
153
        <ContactInformation>
154
          <ContactPersonPrimary>
155
            <ContactOrganization><xsl:value-of select="$providerName"/></ContactOrganization>
156
          </ContactPersonPrimary>
157
        </ContactInformation>
158
        
159
      </General>
160
      <LayerList>
161
        <xsl:choose><xsl:when test="string-length($selectedLayer)>0">
162
          <xsl:apply-templates select="$defLayer">
163
            <xsl:with-param name="version" select="$wmsVersion"/>
164
            <xsl:with-param name="title" select="$wmsTitle"/>
165
            <xsl:with-param name="url" select="$wmsOnlineResource"/>
166
          </xsl:apply-templates>
167
        </xsl:when><xsl:otherwise>
168
          <xsl:apply-templates select="wms:Capability/wms:Layer/*">
169
            <xsl:with-param name="version" select="$wmsVersion"/>
170
            <xsl:with-param name="title" select="$wmsTitle"/>
171
            <xsl:with-param name="url" select="$wmsOnlineResource"/>
172
          </xsl:apply-templates>
173
        </xsl:otherwise></xsl:choose>
174
      </LayerList>
175
    </ViewContext>
176
  </xsl:template>
177

    
178
  <!-- Layer -->
179
  <xsl:template match="wms:Layer">
180
    <xsl:param name="version"/>
181
    <xsl:param name="title"/>
182
    <xsl:param name="url"/>
183
    <xsl:element name="Layer" namespace="http://www.opengis.net/context">
184
      <xsl:attribute name="queryable">
185
        <xsl:value-of select="@queryable"/>
186
      </xsl:attribute>
187
      <xsl:attribute name="hidden">
188
        <xsl:text>0</xsl:text>
189
      </xsl:attribute>
190
      <xsl:element name="Server" namespace="http://www.opengis.net/context">
191
        <xsl:attribute name="service">
192
          <xsl:text>OGC:WMS</xsl:text>
193
        </xsl:attribute>
194
        <xsl:attribute name="version">
195
          <xsl:value-of select="$version"/>
196
        </xsl:attribute>
197
        <xsl:attribute name="title">
198
          <xsl:value-of select="$title"/>
199
        </xsl:attribute>
200
        <xsl:element name="OnlineResource" namespace="http://www.opengis.net/context">
201
          <xsl:attribute name="type">
202
            <xsl:text>simple</xsl:text>
203
          </xsl:attribute>
204
          <xsl:attribute name="xlink:href">
205
            <xsl:value-of select="$url"/>
206
          </xsl:attribute>
207
        </xsl:element>
208
      </xsl:element>
209
      <Name xmlns="http://www.opengis.net/context"><xsl:value-of select="wms:Name"/></Name>
210
      <Title xmlns="http://www.opengis.net/context"><xsl:value-of select="wms:Title"/></Title>
211
      <Abstract xmlns="http://www.opengis.net/context"><xsl:value-of select="wms:Abstract"/></Abstract>
212
      <FormatList xmlns="http://www.opengis.net/context">
213
        <Format current="1" xmlns="http://www.opengis.net/context">image/png</Format>
214
      </FormatList>
215
      <StyleList xmlns="http://www.opengis.net/context">
216
        <Style xmlns="http://www.opengis.net/context" current="1">
217
          <Name xmlns="http://www.opengis.net/context"><xsl:value-of select="$styleName"/></Name>
218
          <xsl:if test="wms:Style/wms:LegendURL">
219
          <LegendURL xmlns="http://www.opengis.net/context">
220
            <OnlineResource xmlns="http://www.opengis.net/context">
221
              <xsl:attribute name="xlink:href">
222
                <xsl:value-of select="wms:Style/wms:LegendURL/wms:OnlineResource/@xlink:href"/>
223
              </xsl:attribute>
224
            </OnlineResource>
225
          </LegendURL>
226
          </xsl:if>
227
        </Style>
228
      </StyleList>
229
      <xsl:if test="wms:Dimension[@name='time']">
230
        <DimensionList xmlns="http://www.opengis.net/context">
231
          <Dimension name="time" units="ISO8601"><xsl:value-of select="wms:Dimension[@name='time']"/></Dimension>
232
        </DimensionList>
233
      </xsl:if>
234
    </xsl:element>
235
  </xsl:template>
236
    
237
</xsl:stylesheet>
(1-1/14)