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
-
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
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
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:
- 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 geojson.io as well as overpass-turbo.eu.
-
-
I know this comment is coming late.. this tool is amazing (it always seems to work) but it is SLOW. We are currently evaluating alternatives to osmtogeojson because even converting a ~100MB Overpass output takes about 40 seconds on a reasonably powerful machine. We are looking to see whether GDAL can be an alternative for speed reasons. If speed is not a concern, I recommend this library. – Tommy Aug 8 '18 at 14:50
If you need library on Python: osm2geojson
Example
import codecs
import osm2geojson
with codecs.open('file.osm', 'r', encoding='utf-8') as data:
xml = data.read()
geojson = osm2geojson.xml2geojson(xml)
# >> { "type": "FeatureCollection", "features": [ ... ] }
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.
Use osmtogeojson like is written in this guide:
osmtogeojson Pittsburgh.xml > Pittsburgh.geojson