|
| 1 | +# ------------------------------------------------------------------ |
| 2 | +# Licensed under the MIT License. See LICENSE in the project root. |
| 3 | +# ------------------------------------------------------------------ |
| 4 | + |
| 5 | +""" |
| 6 | + SpectralIndex(name, [bands...]) |
| 7 | +
|
| 8 | +Compute the spectral index of given `name` from SpectralIndices.jl. |
| 9 | +Optionally, specify the column names corresponding to spectral `bands` |
| 10 | +as keyword arguments. |
| 11 | +
|
| 12 | +# Examples |
| 13 | +
|
| 14 | +```julia |
| 15 | +# vegetation index |
| 16 | +SpectralIndex("NDVI") |
| 17 | +
|
| 18 | +# water index |
| 19 | +SpectralIndex("NDWI") |
| 20 | +
|
| 21 | +# specify "R" (red) and "N" (near infra red) columns |
| 22 | +SpectralIndex("NDVI", R="col1", N="col4") |
| 23 | +``` |
| 24 | +
|
| 25 | +### Notes |
| 26 | +
|
| 27 | +The list of supported indices can be obtained with `spectralindices()` |
| 28 | +and the list of spectral bands for a given index can be obtained with |
| 29 | +`spectralbands(name)`. |
| 30 | +""" |
| 31 | +struct SpectralIndex{B} <: StatelessFeatureTransform |
| 32 | + name::String |
| 33 | + bands::B |
| 34 | + |
| 35 | + function SpectralIndex(name, bands) |
| 36 | + sname = string(name) |
| 37 | + _assert(sname ∈ keys(indices), "$sname not found in SpectralIndices.jl") |
| 38 | + sbands = if isempty(bands) |
| 39 | + nothing |
| 40 | + else |
| 41 | + skeys = string.(keys(bands)) |
| 42 | + vkeys = Tuple(indices[sname].bands) |
| 43 | + _assert(skeys ⊆ vkeys, "bands $skeys are not valid for spectral index $sname, please choose from $vkeys") |
| 44 | + svals = string.(values(values(bands))) |
| 45 | + (; zip(Symbol.(skeys), svals)...) |
| 46 | + end |
| 47 | + new{typeof(sbands)}(sname, sbands) |
| 48 | + end |
| 49 | +end |
| 50 | + |
| 51 | +SpectralIndex(name; bands...) = SpectralIndex(name, bands) |
| 52 | + |
| 53 | +function applyfeat(transform::SpectralIndex, feat, prep) |
| 54 | + # retrieve spectral index |
| 55 | + iname = transform.name |
| 56 | + index = indices[iname] |
| 57 | + |
| 58 | + # extract band names from feature table |
| 59 | + cols = Tables.columns(feat) |
| 60 | + names = Tables.columnnames(cols) |
| 61 | + bnames = Symbol.(index.bands) |
| 62 | + tbands = transform.bands |
| 63 | + snames = map(bnames) do b |
| 64 | + if !isnothing(tbands) && b ∈ keys(tbands) |
| 65 | + Symbol(tbands[b]) |
| 66 | + else |
| 67 | + b |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + # throw helpful error message in case of invalid names |
| 72 | + if !(snames ⊆ names) |
| 73 | + notfound = setdiff(snames, names) |
| 74 | + required = ["$(b.short_name): $(b.long_name)" for b in spectralbands(iname)] |
| 75 | + pprint(names) = "\"" * join(string.(names), "\", \"", "\" and \"") * "\"" |
| 76 | + throw(ArgumentError("""columns $(pprint(notfound)) not found in table. |
| 77 | +
|
| 78 | + Please specify valid columns names for the spectral bands as keyword arguments. |
| 79 | +
|
| 80 | + Required bands for $iname: |
| 81 | +
|
| 82 | + $(join(required, "\n ")) |
| 83 | +
|
| 84 | + Available column names: $(pprint(names)) |
| 85 | + """)) |
| 86 | + end |
| 87 | + |
| 88 | + # compute index for all locations |
| 89 | + icols = [b => Tables.getcolumn(cols, n) for (b, n) in zip(bnames, snames)] |
| 90 | + ivals = compute(index; icols...) |
| 91 | + |
| 92 | + # new table with index feature |
| 93 | + newfeat = (; Symbol(iname) => ivals) |> Tables.materializer(feat) |
| 94 | + |
| 95 | + newfeat, nothing |
| 96 | +end |
| 97 | + |
| 98 | +""" |
| 99 | + spectralindices() |
| 100 | +
|
| 101 | +List of spectral indices supported by SpectralIndices.jl. |
| 102 | +""" |
| 103 | +spectralindices() = indices |
| 104 | + |
| 105 | +""" |
| 106 | + spectralbands(name) |
| 107 | +
|
| 108 | +List of spectral bands for spectral index of given `name`. |
| 109 | +""" |
| 110 | +spectralbands(name) = [bands[b] for b in indices[name].bands] |
0 commit comments