This text shows how to download data with geographic coordinates from IBGE (Instituto Brasileiro de Geografia e Estatística / Brazilian Institute of Geography and Statistics) and import to a MongoDB collection.
8.2.1 – Downloading IBGE data
The file containing the coordinates of the localities of Brazil, BR_Localidades_2010_v1.dbf, was downloaded from page Downloads de Geociências do IBGE, folder Shapefile_SHP (see featured image).
8.2.2 – Conversion of IBGE data to GeoJSON format
The file BR_Localidades_2010_v1.dbf downloaded from IBGE was converted to the GeoJSON format using the OGR utility program ogr2ogr from GDAL, which converts simple features data between file formats, as shown bellow:
$ export SHAPE_ENCODING="ISO-8859-1"
$ ogr2ogr -f GeoJSON BR_Localidades_2010_v1.geojson BR_Localidades_2010_v1.dbf -dialect SQLite -sql "SELECT ST_Point(LONG, LAT) AS geometry, * FROM BR_Localidades_2010_v1"
8.2.3 – Creating a collection in MongoDB to receive IBGE data
To store the data exported from IBGE, the BR_Localidades_2010_v1 collection was created using the commands:
$ mongo
use reficio;
db.createCollection("BR_Localidades_2010_v1");
8.2.4 – Importing data
Importing the IBGE data into BR_Localidades_2010_v1 collection of MongoDB was done using the mongoimport utility, as shown below:
$ mongoimport \
--db reficio \
--collection BR_Localidades_2010_v1 \
--file BR_Localidades_2010_v1.geojson
producing the output:
2018-05-17T11:31:56.715-0300 connected to: localhost
2018-05-17T11:31:58.148-0300 imported 1 document
8.2.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 ibgelocalidades collection as shown below:
$ mongo
use reficio;
db.createCollection("ibgelocalidades", {}, { collation: { locale: "pt", strength: 1 } } );
and executed the command:
db.BR_Localidades_2010_v1.find().forEach(function(data) {
db.ibgelocalidades.insert( data.features );
});
8.2.6 – An example of querying data from ibgelocalidades collection
$ mongo
> use reficio;
switched to db reficio
> // Count the number of documents in the collection ibgelocalidades
> db.ibgelocalidades.find().count();
21886
> // Information about the neighborhood of Frade
> // in the municipality of Angra dos Reis
> // in the State of Rio de Janeiro
> db.ibgelocalidades.find({ $and: [
... {'properties.NM_UF' : 'RIO DE JANEIRO'},
... {'properties.NM_MUNICIP' : 'ANGRA DOS REIS'},
... {'properties.NM_BAIRRO' : 'FRADE'} ] }
... ).pretty();
{
"_id" : ObjectId("5afd9302628272be47c68ac2"),
"type" : "Feature",
"properties" : {
"ID" : 14902,
"CD_GEOCODI" : "330010015000001",
"TIPO" : "URBANO",
"CD_GEOCODB" : "330010015057",
"NM_BAIRRO" : "FRADE",
"CD_GEOCODS" : "33001001500",
"NM_SUBDIST" : null,
"CD_GEOCODD" : "330010015",
"NM_DISTRIT" : "CUNHAMBEBE",
"CD_GEOCODM" : "3300100",
"NM_MUNICIP" : "ANGRA DOS REIS",
"NM_MICRO" : "BAÍA DA ILHA GRANDE",
"NM_MESO" : "SUL FLUMINENSE",
"NM_UF" : "RIO DE JANEIRO",
"CD_NIVEL" : "2",
"CD_CATEGOR" : "15",
"NM_CATEGOR" : "VILA",
"NM_LOCALID" : "CUNHAMBEBE",
"LONG" : -44.4361637206427,
"LAT" : -22.966426093929,
"ALT" : 2.908335,
"GMRotation" : 0
},
"geometry" : {
"type" : "Point",
"coordinates" : [
-44.4361637206427,
-22.966426093929
]
}
}
>
8.2.7 Seeing the cities of the State of Rio de Janeiro on the map
To see the cities of the State of Rio de Janeiro on the map was created the ibge_cidades_rj.py script in Python that generates the GPX (GPS eXchange Format) file ibge_cidades_rj.gpx. The map can be seen below in OpenStreetMap.