Martín González

Visual journalist at The Economist, based in London.

Spanish maps, D3 ready

One of the biggest pain points while making maps with D3 is obtaining the underlying data. Normally this involves outdated government websites and a long, obscure transformation process from Shapefiles to TopoJSON.

To make this easier for everyone who needs to make maps of Spain I spent the last week working on es-atlas, a project heavily based on Mike Bostocks’ world-atlas and us-atlas.

In a nutshell it’s a bash script that downloads and transforms the geometry of the country’s administrative boundaries: municipalities, provinces and autonomous regions. In the end you get a ready to use TopoJSON with the right IDs from the National Statistics Institute.

The boundaries are merged consistently, as I generate the provinces by joining the municipalities and the autonomous regions by joining the provinces. That means that you get the upper administrative divisions for free with the same filesize.

This also plays well with Roger Veciana’s Conic Conformal projection for Spain, which moves the Canary Islands closer to the mainland.

Get the TopoJSONs

Download the repo or clone it, and run npm install inside. Be patient, it has to download a 100mb Shapefile from the Spanish National Geographic Institute.

After that long download the script will generate two files, one with all the municipalities and one with the provinces.

Remember that this is only a starting point. The script provides decent defaults but you should adapt the code if you have other needs. For example, you can generate less simplified geometries or change the polygon properties (city and region names).

The script is written with the latest TopoJSON, which is a bit confusing. To understand how it works you can read this tutorial, made by Ændrew Rininsland.

Bonus: I am also maintaining two URLs with the files: