How can I remove the crossing line while using the buffer code below. I tried to create buffers round some point locations and to have a union but ended up getting a crossline.
please see the codes below
train_data
library(raster)
library(dismo)
library(sf)
bioc1 <- getData('worldclim', var='bio', res=5) #
bio1 <- bioc1[[1]]
plot(bio1)
train <- read.csv("forexample_training.csv") # the points locations to be buffered
head(train)
train.sf <- sf::st_as_sf(train, coords=c("longitude", "latitude"), crs=raster::crs(bio1))
plot(train.sf, add=TRUE)
eckertIV <- "+proj=eck4 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
train.sf <- sf::st_transform(train.sf, crs = eckertIV)
train.buf <- sf::st_buffer(train.sf, dist = 500000) %>%
sf::st_union() %>%
sf::st_sf() %>%
sf::st_transform(crs = raster::crs(bio1))
plot(bio1, main = names(bio1))
train.sf <- sf::st_as_sf(train, coords=c("longitude", "latitude"), crs=raster::crs(bio1))
plot(train.sf, add=TRUE)
# To add sf objects to a plot, use add = TRUE
plot(train.buf, border = "red", lwd = 3, add = TRUE)
I'm trying to interpolate my dataset using spatio-temporal kriging approach from gstat and spacetime packages. For the plotting of the results, I use STplot function.
I want to use the mode 'xt' which produce the space/time plots. But when I read about this from the STplot documentation, it states that:
" Beware: when the x-coordinate is plotted, and for each (x,t) [x-coordinate and time - author] element multiple y-coordinates are sent to the plot, it is not clear which (x,y,t) value becomes the plotted value, so slicing single y values is adviced -- no checking is done. "
So how do I do the slicing? It is very much appreciated if anyone can show some examples. Thanks.
my data:
structure(list(LAT = c(-27.6516, -28.125, -28.0402, -30.7834,
-30.3186, -26.799, -29.1691, -27.8048, -32.0287, -30.7785, -26.8046,
-28.4746, -33.7363, -27.8916, -27.1171, -28.5705, -31.5168, -26.2128,
-29.4817, -30.6332, -27.502, -30.0559, -29.9606, -28.3372, -29.8045,
-30.5696, -28.0601, -32.124, -27.901, -27.1366), LONG = c(122.3147,
123.9281, 120.8122, 122.4, 119.0079, 117.9917, 117.6532, 120.7011,
119.9546, 121.8267, 120.268, 117.8563, 117.2423, 121.2715, 119.2709,
119.0009, 120.4255, 120.3706, 119.7581, 119.3093, 121.1075, 119.3646,
119.4459, 121.1211, 119.8226, 121.4668, 119.5071, 119.024, 121.1539,
118.2815), AGE_MA = c(2719L, 2717L, 2712L, 2711L, 2711L, 2711L,
2710L, 2709L, 2708L, 2705L, 2704L, 2704L, 2702L, 2702L, 2701L,
2701L, 2701L, 2700L, 2699L, 2699L, 2699L, 2698L, 2697L, 2695L,
2693L, 2692L, 2691L, 2691L, 2690L, 2689L), EHFI_new = c(1.015,
0.945, 1.02, -3.165, -2.565, -0.865, -0.65, 0.065, -4.52, -0.375,
-2.76, 1.44, -2.03, 2.07, -7.915, -5.215, -2.115, 1.575, -8.785,
-3.185, 2.015, -5.87, -7.855, 2.575, -8.095, 1.42, -6.665, -3.965,
2.45, 0.665)), row.names = c(NA, 30L), class = "data.frame")
Preparing the session
library("spacetime")
library("gstat")
library("sp")
library("xts")
library("rgdal")
#setting the working directory and load the data
mydata <- read.csv("2627.csv")
df <- data.frame(mydata)
values <- df[, c('EHFI_new')]
time <- df[, c('AGE_MA')]
#Assigning the coordinates and projection
coordinates(df) <- c("LONG","LAT")
proj4string(df) <- CRS("+init=epsg:4326")
#Transform the projection
sp.df <- spTransform(df,CRSobj = "+proj=utm +zone=51 +south
+datum=WGS84
+units=m +no_defs")
Creating object STIDF format
#STIDF consist of dataframe (spatialpoint, temporal, data)
#spatialPoint 0bject
sp_sp <- SpatialPoints(sp.df#coords, CRS("+proj=utm +zone=51 +south
+datum=WGS84 +units=m +no_defs"))
#temporal Object
t <- as.POSIXct(time*60*60*24, origin = "1970-01-01")
#data values
data <- data.frame(values=df$EHFI_new)
#'Merging' the objects into the STIDF format
#Creating the STIDF Object - spatial and temporal object, values
stidf <- STIDF(sp_sp, t, data = data)
Making the prediction grid - space and time grid
#space grid
x.range_1 <- as.numeric(c(-608380.858, 860489.815))
y.range_1 <- as.numeric(c(6080795, 7383164))
grd <- expand.grid(x = seq(from = x.range_1[1], to = x.range_1[2], by
= 10000), y = seq(from = y.range_1[1], to = y.range_1[2], by =
10000))
coordinates(grd) <- ~x + y
grd <- SpatialPixels(grd, proj4string = "+proj=utm +zone=51 +south
+datum=WGS84 +units=m
+no_defs")
#time grid
n <- 5
tgrd <- xts(1:n, seq(min(t)-10, max(t)+100, length = n))
#space-time grid
pred.grd <- STF(grd, tgrd)
Variogram model
vs1 <- variogramST(values~1, stidf, tunit="days",
tlags=seq(0,100, length = 7),
assumeRegular = F,
na.omit = T,
cores = 4)
#fitting Variogram modell
ssm <- vgmST("simpleSumMetric",
space = vgm(9,"Gau", 136417, 1.6),
time = vgm(3,"Gau", 18, 7.5),
joint = vgm(3,"Gau", 500, 2),
nugget=1.6, stAni= 18677.26)
ssm_vgm <- fit.StVariogram(vs1, ssm, fit.method = 7,
method = "L-BFGS-B")
STKriging interpolation
EHFI_krg <- krigeST(values~1, data=stidf, newdata=pred.grd,
modelList=ssm_vgm, nmax = 30, stAni = 18677.26,
bufferNmax=3, progress=TRUE)
Space/time plot using STplot
STmap <- stplot(EHFI_krg, mode = "xt", as.table = T, scaleX = 1)
Is it possible to display a projected raster on a projected basemap in R/leaflet? The addRasterImage() functions says that the projection needs to be in EPSG:3857. Can this not be changed by setting project = false? I am able to display projected vector data on a projected basemap, but not raster ...
My attempt:
library(leaflet)
library(raster)
library(sf)
# Find location in northern Canada
ca_df <- data.frame(long = -114.3717401, lat = 62.4525548, name="Yellowknife", stringsAsFactors = F )
ca_pt <- st_as_sf(ca_df,coords = c("long", "lat"), crs = 4326)
# Project to Alaska Polar Stereographic
ca_pt_5936 <- as_Spatial(st_transform(ca_pt, 5936))#coords
# Create raster around point
r_5936 <- raster(
matrix(round(runif(100)), ncol = 10),
xmn = ca_pt_5936[[1]] - 50000, xmx = ca_pt_5936[[1]] + 50000,
ymn = ca_pt_5936[[2]] - 50000, ymx = ca_pt_5936[[2]] + 50000,
crs = "EPSG:5936"
)
# Project raster to Web Mercator (needed to get the extent in lat/long)
r_3857 <- projectRaster(r_5936, crs="EPSG:3857", method = "ngb")
# Prep for leaflet: https://github.com/rstudio/leaflet/issues/550
tile_url <- 'https://services.arcgisonline.com/arcgis/rest/services/Polar/Arctic_Ocean_Base/MapServer/tile/{z}/{y}/{x}.png'
origin <- c(-2.8567784109255e+07, 3.2567784109255e+07)
resolutions <- c(
238810.813354,119405.406677, 59702.7033384999, 29851.3516692501,14925.675834625,
7462.83791731252,3731.41895865639, 1865.70947932806,932.854739664032,
466.427369832148, 233.213684916074, 116.60684245803701, 58.30342122888621,
29.151710614575396, 14.5758553072877, 7.28792765351156, 3.64396382688807,
1.82198191331174, 0.910990956788164, 0.45549547826179, 0.227747739130895,
0.113873869697739, 0.05693693484887, 0.028468467424435)
epsg5936 <- leafletCRS(
crsClass = 'L.Proj.CRS',
code = 'EPSG:5936',
proj4def = '+proj=stere +lat_0=90 +lat_ts=90 +lon_0=-150 +k=0.994 +x_0=2000000 +y_0=2000000 +datum=WGS84 +units=m +no_defs',
origin = origin,
resolutions = resolutions
)
# Map
leaflet(r_3857,
options= leafletOptions(
crs=epsg5936)) %>%
addTiles(urlTemplate = tile_url,
attribution = "Esri, DeLorme, GEBCO, NOAA NGDC, and other contributors",
options = tileOptions(minZoom = 0, maxZoom = 4)) %>%
addRasterImage(r_5936, project = F)
The output doesn't display the raster.
I have this data frame:
structure(list(Nazwa = c("a", "b"), Miejscowosc = c("aaa", "bbb"
), KodPocztowy = c("09-520", "44-207"), Zainstalowano = c("2020-03-20 00:00:00.000",
"2019-02-27 00:00:00.000"), Szczytowa = c(9.14, 4.5), Latitude = c("52.550000",
"50.101860"), Longitude = c("19.700000", "18.546640")), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"))
I read this data from the aaa.xlsx file.
points <- read_xlsx(paste0("From/",qwerty,"/aaa.xlsx"))
Then I try to use the code:
points_sp <- SpatialPoints(points[,c(6,7)], CRS("+init=EPSG:4326"))
Unfortunately I am getting an error:
cannot derive coordinates from non-numeric matrix
How to solve it?
You just need to convert the character strings to numeric and the tbl_df to a matrix or plain data frame:
points$Latitude <- as.numeric(points$Latitude)
points$Longitude <- as.numeric(points$Longitude)
points_sp <- SpatialPoints(as.data.frame(points[,c(6,7)]), CRS("+init=EPSG:4326"))
points_sp
SpatialPoints:
# Latitude Longitude
# [1,] 52.55000 19.70000
# [2,] 50.10186 18.54664
# Coordinate Reference System (CRS) arguments: +proj=longlat +datum=WGS84 +no_defs
So I have the following script trying to prepare the data to do modelling on them later on using "biomod2" package. It tries to overlay species (xlm) presence points on environmental layers (bio1, bio7).
library(biomod2)
library(raster)
data <- structure(list(longitude = c(-122.84,-119.418,-78.6569,-78.1834, -89.3985), latitude = c(45.28,36.7783,37.4316,-1.8312, 32.3547)), .Names = c("X_WGS84",
"Y_WGS84"), class = "data.frame", row.names = c(NA, -5L))
data$xlm<-1
spdf <- SpatialPointsDataFrame(coords = xy, data = data,
proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
myRespName <- 'xlm'
myResp <- as.numeric(data[,myRespName])
myRespXY <- data[,c('X_WGS84','Y_WGS84')]
myExpl = stack( system.file( "external/bioclim/current/bio3.grd", package="biomod2"),
system.file( "external/bioclim/current/bio4.grd", package="biomod2"))
myBiomodData <- BIOMOD_FormatingData(resp.var = myResp,expl.var = myExpl,resp.xy = myRespXY,resp.name = myRespName)
plot(myBiomodData)
It works in this case, however, the issue is when I want to replace "bio4.grd" and "bio3.grd" layers in the package with my own data, it doesn't work. I have tried it as follow. First i thought it might be "asci" file format but even by converting them to "grd" still I don't get the points overlayed on layers stacked. It does overlay only one point in Southern USA (around Louisiana, Mississippi) though.
library(biomod2)
library(raster)
data <- structure(list(longitude = c(-122.84,-119.418,-78.6569,-78.1834, -89.3985), latitude = c(45.28,36.7783,37.4316,-1.8312, 32.3547)), .Names = c("X_WGS84",
"Y_WGS84"), class = "data.frame", row.names = c(NA, -5L))
data$xylella<-1
spdf <- SpatialPointsDataFrame(coords = xy, data = data,
proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
myRespName <- 'xylella'
myResp <- as.numeric(data[,myRespName])
myRespXY <- data[,c('X_WGS84','Y_WGS84')]
bio2<-raster("C:\\Data\\BioClim_V2_MaxEnt\\wc2.0_bio_10m_02.asc")
crs(bio2) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
writeRaster(bio2, "C:\\Data\\BioClim_V2_MaxEnt\\bio2", overwrite=TRUE)
bio7<-raster("C:\\Data\\BioClim_V2_MaxEnt\\wc2.0_bio_10m_07.asc")
crs(bio7) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
writeRaster(bio7, "C:\\Data\\BioClim_V2_MaxEnt\\bio7", overwrite=TRUE)
myExpl = stack (bio2,bio7)
myBiomodData <- BIOMOD_FormatingData(resp.var = myResp,expl.var = myExpl,resp.xy = myRespXY,resp.name = myRespName)
plot(myBiomodData)
Any help highly appreciated. Many thanks.