Hyperspectral data processing

What are hyperspectral images?

Hyperspectral sensors provide optical radiance data in >200 channels from 350-2500 nm with a spectral resolution of < 10 nm, generating images of contiguous spectral channels often spanning 400–1050 nm (visible to near-infrared) or 400–2500 nm (visible to shortwave-infrared) wavelength range (Figure 1), that can be use to characterize the chemistry of the vegetation, and allow a better discrimination between subtle physiological differences among plant species.

Figure 1. Hyperspectral image is composed by a large number of narrow spectral bands (a) that results in a spectral curve (b) for each image pixel.

What types of variables can be extracted from hyperspectral data?

Hyperspectral data allows to trace, via spectral separability, subtle differences in the leaf pigment, nutrient and structural properties of the vegetation at different levels of aggregation. Such group-specific spectral properties are known as reflectance signatures that can be use to map and monitor vegetation changes from above.

Hyperspectral data has multiple applications, from allowing the differentiation between bare ground and ground with vegetation cover, and invasive vs. native species, to the accurate identification and mapping of single invasive and native species.

The dataset

To create the dataset provided in this section of the tutorial, we generated an artificial hyperspectral image (Figure 2) from the original APEX data of Sylt island (Germany) collected and preprocessed by the Flemish Institute for Technological Research VITO. The artificial image was created by taking a buffer area from the center of the calibration, validation and background points, and then rearranging the buffer squares together with the corresponding points, which resulted in an image with the real hyperspectral values (grid cells) at each point but reorganize in a smaller file (Figure 3). You can download the dataset for this tutorial from HERE.

Figure 2. Creating the artificial image.

Figure 3. Description of the artificial image.

Setting up GRASS GIS within R

It is recommended to first create a folder where the hyperspectral image is located and the outputs and results are saved. Now you can set the working directory in R using the folder you just created:

setwd("/home/garzonc/DIARS/")

Load the rgrass7 library and create a directory that will contain the GRASS GISDBASE for the working session:

library(rgrass7)
dir.create("grassdata")

To initiate GRASS into your R session you need to set the gisBase argument to the GRASS binaries and libraries directory path, this should be something like C:\Program Files\GRASS GIS 7.00beta4 if your are using windows (click HERE for help on how to setup rgrass7 in other operating systems). Note that this will create a new GRASS location called Sylt_island inside the GRASS GISDBASE.

myGRASS <- "/home/garzonc/src/grass7_trunk/dist.x86_64-pc-linux-gnu"
myGISDbase <- "/home/garzonc/Documents/grassData"  #Set the directory path to your GISDbase
myLocation <- "DIARS"  #Name the Location
myMapset <- "PERMANENT"  #Name the mapset

initGRASS(myGRASS, home = tempdir(), gisDbase = myGISDbase, location = myLocation, 
    mapset = myMapset, override = TRUE)
## gisdbase    /home/garzonc/Documents/grassData 
## location    DIARS 
## mapset      PERMANENT 
## rows        170 
## columns     185 
## north       6080249 
## south       6079943 
## west        463975.2 
## east        464308.2 
## nsres       1.8 
## ewres       1.8 
## projection  +proj=utm +no_defs +zone=32 +a=6378137 +rf=298.257223563
## +towgs84=0.000,0.000,0.000 +to_meter=1
execGRASS("g.region", parameters = list(n = "6080248.8", s = "6079942.8", e = "464308.2", 
    w = "463975.2", res = "1.8"))

Importing the hyperspectral data into R

The image provided is ready to use, so you can import it directly into R. Remember to have the image stored in a new folder you created at the beginning of this tutorial. To manage this image (raster file) in R is necessary to install and load the rasterpackage:

library(raster)

Plot the one of the bands (166) of the hyperspectral image:

setwd("/home/garzonc/Desktop/DIARS/")
filelist <- dir(pattern = ".tiff")
mo <- raster(filelist[5], band = 150, package = "raster")
plot(mo, col = colorRampPalette(c("blue", "green", "red"))(255), legend = FALSE)

Exploring the hyperspectral data

Hyperspectral images have many advantages due to the large amount of information contained in each pixel (in this case 248 bands). Lets visualize how all these data might look like. To do that first lets install the raster, rgdal and foreach:

library(raster)
library(rgdal)
library(foreach)

Now lets extract a sample of the bands using the point dataset provided. First, import the point dataset. Lets plot the curve of spectral responses for a sample calibration and background points, that is, the percent reflectance values per point across the range of wavelengths in the hyperspectral image.

setwd("/home/garzonc/Desktop/DIARS/")
spec <- read.table("PointBackground.csv", header = T, sep = " ")[, -c(2:3)]


library(reshape2)
library(ggplot2)
sample <- melt(spec, id = c("type"))

p <- ggplot(sample, aes(sample[, 2], sample[, 3])) + geom_boxplot(aes(fill = sample[, 
    2]), outlier.colour = NULL) + theme(axis.text.x = element_text(angle = 90, 
    hjust = 0), axis.title = element_text(face = "bold", size = 14), title = element_text(face = "bold", 
    size = 16), legend.position = "none") + labs(title = paste("Spectral response"), 
    x = "Wavelength, nm", y = "Response")
p

As you can see from the graph there is a large amount of data/information, this provides a huge potential and at the same time posits major limitations in time and processing power. One of the techniques most commonly used to reduce the dimension of the data, without loosing relevant information, is the Principal Components Analysis (PCA). Some of the variables (bands) might be highly correlated among them and so PCA allows extracting the most relevant variables (in form of components) from a large set of variables available in a data set. Calculate the PCA using the prcomp function using the already extracted values from the previous step:

spec1 <- as.matrix(spec[, -c(1)])
pca <- prcomp(spec1, scale. = T, center = T)
summary(pca)  # Identify the coverage of variance in the dataset by individual principal components                            
## Importance of components:
##                            PC1    PC2     PC3     PC4     PC5     PC6
## Standard deviation     14.6289 5.4589 1.64638 0.79349 0.51266 0.44784
## Proportion of Variance  0.8629 0.1202 0.01093 0.00254 0.00106 0.00081
## Cumulative Proportion   0.8629 0.9831 0.99402 0.99655 0.99761 0.99842
##                            PC7     PC8     PC9    PC10    PC11    PC12
## Standard deviation     0.30434 0.26205 0.20100 0.19221 0.17002 0.14674
## Proportion of Variance 0.00037 0.00028 0.00016 0.00015 0.00012 0.00009
## Cumulative Proportion  0.99880 0.99907 0.99924 0.99939 0.99950 0.99959
##                           PC13    PC14    PC15    PC16    PC17    PC18
## Standard deviation     0.14277 0.11129 0.10043 0.09220 0.07906 0.07331
## Proportion of Variance 0.00008 0.00005 0.00004 0.00003 0.00003 0.00002
## Cumulative Proportion  0.99967 0.99972 0.99976 0.99980 0.99982 0.99984
##                           PC19    PC20    PC21    PC22    PC23    PC24
## Standard deviation     0.06997 0.06672 0.06073 0.05182 0.04905 0.04751
## Proportion of Variance 0.00002 0.00002 0.00001 0.00001 0.00001 0.00001
## Cumulative Proportion  0.99986 0.99988 0.99990 0.99991 0.99992 0.99992
##                           PC25    PC26    PC27    PC28    PC29   PC30
## Standard deviation     0.04510 0.04240 0.04024 0.03719 0.03292 0.0314
## Proportion of Variance 0.00001 0.00001 0.00001 0.00001 0.00000 0.0000
## Cumulative Proportion  0.99993 0.99994 0.99995 0.99995 0.99996 1.0000
##                           PC31    PC32    PC33    PC34    PC35    PC36
## Standard deviation     0.02865 0.02789 0.02577 0.02367 0.02237 0.02044
## Proportion of Variance 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
## Cumulative Proportion  0.99996 0.99997 0.99997 0.99997 0.99997 0.99998
##                           PC37    PC38    PC39    PC40    PC41    PC42
## Standard deviation     0.01967 0.01863 0.01793 0.01627 0.01559 0.01436
## Proportion of Variance 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
## Cumulative Proportion  0.99998 0.99998 0.99998 0.99998 0.99998 0.99998
##                          PC43    PC44    PC45    PC46    PC47    PC48
## Standard deviation     0.0142 0.01386 0.01324 0.01294 0.01261 0.01228
## Proportion of Variance 0.0000 0.00000 0.00000 0.00000 0.00000 0.00000
## Cumulative Proportion  1.0000 0.99998 0.99999 0.99999 0.99999 0.99999
##                           PC49   PC50    PC51    PC52    PC53     PC54
## Standard deviation     0.01152 0.0114 0.01083 0.01052 0.01004 0.009601
## Proportion of Variance 0.00000 0.0000 0.00000 0.00000 0.00000 0.000000
## Cumulative Proportion  0.99999 1.0000 0.99999 0.99999 0.99999 0.999990
##                            PC55     PC56     PC57     PC58     PC59
## Standard deviation     0.009523 0.009263 0.009027 0.008808 0.008628
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  0.999990 0.999990 0.999990 0.999990 0.999990
##                            PC60     PC61     PC62     PC63     PC64
## Standard deviation     0.008437 0.008301 0.008261 0.007928 0.007763
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  0.999990 0.999990 0.999990 0.999990 0.999990
##                            PC65     PC66     PC67     PC68     PC69
## Standard deviation     0.007553 0.007508 0.007444 0.007352 0.007197
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  0.999990 0.999990 0.999990 0.999990 0.999990
##                            PC70     PC71     PC72     PC73     PC74
## Standard deviation     0.007123 0.006974 0.006858 0.006759 0.006704
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  0.999990 0.999990 0.999990 0.999990 0.999990
##                            PC75     PC76     PC77     PC78    PC79
## Standard deviation     0.006593 0.006395 0.006327 0.006293 0.00621
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.00000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.000000 1.00000
##                            PC80     PC81     PC82     PC83     PC84
## Standard deviation     0.006201 0.006057 0.005945 0.005805 0.005748
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.000000 1.000000
##                            PC85     PC86     PC87     PC88     PC89
## Standard deviation     0.005657 0.005512 0.005457 0.005419 0.005338
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.000000 1.000000
##                            PC90     PC91     PC92     PC93     PC94
## Standard deviation     0.005291 0.005137 0.005064 0.005044 0.004964
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.000000 1.000000
##                            PC95     PC96     PC97     PC98     PC99
## Standard deviation     0.004918 0.004842 0.004777 0.004663 0.004617
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.000000 1.000000
##                           PC100    PC101    PC102    PC103    PC104
## Standard deviation     0.004545 0.004541 0.004475 0.004391 0.004334
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.000000 1.000000
##                           PC105   PC106    PC107    PC108    PC109
## Standard deviation     0.004276 0.00422 0.004009 0.003978 0.003905
## Proportion of Variance 0.000000 0.00000 0.000000 0.000000 0.000000
## Cumulative Proportion  1.000000 1.00000 1.000000 1.000000 1.000000
##                           PC110    PC111    PC112    PC113    PC114
## Standard deviation     0.003859 0.003791 0.003772 0.003713 0.003611
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.000000 1.000000
##                           PC115    PC116    PC117    PC118    PC119
## Standard deviation     0.003506 0.003463 0.003408 0.003348 0.003327
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.000000 1.000000
##                           PC120    PC121    PC122    PC123    PC124
## Standard deviation     0.003295 0.003176 0.003065 0.003008 0.002977
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.000000 1.000000
##                           PC125    PC126    PC127   PC128    PC129
## Standard deviation     0.002946 0.002892 0.002821 0.00269 0.002632
## Proportion of Variance 0.000000 0.000000 0.000000 0.00000 0.000000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.00000 1.000000
##                           PC130   PC131    PC132    PC133    PC134
## Standard deviation     0.002616 0.00257 0.002527 0.002444 0.002424
## Proportion of Variance 0.000000 0.00000 0.000000 0.000000 0.000000
## Cumulative Proportion  1.000000 1.00000 1.000000 1.000000 1.000000
##                           PC135    PC136    PC137    PC138   PC139   PC140
## Standard deviation     0.002346 0.002226 0.002198 0.002107 0.00202 0.00197
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.00000 0.00000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.000000 1.00000 1.00000
##                           PC141    PC142    PC143    PC144    PC145
## Standard deviation     0.001947 0.001842 0.001793 0.001749 0.001617
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.000000 1.000000
##                           PC146  PC147   PC148    PC149    PC150    PC151
## Standard deviation     0.001548 0.0015 0.00145 0.001378 0.001346 0.001187
## Proportion of Variance 0.000000 0.0000 0.00000 0.000000 0.000000 0.000000
## Cumulative Proportion  1.000000 1.0000 1.00000 1.000000 1.000000 1.000000
##                           PC152    PC153    PC154     PC155     PC156
## Standard deviation     0.001161 0.001089 0.001013 0.0009727 0.0009526
## Proportion of Variance 0.000000 0.000000 0.000000 0.0000000 0.0000000
## Cumulative Proportion  1.000000 1.000000 1.000000 1.0000000 1.0000000
##                            PC157     PC158     PC159     PC160   PC161
## Standard deviation     0.0008628 0.0007341 0.0006595 0.0006137 0.00057
## Proportion of Variance 0.0000000 0.0000000 0.0000000 0.0000000 0.00000
## Cumulative Proportion  1.0000000 1.0000000 1.0000000 1.0000000 1.00000
##                            PC162     PC163    PC164     PC165     PC166
## Standard deviation     0.0005528 0.0005001 0.000422 0.0004137 0.0004099
## Proportion of Variance 0.0000000 0.0000000 0.000000 0.0000000 0.0000000
## Cumulative Proportion  1.0000000 1.0000000 1.000000 1.0000000 1.0000000
##                            PC167     PC168     PC169     PC170     PC171
## Standard deviation     0.0003796 0.0003488 0.0002875 0.0002759 0.0002324
## Proportion of Variance 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## Cumulative Proportion  1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
##                            PC172     PC173     PC174     PC175     PC176
## Standard deviation     0.0002077 0.0001912 0.0001456 0.0001319 0.0001211
## Proportion of Variance 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## Cumulative Proportion  1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
##                            PC177     PC178     PC179     PC180     PC181
## Standard deviation     9.576e-05 8.971e-05 7.498e-05 5.807e-05 5.614e-05
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion  1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
##                            PC182     PC183     PC184     PC185     PC186
## Standard deviation     4.066e-05 3.703e-05 3.482e-05 2.242e-05 1.638e-05
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion  1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
##                            PC187     PC188     PC189     PC190     PC191
## Standard deviation     1.523e-05 1.441e-05 1.336e-05 1.311e-05 7.177e-06
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion  1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
##                           PC192     PC193     PC194     PC195     PC196
## Standard deviation     6.44e-06 4.416e-06 4.092e-06 3.426e-06 2.452e-06
## Proportion of Variance 0.00e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion  1.00e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
##                            PC197     PC198   PC199     PC200     PC201
## Standard deviation     1.699e-06 1.199e-06 6.6e-07 4.673e-07 3.114e-07
## Proportion of Variance 0.000e+00 0.000e+00 0.0e+00 0.000e+00 0.000e+00
## Cumulative Proportion  1.000e+00 1.000e+00 1.0e+00 1.000e+00 1.000e+00
##                            PC202     PC203     PC204     PC205     PC206
## Standard deviation     2.385e-07 1.683e-07 1.583e-07 1.475e-07 1.433e-07
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion  1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
##                            PC207     PC208     PC209     PC210     PC211
## Standard deviation     1.387e-07 1.372e-07 1.338e-07 1.311e-07 1.255e-07
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion  1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
##                            PC212     PC213     PC214    PC215    PC216
## Standard deviation     1.241e-07 1.227e-07 1.204e-07 1.19e-07 1.18e-07
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.00e+00 0.00e+00
## Cumulative Proportion  1.000e+00 1.000e+00 1.000e+00 1.00e+00 1.00e+00
##                            PC217     PC218     PC219     PC220     PC221
## Standard deviation     1.165e-07 1.138e-07 1.119e-07 1.097e-07 1.083e-07
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion  1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
##                           PC222     PC223     PC224     PC225     PC226
## Standard deviation     1.06e-07 1.013e-07 9.363e-08 9.127e-08 7.368e-08
## Proportion of Variance 0.00e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion  1.00e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
##                            PC227    PC228     PC229    PC230     PC231
## Standard deviation     6.779e-08 6.31e-08 5.982e-08 5.89e-08 5.669e-08
## Proportion of Variance 0.000e+00 0.00e+00 0.000e+00 0.00e+00 0.000e+00
## Cumulative Proportion  1.000e+00 1.00e+00 1.000e+00 1.00e+00 1.000e+00
##                            PC232     PC233     PC234     PC235    PC236
## Standard deviation     5.448e-08 5.162e-08 5.034e-08 4.975e-08 4.88e-08
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.00e+00
## Cumulative Proportion  1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.00e+00
##                            PC237     PC238     PC239     PC240     PC241
## Standard deviation     4.691e-08 4.627e-08 4.535e-08 4.334e-08 4.252e-08
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion  1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
##                            PC242     PC243     PC244     PC245     PC246
## Standard deviation     4.088e-08 4.048e-08 3.951e-08 3.826e-08 3.728e-08
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion  1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
##                           PC247     PC248
## Standard deviation     3.56e-08 3.516e-08
## Proportion of Variance 0.00e+00 0.000e+00
## Cumulative Proportion  1.00e+00 1.000e+00
pca_red <- prcomp(spec1, scale. = T, center = T, tol = 0.01)  # Calculate only first components, tol=0 
pca_red$sdev[1:11]
##  [1] 14.6289380  5.4588915  1.6463772  0.7934939  0.5126644  0.4478358
##  [7]  0.3043380  0.2620548  0.2009957  0.1922074  0.1700204

