5  What are transforms?

5.1 Motivation

In Part I of the book, we learned that our GeoTable representation of geospatial data provides the data access pattern of the DataFrame, a feature that is very convenient for data science. To recap, let’s consider the following geotable with four random variables:

N = 10000
a = [2randn(NĂ·2) .+ 6; randn(NĂ·2)]
b = [3randn(NĂ·2); 2randn(NĂ·2)]
c = randn(N)
d = c .+ 0.6randn(N)

table = (; a, b, c, d)

gt = georef(table, CartesianGrid(100, 100))
10000Ă—5 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
a b c d geometry
Continuous Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits] [NoUnits]
7.03297 7.04051 -0.869112 -0.882662 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 0.408392 -0.173959 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 -2.36187 -2.44824 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 1.49412 0.414215 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 -0.0586912 -0.735608 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 -0.0816521 -0.777168 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 -0.356284 -0.270099 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 -0.020194 -0.268596 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 1.82179 2.39358 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 -0.409735 -0.784129 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹® â‹® â‹®

We can easily retrieve the “a” column of the geotable as a vector, and plot its histogram:

Mke.hist(gt.a, color = "gray80")
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

We can compute the cross-correlation between columns “a” and “b”:

cor(gt.a, gt.b)
0.013562878782622547

And inspect bivariate distributions of the values of the geotable with PairPlots.jl by Thompson (2023):

using PairPlots

pairplot(values(gt))
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

This pattern is useful to answer geoscientific questions via marginal analysis (i.e. entire columns treated as measurements of a single random variable). However, the answers to many questions in geosciences depend on where the measurements were made.

Attempting to answer geoscientific questions with basic access to rows and columns can be very frustrating. In particular, this approach is prone to unintentional removal of geospatial information:

gt.a
10000-element Vector{Float64}:
  7.032972557322369
  8.706727758687954
  4.529985503587062
  5.916841675658435
  7.181911004526936
  5.582351353152719
  5.2573358011323466
  7.155729789550157
  5.210582840269407
  4.3111079736492695
  8.297983823964238
  6.154972066720248
  8.678944937062152
  â‹®
 -0.6244206954607304
 -0.48936724223065065
 -0.219851391769938
  1.0685506791753638
  0.33986278231395883
 -1.0695528765053315
  1.237818061850718
  0.8581408837004093
  0.5645807320987505
  0.31189564904389755
 -0.658249809153975
 -1.0348130059258036
Note

Any script that is written in terms of direct column access has the potential to discard the special geometry column, and become unreadable very quickly with the use of auxiliary indices for rows.

We propose a new approach to geospatial data science with the concept of transforms, which we introduce in three classes with practical examples:

  1. Feature transforms
  2. Geometric transforms
  3. Geospatial transforms

5.2 Feature transforms

A feature transform is a function that takes the values of the geotable and produces a new set of values over the same geospatial domain. The framework provides over 30 such transforms, ranging from basic selection of columns, to data cleaning, to advanced multivariate statistical transforms.

5.2.1 Basic

Let’s start with two basic and important transforms, Select and Reject. The Select transform can be used to select columns of interest from a geotable:

gt |> Select("a", "b") # select columns "a" and "b"
10000Ă—3 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
a b geometry
Continuous Continuous Quadrangle
[NoUnits] [NoUnits]
7.03297 7.04051 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹®

In the example above, we selected the columns “a” and “b” explicitly, but Select has various methods for more flexible column selection:

gt |> Select(1:3) # select columns 1 to 3
10000Ă—4 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
a b c geometry
Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits]
7.03297 7.04051 -0.869112 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 0.408392 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 -2.36187 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 1.49412 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 -0.0586912 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 -0.0816521 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 -0.356284 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 -0.020194 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 1.82179 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 -0.409735 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹® â‹®
gt |> Select(r"[bcd]") # columns matching regular expression
10000Ă—4 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
b c d geometry
Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits]
7.04051 -0.869112 -0.882662 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
4.9458 0.408392 -0.173959 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.81322 -2.36187 -2.44824 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
-0.695966 1.49412 0.414215 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
-2.80423 -0.0586912 -0.735608 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
-3.15934 -0.0816521 -0.777168 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
3.35897 -0.356284 -0.270099 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
-1.21705 -0.020194 -0.268596 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
0.202474 1.82179 2.39358 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
-0.638023 -0.409735 -0.784129 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹® â‹®

A convenient method is also provided to select and rename columns:

gt |> Select("a" => "A", "b" => "B")
10000Ă—3 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
A B geometry
Continuous Continuous Quadrangle
[NoUnits] [NoUnits]
7.03297 7.04051 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹®

The Reject transform can be used to reject columns from a geotable that are not relevant for a given analysis. It supports the same column specification of Select:

gt |> Reject("b") # reject column "b"
10000Ă—4 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
a c d geometry
Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits]
7.03297 -0.869112 -0.882662 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 0.408392 -0.173959 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 -2.36187 -2.44824 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 1.49412 0.414215 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -0.0586912 -0.735608 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -0.0816521 -0.777168 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 -0.356284 -0.270099 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -0.020194 -0.268596 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 1.82179 2.39358 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.409735 -0.784129 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹® â‹®
Note

Unlike direct column access, the Select and Reject transforms preserve geospatial information.

Tip for all users

The Select transform can be used in conjunction with the viewer to quickly visualize a specific variable:

gt |> Select("a") |> viewer
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

The Rename transform can be used to rename specific columns of a geotable. It preserves all other columns that are not part of the column specification:

gt |> Rename("a" => "A", "b" => "B")
10000Ă—5 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
A B c d geometry
Continuous Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits] [NoUnits]
7.03297 7.04051 -0.869112 -0.882662 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 0.408392 -0.173959 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 -2.36187 -2.44824 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 1.49412 0.414215 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 -0.0586912 -0.735608 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 -0.0816521 -0.777168 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 -0.356284 -0.270099 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 -0.020194 -0.268596 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 1.82179 2.39358 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 -0.409735 -0.784129 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹® â‹® â‹®

The Identity transform can be used as a placeholder to forward the geotable without modifications to the next transform:

gt |> Identity()
10000Ă—5 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
a b c d geometry
Continuous Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits] [NoUnits]
7.03297 7.04051 -0.869112 -0.882662 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 0.408392 -0.173959 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 -2.36187 -2.44824 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 1.49412 0.414215 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 -0.0586912 -0.735608 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 -0.0816521 -0.777168 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 -0.356284 -0.270099 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 -0.020194 -0.268596 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 1.82179 2.39358 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 -0.409735 -0.784129 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹® â‹® â‹®

The RowTable and ColTable transforms change the underlying table representation of the values of the geotable as discussed in the first chapter of the book:

rt = gt |> RowTable()
10000Ă—5 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
a b c d geometry
Continuous Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits] [NoUnits]
7.03297 7.04051 -0.869112 -0.882662 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 0.408392 -0.173959 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 -2.36187 -2.44824 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 1.49412 0.414215 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 -0.0586912 -0.735608 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 -0.0816521 -0.777168 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 -0.356284 -0.270099 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 -0.020194 -0.268596 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 1.82179 2.39358 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 -0.409735 -0.784129 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹® â‹® â‹®
rt |> values |> typeof
Vector{@NamedTuple{a::Float64, b::Float64, c::Float64, d::Float64}} (alias for Array{@NamedTuple{a::Float64, b::Float64, c::Float64, d::Float64}, 1})

The Functional transform can be used to apply a function to columns of a geotable in place:

gt |> Functional(cos) |> values |> pairplot
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

gt |> Functional("a" => cos, "b" => sin) |> values |> pairplot
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

The Map transform can be used to create new columns from existing columns in the geotable. It takes a column specification, calls a function on the selected columns row-by-row, and returns the result as a new column:

gt |> Map("a" => sin, "b" => cos => "cos(b)")
10000Ă—7 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
a b c d sin_a cos(b) geometry
Continuous Continuous Continuous Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits] [NoUnits] [NoUnits] [NoUnits]
7.03297 7.04051 -0.869112 -0.882662 0.681483 0.726675 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 0.408392 -0.173959 0.657918 0.231299 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 -2.36187 -2.44824 -0.983411 0.100662 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 1.49412 0.414215 -0.358204 0.767434 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 -0.0586912 -0.735608 0.782534 -0.943632 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 -0.0816521 -0.777168 -0.644855 -0.999843 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 -0.356284 -0.270099 -0.855155 -0.976467 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 -0.020194 -0.268596 0.765967 0.346418 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 1.82179 2.39358 -0.878447 0.979572 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 -0.409735 -0.784129 -0.920561 0.803275 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹® â‹® â‹® â‹® â‹®
gt |> Map([2, 3] => ((b, c) -> 2b + c) => "f(b, c)")
10000Ă—6 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
a b c d f(b, c) geometry
Continuous Continuous Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits] [NoUnits] [NoUnits]
7.03297 7.04051 -0.869112 -0.882662 13.2119 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 0.408392 -0.173959 10.3 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 -2.36187 -2.44824 7.26457 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 1.49412 0.414215 0.102184 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 -0.0586912 -0.735608 -5.66716 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 -0.0816521 -0.777168 -6.40032 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 -0.356284 -0.270099 6.36165 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 -0.020194 -0.268596 -2.45429 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 1.82179 2.39358 2.22674 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 -0.409735 -0.784129 -1.68578 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹® â‹® â‹® â‹®

