wiki:KML

Version 6 (modified by condit, 15 years ago) ( diff )

--

KML

Introduction

Some introduction to KML here. And also you should read an excellent article on converting vector data to KML using OGR by Mano Marks here.

Styling

Until the driver supports the OGR simple feature style, your best bet will be to use XSL to modify the KML output accordingly. This allows a great amount of flexibility in defining your own styles and applying them based on feature attributes. Here's a quick example, based on a shapefile concerning geological datasets. In this case, the attributes are still going into the KML description element. Soon the attributes will go directly to schema elements. In this case, we embed a static style document that colors polygons. Then as we encounter features, we simply apply a style based on our criteria. You can use the same approach to assign icons to points based on some property. Or any other formatting you desire, for that matter. Here's the XSL:

<?xml version="1.0"?>

<!-- 
	This is an xsl stylesheet to add styles to an OGR generated KML file 
	condit@sdsc.edu april2008
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
				xmlns:kml="http://earth.google.com/kml/2.0" version="2.0">
	<xsl:output method="xml" indent="yes" omit-xml-declaration="no" encoding="utf-8"/>

	<!-- This is the name of the geonStyle that you want to use... -->
	<xsl:param name="geonStyle" required="yes" />
	
	<!-- In general, just pass through all elements and attributes -->
	<xsl:template match="*">		
		<xsl:copy>
			<xsl:copy-of select="@*" />
			<xsl:apply-templates  />
		</xsl:copy>
	</xsl:template>

	<!-- We want to eliminate any embedded style because we don't want to hide the external styles -->
	<xsl:template match="kml:Style" />		

	<!-- Use this to get the geonStyle passed in from the geonStyle value... -->
	<xsl:template name="getGeonStyle">
		<!-- TODO: Verify that the style exists -->
		<xsl:copy-of select="document('styles/geonStyles.xml')//geonStyle[@id=$geonStyle]/*" />		
	</xsl:template>
	
	<xsl:template match="kml:Document">
		<xsl:copy>
			<xsl:copy-of select="@*" />
			<xsl:call-template name="getGeonStyle" />
			<xsl:apply-templates  />
		</xsl:copy>
	</xsl:template>

	<xsl:template match="kml:Placemark">				
		<xsl:copy>
			<xsl:copy-of select="@*" />			
			<!-- Now based on the description element, set the styleURL for the Placemark according -->
			<!-- Note that this will have to change with the schema coming in future versions of OGR -->
			<xsl:choose>
				<xsl:when test="contains(kml:description, 'Tertiary')">
					<styleUrl>#Tertiary</styleUrl>
				</xsl:when>
				<xsl:when test="contains(kml:description, 'Jurassic')">
					<styleUrl>#Jurassic</styleUrl>
				</xsl:when>
				<xsl:when test="contains(kml:description, 'Permian')">
					<styleUrl>#Permian</styleUrl>
				</xsl:when>
				<xsl:when test="contains(kml:description, 'Water')">
					<styleUrl>#Water</styleUrl>
				</xsl:when>
				<xsl:when test="contains(kml:description, 'Quaternary')">
					<styleUrl>#Quaternary</styleUrl>
				</xsl:when>
				<xsl:when test="contains(kml:description, 'Carboniferous')">
					<styleUrl>#Carboniferous</styleUrl>
				</xsl:when>
				<xsl:when test="contains(kml:description, 'Algonkian')">
					<styleUrl>#Algonkian</styleUrl>
				</xsl:when>
				<xsl:when test="contains(kml:description, 'Cretaceous')">
					<styleUrl>#Cretaceous</styleUrl>
				</xsl:when>
				<xsl:when test="contains(kml:description, 'Paleozoic')">
					<styleUrl>#Paleozoic</styleUrl>
				</xsl:when>
				<xsl:when test="contains(kml:description, 'Mesozoic')">
					<styleUrl>#Mesozoic</styleUrl>
				</xsl:when>				
			</xsl:choose>			
			<xsl:apply-templates />
		</xsl:copy>
	</xsl:template>		

</xsl:stylesheet>

Here's some information on KML tag handling

Note: See TracWiki for help on using the wiki.