Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
    
Thanks for your suggestion. I have tried the same way you guys have proposed, but unfortunately conversion failed with the following error!!! <i>Can anyone please suggest? Thanks in advance for you time....</i> C:\Program Files\FWTools2.4.7\bin>ogr2ogr -f GeoJSON D:\New.geojson D:\z.pbf <B> FAILURE: Unable to open datasource `z.pbf' with the following drivers. <br>-> ESRI Shapefile <br>-> MapInfo File <br>-> UK .NTF <br>-> SDTS <br>-> TIGER <br>-> S57 <br>-> DGN <br>-> VRT <br>-> REC <br>-> Memory <br>-> BNA <br>-> CSV <br>-> NAS <br>-> GML <br>-> GPX <br>-> KML <br>-> GeoJSON <br>-> Interlis –  user1386596 Oct 30 '13 at 11:38
    
This appears to be a new question rather than an answer. If that is the case, post it as a new question. –  Martin Oct 30 '13 at 12:07
add comment

5 Answers

up vote 7 down vote accepted

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

https://gist.github.com/1396990

share|improve this answer
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.