Skip to content

Instantly share code, notes, and snippets.

Created November 27, 2011 04:57
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;
Copy link

pau1m commented Dec 25, 2012


Copy link

To all interested: I forked this script,

Copy link

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).

Copy link

tibetty commented Jan 5, 2019

I would say it's as is seemingly correct but less than enough solution 'coz in most situations ways can be concatenated (sometimes need reversion before concatenation) to form a LineString/MultiLineString and also Polygon/MultiPolygon. As the tool designer, you should never assume that the ways themselves are perfectly organized.

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