# Spectral Libraries¶

A Spectral Library is a collection of profiles with arbitrary profile-wise data and metadata, stored as pickled dictionaries inside (multiple) binary fields. Dictionary items are:

• x: list of x values (e.g. wavelength)
• y: list of y values (e.g. surface reflectance)
• xUnit: x value units (e.g. nanometers)
• yUnit: y value units (e.g. ???)
• bbl: the bad bands list

## Spectral Library Window ¶

The Spectral Library Window can be used to visualize, collect and label spectra. It directly interacts with the Map Window(s), which means spectra can be directly collected from an image. Furthermore, external libraries (ENVI Spectral Library) can be imported.

• Add a new spectral library window by using the Add Spectral Library Window button in the toolbar or open a new window from the menu bar View ‣ Add Spectral Library Window.

A new view appears where you can start to collect spectra.

• To import an existing library use the Import Spectral Library button.
• Possible formats to be imported: ENVI Spectral Library, Geopackage, ASD Field Spectrometer measurements, Raster Layer.
• You can also import ASD Field Spectrometer measurements and map and modify the imported profiles and attributes accordingly.

Buttons of the Spectral Library Window:

Button Description
Adds currently overlaid profiles to the spectral library
Activate to add profiles automatically into the spectral library
Import Spectral Library
Save Spectral Library
Activate to change spectra representation
Activate to use colors from map vector symbology
Enter the Spectral Library Layer Properties
Toggle editing mode
Toggle multi editing mode
Save edits
Delete selected features
Cut selected rows to clipboard
Copy selected rows to clipboard
Paste features from clipboard
Select by Expression
Selects all elements in the spectral library
Inverts the current selection
Remove selection (deselect everything)
Move selection to the top
Select / filter features using form
Pan map to selected rows
Zoom map to selected rows
Delete field
Conditional formatting
Actions
Switch to form view
Switch to table view
Spectral Processing Dialog
Enables the calculation of new attribute fields

## Spectral Profile Sources¶

This menu manages the connection between raster sources and spectral library windows.

Buttons of the Profile Sources

Button Description
add a new profile source entry
remove selected entries
Profiles
• Define the input data from where to take the spectral information from.
Style
• Change style of displayed spectra, i.e. symbol and color

Source
• Specify a source raster dataset
• Double-clicking in the cell will open up a dropdown menu where you can select from all loaded raster datasets.
Sampling
• Select Single Profile or Kernel by double-clicking into the cell.
Scaling
• Choose how spectra are sampled.
• Define the scaling factors by setting the Offset and Scale value.
Option Description
SingleProfile Extracts the spectral signature of the pixel at the selected location
Sample3x3 Extracts spectral signatures of the pixel at the selected location and its adjacent pixels in a 3x3 neighborhood.
Sample5x5 Extracts spectral signatures of the pixel at the selected location and its adjacent pixels in a 5x5 neighborhood.
Sample3x3Mean Extracts the mean spectral signature of the pixel at the selected location and its adjacent pixels in a 3x3 neighborhood.
Sample5x5Mean Extracts the mean spectral signature of the pixel at the selected location and its adjacent pixels in a 5x5 neighborhood.

Calculating profiles

Different spectral profiles can be calculated in the Spectral Profile Sources window.

• Add a new column in the attribute table with a meaningful name and select Type Spectral Profile
• In the Spectral Profile Sources window navigate to the newly created attribute, select the Source and switch from Single Profile to Kernel.
• Choose the Kernel you want to use and the Aggregation method.
• Collect spectra in the image and visualize the profiles in different colors using the visualization settings.
• Change the color of the different profiles (see also section Colorize spectra by attribute).

## Working with the Spectral Library¶

### Collecting spectra¶

• Make sure to enable the button in the menu bar and open a raster from which you want to collect spectra in a new Map Window.
• Click on a desired location in the Map Window. The pixels spectral profile at this location will now be shown in the plot in the Library Window. Mind that this will only visualize the spectrum, but nothing is saved at this point.
• To add/save a selected spectrum to the library, click the button: A new table entry on the right of the window is added.
• If spectra should be added automatically to the library while a pixel is selected or clicked, enable the button.

Tip

Have a look at the Spectral Profile Sources window for more advanced settings collecting spectra.

If you want to only collect spectra for one class, e.g. water, define the class in the Spectral Profile Source view under the desired column name, e.g. level_1. If you now click into the image, the spectra is automatically added and named as water in the previously specified column.

### The attribute table¶

• Add additional fields to the table, e.g. in order to add information to every spectrum (id, name, classification label, …).
• Enable the Editing mode by activating .
• Now you can use the button to add a new field (mind the type!).
• After the new column is added, you can add information by double-clicking it.
• To delete a column, use the Delete field button
• When you add a new attribute to the table, you can also choose to use it to store new spectral profiles by checking the Use to store spectral profiles checkbox.
• String, text and binary format can be used to store spectral profiles.

Adding information through layer properties window

It is also possible to add new information to the attribute table in the Layer Properties of the Spectral Library.

• Click on to open the spectral library properties.
• Navigate to the Fields tab and add a new field.

Furthermore, you can define a default widget for the different columns in the attribute table.

• Switch to the Attributes Form tab, select the desired column and choose a certain widget type, e.g. a default range, color, spectral profiles etc.

The field calculator

The field calculator allows you to modify or assess spectra and calculate new columns or modify existing ones.

• Open the field calculator and search for spectralData or spectralMath in the Spectral Libraries tab.

SpectralMath allows you to modify spectral profiles with Python code.

• To use the SpectralMath function, select a field from which the spectral profiles are to be taken, define an expression and the format, e.g. text or binary, in which the new profile is to be saved.

SpectralData returns spectral profile values.

Description Example
Multiply the existing profiles spectralMath(“profiles”, ‘y *=2’, ‘text’)
Create a new profile with x and y values spectralMath(‘x,y=[1,2,3],[20,30,25]’)
Return spectral profile values from map with spectral data from spectral profiles in field column “profiles” spectralData(“profiles”)
Return xUnit string of the spectral profile e.g. ‘nm’ for wavelength unit spectralData(“profiles”)[‘xUnit’]

Selecting spectra

Spectra can be selected in the attribute table and in the plot window itself. Selected spectra will be highlighted (blue background in the table; thicker line in a different color in the plot window).

• Hold the Shift key to select multiple spectra.
• A selection can be removed by clicking the button.
• Selected spectra can be removed by using the button.
• Save the collected spectra with the Save Profiles in Spectral Library button.

Tip

You can inspect an individual value of a spectrum by holding the Alt key and clicking some position along the spectrum

You can also select and filter spectra with the common vector filter and selection tools, e.g. select spectra by expression:

Show coordinates of collected spectra in map view

Locations of spectra (if available) can be visualized as a point layer by right-clicking into the map window, and selecting Add Spectral Library

### Visualization settings¶

The visualization settings of the spectral library allow you to customize the view according to your needs. You can either choose predefined themes, e.g. dark or bright. Or select the color that you want.

• Moreover, activate or deactivate the crosshair and choose a color.
• Once the crosshair ist activated and colored, you can move the crosshair via the control and arrow buttons on your keyboard and save a profile to the speclib with Ctrl + S.
• The Current Profiles section shows you all the spectra that have been collected but do not yet appear in the attribute table. To add the spectra to the attribute table, first activate the button.

Changing the units

You can change the units of the axis by either right-clicking into the spectral library and navigating to the respective option. Or in the visualization settings under General Settings

Colorize spectra by attribute:

Spectra can be colorized according to their attributes, e.g. their class name.

• In the Data Views section on the left, right click on the spectral library data that we are currently using and select the Layer Properties
• Switch to the Symbology tab and select the Categorized renderer at the top
• In the Column droplist select the desired column and click Classify
• Confirm with Ok and close the window.
• In the Spectral Library Window activate the visualization settings with the button.
• Right-click on Color and select Use vector symbol colors .

## Spectral Processing¶

The idea of spectral processing is to use the spectral profiles obtained from a raster image to test and modify raster algorithms, i.e. the spectral profiles are transferred to a temporary raster image and thus different algorithms can be calculated.

Each column of the spectral library attribute table is derived and translated into meaningful temporary raster image values, regardless of the column type.

Text fields (strings) are converted to a one-band classification grid, profile fields are translated to a multi-band grid, and numeric fields are converted to a 1-band grid.

• To use the Spectral Processing tool open and choose the desired algorithm, e.g. Spectral resampling.
• Select the input profiles to be translated to the temporary raster layer and specify the outputs. Select an existing field or enter a name to create a new field.

## Labelled Spectral Library¶

The labelled spectral library extents the default .sli format by adding additional metadata information (e.g., class labels, class colors). This information is stored by adding a .csv and .json file to the default spectral library, so that the labelled spectral library consists of

• .sli file (ENVI standard)

• .hdr file (ENVI standard)

• .csv file (containing the additional information)

• should be comma-separated csv

• should have same basename as .sli file

• first row stores the headers, where the first element has to be the spectra names as specified in the .hdr file:

• Example from the EnMAP-Box test dataset:

• .json file (stores class name and class color information)

• should have same basename as .sli file

• class name and color information should be provided for every attribute in the csv:

{
"attribute_name": {
"categories":  [
[0, "unclassified", [0, 0, 0]],
[1, "class1", [230, 0, 0]],
[2, "class2", [56, 168, 0]],
[3, "class3", [168, 112, 0]],
[4, "class4", [0,100,255]]
],
"no data value": 0,
"description": "Classification"
}

• The keys categories, no data value and description should not be altered. But change attribute_name according to your data.

• no data value should be supplied

• Example from the EnMAP-Box test dataset: