library("rerddap")
Create a buffer
This post is about how to create a spatial buffer of 1 km around a point.
Intro
In this blog, a buffer subsetting environmental data would be created and the average values within the buffer calculated.
Download data
Load the package rerddap
<- info('erdMWsstd1day_LonPM180') SST_info
To subset the data select the coordinates of a smaller area.
<--111
lonmin<--109
lonmax<-23
latmin<-27 latmax
To download, provide the parameters such as the server, the coordinates and the time frame. It takes some time to download.
<-griddap(SST_info,
SST_valueslatitude= c(latmin, latmax), longitude = c(lonmin, lonmax),
time = c('2023-03-01T00:00:00Z','2023-03-30T00:00:00Z'),
fields = 'sst')
To access the data as data frame, extract the data.
<-SST_values$data SST_df
Using functions from the package tidyverse, remove all NAs.
library(tidyverse)
<-SST_df %>%
SST_dfcleanfilter(sst!='NaN')
Map
Check data downloaded using ggplot.
library(ggplot2)
Download data from land from the package rworldmap.
library(rworldmap)
The function getMap() loads the map in your environment.
<- getMap() world_map
To plot using ggplot a data frame is recommended. Use the function fortify to be able to get the data in a data frame format.
<- fortify(world_map)
world_points $region <- world_points$id
world_points<-world_points[,c("long","lat","group", "region")] world_df
ggplot() +
geom_raster(data=SST_dfclean,aes(x=longitude, y=latitude, fill = sst))+ scale_fill_viridis_c(option = "H")+
geom_polygon(data = world_df, aes(x = long, y = lat, group = group), colour = '#403d39', fill = "#e5e5e5") +
coord_sf(xlim = c(-109.7,-109.1),ylim = c(24.6,25.2))
Create buffer
Load package sp.
library(sp)
Give coordinates from the central point.
<-data.frame(Longitude=-109.37728617302638,Latitude=24.92572600240793) This_point
Use this custom function to create a buffer. Note that it uses epsg 4236, adjust if necessary.
<-function(central_point=central_point, buffer_km=buffer_km){
create_buffer<- sp::SpatialPoints(cbind(central_point$Longitude,central_point$Latitude))
central_spatial::proj4string(central_spatial)= sp::CRS("+init=epsg:4326")
sp<- sp::spTransform(central_spatial, sp::CRS("+init=epsg:4326"))
central_spatial <-sf::st_as_sf(central_spatial)
central_spatial<-buffer_km*1000
buffer_dist<-sf::st_buffer(central_spatial, buffer_dist)
central_bufferreturn(central_buffer)
}
The central point and the kilometers are the arguments needed.
<-create_buffer(central_point=This_point,buffer_km=20) This_buffer
Confirm that the buffer is not a SpatialPolygons.
class(This_buffer)
Map
Use ggplot to create the map.
Use the functions geom_raster to plot the SST data.
The function geom_polygon to plot the base maps data.
The function geom_polygon to plot the buffer.
Adjust to your corresponding area on the coord_sf.
ggplot() +
geom_raster(data=SST_dfclean,aes(x=longitude, y=latitude, fill = sst))+ scale_fill_viridis_c(option = "H")+
geom_polygon(data = world_df, aes(x = long, y = lat, group = group), colour = '#403d39', fill = "#e5e5e5") +
geom_sf(data = This_buffer,
color = "black",
fill = NA, # equivalent to 'transparent'
linetype = "dashed")+
geom_point(data=This_point, aes(x=Longitude, y=Latitude), colour = "black", size = 4)+
coord_sf(xlim = c(-109.7,-109.1),ylim = c(24.6,25.2))
Extract information
Convert the SST data into a SpatialPointsDataFrame.
<- SST_dfclean
SST_sp ::coordinates(SST_sp) <- ~longitude + latitude
sp::proj4string(SST_sp) = sp::CRS("+init=epsg:4326")
sp<-sf::st_as_sf(SST_sp) SST_sp
Use the function over from the package sp to extract the SST data using the buffer.
<-sapply(sf::st_intersects(SST_sp,This_buffer), function(z) if (length(z)==0) NA_integer_ else z[1]) SST_buffer
The SST values that fall inside the buffer are now added as a column named sst_inside in the data frame.
$sst_inside <- as.numeric(SST_buffer) SST_dfclean
The values that fall inside the buffer are shown as 1.
To keep only the data inside the buffer select only those rows with values inside the buffer using the function filter.
<-SST_dfclean %>%
SST_insidefilter(sst_inside==1)%>%
drop_na(sst)
Subset
To plot the SST data, select the information from the new data frame SST_inside and the function geom_raster to plot the information.
ggplot() +
geom_raster(data=SST_inside,aes(x=longitude, y=latitude, fill = sst))+ scale_fill_viridis_c(option = "H")+
geom_polygon(data = world_df, aes(x = long, y = lat, group = group), colour = '#403d39', fill = "#e5e5e5") +
geom_sf(data = This_buffer,
color = "black",
fill = NA, # equivalent to 'transparent'
linetype = "dashed")+
geom_point(data=This_point, aes(x=Longitude, y=Latitude), colour = "black", size = 4)+
theme(panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill='transparent'))+
coord_sf(xlim = c(-109.7,-109.1),ylim = c(24.6,25.2))+
NULL
Calculate mean and sd
Use the values inside the buffer to calculate the average SST.
mean(SST_inside$sst)
sd(SST_inside$sst)
Mean 20.61 and sd 1.65, well done!