The name of the resulting column can be provided or omitted. If the name is omitted like in the example above with the column “a”, it is created by concatenation of column and function names.

Note

The Map transform mimics the behavior of the transform function in DataFrames.jl, except that it always broadcasts the functions to the rows of the selected columns and always produces a single column for each function.

To filter rows in the geotable based on a given predicate (i.e., a function that returns true or false), we can use the Filter transform:

gt |> Filter(row -> row.a < 0 && row.b > 0)
1206Ă—5 GeoTable over 1206 view(::CartesianGrid{2,Float64}, [396, 4119, 5010, 5011, ..., 9983, 9990, 9991, 9994])
a b c d geometry
Continuous Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits] [NoUnits]
-0.045693 0.67769 -2.09922 -2.25369 Quadrangle((95.0, 3.0), ..., (95.0, 4.0))
-1.68436 3.56813 -0.443975 -0.700781 Quadrangle((18.0, 41.0), ..., (18.0, 42.0))
-0.314276 5.0873 0.349054 0.0504785 Quadrangle((9.0, 50.0), ..., (9.0, 51.0))
-1.08172 1.38597 1.69351 0.726771 Quadrangle((10.0, 50.0), ..., (10.0, 51.0))
-0.776412 0.0772544 -0.489086 -0.0267206 Quadrangle((11.0, 50.0), ..., (11.0, 51.0))
-0.401723 0.674267 -0.937714 -0.534763 Quadrangle((12.0, 50.0), ..., (12.0, 51.0))
-0.354803 2.35398 0.432433 0.964838 Quadrangle((13.0, 50.0), ..., (13.0, 51.0))
-1.08917 1.14031 0.260893 0.654874 Quadrangle((15.0, 50.0), ..., (15.0, 51.0))
-0.821409 0.135493 -0.613396 -1.9852 Quadrangle((17.0, 50.0), ..., (17.0, 51.0))
-0.629315 2.78537 -0.0331461 0.519363 Quadrangle((25.0, 50.0), ..., (25.0, 51.0))
â‹® â‹® â‹® â‹® â‹®

To sort rows based on specific columns we can use the Sort transform:

gt |> Sort("a", "b")
10000Ă—5 GeoTable over 10000 view(::CartesianGrid{2,Float64}, [6044, 6898, 6794, 9357, ..., 3625, 2354, 1420, 3311])
a b c d geometry
Continuous Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits] [NoUnits]
-3.44604 3.00793 0.0444115 -0.202782 Quadrangle((43.0, 60.0), ..., (43.0, 61.0))
-3.42403 0.842033 -1.30943 -1.16721 Quadrangle((97.0, 68.0), ..., (97.0, 69.0))
-3.24646 -1.54802 1.1062 0.416365 Quadrangle((93.0, 67.0), ..., (93.0, 68.0))
-3.15564 2.62999 1.59177 0.837413 Quadrangle((56.0, 93.0), ..., (56.0, 94.0))
-3.11385 2.49344 -0.393886 0.841016 Quadrangle((56.0, 78.0), ..., (56.0, 79.0))
-3.10487 1.29962 -0.147484 0.38261 Quadrangle((71.0, 95.0), ..., (71.0, 96.0))
-3.02369 0.816041 -0.187164 -0.297284 Quadrangle((77.0, 97.0), ..., (77.0, 98.0))
-2.99934 0.655763 -2.01968 -2.38655 Quadrangle((48.0, 80.0), ..., (48.0, 81.0))
-2.88437 -0.658308 -0.390809 -1.88726 Quadrangle((40.0, 66.0), ..., (40.0, 67.0))
-2.87935 -4.7041 1.4012 0.856147 Quadrangle((84.0, 67.0), ..., (84.0, 68.0))
â‹® â‹® â‹® â‹® â‹®

This transform accepts all options of the sortperm function in Julia, including the option to sort in reverse order:

gt |> Sort("a", "b", rev=true)
10000Ă—5 GeoTable over 10000 view(::CartesianGrid{2,Float64}, [3311, 1420, 2354, 3625, ..., 9357, 6794, 6898, 6044])
a b c d geometry
Continuous Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits] [NoUnits]
13.906 -2.95281 0.00913763 -0.695402 Quadrangle((10.0, 33.0), ..., (10.0, 34.0))
12.6558 1.32287 -0.252537 -1.00621 Quadrangle((19.0, 14.0), ..., (19.0, 15.0))
12.617 0.282707 0.43943 0.936248 Quadrangle((53.0, 23.0), ..., (53.0, 24.0))
12.3679 3.50374 -0.333697 0.316124 Quadrangle((24.0, 36.0), ..., (24.0, 37.0))
12.2819 0.492711 -0.495305 -1.21366 Quadrangle((36.0, 11.0), ..., (36.0, 12.0))
12.2522 -1.35626 0.00487039 0.0601103 Quadrangle((83.0, 48.0), ..., (83.0, 49.0))
12.1872 -1.49028 -0.698328 -0.565758 Quadrangle((88.0, 17.0), ..., (88.0, 18.0))
12.1355 -2.47029 1.44673 1.87006 Quadrangle((58.0, 47.0), ..., (58.0, 48.0))
12.0114 -0.86515 0.349172 0.433752 Quadrangle((57.0, 44.0), ..., (57.0, 45.0))
11.9931 -1.66335 0.129868 0.344691 Quadrangle((73.0, 43.0), ..., (73.0, 44.0))
â‹® â‹® â‹® â‹® â‹®

