Skip to content

RStoolbox

Installing and Loading the RSToolbox Package

# Install the RSToolbox package
install.packages("RSToolbox")

# Load the RSToolbox package
library(RSToolbox)

Reading and Writing Data

Reading Raster Data

# Read a raster file
raster <- raster("path/to/raster/file.tif")

# Read multiple raster files as a stack
stack <- stack(list.files(pattern="*.tif"))

Writing Raster Data

# Write a raster to a file
writeRaster(raster, "output_raster.tif", overwrite=TRUE)

Preprocessing

Normalizing Data

# Normalize raster data
normalized <- normalize(raster)

Principal Component Analysis (PCA)

# Perform PCA on raster data
pca_result <- rasterPCA(raster)

Image Enhancement

# Enhance contrast of raster data
enhanced <- stretch(raster, minq=0.02, maxq=0.98)

Classification

Unsupervised Classification (K-means)

# Perform K-means classification
kmeans_result <- unsuperClass(raster, nSamples=1000, nClasses=5)

Supervised Classification (Random Forest)

# Perform supervised classification using Random Forest
trainingData <- read.csv("path/to/training/data.csv")
rf_model <- superClass(raster, trainData=trainingData, responseCol="class", model="rf")

Accuracy Assessment

Confusion Matrix

# Create a confusion matrix for classification results
referenceData <- read.csv("path/to/reference/data.csv")
conf_matrix <- validateMap(rf_model$map, valData=referenceData, responseCol="class")

Overall Accuracy

# Calculate overall accuracy
overall_acc <- conf_matrix$overallAccuracy
print(overall_acc)

Change Detection

Change Detection Using Difference

# Perform change detection using difference
raster1 <- raster("path/to/raster1.tif")
raster2 <- raster("path/to/raster2.tif")
change <- raster2 - raster1

Change Vector Analysis

# Perform change vector analysis
cva_result <- cva(raster1, raster2)

Visualization

Plotting Rasters

# Plot a single raster layer
plot(raster)

# Plot a raster stack
plot(stack)

Plotting Classification Results

# Plot classification map
plot(rf_model$map)

# Plot classification probability map
plot(rf_model$map$probability)

Plotting PCA Results

# Plot PCA results
plot(pca_result$map)

Advanced Analysis

Texture Analysis

# Perform texture analysis
texture <- glcm(raster, window=c(3,3), statistics="mean")

Spectral Indices

# Calculate NDVI
ndvi <- spectralIndices(raster, red="band3", nir="band4", index="NDVI")

Anomaly Detection

# Perform anomaly detection
anomalies <- anomalyDetection(raster, method="mad")