GeoRSS vers KML

Abstract

A "howto" to convert geoRSS to KML using an XSL transformation with PHP

Résumé

Google Earth, contrairement à Google Maps ne sait pas "consommer" du geoRSS (au 01/05/2007 ; ça viendra sans doute). Il existe des outils en ligne de conversion, mais si on veut tout maitriser il faut passer par une tranformation XSL : mode d'emploi pour ceux qui en auraient besoin pour ne pas avoir à chercher comme je l'ai fait...
(pratique pour Flickr ou Wordpress avec le plugin GeoPress qui nous servent du GeoRSS)

Mode d'emploi

Il faut

georss2kml.php

<?php      
header('Content-type: application/vnd.google-earth.kml+xml');
$xslt = new xsltProcessor;
$xslt->importStyleSheet(DomDocument::load('georss2kml.xsl'));
echo $xslt->transformToXML(DomDocument::loadXML(file_get_contents('http://server/georss.rss')));
?>
      

georss2kml.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
  xmlns:georss="http://www.georss.org/georss"
  xmlns:gml="http://www.opengis.net/gml">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/rss/channel">
<kml xmlns="http://earth.google.com/kml/2.0">
  <Document>
  <Style id="myicon">
  <IconStyle>
    <scale>0.8</scale>
    <Icon>
        <href>http://server/icon.png</href>
    </Icon>
  </IconStyle>
  </Style>
  <Folder>
    <name>Billets</name>
    <xsl:for-each select="item">
    <xsl:if test="georss:where/text()">
    <Placemark>
      <name><xsl:value-of select="title"/></name>
      <description>
        <xsl:value-of select="description"/>
        <xsl:value-of select="link" />
      </description>
      <Point>
        <xsl:choose>
          <xsl:when test="georss:where/text()">
            <coordinates><xsl:value-of select="substring-after(normalize-space(georss:where/gml:Point/gml:pos), ' ')"/>,<xsl:value-of select="substring-before(normalize-space(georss:where/gml:Point/gml:pos), ' ')"/>,0</coordinates>
          </xsl:when>
          <xsl:otherwise>
            <coordinates>0,0,0</coordinates>
          </xsl:otherwise>
        </xsl:choose>
      </Point>
      <styleUrl>#myicon</styleUrl>
    </Placemark>
    </xsl:if>
      </xsl:for-each>
  </Folder>
  </Document>
</kml>
</xsl:template>
</xsl:stylesheet>
      

Il ne reste plus qu'à faire un network link :

georss.kml

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
  <NetworkLink>
    <name>Demo</name>
    <open>1</open>
    <visibility>1</visibility>
    <description>An interesting conversion</description>
    <Url>
      <href>http://server/georss2kml.php</href>
      <refreshMode>onInterval</refreshMode>
      <refreshInterval>21600</refreshInterval>
    </Url>
  </NetworkLink>
</kml>
      

Et il suffit de pointer vers ce georss.kml.
Éventuellement, ne pas oublier de configurer Apache pour bien servir le KML : AddType application/vnd.google-earth.kml+xml .kml dans httpd.conf