The PCA results shows the variance contained in the first component and the weight of each variable (band) contributing to the component. Based on those results we can exclude the bands that contribute the least.

The artificial image is already provided as part of the tutorial dataset so you don’t need to perform the process explained below. This is only aimed to guide those interested in developing an artificial image with their own data

Creating an artificial hyperspectral image - Optional (advanced GRASS GIS and bash programming skills)

The artificial image developed as part of this dataset was created using GRASS GIS software. The following guide requires knowledge of GRASS GIS and bash programming

The workflow presented here (Figure 4) summarizes the steps followed to generate an artificial image:

After location and mapset have been set, import the hyperspectral image and the point data which in this case correspond to the validation, calibration and background locations previously selected.

r.in.gdal -o input=mosaic_Sylt_20140716_v3.1 output=Sylt_mosaic

v.in.ogr input=/home/user/val_pointsDataset.shp output=val_points

Create buffer areas around the points in the point file. Make sure the point is in the middle of a pixel to ensure selecting the exact same number of pixels per buffer area.

%Convert points to raster to select the pixel that overlaps with the point

v.to.rast input=ALLpoints@Sylt type=point output=ALLpoints use=cat

%Convert raster back to points that will be located exactly at the center of the pixel

r.to.vect -v input=ALLpoints@Sylt output=ALLpointsUP type=point

