Technomancy

How to make a map based on OpenStreetMap

written by rory, on Sep 12, 2009 9:06:00 PM.

I recently made an OpenStreetMap (OSM) based AJAX map of all the Hackerspaces in OSM. It pulls out the list of hackerspaces from XAPI, converts it from OSM to GeoRSS using XSLT, then displayes it on a map using OpenLayers.

The XAPI URL I'm using is:

wget -q -O /var/www/osm-hackerspaces/hackerspaces.osm 
"http://osmxapi.hypercube.telascience.org/api/0.6/*[leisure=hackerspace]"

I'm also setting a nice User-Agent giving my contact details and I've installed a local squid proxy and using that. I'm being a good web citizen

I convert the OSM file to GeoRSS using the following XSLT file:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/osm">
<?xml-stylesheet href="http://yelp.com/css/atom.css" type="text/css" media="screen"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> 
	<rights></rights> 
	<title>Hackerspaces in OpenStreetMap</title>
	<link href="http://www.technomancy.org/osm-hackerspaces/" />
	<updated>
        <xsl:for-each select="./node">
            <xsl:sort select="@timestamp" data-type="text" order="descending" />
            <xsl:if test="position() = 1">
                <xsl:value-of select="@timestamp" />
            </xsl:if>
        </xsl:for-each>
    </updated>
	<author>
		<name>OpenStreetMap contributors</name>
	</author>

    
    <xsl:for-each select="./node">
        <entry>
            <title><xsl:value-of select="./tag[@k='name']/@v" /></title>
            <xsl:choose>
                <xsl:when test="not(./tag[@k='website']/@v)">
                    <link rel="alternate" type="text/html" href="" />
                    <id></id>
                </xsl:when>
                <xsl:when test="substring(./tag[@k='website']/@v, 0, 5) = 'http'">
                    <xsl:variable name="website" select="./tag[@k='website']/@v" />
                    <link rel="alternate" type="text/html" href="{$website}" />
                    <id><xsl:value-of select="$website" /></id>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:variable name="website">http://<xsl:value-of select="./tag[@k='website']/@v" /></xsl:variable>
                    <link rel="alternate" type="text/html" href="{$website}" />
                    <id><xsl:value-of select="$website" /></id>
                </xsl:otherwise>
            </xsl:choose>
            <updated><xsl:value-of select="./@timestamp" /></updated>

            <summary type="html"><xsl:value-of select="./tag[@k='addr:full']/@v" /><xsl:text> </xsl:text><xsl:value-of select="./tag[@k='note']/@v" /></summary>
            <geo:long><xsl:value-of select="./@lon" /></geo:long>	
            <geo:lat><xsl:value-of select="./@lat" /></geo:lat>
        </entry>
    </xsl:for-each>

</feed>
</xsl:template>

</xsl:stylesheet>

I use xmlstarlet to convert using this command line:

xmlstarlet tr osm2georss.xslt hackerspaces.osm > hackerspaces.georss.xml

I made a little 20x20 logo from the hackerspace logo.

OpenLayers has built in support for GeoRSS, so it was just a matter of giving it the filename of the above.

I have a cron running that every 6 hours to update it.

Comments

  • OpenLayers has built-in support for OSM files too.

    Comment by Edgemaster — Sep 12, 2009 9:46:37 PM | # - re

  • Doesn't OpenLayers support the osm xml format directly?

    Comment by saerdnaer — Sep 12, 2009 9:57:28 PM | # - re

  • Ah, i should read the other comments first ;-)

    Comment by saerdnaer — Sep 12, 2009 9:58:18 PM | # - re

  • OpenStreetMap is singular. The common error with an incorrect plural will lead to a link farm web site.

    OpenStreetMap; one map, full of everything.

    Comment by rweait — Sep 13, 2009 2:10:31 AM | # - re

    • You're right, I've corrected the post. I'm frequently making that mistake and entered "openstreetmaps.org"

      Comment by rory — Sep 13, 2009 1:39:11 PM | # - re

  • Yeah OpenLayers can show .osm files, however if I use GeoRSS then I get clickable popups, custom icons, and custom text in the popup boxes.

    Comment by rory — Dec 9, 2009 11:08:55 AM | # - re

  • Great Idea! Strangely the map does not seem to be picking up our Hackerspace "Warpzone" in Münster, although this is marked with the leisure=hackerspace tag.

    Comment by StuC — Jan 3, 2010 12:17:22 PM | # - re

  • OK - the problem is not with your Map, but with the "ancient" data from the xapi url (PlanetDate 31.12.09).

    Calling data direct from OSM.org works.

    Comment by StuC — Jan 4, 2010 10:58:46 PM | # - re

Leave a Reply