I want to take the XML output of a Xapi query and display it using leaflet or polymaps. How can I do that? Is there an easy way to convert XML to geojson
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 F##% You Want To Public License * LICENSE URL: http://sam.zoy.org/wtfpl/ * * 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 returns the corresponding * GeoJson object."
Credit to P.Arunmozhi
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
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
osmtogeojson is yet another OSM-to-GeoJSON converter, which has a few benefits when compared to this (OSM2GEO) or osm-and-geojson:
- proper multipolygon support
- sophisticated polygon detection
- stable (can cope with incomplete OSM data)
- well tested
OpenStreetMap plugin for Leaflet is another way to do this. This way you can use your Xapi query directly in your code. Though it only makes sense for a small datasets since the request takes a while.