%Create buffer area around the of 4.5 m distance around the recentered points

v.buffer -t input=ALLpointsUP@Sylt output=ALL_buffers distance=4.5

All the buffers are saved as one single layer, but we need to have each buffer area as a separate file. You can do it one by one, using the ` or you can use the following batch script:

for i in `seq 1 646`; do
  v.extract input=ALL_buffers cats=`$`{i} output=Bufdata`$`{i} --o
done 

Now each buffer area is in a single file but the buffer areas are circular, to convert them to squares you can use the following bash script:

for i in `seq 1 646`; do
  num=`expr $num + 1`
  g.region vector=Bufdata`$`{i} res=`$`PIXELSIZE -p
  v.in.region all_bufferUP_`$`num --o
done

%Set region back to the entire image

g.region raster=mosaic_Sylt_20140716_v3.1 -p

Now we can create an empty grid in which the artificial image is going to be put together. The size of the cell (box=9,9) must match the size of the buffer areas and the number of grid cells (grid=34,35) must add up to the total number of points/buffers.

v.mkgrid –o map=ALL_buffers grid=34,35 position=coor coordinates=699402.6,6912889.5 box=9,9

Now each grid cell must be placed in a single file, to accomplish that you can use the same bash script used when extracting the buffer areas:

for i in `seq 1 646`; do
    v.extract input=ALL_buffers@Sylt cats=`$`{i} output=grid_`$`{i} --o
done
The next step is to clip the original raster image using the buffer area files and move those rasters to the a new grid cell. This process must be done per wavelength band (the hyperspectral image has a total of 248 bands), you can do it using the following bash script (the process might take a couple of days depending on the number of bands and buffer areas to clip and move):

x=0
for a in `seq 1 248`; do

  for i in `seq 1 646`; do
    g.region vector=all_bufferUP_`$`{i} -ap
    r.mask vect=all_bufferUP_`$`{i}
    echo "mosaic_Sylt_20140716_v3.`$`{a}"
    r.mapcalc expression="rast_buffer_`$`{a}_`$`{i} = mosaic_Sylt_20140716_v3.`$`{a}" --o
    r.mask -r
    x=`expr $x + 1`
    echo "grid `$`x"
    g.region vector=grid_`$`x
    g.region res=1.8 -ap
    eval `g.region -g`
    r.region map=rast_buffer_`$`{a}_`$`{i} n=`$`n s=n-9 w=e-9 e=`$`e
    g.region raster=mosaic -p
    echo "bufffer `$`{i} band `$`{a}"
    done

echo "Band `$`{a} and `$`x grids done"
x=0
echo "End `$`SECONDS seconds"
done
After all the clipped rasters have been move to the new grid, we can patch them into one single image (per wavelength band). The following bash script allows you to patch all the images and in all the bands. In this particular case we are patching 646 rasters, be careful to adapt the number sequence in this script when working with your own data:

for i in `seq 1 248`; do

  for a in `seq 1 248`; do
    MAPS1=`g.list -e type=raster sep=, pat="^rast_buffer_${a}_([1-9]|[1-9][0-9]|[1-5][0-9][0-9]|6[0-4][0-9])$"`
    g.region raster=$MAPS1
    r.patch in=$MAPS1 out=grid_mosaicC_1_${a} --o
    MAPS2=`g.list -e type=raster sep=, pat="^rast_buffer_${a}_([2-3][0-9][0-9])$"`
    g.region raster=$MAPS2
    r.patch in=$MAPS2 out=grid_mosaicC_2_${a} --o
    MAPS3=`g.list -e type=raster sep=, pat="^rast_buffer_${a}_([4-5][0-9][0-9])$"`
    g.region raster=$MAPS3
    r.patch in=$MAPS3 out=grid_mosaicC_3_${a} --o
    MAPS4=`g.list -e type=raster sep=, pat="^rast_buffer_${a}_(6[0-4][0-9])$"`
    g.region raster=$MAPS4
    r.patch in=$MAPS4 out=grid_mosaicC_4_${a} --o
  echo "grid_mosaicC_${a}"
  done
MAPS=`g.list -e type=raster sep=, pat="^grid_mosaicC_[1-4]_${i}$"`
g.region raster=$MAPS
r.patch in=$MAPS out=grid_mosaicC_all_${i} --o
echo "grid_mosaic_${i}"
done

The resulting artificial raster bands need to be renumbered to avoid misplacing when exporting the image as a tiff file. We will add 00 in front to have it organized in the right band order using the following bash script:

prefix="mosaicC_full_"

for map in `g.list type=raster pattern=${prefix}*`; do
  part=`echo $map | cut -d "_" -f 3`
  numero=`echo $part | awk '{ printf("%04d\n", $1) }'`
  names="${prefix}${numero}"
  g.rename raster=$map,$names
done

We can now verify that the hyperspectral artificial image has the correct band order, and if so, export it:

MAPS=g.list type=raster sep=, pat="mosaicC_full_*" i.group group=mosaic_datasetComp input=$MAPS

i.spectral -g group=mosaic_datasetComp@compiegne coordinates=699412.07161,6912899.83863

r.out.gdal in=mosaic_datasetComp out=mosaic_datasetComp.tiff -c –o

Back to the top

Authors: Carol Garzon-Lopez, Ruben Van De Kerchove, Duccio Rocchini & Jonathan Lenoir

Date: 2018-08-24

Built with RMarkdown and hosted on Github Pages.