#' @title crop_map_spdf_maker
#' @description A function to plot biodiversity data.
#' @param input df or raster
#' @param crs projection of data provided ('longlat'/'cea'/'auto')
#' @param zoom_out zoom level for the map
#' @examples
#' crop_map_world(df)
#' crop_map_world(df,crs='longlat')
#' @export
crop_map_spdf_maker=function(input, crs='auto',zoom_out=1){
flpth <- system.file('wmap_hr.RDS', package = 'ggg')
load(flpth)
if(any(class(input)=='data.frame')){
df=input
df=input[,c(1,2,3)]
if(grepl(pattern = "(x|long)" , x = colnames(input[2]), ignore.case = T))
df=input[,c(1,3,2)]
colnames(df)=c('species','Latitude','Longitude')
if(crs=='longlat'){
df_coord=df[,3:2]
spdf <- SpatialPointsDataFrame(df_coord, df,proj4string =
CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
map <- worldMap %>% st_crop(extent(spdf))
}
if(crs=='cea') {
df_coord=df[,3:2]
spdf <- SpatialPointsDataFrame(df_coord, df,proj4string =
CRS('+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'))
map = st_transform(worldMap, '+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs')
map <- map %>% st_crop(extent(spdf))
}
if(crs=='auto'){
if (max(df$Latitude)^2>1000){
df_coord=df[,3:2]
spdf <- SpatialPointsDataFrame(df_coord, df,proj4string =
CRS('+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'))
map = st_transform(worldMap, '+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs')
map <- map %>% st_crop(extent(spdf))
}
else{
df_coord=df[,3:2]
spdf <- SpatialPointsDataFrame(df_coord, df,proj4string =
CRS('+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'))
map = st_transform(worldMap, '+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs')
map <- map %>% st_crop(extent(spdf))
}
}#'auto'
}#'daframe'
else{
temp=as.list(bbox(input))
#raster_input
if ((grepl(pattern = '+proj=cea', x = proj4string(input)))|(temp[[1]]^2>1000))
{
r_points = rasterToPoints(input)
r_df = data.frame(r_points)
r_df=r_df[,c(3,2,1)]
df_coord=r_df[,c(2,3)]
spdf <- SpatialPointsDataFrame(df_coord, r_df,proj4string =
CRS('+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'))
worldMap = st_transform(worldMap, '+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs')
map <- worldMap %>% st_crop(extent(input)*zoom_out)
}else{
r_points = rasterToPoints(input)
r_df = data.frame(r_points)
r_df=r_df[,c(3,2,1)]
df_coord=r_df[,c(2,3)]
spdf <- SpatialPointsDataFrame(df_coord, r_df,proj4string =
CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
map <- worldMap %>% st_crop(extent(input)*zoom_out)
}
spdf_sf=st_as_sf(spdf)
map_and_spdfs=list(map,spdf,spdf_sf)
return(map_and_spdfs)
}#end raster if
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.