5.2.2 Cleaning

Some feature transforms are used to clean the data before geostatistical analysis. For example, the StdNames transform can be used to standardize variable names that are not very readable due to file format limitations. To illustrate this transform, let’s create a geotable with unreadable variable names:

ut = gt |> Select("a" => "aBc De-F", "b" => "b_2 (1)")
10000Ă—3 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
aBc De-F b_2 (1) geometry
Continuous Continuous Quadrangle
[NoUnits] [NoUnits]
7.03297 7.04051 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹®

We can standardize the names with:

ut |> StdNames()
10000Ă—3 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
ABC_DE_F B_2_1 geometry
Continuous Continuous Quadrangle
[NoUnits] [NoUnits]
7.03297 7.04051 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹®

By default the transform, uses the :uppersnake naming convention. Other conventions can be specified depending on personal preference:

ut |> StdNames(:uppercamel)
10000Ă—3 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
AbcDeF B21 geometry
Continuous Continuous Quadrangle
[NoUnits] [NoUnits]
7.03297 7.04051 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹®
ut |> StdNames(:upperflat)
10000Ă—3 GeoTable over 100Ă—100 CartesianGrid{2,Float64}
ABCDEF B21 geometry
Continuous Continuous Quadrangle
[NoUnits] [NoUnits]
7.03297 7.04051 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
8.70673 4.9458 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
4.52999 4.81322 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
5.91684 -0.695966 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
7.18191 -2.80423 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
5.58235 -3.15934 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
5.25734 3.35897 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
7.15573 -1.21705 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
5.21058 0.202474 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.31111 -0.638023 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
â‹® â‹® â‹®

The Replace transform can be used to replace specific values in the geotable by new values that are meaningful to the analysis. For example, we can replace the values -999 and NaN that are used to represent missing values in some file formats:

rt = georef((a=[1,-999,3], b=[NaN,5,6]))
3Ă—3 GeoTable over 3 CartesianGrid{1,Float64}
a b geometry
Categorical Continuous Segment
[NoUnits] [NoUnits]
1 NaN Segment((0.0,), (1.0,))
-999 5.0 Segment((1.0,), (2.0,))
3 6.0 Segment((2.0,), (3.0,))
rt |> Replace(-999 => missing, NaN => missing)
3Ă—3 GeoTable over 3 CartesianGrid{1,Float64}
a b geometry
Categorical Continuous Segment
[NoUnits] [NoUnits]
1 missing Segment((0.0,), (1.0,))
missing 5.0 Segment((1.0,), (2.0,))
3 6.0 Segment((2.0,), (3.0,))

or replace all negative values using a predicate function:

rt |> Replace(<(0) => missing)
3Ă—3 GeoTable over 3 CartesianGrid{1,Float64}
a b geometry
Categorical Continuous Segment
[NoUnits] [NoUnits]
1 NaN Segment((0.0,), (1.0,))
missing 5.0 Segment((1.0,), (2.0,))
3 6.0 Segment((2.0,), (3.0,))
Note

In Julia, the expression <(0) is equivalent to the predicate function x -> x < 0.

Although Replace could be used to replace missing values by new values, there is a specific transform for this purpose named Coalesce:

ct = georef((a=[1,missing,3], b=[4,5,6])) |> Coalesce(value=2)
3Ă—3 GeoTable over 3 CartesianGrid{1,Float64}
a b geometry
Categorical Categorical Segment
[NoUnits] [NoUnits]
1 4 Segment((0.0,), (1.0,))
2 5 Segment((1.0,), (2.0,))
3 6 Segment((2.0,), (3.0,))
Note

Unlike Replace, the Coalesce transform also changes the column type to make sure that no missing values can be stored in the future:

typeof(ct.a)
Vector{Int64} (alias for Array{Int64, 1})

In many applications, it is enough to simply drop all rows for which the selected column values are missing. This is the purpose of the DropMissing transform:

