sgsPy
structurally guided sampling
Loading...
Searching...
No Matches

Functions

 sgspy.calculate.pca.pca.pca (SpatialRaster rast, int num_comp, str filename='', bool return_metadata=False, dict driver_options=None)
 This functions conducts principal component analysis on the given raster using the covariance matrix.

Detailed Description

Function Documentation

◆ pca()

sgspy.calculate.pca.pca.pca ( SpatialRaster rast,
int num_comp,
str filename = '',
bool return_metadata = False,
dict driver_options = None )

This functions conducts principal component analysis on the given raster using the covariance matrix.

Outputs are automatically scaled and shifted.

A number of output components must be provided as an integer. This integer must be less than or equal to the total number of bands in the input raster, and will be the number of bands in the output raster. A filename may be given to specify an output file location, otherwise a virtual file type will be used. The driver_options parameter is used to specify creation options for a the output raster. See options for the Gtiff driver here: https://gdal.org/en/stable/drivers/raster/gtiff.html#creation-options

Principal components are calculated across all raster bands, along with mean and standard deviation of each raster band. The raster is both centered and scaled, then output values are calculated for each principal component.

Examples

rast = sgspy.SpatialRaster("raster.tif")
pcomp = sgspy.calculate.pca(rast, 3)

rast = sgspy.SpatialRaster("raster.tif")
pcomp, metadata = sgspy.calculate.pca(rast, 2, filename="pca.tif", return_metadata=True) eigenvectors, eigenvalues, means, stdevs = metadata

rast = sgspy.SpatialRaster("raster.tif")
pcomp = sgspy.calculate.pca(rast, 1, filename="pca.tif", driver_options={"COMPRESS": "LZW"})

Parameters

rast : SpatialRaster
raster data structure containing input raster bands

num_comp : int
the number of components

filename : str
output filename or '' if there should not be an output file

return_metadata : bool
whether to return the eigenvectors, eigenvalues, means, and stdevs with the SpatialRaster

driver_options : dict
the creation options as defined by GDAL which will be passed when creating output files

Returns

a SpatialRaster object containing principal component bands