Skip to content

Commit 3dc18c3

Browse files
committed
Add another Choropleths tutorial.
1 parent 82abd0c commit 3dc18c3

File tree

6 files changed

+113
-2
lines changed

6 files changed

+113
-2
lines changed

documentation/utilities/getbyattrib.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ NOTE: Instead of ``getbyattrib`` one can use instead ``filter`` (..., `index=fal
2222
Add as many as wished. If using two `kwargs` the second works as a condition. ``(..., NAME=("Antioquia", "Caldas"), feature_id=0)``
2323
means select all elements from ``Antioquia`` and ``Caldas`` that have the attribute `feature_id` = 0.
2424

25+
A second set of attributes can be used to select elements by region, number of polygon vertices and area.
26+
For that, name the keyword with a leading underscore, e.g. `_region`, `_nps`, `_area`. Their values are
27+
passed respectively a 4 elements tuple and numbers. E.g. `_region=(-8.0, -7.0, 37.0, 38.0)`, `_nps=100`, `_area=10`.
28+
Areas are in square km when input is in geographic coordinates, otherwise squre data unites.
29+
2530
- `attrib` or `att`: (OLD SYNTAX) A NamedTuple with the attribname, attribvalue as in `att=(NAME_2="value",)`.
2631
Use more elements if wishing to do a composite match. E.g. `att=(NAME_1="val1", NAME_2="val2")` in which
2732
case only segments matching the two conditions are returned.
@@ -41,6 +46,10 @@ Examples
4146
D = filter(D, NAME_2="Porto");
4247
```
4348

49+
```julia
50+
D = filter(D, _region="(-8.0, -7.0, 37.0, 38.0)", _nps=100);
51+
```
52+
4453
See Also
4554
--------
4655

documentation/utilities/polygonlevels.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# polygonlevels
22

33
```julia
4-
zvals = polygonlevels(D::GDtype, ids::Vector{String}, vals::Vector{<:Real}; kw...) -> Vector{Float64}
4+
zvals = polygonlevels(D::GDtype, ids::VecOrMat{String}, vals::Vector{<:Real}; kw...) -> Vector{Float64}
55

66
or
77

8-
zvals = polygonlevels(D::GDtype, ids::Matrix{String}, vals::Vector{<:Real}; kw...) -> Vector{Float64}
8+
zvals = polygonlevels(D::GDtype, idvals::GMTdataset; kw...) -> Vector{Float64}
99
```
1010

1111
Creates a vector with `zvals` to use in `plot` and where length(zvals) == length(D)
@@ -18,6 +18,10 @@ The elements of `zvals` are made up from the `vals`.
1818

1919
- `vals`: is a vector with the numbers to be used in plot `level` to color the polygons.
2020

21+
- `idvals`: is a GMTdataset with the `text` field containing the ids to match against the `ids` strings.
22+
The first column of `id_vals` must contain the values to be used in `vals`. This is a comodity
23+
function when both the `ids` and `vals` are store in a GMTdataset.
24+
2125
- `attrib` or `att`: keyword to select which attribute to use when matching with contents of the `ids` strings.
2226

2327
- `nocase` or `insensitive`: a keyword from `kw`. Perform a case insensitive comparision between the contents of

tutorials.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@
4545
~~~</a>~~~
4646
@@
4747

48+
@@box
49+
~~~<a class="boxlink" href="choropleths/choropleths/">~~~
50+
@@title US Choropleths@@
51+
@@box-content
52+
~~~
53+
<img src="/tutorials/choropleths/tilelogo.png">
54+
~~~
55+
@@
56+
~~~</a>~~~
57+
@@
58+
4859
@@box
4960
~~~<a class="boxlink" href="dlakelan/GMTMaps/">~~~
5061
@@title @dlakelan choropleths@@

tutorials/choropleths.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Choropleths
2+
3+
{{list_folder_with_images choropleths}}

tutorials/choropleths/choropleths.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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}

tutorials/choropleths/tilelogo.png

73.7 KB
Loading

0 commit comments

Comments
 (0)