georef((a=[1,missing,3], b=[4,5,6])) |> DropMissing()
2Ă—3 GeoTable over 2 view(::CartesianGrid{1,Float64}, [1, 3])
a b geometry
Categorical Categorical Segment
[NoUnits] [NoUnits]
1 4 Segment((0.0,), (1.0,))
3 6 Segment((2.0,), (3.0,))

The DropNaN is an alternative to drop all rows for which the selected column values are NaN.

5.2.3 Statistical

The framework provides various feature transforms for statistical analysis. We will cover some of these transforms in more detail in Part V of the book with real data. In the following examples we illustrate the most basic statistical transforms with synthetic data.

The Sample transform can be used to sample rows of the geotable at random, with or without replacement depending on the replace option. Other options are available such as rng to set the random number generator and ordered to preserve the order of rows in the original geotable:

gt |> Sample(1000, replace=false) |> viewer
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

Note

Similar to Filter and Sort, the Sample transform is lazy. It simply stores the indices of sampled rows for future construction of the new geotable.

The Center and Scale transforms can be used to standardize the range of values in a geotable. Aliases are provided for specific types of Scale such as MinMax and Interquartile. We can use the describe function to visualize basic statistics before and after the transforms:

gt |> describe
Table with 6 columns and 4 rows:
     variable  mean         minimum   median       maximum  nmissing
   ┌────────────────────────────────────────────────────────────────
 1 │ a         3.00358      -3.44604  2.00567      13.906   0
 2 │ b         -0.0611093   -10.8009  -0.0394989   11.9953  0
 3 │ c         0.00507455   -3.71138  0.0002679    3.38338  0
 4 │ d         0.000517548  -4.44957  -0.00421016  4.72214  0
gt |> Center("a") |> describe
Table with 6 columns and 4 rows:
     variable  mean         minimum   median       maximum  nmissing
   ┌────────────────────────────────────────────────────────────────
 1 │ a         3.63798e-16  -6.44962  -0.997909    10.9024  0
 2 │ b         -0.0611093   -10.8009  -0.0394989   11.9953  0
 3 │ c         0.00507455   -3.71138  0.0002679    3.38338  0
 4 │ d         0.000517548  -4.44957  -0.00421016  4.72214  0
gt |> MinMax() |> describe
Table with 6 columns and 4 rows:
     variable  mean      minimum  median    maximum  nmissing
   ┌─────────────────────────────────────────────────────────
 1 │ a         0.371693  0.0      0.314183  1.0      0
 2 │ b         0.471122  0.0      0.47207   1.0      0
 3 │ c         0.523831  0.0      0.523153  1.0      0
 4 │ d         0.485197  0.0      0.484681  1.0      0

The ZScore transform is similar to the Scale transform, but it uses the mean and the standard deviation to standardize the range:

gt |> ZScore() |> describe
Table with 6 columns and 4 rows:
     variable  mean          minimum   median       maximum  nmissing
   ┌─────────────────────────────────────────────────────────────────
 1 │ a         9.09495e-17   -1.90121  -0.294162    3.2138   0
 2 │ b         1.24345e-17   -4.21629  0.00848397   4.73318  0
 3 │ c         1.56319e-17   -3.73692  -0.00483311  3.3969   0
 4 │ d         -2.06057e-17  -3.82801  -0.00406683  4.0616   0

Another important univariate transform is the Quantile transform, which can be used to convert empirical distribution in a column of the geotable to any given distribution from Distributions.jl by Lin et al. (2023). Selected columns are converted to a Normal distribution by default, but more than 60 distributions are available:

gt |> Quantile() |> values |> pairplot
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

In data science, scientific traits are used to link data types to adequate statistical algorithms. The most popular scientific traits encountered in geoscientific applications are the Continuous and the Categorical scientific traits. To convert (or coerce) the scientific traits of columns in a geotable, we can use the Coerce transform:

using GeoStats.DataScienceTraits: Continuous

st = georef((a=[1,2,2,2,3,3], b=[1,2,3,4,5,6])) |> Coerce("b" => Continuous)
6Ă—3 GeoTable over 6 CartesianGrid{1,Float64}
a b geometry
Categorical Continuous Segment
[NoUnits] [NoUnits]
1 1.0 Segment((0.0,), (1.0,))
2 2.0 Segment((1.0,), (2.0,))
2 3.0 Segment((2.0,), (3.0,))
2 4.0 Segment((3.0,), (4.0,))
3 5.0 Segment((4.0,), (5.0,))
3 6.0 Segment((5.0,), (6.0,))
eltype(st.b)
Float64
Note

All scientific traits are documented in the DataScienceTraits.jl module.

The Levels transform can be used to adjust the categories (or levels) of Categorical columns in case the sampling process does not include all possible values:

