|
| 1 | +# Choropleths |
| 2 | + |
| 3 | +### What color is your State? |
| 4 | + |
| 5 | +This example shows an adaptation of the [Average color of World](https://erdavis.com/2021/06/26/average-colors-of-the-world/) examples. |
| 6 | +It differs slightly for the also shown US case likely due to the different data used. Here we retrieve |
| 7 | +the image data (Sentinel 2) from the [EOX WMS server](https://eox.at/) and use the year 2020. |
| 8 | + |
| 9 | +Note how the sates polygons are read directly from source, without any previous download, uncompressing file format conversions, etc... |
| 10 | +We will also filter the states polygons to use only those inside the US continental zone and limit them by number |
| 11 | +of points and with that dropping those polygons of small islands (smaller in area than 10 km^2). |
| 12 | + |
| 13 | +\begin{examplefig}{} |
| 14 | +```julia |
| 15 | +using GMT |
| 16 | + |
| 17 | +# Fetch the state polygons from the US Census Bureau |
| 18 | +D = gmtread("/vsizip//vsicurl/https://www2.census.gov/geo/tiger/GENZ2024/shp/cb_2024_us_state_500k.zip"); |
| 19 | + |
| 20 | +# Filter to keep only the continental US states (except Alaska), with an area larger than 10 km^2. |
| 21 | +Df = filter(D, _region=(-125,-66,24,50), _area=10); |
| 22 | + |
| 23 | +# Fetch the Sentinel 2 image that is used to calculate the average color. Restrain to pixel size of 2000 m. |
| 24 | +wms = wmsinfo("http://tiles.maps.eox.at/wms?"); |
| 25 | +img = wmsread(wms, layer=4, region=(-125,-66,24,50), pixelsize=2000); |
| 26 | + |
| 27 | +# Calculate the average color per State. |
| 28 | +colorzones!(Df, median, img=img) |
| 29 | + |
| 30 | +viz(Df, region=img, proj=:guess, plot=(data=Df, lw=0), title="Sate Color (median)") |
| 31 | +``` |
| 32 | +\end{examplefig} |
| 33 | + |
| 34 | +### Represent colors by average altitude |
| 35 | + |
| 36 | +We can also use the same polygons to represent some other variable, such as the average altitude of each state. |
| 37 | +We will use the [Earth Relief 06m](https://www.generic-mapping-tools.org/remote-datasets/earth-relief.html) dataset |
| 38 | + |
| 39 | +\begin{examplefig}{} |
| 40 | +```julia |
| 41 | +using GMT # Hide |
| 42 | +D = gmtread("/vsizip//vsicurl/https://www2.census.gov/geo/tiger/GENZ2024/shp/cb_2024_us_state_500k.zip"); # Hide |
| 43 | + |
| 44 | +G = gmtread("@earth_relief_06m"); |
| 45 | + |
| 46 | +# Calculate the mean elevation per State. |
| 47 | +Dh = zonal_statistics(G, D, mean); |
| 48 | + |
| 49 | +# Create a color table for the mean elevation values. |
| 50 | +C = makecpt(range=(0, ceil(Dh.ds_bbox[2])), cmap=:bamako); |
| 51 | + |
| 52 | +viz(D, region=(-125,-66,24,50), proj=:guess, levels=Dh, cmap=C, |
| 53 | + plot=(data=D,lw=0), title="Mean Elevation", colorbar=true) |
| 54 | +``` |
| 55 | +\end{examplefig} |
| 56 | + |
| 57 | +The case above was relatively easy because the data was already prepared in a form that GMT could use. |
| 58 | +That is, the `zvals` vector, which together with the colorscale, determines the color of the polygons was |
| 59 | +already in the same order as the polygons themselves (in the GMTdataset `D`). However, often that is not the case, |
| 60 | +and we have the variable that contains the information that we want to _colorize_ in a different source and |
| 61 | +with a different order than that of the polygons in ``D``. So, we need to do a kind of _join_ operation. |
| 62 | +That can be done with _join_ functions or use the internal `polygonlevels` function that links the polygon |
| 63 | +names provide by a stored attribute in ``D`` and the values in a vector or a table that must also have some |
| 64 | +text information (a name) associated with each value. It is that we will do in the next example. |
| 65 | + |
| 66 | + |
| 67 | +\begin{examplefig}{} |
| 68 | +```julia |
| 69 | +using GMT # Hide |
| 70 | +D = gmtread("/vsizip//vsicurl/https://www2.census.gov/geo/tiger/GENZ2024/shp/cb_2024_us_state_500k.zip"); # Hide |
| 71 | + |
| 72 | +# Read a simple text file that has population and State name, one per row. |
| 73 | +pop = gmtread(TESTSDIR * "assets/uspop.csv"); |
| 74 | + |
| 75 | +# Use the polygonlevels function to get the values in the same order as the polygons in D. |
| 76 | +zvals = polygonlevels(D, pop, att="NAME") / 1e6; |
| 77 | + |
| 78 | +# Create a color table for the values in zvals. |
| 79 | +C = makecpt(zvals, auto=:r, reverse =true, cmap=:bamako); |
| 80 | + |
| 81 | +viz(D, region=(-125,-66,24,50), level=zvals, cmap=C, proj=:guess, |
| 82 | + plot=(data=D,lw=0), title="Population (Millions)", colorbar=true) |
| 83 | +``` |
| 84 | +\end{examplefig} |
0 commit comments