9

Can anyone tell me how to import OSM data in mongoDB step by step ?

This is the link to the data http://download.geofabrik.de/europe/germany/baden-wuerttemberg/karlsruhe-regbez.html


https://gis.stackexchange.com/users/12320/ylkaenter image description here

this is the error i am getting in the 1 step:

Unable to open datasource with the following drivers ... [huge list]

3

3 Answers 3

6

So this is how I do it:
1.) convert the osm.pbf file to geojson with ogr2ogr like this:

ogr2ogr -f  GeoJSON points.json C:\data\osm\karlsruhe-regbez-latest.osm.pbf points

the only trick is that you have to enter this statement five times, changing the type of the geometry at the end of the statement :

points / lines / multilinestrings / multipolygons / other_relations

now you have five .json files

2.) Edit each file by deleting these parts:

  • the first two lines of the file:
{"type": "FeatureCollection",           
           "features": [
  • the last two lines of the file:
]
}
  • and the comma at the end of each row
    this step I do with the search&replace feature of notepad++, like this:

replace commas in notepad++
(the number of the brackets may vary for different geometry types)

so now you have five json files which can be imported to mongodb

3.) import the json data to mongodb
in the commander cd to mongodb/bin and type:

> mongoimport --db dbname --collection points  --file C:\data\path\to\points.json

Here you can also put all 5 files in one collection.

I know this sounds a lot of work at first, but I did not find any other way which would be quicker.
I hope this helps :)

EDIT
I found a software with which you can do it in one step: osm2mongo
Please note that for this you need to download the osm.bz2 format, not the osm.pbf one. After download you should extract it, so you have a karlsruhe-regbez-latest.osm file.
Then in the commander cd to the osm2mongo.exe and type

>osm2mongo -i karlsruhe-regbez-latest.osm -h localhost:27017 -d dbname

The drawback is, that it has a bit different data structure. It creates 3 collections: nodes, relations and ways.
But it is definitely easier to import with this software in just one step.

3
  • osm2mongo in this can u explain bit clear i downloaded the file after that ..? Commented Aug 9, 2014 at 9:58
  • Why do I need all the 5 files? Any reasons?
    – joesan
    Commented Oct 13, 2015 at 19:40
  • ogr2ogr sorts the osm data into 5 categories based on their geometry type. If you only need points for example, then of course import only the points.
    – ylka
    Commented Oct 14, 2015 at 8:40
1

This can be done using the mongosm importer.

  1. Download the osm file: http://download.geofabrik.de/europe/germany/baden-wuerttemberg/karlsruhe-regbez-latest.osm.bz2.
  2. Extract the file. Try f.ex. 7zip, or bunzip2 karlsruhe-regbez-latest.osm.bz2 in a shell.
  3. Download the repo : git clone --depth 1 https://github.com/iandees/mongosm.git.
  4. python mongosm/insert_osm_data.py karlsruhe-regbez-latest.osm imports the data.
0

Only thing that I know is Osmo but IMHO the best/best supported method is using PostgreSQL as it's most supported solution by the OSM tools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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