knitr::opts_chunk$set(echo = TRUE)
Utility functions for processing spatial data.
The package can be installed from github:
install.packages("devtools) devtools::install_github("matthewjwhittle/spatialutils")
Load other packages:
library(spatialutils) library(tidyverse) library(sf) library(raster)
x <- c(435532, 325646) y <- c(235353, 265293) points <- xy_to_points(x, y) # View the sf object print(points) # Plot the sf object plot(points)
# Generate a bounding box bbox <- xy_to_points(x = 235353, y = 265293, crs = 27700) %>% # Buffer the point and calculate the bounding box st_buffer(dist = 1000) %>% st_bbox() # Generate random points within the bounding box sample_points <- random_points(bbox = bbox, n_points = 100, seed = 1) # Plot the bounding box and the points plot(st_as_sfc(bbox)) # Convert the bounding box to sfc for plotting # Add the points to the plot plot(sample_points, add =TRUE, col = "blue")
# Buffer the points - which converts them to a polygon buffers <- st_buffer(sample_points, 100) plot(buffers) # Create a model raster with a 100m resolution model_raster <- sf_to_raster(sf = buffers, resolution = 100) # Calculate the cover raster from the sf cover_raster <- get_cover(buffers, model_raster) # Plot the buffers and their cover raster plot(cover_raster) plot(buffers, add = T)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.