<commit_before>---
author: roberto
comments: false
date: "2016-04-12"
layout: post
slug: "natural-earth-to-d3-making-a-world-map"
title: "Natural Earth to D3 — Making a world map"
categories:
- Journal
tags:
 -
---

Here’s a few notes on making a world map using D3 based on data from Natural Earth. A lot is based on [Let’s Make a Map](https://bost.ocks.org/mike/map/) by Mike Bostock, but I’ve adapted it and filled in the gaps for my situation.

## Get the data from Natural Earth
Here’s the [main download page for Natural Earth](http://www.naturalearthdata.com/downloads/). Depending on the level of detail you can choose from:

* 110m is the least detailed and the smallest file size.
* 50m is in the middle — quite detailed and OK file size.
* 10m is the most detailed and the largest file size. It’s “suitable for making zoomed-in maps of countries and regions.”

I went for 50m scale. Since I needed countries, I chose [“Admin 0 – Countries”](http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip). In case you’re wondering how this data has evolved you can go through the [changelog](https://github.com/nvkelso/natural-earth-vector/blob/master/CHANGELOG) and see what’s been added and changed.

## Install the tools you need
Mike Bostock’s tutorial has great instructions on [installing gdal and topojson](https://bost.ocks.org/mike/map/#installing-tools) for use on the command line. After following those instructions you should be able to type:

<code>
which ogr2ogr
which topojson
</code>

in your terminal and it will print out the path to those programs. If not, start searching on Stack Overflow for your particular error messages.

## Convert to shapefiles to geojson
Natural Earth data comes as shapefiles, which we can convert to geojson using ogr2ogr. Unzip the Natural Earth files you downloaded and cd into that folder in your terminal window. Then paste this into your window:

<code>
ogr2ogr \
  -f GeoJSON \
  world.json \
  ne_50m_admin_0_countries/ne_50m_admin_0_countries.shp
</code>

We’re taking the shapefiles from the folder ne_50m_admin_0_countries and converting them to geojson and saving it as world.json. You can also do things like filter by [ISO 3166-1 alpha-3 country codes](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) if you only want certain countries, e.g.

<code>
ogr2ogr \
  -f GeoJSON \
 -where “ADM0_A3 IN (‘GBR’, ‘IRL’)” \
  world.json \<commit_msg>Added line break to code<commit_after>---
author: roberto
comments: false
date: "2016-04-12"
layout: post
slug: "natural-earth-to-d3-making-a-world-map"
title: "Natural Earth to D3 — Making a world map"
categories:
- Journal
tags:
 -
---

Here’s a few notes on making a world map using D3 based on data from Natural Earth. A lot is based on [Let’s Make a Map](https://bost.ocks.org/mike/map/) by Mike Bostock, but I’ve adapted it and filled in the gaps for my situation.

## Get the data from Natural Earth
Here’s the [main download page for Natural Earth](http://www.naturalearthdata.com/downloads/). Depending on the level of detail you can choose from:

* 110m is the least detailed and the smallest file size.
* 50m is in the middle — quite detailed and OK file size.
* 10m is the most detailed and the largest file size. It’s “suitable for making zoomed-in maps of countries and regions.”

I went for 50m scale. Since I needed countries, I chose [“Admin 0 – Countries”](http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip). In case you’re wondering how this data has evolved you can go through the [changelog](https://github.com/nvkelso/natural-earth-vector/blob/master/CHANGELOG) and see what’s been added and changed.

## Install the tools you need
Mike Bostock’s tutorial has great instructions on [installing gdal and topojson](https://bost.ocks.org/mike/map/#installing-tools) for use on the command line. After following those instructions you should be able to type:

<code>
which ogr2ogr</br>
which topojson
</code>

in your terminal and it will print out the path to those programs. If not, start searching on Stack Overflow for your particular error messages.

## Convert to shapefiles to geojson
Natural Earth data comes as shapefiles, which we can convert to geojson using ogr2ogr. Unzip the Natural Earth files you downloaded and cd into that folder in your terminal window. Then paste this into your window:

<code>
ogr2ogr \
  -f GeoJSON \
  world.json \
  ne_50m_admin_0_countries/ne_50m_admin_0_countries.shp
</code>

We’re taking the shapefiles from the folder ne_50m_admin_0_countries and converting them to geojson and saving it as world.json. You can also do things like filter by [ISO 3166-1 alpha-3 country codes](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) if you only want certain countries, e.g.

<code>
ogr2ogr \
  -f GeoJSON \
 -where “ADM0_A3 IN (‘GBR’, ‘IRL’)” \
  world.json \
{'fraction_non_alphanumeric': 0.084783488466208, 'fraction_numerical': 0.021448806151355727, 'mean_word_length': 4.192226890756302, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 6, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6615067', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1508, 'n_words': 658}