Skip to content

Instantly share code, notes, and snippets.

@ozjimbob
Created December 15, 2022 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozjimbob/fb064b2cc39f20139408738316fed235 to your computer and use it in GitHub Desktop.
Save ozjimbob/fb064b2cc39f20139408738316fed235 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(terra)
tas <- vect("E:/geodata/Aus_Coastline/tasmania/csttascd_r.shp")
bclm <- rast(list.files("E:/geodata/bioclim/2_5m/",full.names=TRUE))
bclm <- crop(bclm,tas)
ext <- extract(bclm,tas)
ext <- ext[complete.cases(ext),]
ext <- ext[,-1]
pc <- prcomp(ext)
bclm2 <- rast(list.files("E:/geodata/bioclim/2_5m/",full.names=TRUE))
nz <- vect("nz.shp")
bclm_nz <- crop(bclm2,nz)
ext_nz <- extract(bclm2,nz)
ext_nz <- ext_nz[complete.cases(ext_nz),]
ext_nz <- ext_nz[,-1]
pc_nz <- predict(pc,newdata=ext_nz)
pc_dist <- sqrt(pc_nz[,1]^2 + pc_nz[,2]^2)
ext_nz <- extract(bclm2,nz,xy=TRUE)
ext_nz$prdist = pc_dist
tst <- vect(ext_nz,geom=c("x","y"),crs="EPSG:4326")
plot(tst,"prdist")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment