install.packages("raster")
install.packages("sp")
install.packages("rgdal")
setwd("~XXXXXX”) r <- raster("Suitability_Prob_v30.tif")
Reading in 16 rasters
r_31 <- raster("Suitability_Prob_v31.tif") r_32 <- raster("Suitability_Prob_v32.tif") r_33 <- raster("Suitability_Prob_v33.tif") r_34 <- raster("Suitability_Prob_v34.tif") r_35 <- raster("Suitability_Prob_v35.tif") r_36 <- raster("Suitability_Prob_v36.tif") r_37 <- raster("Suitability_Prob_v37.tif") r_38 <- raster("Suitability_Prob_v38.tif") r_39 <- raster("Suitability_Prob_v39.tif") r_310 <- raster("Suitability_Prob_v310.tif") r_311 <- raster("Suitability_Prob_v311.tif") r_312 <- raster("Suitability_Prob_v31.tif") r_313 <- raster("Suitability_Prob_v313.tif") r_314 <- raster("Suitability_Prob_v314.tif") r_315 <- raster("Suitability_Prob_v315.tif")
TABLE2 <- read.csv(file=“XXXXX”, header=TRUE, sep=",")
Read in cvs that has farm #'s
install.packages("raster")
install.packages("sp")
install.packages("rgdal")
setwd("~XXXXXX”)
r <- raster("Suitability_Prob_v30.tif") #Reading in 16 rasters
r_31 <- raster("Suitability_Prob_v31.tif")
r_32 <- raster("Suitability_Prob_v32.tif")
r_33 <- raster("Suitability_Prob_v33.tif")
r_34 <- raster("Suitability_Prob_v34.tif")
r_35 <- raster("Suitability_Prob_v35.tif")
r_36 <- raster("Suitability_Prob_v36.tif")
r_37 <- raster("Suitability_Prob_v37.tif")
r_38 <- raster("Suitability_Prob_v38.tif")
r_39 <- raster("Suitability_Prob_v39.tif")
r_310 <- raster("Suitability_Prob_v310.tif")
r_311 <- raster("Suitability_Prob_v311.tif")
r_312 <- raster("Suitability_Prob_v31.tif")
r_313 <- raster("Suitability_Prob_v313.tif")
r_314 <- raster("Suitability_Prob_v314.tif")
r_315 <- raster("Suitability_Prob_v315.tif")
TABLE2 <- read.csv(file=“XXXXX”, header=TRUE, sep=",") #Read in cvs that has farm #'s
Here performing ONLY on the first of the 16 raster files
Set NAs to 0
r[==-3.402823e+38] <- NA r[is.na(r)]=0
r[==-3.402823e+38] <- NA
r[is.na(r)]=0
Calculating number of cells
ncells<-dim(r)[1]*dim(r)[2] ncells
ncells<-dim(r)[1]*dim(r)[2]
ncells
Calling in the # of farms for this state, stored in CSV
TotalFarm<-sum(TABLE2[,'X30']) TotalFarm
TotalFarm<-sum(TABLE2[,'X30'])
TotalFarm
For point selection, set number of cells (ncells) and the number of points to be selected, (specific to each state)
Weighted by the value in the cells:
ptscell = sample(ncells, TotalFarm, prob=r[], replace=TRUE)
ptscell = sample(ncells, TotalFarm, prob=r[], replace=TRUE)
Distribute the points throughout the grid cells
Get the cell half-width:
hs = res(r)/2
hs = res(r)/2
Now find the center of those points:
centers = xyFromCell(r,ptscell)
centers = xyFromCell(r,ptscell)
Generate random uniform points in the cell by using the center and the
half-width/height
pts = cbind(runif(nrow(centers),centers[,1]-hs[1],centers[,1]+hs[1]), runif(nrow(centers),centers[,2]-hs[2],centers[,2]+hs[2]))
pts = cbind(runif(nrow(centers),centers[,1]-hs[1],centers[,1]+hs[1]),
runif(nrow(centers),centers[,2]-hs[2],centers[,2]+hs[2]))
Print and finish:
plot(r) points(pts) print(pts) write.csv(pts, file = "Points_5.txt",row.names=FALSE)
plot(r)
points(pts)
print(pts)
write.csv(pts, file = "Points_5.txt",row.names=FALSE)