st = st |> Levels("a" => [1,2,3,4])
6Ă—3 GeoTable over 6 CartesianGrid{1,Float64}
a b geometry
Categorical Continuous Segment
[NoUnits] [NoUnits]
1 1.0 Segment((0.0,), (1.0,))
2 2.0 Segment((1.0,), (2.0,))
2 3.0 Segment((2.0,), (3.0,))
2 4.0 Segment((3.0,), (4.0,))
3 5.0 Segment((4.0,), (5.0,))
3 6.0 Segment((5.0,), (6.0,))
levels(st.a)
4-element Vector{Int64}:
 1
 2
 3
 4

Another popular transform in statistical learning is the OneHot transform. It converts a Categorical column into multiple columns of true/false values, one column for each level:

st |> OneHot("a")
6Ă—6 GeoTable over 6 CartesianGrid{1,Float64}
a_1 a_2 a_3 a_4 b geometry
Categorical Categorical Categorical Categorical Continuous Segment
[NoUnits] [NoUnits] [NoUnits] [NoUnits] [NoUnits]
true false false false 1.0 Segment((0.0,), (1.0,))
false true false false 2.0 Segment((1.0,), (2.0,))
false true false false 3.0 Segment((2.0,), (3.0,))
false true false false 4.0 Segment((3.0,), (4.0,))
false false true false 5.0 Segment((4.0,), (5.0,))
false false true false 6.0 Segment((5.0,), (6.0,))

A similar transform for Continuous columns is the Indicator transform. It converts the column into multiple columns based on threshold values on the support of the data. By default, the threshold values are computed on a quantile scale:

st |> Indicator("b", k=3, scale=:quantile)
6Ă—5 GeoTable over 6 CartesianGrid{1,Float64}
a b_1 b_2 b_3 geometry
Categorical Categorical Categorical Categorical Segment
[NoUnits] [NoUnits] [NoUnits] [NoUnits]
1 true true true Segment((0.0,), (1.0,))
2 true true true Segment((1.0,), (2.0,))
2 false true true Segment((2.0,), (3.0,))
2 false true true Segment((3.0,), (4.0,))
3 false false true Segment((4.0,), (5.0,))
3 false false true Segment((5.0,), (6.0,))

More advanced statistical transforms such as EigenAnalysis, PCA, DRS, SDS, ProjectionPursuit for multivariate data analysis and Remainder, Closure, LogRatio, ALR, CLR, ILR for compositional data analysis will be covered in future chapters.

5.3 Geometric transforms

While feature transforms operate on the values of the geotable, geometric transforms operate on the geospatial domain. The framework provides various geometric transforms for 2D and 3D space.

5.3.1 Coordinate

A coordinate transform is a geometric transform that modifies the coordinates of all points in the domain without any advanced topological modification (i.e., connectivities are preserved). The most prominent examples of coordinate transforms are Translate, Rotate and Scale.

Let’s load an additional geotable to see these transforms in action:

using GeoIO

bt = GeoIO.load("data/beethoven.ply")

viz(bt.geometry)
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

The Beethoven domain has been saved in the .ply file in a position that is not ideal for visualization. We can rotate this domain with any active rotation specification from Rotations.jl by Koolen et al. (2023) to improve the visualization. For example, we can specify that we want to rotate all points in the mesh by analogy with a rotation between coordinates (0, 1, 0) and coordinates (0, 0, 1):

rt = bt |> Rotate((0, 1, 0), (0, 0, 1))

viz(rt.geometry)
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

Beethoven is now standing up, but still facing the wall. Let’s rotate it once again by analogy between coordinates (1, 0, 0) and (-1, 1, 0):

rt = rt |> Rotate((1, 0, 0), (-1, 1, 0))

viz(rt.geometry)
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

Rotation specifications are also available in 2D space. As an example, we can rotate the 2D grid of our synthetic geotable by the counter clockwise angle π/4:

gt |> Rotate(Angle2d(Ď€/4)) |> viewer
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

In GIS, this new geotable would be called a rotated “raster”. As another example, let’s translate the geotable to the origin of the coordinate system with the Translate transform:

c = centroid(gt.geometry)

gt |> Translate(-coordinates(c)...) |> viewer
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

and scale it with a positive factor for each dimension:

gt |> Scale(0.1, 0.2) |> viewer
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

The StdCoords transform combines Translate and Scale to standardize the coordinates of the domain to the interval [-0.5, 0.5]:

gt |> StdCoords() |> viewer
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

In GIS, another very important coordinate transform is the Proj transform. We will cover this transform in the next chapter because it depends on the concept of map projection, which deserves more attention.

Note

In our framework, the Proj transform is just another coordinate transform. It is implemented with the same code optimizations, and can be used in conjunction with many other transforms that are not available elsewhere.

5.3.2 Advanced

