<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:srv="http://www.isotc211.org/2005/srv" xmlns:gml="http://www.opengis.net/gml" xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<!-- 
		Purpose: To display the extents described in an ISO 19139 XML in a Google Earth application.
		Anna.Milan@noaa.gov
	-->
	<xsl:strip-space elements="*"/>
	<xsl:template name="kmlExtents" match="/">
		<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
			<Document>
				<name>
					<xsl:value-of select="substring-after(//gmd:fileIdentifier/gco:CharacterString, ':')"/>
				</name>
				<visibility>1</visibility>
				<open>1</open>
				<Style id="footprint">
					<LineStyle>
						<color>ff0000ff</color>
						<width>2</width>
					</LineStyle>
					<PolyStyle>
						<color>7f7faaaa</color>
						<colorMode>random</colorMode>
					</PolyStyle>
				</Style>
				<xsl:choose>
					<xsl:when test="//gmd:MD_DataIdentification/gmd:extent">
						<xsl:for-each select="//gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent">
							<xsl:call-template name="geographicElement"/>
						</xsl:for-each>
					</xsl:when>
					<xsl:otherwise>No data extents found</xsl:otherwise>
				</xsl:choose>
			</Document>
		</kml>
	</xsl:template>
	<xsl:template name="geographicElement">
		<xsl:for-each select=".//gmd:geographicElement">
			<xsl:for-each select="gmd:EX_GeographicBoundingBox">
				<xsl:variable name="bboxID">
					<xsl:choose>
						<xsl:when test="@id">
							<xsl:value-of select="@id"/>
						</xsl:when>
						<xsl:when test="../../../gmd:EX_Extent/@id">
							<xsl:value-of select="../../../gmd:EX_Extent/@id"/>
						</xsl:when>
						<xsl:otherwise><xsl:value-of select="'tmpID'"/></xsl:otherwise>
					</xsl:choose>
					<!--<xsl:value-of select="./gmd:EX_GeographicBoundingBox/@id"/>-->
				</xsl:variable>
				<Placemark>
					<name>
						<xsl:value-of select="../../../../../gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:title"/>
						<!--<xsl:value-of select="$title"/>-->
						<xsl:text> (ID: </xsl:text>
						<xsl:choose>
							<xsl:when test="@id">
								<xsl:value-of select="@id"/>
							</xsl:when>
							<xsl:when test="../../../gmd:EX_Extent/@id">
								<xsl:value-of select="../../../gmd:EX_Extent/@id"/>
							</xsl:when>
							<xsl:otherwise><xsl:value-of select="'tmpID'"/></xsl:otherwise>
						</xsl:choose>
						<xsl:text>) </xsl:text>
					</name>
					<description>
						<xsl:value-of select="../../gmd:description"/>
						<xsl:text> at </xsl:text>
						<xsl:text>E: </xsl:text>
						<xsl:value-of select="gmd:eastBoundLongitude"/>
						<xsl:text>, </xsl:text>
						<xsl:text>W: </xsl:text>
						<xsl:value-of select="gmd:westBoundLongitude"/>
						<xsl:text>, </xsl:text>
						<xsl:text>N: </xsl:text>
						<xsl:value-of select="gmd:northBoundLatitude"/>
						<xsl:text>, </xsl:text>
						<xsl:text>S: </xsl:text>
						<xsl:value-of select="gmd:southBoundLatitude"/>
					</description>
					<!-- for TimeSpan call template for temporal info with bbox id parameter and add a hash mark so that it will match the xlink:href value -->
					<xsl:choose>
						<xsl:when test="../../gmd:temporalElement/gmd:EX_SpatialTemporalExtent">
							<xsl:call-template name="SpatialTemporalExtent">
								<xsl:with-param name="bboxid" select="concat('#',./@id)"/>
							</xsl:call-template>
						</xsl:when>
						<xsl:when test="../../gmd:temporalElement/gmd:EX_TemporalExtent">
							<xsl:call-template name="TemporalExtent"> </xsl:call-template>
						</xsl:when>
					</xsl:choose>
					<styleUrl>#footprint</styleUrl>
					<!-- creates 2 polygons when the bounding box crosses the anti-meridian -->
					<xsl:choose>
						<xsl:when test="gmd:westBoundLongitude &gt; gmd:eastBoundLongitude">
							<xsl:variable name="anti-West">180<xsl:value-of select="./gmd:westBoundLongitude"/></xsl:variable>
							<MultiGeometry>
								<Polygon id="antiMeridian">
									<outerBoundaryIs>
										<LinearRing>
											<coordinates><!-- W, S -->
												<xsl:value-of select="./gmd:westBoundLongitude"/>
												<xsl:text>,</xsl:text>
												<xsl:value-of select="./gmd:southBoundLatitude"/><xsl:text> </xsl:text><!-- E, S--> 179.99<xsl:text>,</xsl:text><xsl:value-of select="./gmd:southBoundLatitude"/><xsl:text> </xsl:text><!-- E, N --> 179.99<xsl:text>,</xsl:text><xsl:value-of select="./gmd:northBoundLatitude"/><xsl:text> </xsl:text><!-- W, N --><xsl:value-of select="./gmd:westBoundLongitude"/><xsl:text>,</xsl:text><xsl:value-of select="./gmd:northBoundLatitude"
													/><xsl:text> </xsl:text><!-- same as first --><xsl:value-of select="./gmd:westBoundLongitude"/><xsl:text>,</xsl:text><xsl:value-of select="./gmd:southBoundLatitude"/><xsl:text> </xsl:text></coordinates>
										</LinearRing>
									</outerBoundaryIs>
								</Polygon>
								<Polygon id="proMeridian">
									<outerBoundaryIs>
										<LinearRing>
											<coordinates>
												<!-- W, S --> -179.99<xsl:text>,</xsl:text><xsl:value-of select="./gmd:southBoundLatitude"/><xsl:text> </xsl:text><!-- E, S--><xsl:value-of select="./gmd:eastBoundLongitude"/><xsl:text>,</xsl:text><xsl:value-of select="./gmd:southBoundLatitude"/><xsl:text> </xsl:text><!-- E, N --><xsl:value-of select="./gmd:eastBoundLongitude"/><xsl:text>,</xsl:text><xsl:value-of select="./gmd:northBoundLatitude"/><xsl:text> </xsl:text><!-- W, N -->
													-179.99<xsl:text>,</xsl:text><xsl:value-of select="./gmd:northBoundLatitude"/><xsl:text> </xsl:text><!-- same as first --> -179.99<xsl:text>,</xsl:text><xsl:value-of select="./gmd:southBoundLatitude"/><xsl:text> </xsl:text></coordinates>
										</LinearRing>
									</outerBoundaryIs>
								</Polygon>
							</MultiGeometry>
						</xsl:when>
						<!-- otherwise creates one bounding box -->
						<xsl:otherwise>
							<Polygon id="poly_{$bboxID}">
								<outerBoundaryIs>
									<LinearRing>
										<coordinates>
											<!-- W, S -->
											<xsl:value-of select="./gmd:westBoundLongitude"/>
											<xsl:text>,</xsl:text>
											<xsl:value-of select="./gmd:southBoundLatitude"/>
											<xsl:text> </xsl:text>
											<!-- E, S-->
											<xsl:value-of select="./gmd:eastBoundLongitude"/>
											<xsl:text>,</xsl:text>
											<xsl:value-of select="./gmd:southBoundLatitude"/>
											<xsl:text> </xsl:text>
											<!-- E, N -->
											<xsl:value-of select="./gmd:eastBoundLongitude"/>
											<xsl:text>,</xsl:text>
											<xsl:value-of select="./gmd:northBoundLatitude"/>
											<xsl:text> </xsl:text>
											<!-- W, N -->
											<xsl:value-of select="./gmd:westBoundLongitude"/>
											<xsl:text>,</xsl:text>
											<xsl:value-of select="./gmd:northBoundLatitude"/>
											<xsl:text> </xsl:text>
											<!-- same as first -->
											<xsl:value-of select="./gmd:westBoundLongitude"/>
											<xsl:text>,</xsl:text>
											<xsl:value-of select="./gmd:southBoundLatitude"/>
											<xsl:text> </xsl:text>
										</coordinates>
									</LinearRing>
								</outerBoundaryIs>
							</Polygon>
						</xsl:otherwise>
					</xsl:choose>
				</Placemark>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="SpatialTemporalExtent">
		<xsl:param name="bboxid"/>
		<xsl:for-each select="../../gmd:temporalElement">
			<xsl:choose>
				<xsl:when test="./gmd:EX_SpatialTemporalExtent/gmd:spatialExtent/@xlink:href=$bboxid">
					<TimeSpan>
						<begin>
							<xsl:value-of select="./gmd:EX_SpatialTemporalExtent/gmd:extent/gml:TimePeriod/gml:beginPosition"/>
						</begin>
						<end>
							<xsl:value-of select="./gmd:EX_SpatialTemporalExtent/gmd:extent/gml:TimePeriod/gml:endPosition"/>
						</end>
					</TimeSpan>
				</xsl:when>
			</xsl:choose>
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="TemporalExtent">
		<xsl:for-each select="../../gmd:temporalElement">
			<TimeSpan>
				<begin>
					<xsl:choose>
						<xsl:when test="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:beginPosition/@indeterminatePosition">
							<xsl:value-of select="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:beginPosition/@indeterminatePosition"/>
						</xsl:when>
						<xsl:when test="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:begin/@indeterminatePosition">
							<xsl:value-of select="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:begin/@indeterminatePosition"/>
						</xsl:when>
						<xsl:when test="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:begin/gml:TimeInstant/gml:timePosition/@indeterminatePosition">
							<xsl:value-of select="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:begin/gml:TimeInstant/gml:timePosition/@indeterminatePosition"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:beginPosition|./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:begin"/>
						</xsl:otherwise>
					</xsl:choose>
				</begin>
				<end>
					<xsl:choose>
						<xsl:when test="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:endPosition/@indeterminatePosition">
							<xsl:value-of select="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:endPosition/@indeterminatePosition"/>
						</xsl:when>
						<xsl:when test="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:end/@indeterminatePosition">
							<xsl:value-of select="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:end/@indeterminatePosition"/>
						</xsl:when>
						<xsl:when test="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:end/gml:TimeInstant/gml:timePosition/@indeterminatePosition">
							<xsl:value-of select="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:end/gml:TimeInstant/gml:timePosition/@indeterminatePosition"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:endPosition|./gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:end"/>
						</xsl:otherwise>
					</xsl:choose>
				</end>
			</TimeSpan>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
