How do you convert OSM XML to geojson?

You can convert OSM XML to GeoJson with ogr2ogr. To convert to GeoJSON without getting the following error:

ERROR 6: GeoJSON driver doesn't support creating more than one layer

You can use one of the following commands or all of them:

ogr2ogr -f GeoJSON points.json data.osm.pbf points
ogr2ogr -f GeoJSON lines.json data.osm.pbf lines
ogr2ogr -f GeoJSON multilinestrings.json data.osm.pbf multilinestrings
ogr2ogr -f GeoJSON multipolygons.json data.osm.pbf multipolygons
ogr2ogr -f GeoJSON other_relations.json data.osm.pbf other_relations

The development (as march 2013) version of gdal has read support for OSM, both for plain XML and PBF format.

So you can convert your OSM file with:

ogr2ogr -f GeoJSON myfile.geojson myfile.osm.pbf


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

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

The library is already in use on geojson.io as well as overpass-turbo.eu.