###EDA-------------

forest <- read.delim("forest_JMK.txt", header=T, 
                     row.names = 1)
head(forest)

# write.table(forest, file="forest_export.txt", sep = "\t", dec=".")

colnames(forest)

plot(forest[, c("X_WGS84", "Y_WGS84")])

colnames(forest)[6:7] <- c("Y", "X")

plot(forest[, c("X", "Y")], pch=8, main="Vegetation plots",
     col="darkblue")

plot(forest[, c("X", "Y")], type="n", axes=F, 
     main='Vegetation plots', xlab="Longitude", 
     ylab="Latitude")
points(forest[, c("X", "Y")], pch=21, col="darkblue", bg="pink",
       cex=1.2)
axis(1, at=c(16, 17), labels = c("16°E", "17°E"))
axis(2, at=c(49), labels = c("49°N"))
box()

library(berryFunctions)
colPoints(x=forest$X, y=forest$Y, z=forest$Diversity, add=F)

library(viridis)

colPoints(x=forest$X, y=forest$Y, z=forest$Diversity, add=F,
          col=plasma(100))

library(raster)

paths <- list.files(path = "./Grids", full.names = TRUE)
# getwd()

env <- stack(paths)
env

r <- raster("./Grids/Altitude.tif")
r

crs(r) <- "+proj=utm +zone=33 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
env

plot(env$Altitude, col=terrain.colors(100))
plot(env$Altitude, col=plasma(100))

library(sp)
library(spdep)
library(rgdal)

forest
p <- SpatialPointsDataFrame(coords = forest[, c("X", "Y")],
                            data=forest, 
                            proj4string = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
p
plot(p)

#WGS84 - stupne
"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
#UTM zone 33 - metry
"+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs"

p <- spTransform(p, crs(env))
coordinates(p)

plot(env$Altitude)
points(coordinates(p))

forest[, c("x", "Y")] <- coordinates(p)
