In order to classify the raster we will get rid of unnecesary outlayers, and change NAs to 0
df.bb <- data.frame(id=c(NA,1,2,3,4,5,6,7,255), v=c(0,1,2,3,4,5,6,7,8))
bb1 <- subs(bb, df.bb,subswithNA=FALSE)
df.bs <- data.frame(id=c(NA,1,2,3,4,255), v=c(0,1,2,3,4,5))
bs1 <- subs(bs, df.bs,subswithNA=FALSE)
df.bc <- data.frame(id=c(NA,1,2,3,4,5,255), v=c(0,1,2,3,4,5,6))
bc1 <- subs(bc, df.bc,subswithNA=FALSE)
roads.v <- spTransform(roads.v, CRS("+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs"))
bb<-projectRaster(bb1, PNF)
bs<-projectRaster(bs1, PNF)
Vegetation_existing<-projectRaster(Vegetation_existing, PNF)
FireReturnIntervalDeparture<-projectRaster(FireReturnIntervalDeparture, PNF)
TreatmentsStorrie<-projectRaster(TreatmentsStorrie, PNF)
bc<-resample(bc, PNF)
bb<-resample(bb, PNF)
bs<-resample(bs, PNF)
Vegetation_existing<-resample(Vegetation_existing, PNF)
FireReturnIntervalDeparture<-resample(FireReturnIntervalDeparture, PNF)
TreatmentsStorrie<-resample(TreatmentsStorrie, PNF)
topo<-resample(topo,PNF)
roads.v <- readOGR(dsn="C:/Users/usuario/Bats_California/layers",layer="Roads")
## OGR data source with driver: ESRI Shapefile
## Source: "C:/Users/usuario/Bats_California/layers", layer: "Roads"
## with 4127 features
## It has 22 fields
## Warning in readOGR(dsn = "C:/Users/usuario/Bats_California/layers", layer
## = "Roads"): Z-dimension discarded
roads.v <- spTransform(roads.v, CRS("+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs"))
plot(PNF)
lines(roads.v)
template <- PNF # this will be the template
template[] <- NA # assigns all values as NA
roads.r <- rasterize(roads.v, template, field=1)
summary(roads.r) # pixels crossed by a road have "1"
## layer
## Min. 1
## 1st Qu. 1
## Median 1
## 3rd Qu. 1
## Max. 1
## NA's 86439
plot(roads.r, add=TRUE)
after rasterizing the roads we make the new raster
roaddist.r <- distance(roads.r)
class(roaddist.r)
## [1] "RasterLayer"
## attr(,"package")
## [1] "raster"
# Check:
plot(roaddist.r)
lines(roads.v)
#Check for correlation between rasters
burn <- stack(bc1, bs1, bb1)
AllLayers <-stack(topo,TreatmentsStorrie,FireReturnIntervalDeparture, Vegetation_existing, roaddist.r)
plot (AllLayers, colNA="black")
pairs(AllLayers)
AllLayers[is.na(AllLayers)] <- 0
plot(AllLayers)
Even though we shouldn’t classify using 3 layers of such high classification we will use the RasterBrick of the three burn classifications to exemplify how we will divide the area into areas of similar characteristics. Here we will ask R to use kmeans to sort the area into 3 types of habitat using the abovementioned rasterbrick:
## v.1 v.2 v.3
## [1,] 0 0 0
## [2,] 0 0 0
## [3,] 0 0 0
## [4,] 0 0 0
## [5,] 0 0 0
## [6,] 0 0 0
## v.1 v.2 v.3 band1.1 band1.2 band1.3 band1.4
## [1,] 0 0 0 0 0 0 0
## [2,] 0 0 0 0 0 0 0
## [3,] 0 0 0 0 0 0 0
## [4,] 0 0 0 0 0 0 0
## [5,] 0 0 0 0 0 0 0
## [6,] 0 0 0 0 0 0 0
## layer
## Min. 1
## 1st Qu. 1
## Median 4
## 3rd Qu. 5
## Max. 5
## NA's 0
More info on how to do this clasification in https://geoscripting-wur.github.io/AdvancedRasterAnalysis/
df.class.4 <- data.frame(id=c(1,2,3,4,5), v=c(NA,NA,NA,4,NA))
class4 <- subs(classes2, df.class.4,subswithNA=FALSE)
points4<-sampleRandom(class4,10, na.rm=TRUE, xy=TRUE)
df.class.3 <- data.frame(id=c(1,2,3,4,5), v=c(NA,NA,3,NA,NA))
class3 <- subs(classes2, df.class.3,subswithNA=FALSE)
points3<-sampleRandom(class3,10, na.rm=TRUE, xy=TRUE)
plot(classes2)
points (points4, col= "black")
points (points3, col= "red")