Quick tutorial: displaying events in Barcelona on a map

In my search for easy to use mapping tools I decided to test CartoDB. This is a quick tutorial for importing this week’s event calendar from the City of Barcelona’s Open Data Portal into CartoDB. Here is the result:

The data is in XML (which is not supported by CartoDB) and contains way too much info, so first I had to convert it to CSV.

Here is a short PHP script that will grab the XML and turn it into CSV with event name, latitude and longitude. Other fields could be added easily if necessary.


$fp = fopen('agenda.csv', 'w');
$xml = new SimpleXMLElement(file_get_contents('http://www.bcn.cat/tercerlloc/agendaAvui.xml'));
foreach ($xml->body->resultat->actes->acte as $acte) {
$name = $acte->nom;
$co = $acte->lloc_simple->adreca_simple->coordenades->googleMaps;
fputcsv($fp, array($name, $co['lat'], $co['lon']));
}

You can save this into a file called grab_bcn_events.php and run this from the command line:


php grab_bcn_events.php

It will dump the CSV data into a file called agenda.csv in the same directory as the PHP script.

After creating a CartoDB account you can import to file into it. You’ll need to let CartoDB know which are the location fields by clicking on ‘georeference’. Then you can switch to the map view, and on the right there’s an icon to edit the contents of the info window. If you enable the title field then that piece of information will appear when clicking on the icon in the map.

This entry was posted in Bert Balcaen, Smart Net, Students. Bookmark the permalink. Comments are closed, but you can leave a trackback: Trackback URL.