Cartograms
Cartograms
Source:
C:\Users\steve\S\TELOS\Python\cartogram_project\notes
TODO
- Global cartos using Penn World Tables
- Use gridded data
- LP approach to shape placement
Story Write-up
What are cartograms?
Responding to Reddit comments
Program design and workflow
Cartogram C
code to Python
C/C++ arrays are row major, meaning rows are stored contiguously in memory.
G&N talk about the horizontal (xsize
) and vertical (ysize
) of a grid to mean the number of columns and rows, respectively, in the corresponding matrix.
Item | C variable | Python variable |
---|---|---|
width | xsize | columns |
height | ysize | rows |
The GN uspop.dat file has 512 rows and 1024 columns, xsize=1024 and ysize=512.
Carography
The Globe
Latitude (altitude) \(\phi\) measures the elevation from the equator, \(-\pi/2\le\phi\le\pi/2\).
Longitude (azimith, heading) \(\lambda\) measures the angle from the prime meridian (Greenwich), positive East and negative West, \(-\pi < \phi \le \pi\). Longitude wraps at \(\pi\) and is undefined at the poles.
Spelling out lat/long is essentially \((y,x)\) in Cartesian coordinates.
- ISO 6709
- ISO 19111
A graticle (graticule) is a grid of lat/long lines.
A geoid is a model of the surface of the ocean accounting for gravity and the rotation of the Earth, but no other effects. It is an idealized surface model. Influences such as winds and tides are ignored.
Data
US Data
There is no FIPS 3…it is reserved for American Samoa. etc.
Python Packages
FFTW
pyfftw
and builders…should be able to reproduce the DCT with some transposing?
Basic Real FFT help: https://www.fftw.org/fftw3_doc/Real_002dto_002dReal-Transforms.html
FFTW_REDFT10
computes an REDFT10
transform, i.e. a DCT-II (sometimes called “the” DCT).
Flag: FFTW_ESTIMATE
specifies that, instead of actual measurements of different algorithms, a simple heuristic is used to pick a (probably sub-optimal) plan quickly. With this flag, the input/output arrays are not overwritten during planning.
R Packages
Rcartogram
, an interface to C code for “fitting” diffusion cartograms to a grid of densities.cartogram
Construct a continuous area cartogram by a rubber sheet distortion algorithm (Dougenik et al. 1985), non-contiguous Area Cartograms (Olson 1976), and non-overlapping Circles Cartogram (Dorling el al. 1996) in R.
Go Cart Program
(from README.md)
Run the cartogram generator at the command line by running cartogram
. You should see the following output:
$ cartogram
USAGE:
Process GeoJSON File: cartogram -p json_file_name
Generate Cartogram: cartogram [-dei] -g map_file_name -a area_file_name
OR:
cartogram [-dei] -g map_file_name -s
-d: use Gastner-Newman (i.e. diffusion) method
-e: generate cartogram figure in EPS format
-i: calculate inverse transform
Note: If you run into issues, look at the Troubleshooting section below.
Running the cartogram generator
To generate a cartogram, you need a map and the associated statistical data you want to visualize. The cartogram generator accepts maps in GeoJSON and ArcInfo Generate formats, and statistical data in CSV format. To generate a cartogram, you need to:
- Process the original map (you only need to do this once)
- Generate a CSV file with the data you want to visualize
- Run the cartogram generator
Processing Your Map (GeoJSON)
Process your GeoJSON map file using the following command:
cartogram -p json_file_name
The cartogram will generate 2 new files: a XX_processedmap.json file and XX_data.csv file. Open the CSV file with your preferred spreadsheet program, and fill in the data value for each region under the ‘Region Data’ column.
Support for the ArcInfo Generate format is provided for backwards compatability with existing maps.
Generating a Cartogram
Once you have processed your map file and generated a CSV file with the data you want to visualize, you can run the following command to generate a cartogram:
cartogram -g processedmap_file_name -a csv_file_name
The generated cartogram will be saved in the same format as the map input in the current working directory with the filename cartogram.json
, if the input map is in GeoJSON format (cartogram.gen
, if the input map is in ArcInfo Generate format).
Examples
For the 2016 US presidential election data, navigate to the sample_data/
directory.
cd sample_data/
Then run the following command. Note that the sample data has already been processed using cartogram -p
cartogram -g usa_contiguous_arcgis.json -a usa_contiguous_electors.csv
To generate an EPS image of the original map and cartogram, use the -e
option:
cartogram -eg usa_contiguous_arcgis.json -a usa_contiguous_electors.csv
Running this command will produce three files:
cartogram.json
contains the cartogram in GeoJSON formatmap.eps
contains the original map in EPS formatcartogram.eps
contains the generated cartogram in EPS format
For our example, map.eps
should look as follows:
cartogram.eps
should look like:
Options
Option | Function |
---|---|
-e | Output an EPS file of the original map (map.eps ) and the cartogram (cartogram.eps ) |
-d | Use Gastner-Newman (i.e. diffusion) instead of fast flow-based method |
-i | Print inverse transform to file |
-g | Location of file for the original map |
-a | Location of the area information file. Cannot be used with -s. |
-s | Read the area information from stdin , and output the cartogram gen file to stdout . Cannot be used with -a. |
References (from cartogram)
Dorling (1996), Dougenik, Chrisman, and Niemeyer (1985), Olson (1976).