8.3 Buenos Aires Data

This text shows how to download data with geographic coordinates from site Buenos Aires Data and import to a MongoDB collection.

8.3.1 – Downloading from Buenos Aires Data

The file with the neighborhood data (barrios.csv) was downloaded from Internet page Buenos Aires Data (see featured image).

8.3.2 – Conversion of Buenos Aires Data to GeoJSON format

The file barrios.csv, downloaded from Buenos Aires Data, first was converted from ISO-8859-1 encoding to UTF-8 using the Linux program iconv and, after this, to GeoJSON format using the OGR utility program ogr2ogr from GDAL, which converts simple features data between file formats, as shown bellow:

$ iconv -f ISO-8859-1 -t UTF-8 barrios.csv > barrios_utf8.csv
$ ogr2ogr -f GeoJSON barrios.geojson barrios.vrt

To convert the data from CSV format to GeoJSON using ogr2ogr was necessary to create the VRT file, used above, with the OGR Virtual Format, to associate coordinate system information with the data source. The file barrios.vrt is shown bellow:

<OGRVRTDataSource>
    <OGRVRTLayer name="barrios_utf8">
        <SrcDataSource>barrios_utf8.csv</SrcDataSource>
        <GeometryType>wkbPolygon</GeometryType>
        <LayerSRS>WGS84</LayerSRS>
        <GeometryField encoding="WKT" field='WKT' />
    </OGRVRTLayer>
</OGRVRTDataSource>

8.3.3 – Creating a collection in MongoDB to import the Buenos Aires Data

To import the data downloaded from Buenos Aires Data, the buenosairesbarrios collection was created using the commands:

$ mongo
use reficio;
// Drop the collection, if exists
db.buenosairesbarrios.drop();
// Create the collection
db.createCollection("buenosairesbarrios");

8.3.4 – Importing data

Importing the Buenos Aires Data into buenosairesbarrios collection of MongoDB was done using the mongoimport utility, as shown below:

$ mongoimport \
    --stopOnError \
    --db reficio \
    --collection buenosairesbarrios \
    --file barrios.geojson

producing the output:

2018-05-18T18:20:03.276-0300    connected to: localhost
2018-05-18T18:20:03.406-0300    imported 1 document

8.3.5 – A document for each point

As can be seen above, all data were imported into a single document. To place each neighborhood in a separate document, was created the barrios_porteños collection as shown below:

$ mongo
use reficio;
// Drop the collection, if exists
db.barrios_porteños.drop();
// Create the collection
db.createCollection("barrios_porteños");

and executed the command:

db.buenosairesbarrios.find().forEach(function(data) {
    db.barrios_porteños.insert( data.features );
});

8.3.6 – Example of using a Python script to show the neighborhoods of Buenos Aires

import pymongo
from pymongo import MongoClient
import numpy as np

conn = MongoClient('localhost', 27017)
db = conn.reficio
barrios_porteños = db.barrios_porteños

# Array with the neighborhoods of Buenos Aires
barrios = []
for barrio in barrios_porteños.find():
    barrios.append( np.array(barrio['properties']['BARRIO']) )

# Print the neighborhoods in alphabetical order
for barrio in sorted(barrios):
    print(barrio)

which executed on Python interactive shell produces:

$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> from pymongo import MongoClient
>>> import numpy as np
>>>
>>> conn = MongoClient('localhost', 27017)
>>> db = conn.reficio
>>> barrios_porteños = db.barrios_porteños
>>>
>>> # Array with the neighborhoods of Buenos Aires
... barrios = []
>>> for barrio in barrios_porteños.find():
...     barrios.append( np.array(barrio['properties']['BARRIO']) )
...
>>> # Print the neighborhoods in alphabetical order
... for barrio in sorted(barrios):
...     print(barrio)
...
AGRONOMIA
ALMAGRO
BALVANERA
BARRACAS
BELGRANO
BOCA
BOEDO
CABALLITO
CHACARITA
COGHLAN
COLEGIALES
CONSTITUCION
FLORES
FLORESTA
LINIERS
MATADEROS
MONSERRAT
MONTE CASTRO
NUEVA POMPEYA
NUÑEZ
PALERMO
PARQUE AVELLANEDA
PARQUE CHACABUCO
PARQUE CHAS
PARQUE PATRICIOS
PATERNAL
PUERTO MADERO
RECOLETA
RETIRO
SAAVEDRA
SAN CRISTOBAL
SAN NICOLAS
SAN TELMO
VELEZ SARSFIELD
VERSALLES
VILLA CRESPO
VILLA DEL PARQUE
VILLA DEVOTO
VILLA GRAL. MITRE
VILLA LUGANO
VILLA LURO
VILLA ORTUZAR
VILLA PUEYRREDON
VILLA REAL
VILLA RIACHUELO
VILLA SANTA RITA
VILLA SOLDATI
VILLA URQUIZA
>>>

8.3.7 Perimeter of the neighborhood of Recoleta

To see the perimeter of the neighborhood of Recoleta on the map was created the script perimeter_recoleta.py in Python that generates the GPX (GPS eXchange Format) file perimeter_recoleta.gpx. The map can be seen below in OpenStreetMap.

8. External data