Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
OSM2GEO - A JS Converter to convert OSM to GeoJSON
* OSM2GEO - OSM to GeoJSON converter
* OSM to GeoJSON converter takes in a .osm XML file as input and produces
* corresponding GeoJSON object.
* AUTHOR: P.Arunmozhi <>
* DATE : 26 / Nov / 2011
* LICENSE : WTFPL - Do What The Fuck You Want To Public License
* DEPENDENCY: OSM2GEO entirely depends on jQuery for the XML parsing and
* DOM traversing. Make sure you include <script src="somewhere/jquery.js">
* </script> before you include osm2geo.js
* USAGE: This script contains a single function -> geojson osm2geo(osmXML)
* It takes in a .osm (xml) as parameter and retruns the corresponding
* GeoJson object.
* ***********************************************************************/
var osm2geo = function(osm){
// Check wether the argument is a Jquery object and act accordingly
// Assuming it as a raw server response for now
var $xml = jQuery(osm);
// Initialize the empty GeoJSON object
var geo = {
"type" : "FeatureCollection",
"features" : []
// setting the bounding box [minX,minY,maxX,maxY]; x -> long, y -> lat
function getBounds(bounds){
var bbox = new Array;
return bbox;
geo["bbox"] = getBounds($xml.find("bounds"));
// Function to set props for a feature
function setProps(element){
var properties = {};
var tags = $(element).find("tag");
tags.each(function(index, tag){
properties[$(tag).attr("k")] = $(tag).attr("v");
return properties;
// Generic function to create a feature of given type
function getFeature(element, type){
return {
"geometry" : {
"type" : type,
"coordinates" : []
"type" : "Feature",
"properties" : setProps(element)
// Ways
var $ways = $("way", $xml);
$ways.each(function(index, ele){
var feature = new Object;
// List all the nodes
var nodes = $(ele).find("nd");
// If first and last nd are same, then its a polygon
if($(nodes).last().attr("ref") === $(nodes).first().attr("ref")){
feature = getFeature(ele, "Polygon");
feature = getFeature(ele, "LineString");
nodes.each(function(index, nd){
var node = $xml.find("node[id='"+$(nd).attr("ref")+"']"); // find the node with id ref'ed in way
var cords = [parseFloat(node.attr("lon")), parseFloat(node.attr("lat"))]; // get the lat,lon of the node
// If polygon push it inside the cords[[]]
if(feature.geometry.type === "Polygon"){
}// if just Line push inside cords[]
// Save the LineString in the Main object
// Points (POI)
var $points = $("node:has('tag')", $xml);
$points.each(function(index, ele){
var feature = getFeature(ele, "Point");
// Save the point in Main object
// Finally return the GeoJSON object
return geo;

pau1m commented Dec 25, 2012


To all interested: I forked this script,

tyrasd commented Oct 21, 2013

osmtogeojson is yet another OSM-to-GeoJSON converter, which has a few benefits when compared to this (OSM2GEO) or osm-and-geojson:

  • can be used as a command line tool as well as a javascript (browser and nodejs) library.
  • proper multipolygon support
  • sophisticated polygon detection
  • stable (can cope with incomplete OSM data)
  • well tested
  • faster

The library is already in use on as well as (of which it is a spin-off, actually).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment