rbias

Data preparation

Download data from OpenStreetMap

The package offers a simple frontend to download and dissolve vector data from OpenStreetMap via Geofabrik. The function download_geofabrik_data() requires an area of interest to be downloaded and a target directory.

Define target directory

As a first step, we have to define a target directory for the download of the raw data

osm_data_dir <- file.path("data", "raw_data")

and create it if it doesn’t exist already

if (!dir.exists(osm_data_dir)) {
    dir.create(osm_data_dir, 
               recursive = TRUE)
}

Now we can download the data.

Download data

Regions or subregions: Several regions or subregions can be downloaded using the Geofabrik service. A list of all possible areas of interest can be found here. For this example, we use the federal state of Baden-Württemberg as our area of interest

area_of_interest <- "Baden-Württemberg"

and use the area_of_interest as well as the target_dir as input for the download_geofabrik_data() function to download our data

rbias::download_geofabrik_data(area_of_interest,
                               osm_data_dir)

The data can now be found in the target directory.

Data preparation

In order to work with the downloaded OSM data, we dissolve the objects of each layer to a single one. In case the input is a polygon layer, common boundaries of adjacent polygons being dissolved will get erased. The resulting object is Geopackage, consisting of the dissolved layers.

Create target directory

Create a directory where modifications of raw_data and results will be stored:

target_dir <- file.path("data", "derived_data")

and create it if it doesn’t exist already

if (!dir.exists(target_dir)) {
    dir.create(target_dir,
               recursive = TRUE)
}

Dissolve datasets into layers of interest (lois).

Now we have to decide and define which datasets should be dissolved for our future analysis. Currently, possible layers of interest (lois) are

  • landuse as “landuse_a”,
  • railways as “railways” and
  • roads as “roads”.

In addition to the path to our osm_data, our targetdir and the lois, we have to define the coordinate reference system (CRS) of our OSM data using the appropriate an EPSG code.

We can either dissolve a single layer

rbias::dissolve_osmdata(osmdata_path = osm_data_dir,
                        lois = ("landuse_a"),
                        crs = 2062,
                        targetdir = target_dir)

or multiple layers

rbias::dissolve_osmdata(osmdata_path = osm_data_dir,
                        lois = c("landuse_a", "roads", "railways"),
                        crs = 2062,
                        targetdir = target_dir)

Either way, the dissolved OSM data can be found (as a GeoPackage) in the target directory.

Create a bias surface raster

The package offers the possibility to create a bias surface raster with distance to bias features, based on vector data like the one we just downloaded and dissolved. The function bias_surface requires the path to the dissolved vector data as well as information regarding the cell size of the raster which is created.

Therefore, we define the path to dissolved Geopackage dataset

osm_data <- file.path(target_dir, "osm_data_dissolved.gpkg")

To create the bias surface raster, a cell size has to be defined. We can either use an existing reference raster or we define the cell size manually

bias_surface_raster <- rbias::bias_surface(bias = osm_data, 
                                           cellsize = 500)

The function creates an object of the type SpatRaster which we can plot using the plot function of the terra package

terra::plot(bias_surface_raster)

Create a raster with fuzzy values of bias influences

The package can also be used to create a raster with fuzzy values of bias influences using the bias_influence function. Thus, functions from the FuzzyLandscapes package are required.

To create fuzzy rasters with bias influence, we use the bias surface raster object as input. Furthermore, we have to define the range of the bias influence. Hereby, the length depends on the chosen type (see FuzzyLandscapes::fl_create_ras for further information). As type, we define the shape of the fuzzy-influence function, which can be one of:

fuzzy_bias_influence <- rbias::bias_influence(bias_raster = bias_surface_raster,
                                              range = c(1e-2, 1, 1e-2, 0),
                                              type = "bell")

To plot the results, we use the plot_fs function from the FuzzyLandscapes package.

FuzzyLandscapes::plot_fs(fuzzy_bias_influence, 
                         xyrange = c(0, 3000))

Sites vs. background

The sites_vs_background function can be used to create frequency plots for covariates at sampling locations. In this case, the background is simulated as random sampling process.

At first, we load

dem_raster <- terra::rast(file.path(target_dir, "dem_smooth.tif"))

points <- sf::st_read(file.path(target_dir, "shkr.gpkg"))

and plot the digital elevation model and the findings.

terra::plot(dem_raster)
plot(points,
     col = "black",
     add = TRUE)

Now we can use the sites_vs_background function. For this purpose, we need a covariate. Thus, we use a raster containing elevation data of our study area as well the locations of findings. At last, we can define the number of random sampling simulations, which is by default 999.

bias <- rbias::sites_vs_background(covariate = dem_raster,
                                   sites = points,
                                   nsim = 999)

And we can also plot the results

rbias::sites_vs_background_plot(bias)

If we now compare our sites (the solid line) and the median of the random points (dashed line), we can see that the distribution of our sites follows a different pattern and therefore is not random.