본문 바로가기

카테고리 없음

Spatial random sampling - GNU R code snippet

### 2 Apr 2012 

### Writen by Alan B. Seo 

 

# Do stratified spatial random sampling on SpatialGrid 

# Associate with each point information from other environmental layers

 

require(raster)

require(rgdal)

 

baseraster.filename <- "/Users/alan/Dropbox/GIS/dem/haeandemfull.img"

baseraster <- raster(baseraster.filename)

 

baseraster.grid <- as(baseraster, 'SpatialGridDataFrame') # It must be converted into a SpatialGridDataFrame class

 

set.seed(2)

n <- 100

randomPoints <- spsample(baseraster.grid, n, type="random") # Sample n random points within the base raster

 

plot(baseraster)

plot(randomPoints, add=T, col="red")

 

aspect <- terrain(baseraster, opt="aspect") # Calculate terrain characteristics,  slope, aspect, TPI, TRI, roughness, flowdir (see Details)

 

 

aspect <- extract(aspect, randomPoints) # Extract raster property

aspect.df <- data.frame(aspect)

 

rp <- SpatialPointsDataFrame(randomPoints, aspect.df) # Combining the points with the extracted data

 

require(calibrate)

textxy(X=rp@coords[,1], Y=rp@coords[,2], labs=rp$aspect)