Changes between Version 1 and Version 2 of KML


Ignore:
Timestamp:
Apr 18, 2008, 11:50:19 AM (16 years ago)
Author:
condit
Comment:

Added a style example

Legend:

Unmodified
Added
Removed
Modified
  • KML

    v1 v2  
    1 testing
     1= KML =
     2== Introduction ==
     3Some introduction to KML here...
    24
     5== Styling ==
     6Until the driver supports the OGR simple feature style, you 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:
     7{{{
     8<?xml version="1.0"?>
     9
     10<!--
     11        This is an xsl stylesheet to add styles to an OGR generated KML file
     12        condit@sdsc.edu april2008
     13-->
     14<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     15                                xmlns:kml="http://earth.google.com/kml/2.0" version="2.0">
     16        <xsl:output method="xml" indent="yes" omit-xml-declaration="no" encoding="utf-8"/>
     17
     18        <!-- This is the name of the geonStyle that you want to use... -->
     19        <xsl:param name="geonStyle" required="yes" />
     20       
     21        <!-- In general, just pass through all elements and attributes -->
     22        <xsl:template match="*">               
     23                <xsl:copy>
     24                        <xsl:copy-of select="@*" />
     25                        <xsl:apply-templates  />
     26                </xsl:copy>
     27        </xsl:template>
     28
     29        <!-- We want to eliminate any embedded style because we don't want to hide the external styles -->
     30        <xsl:template match="kml:Style" />             
     31
     32        <!-- Use this to get the geonStyle passed in from the geonStyle value... -->
     33        <xsl:template name="getGeonStyle">
     34                <!-- TODO: Verify that the style exists -->
     35                <xsl:copy-of select="document('styles/geonStyles.xml')//geonStyle[@id=$geonStyle]/*" />         
     36        </xsl:template>
     37       
     38        <xsl:template match="kml:Document">
     39                <xsl:copy>
     40                        <xsl:copy-of select="@*" />
     41                        <xsl:call-template name="getGeonStyle" />
     42                        <xsl:apply-templates  />
     43                </xsl:copy>
     44        </xsl:template>
     45
     46        <xsl:template match="kml:Placemark">                           
     47                <xsl:copy>
     48                        <xsl:copy-of select="@*" />                     
     49                        <!-- Now based on the description element, set the styleURL for the Placemark according -->
     50                        <!-- Note that this will have to change with the schema coming in future versions of OGR -->
     51                        <xsl:choose>
     52                                <xsl:when test="contains(kml:description, 'Tertiary')">
     53                                        <styleUrl>#Tertiary</styleUrl>
     54                                </xsl:when>
     55                                <xsl:when test="contains(kml:description, 'Jurassic')">
     56                                        <styleUrl>#Jurassic</styleUrl>
     57                                </xsl:when>
     58                                <xsl:when test="contains(kml:description, 'Permian')">
     59                                        <styleUrl>#Permian</styleUrl>
     60                                </xsl:when>
     61                                <xsl:when test="contains(kml:description, 'Water')">
     62                                        <styleUrl>#Water</styleUrl>
     63                                </xsl:when>
     64                                <xsl:when test="contains(kml:description, 'Quaternary')">
     65                                        <styleUrl>#Quaternary</styleUrl>
     66                                </xsl:when>
     67                                <xsl:when test="contains(kml:description, 'Carboniferous')">
     68                                        <styleUrl>#Carboniferous</styleUrl>
     69                                </xsl:when>
     70                                <xsl:when test="contains(kml:description, 'Algonkian')">
     71                                        <styleUrl>#Algonkian</styleUrl>
     72                                </xsl:when>
     73                                <xsl:when test="contains(kml:description, 'Cretaceous')">
     74                                        <styleUrl>#Cretaceous</styleUrl>
     75                                </xsl:when>
     76                                <xsl:when test="contains(kml:description, 'Paleozoic')">
     77                                        <styleUrl>#Paleozoic</styleUrl>
     78                                </xsl:when>
     79                                <xsl:when test="contains(kml:description, 'Mesozoic')">
     80                                        <styleUrl>#Mesozoic</styleUrl>
     81                                </xsl:when>                             
     82                        </xsl:choose>                   
     83                        <xsl:apply-templates />
     84                </xsl:copy>
     85        </xsl:template>         
     86
     87</xsl:stylesheet>
     88}}}
     89
     90
     91