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
7 Answers
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
- 161
- 1
- 3
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
- 121
- 1
- 3
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.
- 8,380
- 3
- 52
- 91
-
-
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 08 '18 at 14:50
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
- 49,701
- 9
- 73
- 132
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": [ ... ] }
- 21
- 2
Use osmtogeojson like is written in this guide:
osmtogeojson Pittsburgh.xml > Pittsburgh.geojson
- 229
- 1
- 2
- 7
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.
- 7,994
- 10
- 72
- 118
-> ESRI Shapefile
-> MapInfo File
-> UK .NTF
-> SDTS
-> TIGER
-> S57
-> DGN
-> VRT
-> REC
-> Memory
-> BNA
-> CSV
-> NAS
-> GML
-> GPX
-> KML
-> GeoJSON
-> Interlis – user1386596 Oct 30 '13 at 11:38