Advanced geometric transforms are provided that change the topology of the domain besides the coordinates of points. Some of these transforms can be useful to repair problematic geometries acquired from sensors in the real world.

The Repair transform is parameterized by an integer K that identifies the repair to be performed. For example, Repair{0}() is a transform that removes duplicated vertices and faces in a domain represented by a mesh. The Repair{9}() on the other hand fixes the orientation of rings in polygonal areas so that the external boundary is oriented counter clockwise and the inner boundaries are oriented clockwise. The list of available repairs will continue to grow with the implementation of new geometric algorithms in the framework.

To understand why geometric transforms are more general than coordinate transforms, let’s consider the following polygonal area with holes:

outer = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]
hole1 = [(0.2, 0.2), (0.4, 0.2), (0.4, 0.4), (0.2, 0.4)]
hole2 = [(0.6, 0.2), (0.8, 0.2), (0.8, 0.4), (0.6, 0.4)]
poly  = PolyArea([outer, hole1, hole2])

viz(poly)
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

We can connect the holes with the external boundary (or ring) using the Bridge transform:

poly |> Bridge(0.01) |> viz
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

By looking at the visualization, we observe that the number of vertices changed to accommodate the so called “bridges” between the rings. The topology also changed as there are no holes in the resulting geometry.

As a final example of advanced geometric transform, we illustrate the TaubinSmoothing transform, which gradually removes sharp boundaries of a manifold mesh:

st = bt |> TaubinSmoothing(30)

fig = Mke.Figure()
viz(fig[1,1], bt.geometry)
viz(fig[1,2], st.geometry)
fig
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

For more advanced geometric transforms, please consult the official documentation.

5.4 Geospatial transforms

Geospatial transforms are those transforms that operate on both the values and the domain of the geotable. They are common in geostatistical workflows that need to remove geospatial “trends” or workflows that need to extract geometries from domains.

As an example, let’s consider the following geotable with a variable z that made of a trend component μ and a noise component ϵ:

# quadratic + noise
r = range(-1, stop=1, length=100)
ÎĽ = [x^2 + y^2 for x in r, y in r]
ϵ = 0.1rand(100, 100)
t = georef((z=μ+ϵ,))

viewer(t)
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

We can use the Detrend transform to remove a trend of polynomial degree 2:

t |> Detrend(degree=2) |> viewer
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

The remaining component can then be modeled with geostatistical models of geospatial correlation, which will be covered in Part IV of the book.

Models of geospatial correlation such as variograms (Hoffimann and Zadrozny 2019) require unique coordinates in the geotable and that is the purpose of the UniqueCoords transform. It removes duplicate points in the geotable and aggregates the values with custom aggregation functions.

Let’s consider the following geotable stored in a .png file to illustrate another geospatial transform:

letters = GeoIO.load("data/letters.png")
44255Ă—2 GeoTable over 265Ă—167 TransformedGrid{2,Float64}
color geometry
Unknown Quadrangle
[NoUnits]
Gray{N0f8}(1.0) Quadrangle((-1.62266e-14, 265.0), ..., (1.0, 265.0))
Gray{N0f8}(1.0) Quadrangle((-1.61653e-14, 264.0), ..., (1.0, 264.0))
Gray{N0f8}(1.0) Quadrangle((-1.61041e-14, 263.0), ..., (1.0, 263.0))
Gray{N0f8}(1.0) Quadrangle((-1.60429e-14, 262.0), ..., (1.0, 262.0))
Gray{N0f8}(1.0) Quadrangle((-1.59816e-14, 261.0), ..., (1.0, 261.0))
Gray{N0f8}(1.0) Quadrangle((-1.59204e-14, 260.0), ..., (1.0, 260.0))
Gray{N0f8}(1.0) Quadrangle((-1.58592e-14, 259.0), ..., (1.0, 259.0))
Gray{N0f8}(1.0) Quadrangle((-1.57979e-14, 258.0), ..., (1.0, 258.0))
Gray{N0f8}(1.0) Quadrangle((-1.57367e-14, 257.0), ..., (1.0, 257.0))
Gray{N0f8}(1.0) Quadrangle((-1.56755e-14, 256.0), ..., (1.0, 256.0))
â‹® â‹®

The Potrace transform can be used to extract complex geometries from a geotable over a 2D Grid. It transforms the Grid domain into a GeometrySet based on any column that contains a discrete set of marker values. In this example, we use the color as the column with markers:

Ab = letters |> Potrace("color", ϵ=0.8)
2Ă—2 GeoTable over 2 GeometrySet{2,Float64}
color geometry
Unknown MultiPolygon
[NoUnits]
Gray{N0f8}(1.0) Multi(4Ă—PolyArea)
Gray{N0f8}(0.0) Multi(2Ă—PolyArea)

