This text shows how to export OpenStreetMap geographic coordinates and import to a MongoDB collection.
8.1.1 – Exporting OpenStreetMap data
With OpenStreetMap open in the browser showing the Center of the City of Rio de Janeiro, the Export button in the upper left corner of the page was enabled, and selected a rectangle with latitudes -22.8916 and -22.9180 and longitudes -43.2110 and -43.1574 (see featured image). The link API Overpass was then clicked to download this bounding box from a mirror of OpenStreetMap. The downloaded file name was map, without extension.
8.1.2 – Conversion of OpenStreetMap data to GeoJSON
The map file downloaded from OpenStreetMap was renamed map.osm
$ mv map map.osm
and then converted to the GeoJSON format using the OGR utility program ogr2ogr from GDAL, which converts simple features data between file formats, as shown bellow:
$ ogr2ogr -f GeoJSON map.geojson map.osm points
producing the output:
Warning 1: Input datasource uses random layer reading, but output datasource does not support random layer writing
0...10...20...30...40...50...60...70...80...90...100 - done.
8.1.3 – Creating a collection in MongoDB to receive OpenStreetMap data
To store the data exported from OpenStreetMap, the osmexport collection was created using the commands:
$ mongo
use reficio;
// Drop the collection, if exists
db.osmexport.drop();
// Create the collection
db.createCollection("osmexport");
8.1.4 – Importing data
Importing the OpenStreetMap data into osmexport collection of MongoDB was done using the mongoimport utility, as shown below:
$ mongoimport \
--db reficio \
--collection osmexport \
--file map.geojson
producing the output:
2018-05-17T09:48:42.767-0300 connected to: localhost
2018-05-17T09:48:42.928-0300 imported 1 document
8.1.5 – A document for each point
As can be seen above, all points were imported into a single document. To place each point in a separate document, was created the osmpoints collection as shown below:
$ mongo
use reficio;
db.createCollection("osmpoints");
and executed the command:
db.osmexport.find().forEach(function(data) {
db.osmpoints.insert( data.features );
});
8.1.6 – An example of querying data from osmpoints collection
db.osmpoints.find(
{ 'properties.other_tags' : RegExp('"denomination"=>"catholic"')},
{ 'properties.name' : 1, 'geometry' : 1, _id: 0}
).pretty();
Output produced by the query:
> db.osmpoints.find(
... { 'properties.other_tags' : RegExp('"denomination"=>"catholic"')},
... { 'properties.name' : 1, 'geometry' : 1, _id: 0}
... ).pretty();
{
"properties" : {
"name" : "Igreja de Nossa Senhora do Rosário e São Benedito dos Homens Pretos"
},
"geometry" : {
"type" : "Point",
"coordinates" : [
-43.1803813,
-22.9041351
]
}
}
{
"properties" : {
"name" : "Santuário Nossa Senhora de Fátima"
},
"geometry" : {
"type" : "Point",
"coordinates" : [
-43.1921416,
-22.9126505
]
}
}
{
"properties" : {
"name" : "Igreja de Nossa Senhora do Parto"
},
"geometry" : {
"type" : "Point",
"coordinates" : [
-43.1769091,
-22.9058593
]
}
}
{
"properties" : {
"name" : "Igreja de Nossa Senhora do Carmo da Lapa do Desterro"
},
"geometry" : {
"type" : "Point",
"coordinates" : [
-43.1777432,
-22.914651
]
}
}
{
"properties" : {
"name" : "Igreja de Nossa Senhora do Terço e Senhor dos Passos"
},
"geometry" : {
"type" : "Point",
"coordinates" : [
-43.182868,
-22.905319
]
}
}
{
"properties" : {
"name" : "Igreja de Nossa Senhora da Conceição e Boa Morte"
},
"geometry" : {
"type" : "Point",
"coordinates" : [
-43.1787691,
-22.9030222
]
}
}
{
"properties" : {
"name" : "Igreja Nossa Senhora Sallete"
},
"geometry" : {
"type" : "Point",
"coordinates" : [
-43.195651,
-22.9162055
]
}
}
{
"properties" : {
"name" : "Igreja de Nossa Senhora da Saúde"
},
"geometry" : {
"type" : "Point",
"coordinates" : [
-43.1915095,
-22.8937347
]
}
}
{
"properties" : {
"name" : "Igreja de São Francisco da Prainha"
},
"geometry" : {
"type" : "Point",
"coordinates" : [
-43.1836126,
-22.8978431
]
}
}
{
"properties" : {
"name" : "Igreja Irmandade dos Mártires São Crispim e São Crispiniano"
},
"geometry" : {
"type" : "Point",
"coordinates" : [
-43.1884606,
-22.9134891
]
}
}
>
8.1.7 View the points on the map
To view the points from the previous example on the map, was created the script osm_denomination_catholic.py in Python to generate the GPX (GPS eXchange Format) osm_denomination_catholic.gpx file. The map can be seen below in OpenStreetMap.
Sources