This is the process for preparing map data to generate cartograms.
- Step 1: Download map from GADM
- Step 2: Process map in Mapshaper
- Step 3: Generate CSV and JSON file
- Optional: What if my map is NOT from GADM?
- Optional: What if I need to remove regions?
First, visit https://gadm.org/download_country_v3.html and select the country. Click on "Shapefile", which will download a zip file.
Then, visit https://mapshaper.org/ and import the zip file.
Click on the correct layer, which should be gadm36_[country]_1
, as seen in the image below:
You will need to know the desired projection for this map. Then, click on console (upper right corner) and type the following command to project the map, replacing [spatial_reference]
with your chosen spatial reference system:
$ -proj +init=EPSG:[spatial_reference]
Check to make sure that the map is projected correctly.
Type the following command to find out the total number of vertices.
$ -simplify 100% stats
The unique coordinate locations tells you the number of vertices. Use the following equation to calculate the percentage required.
[percentage] = 100 * [desired no. of vertices] / [unique coordinate locations]
So, if you would like 50,000 verticies, then the percentage will be 100 * 50,000 / 573,076 ≈ 9
Then, type the following command to execute the simplification, replacing [percentage]
with the required percentage:
$ -simplify [percentage]% stats
Now, click on export at the top right corner. Select ONLY the correct layer gadm36_[country]_1
and click on SVG file format.
Open the SVG file in Inkscape and check whether the bounding box is sufficiently tight. If yes, proceed to Step 2.4: Export. If not, continue with the following steps.
As we can see in the image above, there is unnecessary white space on the right side of the image. We would like to get rid of that.
In Mapshaper, type the following command:
$ info
You will see a bunch of information pop up. Scroll to the correct layer (make sure it is the right one!) and copy the bounds
information.
Then, type the following command but don't press enter yet.
$ -clip remove-slivers bbox=[paste bounds here]
For example, for Japan, we would get this:
$ -clip remove-slivers bbox=-819424.5537347307,-198081.9783434747,2375994.3457566854,2224098.3370094863
Before pressing enter, you need to edit the command. -clip
will remove everything that is outside the specified bbox. The bbox is in the format xmin, ymin, xmax, ymax
. Since the extra space is in the right side of the map, we need to reduce xmax. We can check what value to put in by placing our cursor over the map. Place your cursor to the rightmost edge which you wish to retain (Refer to the cursor in the image below). Read the x value, round it to give a bit of allowance (round up for xmax and ymax, round down for xmin and ymin), and put it in the command, replacing the old value.
For example, for Japan, we could change it to this:
$ -clip remove-slivers bbox=-819424.5537347307,-198081.9783434747,1210000,2224098.3370094863
You can now press enter. Download the SVG file and check the bounding box again. Repeat as necessary. In this image below, we could remove the islands in the bottom left if we wanted to. We could also keep it in. Once it is satisfactory, proceed to the next step.
In Mapshaper, export the map using the following command, replacing [country_name]
accordingly.
$ -o format=geojson bbox precision=0.1 [country_name].json
Open geojson2csv_cartogram-web.r
in RStudio. Make sure that the GeoJSON file which you just downloaded is in the same folder. Rename file_name
accordingly. Then, click on source. In the console, it should say "All done.".
You will be able to find [country]_data.csv
in your folder. Here, you should check whether the region names are accurate. Open the csv file using a text editor or spreadsheet program, and make sure that you save in the csv format subsequently. Fill in the Region.Data
column with something other than population. At this stage, you should also check whether the region names and abbreviations are correct by comparing with an official source. Editing the names/abbreviations in the csv file is sufficient to correct any typos.
After saving, you now have your [country]_processedmap.json
and [country]_data.csv
files ready to use to generate cartograms!
Please proceed to the Add Map Guide to continue the process.
If you do not have the rgdal package installed, install it using
install.packages("rgdal")
.
If you encounter "Error in ogrListLayers(dsn = dsn) : Cannot open data source", check that the file_name is correct, and that the json file and R script are in the same folder. Also, make sure that you click Session --> Set Working Directory --> To Source File Location.
If your map is not from GADM, you will see the following error in RStudio:
To fix this, look through the properties and values and figure out which property contains the region name and region abbreviation.
Then, edit "nameproperty" and "abbrproperty" accordingly, as seen below.
Then, re-run the program (click on source).
If you need to remove regions, like a small island territory that has a population of 4 people, then perform the following step before Step 2.1: Projection
.
You should know the name of the region which you would like to remove. Then, in mapshaper's console, type in the following command, replacing [region_name]
accordingly:
$ filter 'NAME_1 != "[region_name]"'
If successful, you should see the following message: "[filter] Retained XX of XX features"