The option ϵ controls the deviation tolerance used to simplify the boundaries of the geometries. The higher is the tolerance, the less is the number of segments in the boundary:

viz(Ab.geometry[2], color = "black")
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

In the reverse direction, we have the Rasterize transform, which takes a geotable over a GeometrySet and assigns the geometries to a Grid. In this transform, we can either provide an external grid for the the assignments, or request a grid size to discretize the boundingbox of all geometries:

A = [1, 2, 3, 4, 5]
B = [1.1, 2.2, 3.3, 4.4, 5.5]
p1 = PolyArea((2, 0), (6, 2), (2, 2))
p2 = PolyArea((0, 6), (3, 8), (0, 10))
p3 = PolyArea((3, 6), (9, 6), (9, 9), (6, 9))
p4 = PolyArea((7, 0), (10, 0), (10, 4), (7, 4))
p5 = PolyArea((1, 3), (5, 3), (6, 6), (3, 8), (0, 6))
gt = georef((; A, B), [p1, p2, p3, p4, p5])
5Ă—3 GeoTable over 5 GeometrySet{2,Float64}
A B geometry
Categorical Continuous PolyArea
[NoUnits] [NoUnits]
1 1.1 PolyArea((2.0, 0.0), (6.0, 2.0), (2.0, 2.0))
2 2.2 PolyArea((0.0, 6.0), (3.0, 8.0), (0.0, 10.0))
3 3.3 PolyArea((3.0, 6.0), ..., (6.0, 9.0))
4 4.4 PolyArea((7.0, 0.0), ..., (7.0, 4.0))
5 5.5 PolyArea((1.0, 3.0), ..., (0.0, 6.0))
gt |> viewer
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

nt = gt |> Rasterize(20, 20)
400Ă—3 GeoTable over 20Ă—20 CartesianGrid{2,Float64}
A B geometry
Categorical Continuous Quadrangle
[NoUnits] [NoUnits]
missing missing Quadrangle((0.0, 0.0), ..., (0.0, 0.5))
missing missing Quadrangle((0.5, 0.0), ..., (0.5, 0.5))
missing missing Quadrangle((1.0, 0.0), ..., (1.0, 0.5))
1 1.1 Quadrangle((1.5, 0.0), ..., (1.5, 0.5))
1 1.1 Quadrangle((2.0, 0.0), ..., (2.0, 0.5))
missing missing Quadrangle((2.5, 0.0), ..., (2.5, 0.5))
missing missing Quadrangle((3.0, 0.0), ..., (3.0, 0.5))
missing missing Quadrangle((3.5, 0.0), ..., (3.5, 0.5))
missing missing Quadrangle((4.0, 0.0), ..., (4.0, 0.5))
missing missing Quadrangle((4.5, 0.0), ..., (4.5, 0.5))
â‹® â‹® â‹®
nt |> viewer
┌ Warning: Found `resolution` in the theme when creating a `Scene`. The `resolution` keyword for `Scene`s and `Figure`s has been deprecated. Use `Figure(; size = ...` or `Scene(; size = ...)` instead, which better reflects that this is a unitless size and not a pixel resolution. The key could also come from `set_theme!` calls or related theming functions.
â”” @ Makie ~/.julia/packages/Makie/ND0gA/src/scenes.jl:220

The values of the variables are aggregated at geometric intersections using a default aggregation function, which can be overwritten with an option. Once the geotable is defined over a Grid, it is possible to refine or coarsen the grid with the Downscale and Upscale transforms.

The Transfer of values to a new geospatial domain is another very useful geospatial transform. The Aggregate transform is related, but aggregates the values with given aggregation functions.

5.5 Remarks

In this chapter we learned the important concept of transforms, and saw examples of the concept in action with synthetic data. In order to leverage the large number of transforms implemented in the framework, all we need to do is load our geospatial data as a geotable using georef or GeoIO.jl.

Some additional remarks:

  • One of the major advantages of transforms compared to traditional row/column access in data science is that they preserve geospatial information. There is no need to keep track of indices in arrays to repeatedly reattach values to geometries.
  • Transforms can be organized into three classes—feature, geometric and geospatial—depending on how they operate with the values and the domain of the geotable:
    • Feature transforms operate on the values. They include column selection, data cleaning, statistical analysis and any transform designed for traditional Tables.jl.
    • Geometric transforms operate on the domain. They include coordinate transforms that simply modify the coordinates of points as well as more advanced transforms that can change the topology of the domain.
    • Geospatial transforms operate on both the values and domain. They include geostatistical transforms and transforms that use other columns besides the geometry column to produce new columns and geometries.

In the next chapters, we will review map projections with the Proj coordinate transform, and will introduce one of the greatest features of the framework known as transform pipelines.