Here we’ll cover building a mesh from some spatial data, modifying its parameters and plotting it and the data on an interactive map.
Load in spatial data - CSV
To load data into the mesh builder we need to make sure it’s in the
correct format. This can be in the form of a
SpatialPolygonsDataFrame
or a
SpatialPointsDataFrame
or just a normal
data.frame
or tibble
. Let’s load in some data
that covers the Tibetan plateau from CSV.
First we download the data from our data repository. And then load in
the data using read.csv
fdmr::retrieve_tutorial_data(dataset = "tibetan_lakes")
##
## Tutorial data extracted to /home/runner/fdmr/tutorial_data/tibetan_lakes
lakes_data_filepath <- fdmr::get_tutorial_datapath(dataset = "tibetan_lakes", filename = "example_lake_data.csv")
lakes_data <- read.csv(lakes_data_filepath)
Now let’s have a look at the names of the columns in the
data.frame
.
names(lakes_data)
## [1] "X.1" "X" "precip" "year"
## [5] "HYBAS_ID" "NEXT_DOWN" "NEXT_SINK" "MAIN_BAS"
## [9] "DIST_SINK" "DIST_MAIN" "SUB_AREA" "UP_AREA"
## [13] "PFAF_ID" "ENDO" "COAST" "ORDER"
## [17] "SORT" "geometry" "centroid_lon" "centroid_lat"
## [21] "Unnamed..0" "lake_growth" "lake_decline" "all"
## [25] "region" "growth_ratio" "decline_ratio" "water_balance"
## [29] "water_balance_m3"
To create the mesh we need to pass in the latitude and longitude data
to the fmesher::fmesher::fm_mesh_2d_inla
function. Looking at the column names we can see that
centroid_long
and centroid_lat
look like
likely candidates. Let’s have a quick look at them.
## centroid_lon centroid_lat
## 1 89.01703 42.85078
## 2 98.39610 40.92234
## 3 77.07896 37.33994
## 4 89.62283 31.04314
## 5 93.44273 29.92501
## 6 76.11543 38.76519
That looks correct. By default mesh_builder
will look
for "LAT"
and "LONG"
columns and raise an
error if it can’t find them. To avoid this we can either change the
names of the columns in our data or pass in the names of those columns
when we call the function.
Let’s call the mesh_builder
function and pass in the
data and tell it to use centroid_long
and
centroid_lat
as the longitude and latitude columns. As we
only have have a data.frame
here we also need to pass in
the coordinate reference system we want to use. We’ll use the WGS84
coordinate reference system and pass in a proj4string
.
We’re now ready to launch the mesh builder app.
NOTE: The mesh builder may take a short time (~ 20s) to build this mesh due to its size.
fdmr::mesh_builder(spatial_data = lakes_data, x_coord = "centroid_lon", y_coord = "centroid_lat")
Loading data - rds
file
In other cases we may be given an .rds
file. Let’s load
in an example from our COVID-19 dataset. This is the same data we
previously used in the COVID-19 tutorial.
fdmr::retrieve_tutorial_data(dataset = "covid")
##
## Tutorial data extracted to /home/runner/fdmr/tutorial_data/covid
sp_data <- fdmr::load_tutorial_data(dataset = "covid", filename = "spatial_data.rds")
head(sp_data)
## An object of class "SpatialPolygonsDataFrame"
## Slot "data":
## MSOA11CD OBJECTID MSOA11NM MSOA11NMW BNG_E
## 1 E02000002 2 Barking and Dagenham 001 Barking and Dagenham 001 548267
## 2 E02000003 3 Barking and Dagenham 002 Barking and Dagenham 002 548259
## 3 E02000004 4 Barking and Dagenham 003 Barking and Dagenham 003 551004
## 4 E02000005 5 Barking and Dagenham 004 Barking and Dagenham 004 548733
## 5 E02000007 6 Barking and Dagenham 006 Barking and Dagenham 006 549697
## 6 E02000008 7 Barking and Dagenham 007 Barking and Dagenham 007 548220
## BNG_N LONG LAT Shape__Are Shape__Len
## 1 189693 0.138759 51.58659 2064081 7071.368
## 2 188522 0.138150 51.57607 2226199 7772.531
## 3 186418 0.176830 51.55644 2599306 6982.412
## 4 186827 0.144269 51.56071 1221861 5599.766
## 5 186608 0.158072 51.55849 1593794 5281.386
## 6 185920 0.136492 51.55270 1459217 6493.072
## GlobalID area_sqkm
## 1 f0ca54f0-1a1e-4c72-8fcb-85e21be6de79 2.064497
## 2 3772a2ec-b052-4000-b62b-2c85ac401a7f 2.226646
## 3 3388e1f6-e578-4907-b271-168756f05856 2.599779
## 4 1af0aed4-60d0-4fd6-b326-4b868968c12f 1.222105
## 5 2b82bb5b-6b32-4d32-9d29-0a06145e0a8d 1.594102
## 6 d9cbd087-0359-4f10-9c33-d090764045a5 1.459514
##
## Slot "polygons":
## [[1]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] 0.1394928 51.5885356
##
## Slot "area":
## [1] 0.0002677328
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] 0.1498162 51.59700
## [2,] 0.1510833 51.58708
## [3,] 0.1415709 51.58636
## [4,] 0.1402143 51.57835
## [5,] 0.1351997 51.57974
## [6,] 0.1316113 51.57713
## [7,] 0.1329370 51.58090
## [8,] 0.1276388 51.58164
## [9,] 0.1295286 51.59006
## [10,] 0.1418993 51.59658
## [11,] 0.1481361 51.59905
## [12,] 0.1498162 51.59700
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] 0.1394928 51.5885356
##
## Slot "ID":
## [1] "1"
##
## Slot "area":
## [1] 0.0002677328
##
##
## [[2]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] 0.1412056 51.5749747
##
## Slot "area":
## [1] 0.000288678
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] 0.1483820 51.58075
## [2,] 0.1497524 51.56970
## [3,] 0.1466431 51.56941
## [4,] 0.1498781 51.56807
## [5,] 0.1460282 51.56465
## [6,] 0.1433808 51.56752
## [7,] 0.1369271 51.56706
## [8,] 0.1293623 51.56653
## [9,] 0.1296766 51.56810
## [10,] 0.1316113 51.57713
## [11,] 0.1351997 51.57974
## [12,] 0.1402143 51.57835
## [13,] 0.1415709 51.58636
## [14,] 0.1510833 51.58708
## [15,] 0.1483820 51.58075
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] 0.1412056 51.5749747
##
## Slot "ID":
## [1] "2"
##
## Slot "area":
## [1] 0.000288678
##
##
## [[3]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] 0.1765196 51.5552779
##
## Slot "area":
## [1] 0.0003369077
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] 0.1901795 51.55268
## [2,] 0.1859954 51.54754
## [3,] 0.1680182 51.54731
## [4,] 0.1648860 51.54953
## [5,] 0.1697093 51.55062
## [6,] 0.1636815 51.55278
## [7,] 0.1683205 51.56353
## [8,] 0.1850808 51.56480
## [9,] 0.1826189 51.56131
## [10,] 0.1901795 51.55268
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] 0.1765196 51.5552779
##
## Slot "ID":
## [1] "3"
##
## Slot "area":
## [1] 0.0003369077
##
##
## [[4]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] 0.1430805 51.5617140
##
## Slot "area":
## [1] 0.0001583977
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] 0.1543871 51.56607
## [2,] 0.1478721 51.56108
## [3,] 0.1481942 51.55465
## [4,] 0.1433187 51.55311
## [5,] 0.1452208 51.55581
## [6,] 0.1356112 51.55744
## [7,] 0.1343250 51.56048
## [8,] 0.1369271 51.56706
## [9,] 0.1433808 51.56752
## [10,] 0.1460282 51.56465
## [11,] 0.1498781 51.56807
## [12,] 0.1543871 51.56607
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] 0.1430805 51.5617140
##
## Slot "ID":
## [1] "4"
##
## Slot "area":
## [1] 0.0001583977
##
##
## [[5]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] 0.1566991 51.5578705
##
## Slot "area":
## [1] 0.0002065953
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] 0.1683205 51.56353
## [2,] 0.1636815 51.55278
## [3,] 0.1563493 51.55102
## [4,] 0.1496823 51.55128
## [5,] 0.1481942 51.55465
## [6,] 0.1478721 51.56108
## [7,] 0.1543871 51.56607
## [8,] 0.1618527 51.56160
## [9,] 0.1683205 51.56353
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] 0.1566991 51.5578705
##
## Slot "ID":
## [1] "5"
##
## Slot "area":
## [1] 0.0002065953
##
##
## [[6]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] 0.1351009 51.5530095
##
## Slot "area":
## [1] 0.0001891334
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] 0.1481942 51.55465
## [2,] 0.1496823 51.55128
## [3,] 0.1340892 51.54857
## [4,] 0.1308761 51.54506
## [5,] 0.1257157 51.54725
## [6,] 0.1304090 51.55178
## [7,] 0.1243038 51.55144
## [8,] 0.1269643 51.55787
## [9,] 0.1343250 51.56048
## [10,] 0.1356112 51.55744
## [11,] 0.1452208 51.55581
## [12,] 0.1433187 51.55311
## [13,] 0.1481942 51.55465
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] 0.1351009 51.5530095
##
## Slot "ID":
## [1] "6"
##
## Slot "area":
## [1] 0.0001891334
##
##
##
## Slot "plotOrder":
## [1] 3 2 1 5 6 4
##
## Slot "bbox":
## min max
## x 0.1243038 0.1901795
## y 51.5450603 51.5990480
##
## Slot "proj4string":
## Coordinate Reference System:
## Deprecated Proj.4 representation: +proj=longlat +datum=WGS84 +no_defs
## WKT2 2019 representation:
## GEOGCRS["unknown",
## DATUM["World Geodetic System 1984",
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]],
## ID["EPSG",6326]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8901]],
## CS[ellipsoidal,2],
## AXIS["longitude",east,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433,
## ID["EPSG",9122]]],
## AXIS["latitude",north,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433,
## ID["EPSG",9122]]]]
The sp_data
object is a
SpatialPolygonsDataFrame
containing 6789 features for areas
of England. This object contains all the contains all the values we We
can build a mesh by passing sp_data
to the
mesh_builder
function.
fdmr::mesh_builder(spatial_data = sp_data)
Customise initial parameters
If you want to customise the initial parameters used to build the
mesh you can pass those values to the mesh_builder
function. To see what parameters you can pass to the function you can
use the args
function.
args(fdmr::mesh_builder)
## function (spatial_data, obs_data = NULL, crs = NULL, max_edge = NULL,
## offset = NULL, cutoff = NULL, y_coord = "LAT", x_coord = "LONG")
## NULL
Now we have the spatial data loaded we can think about some initial
parameters for the mesh we’re going to create. We’ll calculate the
initial range and max edge values from our data and pass them into the
mesh_builder
function. These values will then be used as
values for the initial mesh that’s created but can be changed within the
app.
initial_range <- diff(range(sp_data@data[, "LONG"])) / 5
max_edge <- initial_range / 8
max_edge_fin <- c(1, 2) * max_edge
offset <- c(initial_range / 4, initial_range)
cutoff <- max_edge / 7
NOTE: By default we let
fmesher
select the defaults for these values itself. If you encounter long mesh build times try using themeshbuilder
defaults of NULL values formax_edge
etc.
Now we’re ready to start the app.
fdmr::mesh_builder(spatial_data = sp_data, max_edge = max_edge_fin, offset = offset, cutoff = cutoff)