GDAL
vrtreclassifier.h
1/******************************************************************************
2*
3 * Project: Virtual GDAL Datasets
4 * Purpose: Implementation of Reclassifier
5 * Author: Daniel Baston
6 *
7 ******************************************************************************
8 * Copyright (c) 2025, ISciences LLC
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13#pragma once
14
15#include "gdal.h"
16#include "cpl_error.h"
17
18#include <map>
19#include <optional>
20#include <utility>
21#include <vector>
22
23namespace gdal
24{
25
30{
31 public:
33 static constexpr char MAPPING_INTERVAL_SEP_CHAR = ';';
34
36 static constexpr char MAPPING_FROMTO_SEP_CHAR = '=';
37
41 struct Interval
42 {
44 double dfMin;
45
47 double dfMax;
48
50 void SetToConstant(double dfVal);
51
60 CPLErr Parse(const char *pszText, char **end);
61
63 bool IsConstant() const
64 {
65 return dfMin == dfMax;
66 }
67
69 bool Contains(double x) const
70 {
71 return x >= dfMin && x <= dfMax;
72 }
73
75 bool Overlaps(const Interval &other) const;
76 };
77
103 CPLErr Init(const char *pszText, std::optional<double> noDataValue,
104 GDALDataType eBufType);
105
111 void AddMapping(const Interval &interval, std::optional<double> dfDstVal);
112
119 double Reclassify(double srcVal, bool &bFoundInterval) const;
120
125 void SetDefaultPassThrough(bool value)
126 {
127 m_defaultPassThrough = value;
128 }
129
132 void SetDefaultValue(double value)
133 {
134 m_defaultValue = value;
135 }
136
139 void SetNaNValue(double value)
140 {
141 m_NaNValue = value;
142 }
143
147
148 private:
150 std::vector<std::pair<Interval, std::optional<double>>>
151 m_aoIntervalMappings{};
152
154 std::optional<double> m_NaNValue{};
155
157 std::optional<double> m_defaultValue{};
158
160 bool m_defaultPassThrough{false};
161};
162
163} // namespace gdal
Class to manage reclassification of pixel values.
Definition vrtreclassifier.h:30
void SetNaNValue(double value)
Sets a value for an input NaN value.
Definition vrtreclassifier.h:139
static constexpr char MAPPING_INTERVAL_SEP_CHAR
Character separating elements in a list of mapping.
Definition vrtreclassifier.h:33
void SetDefaultValue(double value)
Sets a default value for any value not matched by any interval.
Definition vrtreclassifier.h:132
void AddMapping(const Interval &interval, std::optional< double > dfDstVal)
Set a mapping between an interval and (optionally) a destination value.
Definition vrtreclassifier.cpp:196
double Reclassify(double srcVal, bool &bFoundInterval) const
Reclassify a value.
Definition vrtreclassifier.cpp:398
CPLErr Finalize()
Prepare reclassifier for use.
Definition vrtreclassifier.cpp:166
void SetDefaultPassThrough(bool value)
If true, values not matched by any interval will be returned unmodified.
Definition vrtreclassifier.h:125
static constexpr char MAPPING_FROMTO_SEP_CHAR
Character separating source interval from target value.
Definition vrtreclassifier.h:36
CPLErr Init(const char *pszText, std::optional< double > noDataValue, GDALDataType eBufType)
Initialize a Reclassifier from text.
Definition vrtreclassifier.cpp:202
CPL error handling services.
CPLErr
Error category.
Definition cpl_error.h:37
Public (C callable) GDAL entry points.
GDALDataType
Definition gdal.h:48
OGRLayer::FeatureIterator end(OGRLayer *poLayer)
Return end of feature iterator.
Definition ogrsf_frmts.h:478
Internal struct to hold an interval of values to be reclassified.
Definition vrtreclassifier.h:42
bool Contains(double x) const
Returns true if the interval contains a value.
Definition vrtreclassifier.h:69
bool IsConstant() const
Returns true of the interval represents a single value [x,x].
Definition vrtreclassifier.h:63
void SetToConstant(double dfVal)
Set the interval to represent a single value [x,x].
Definition vrtreclassifier.cpp:160
CPLErr Parse(const char *pszText, char **end)
Parse an interval.
Definition vrtreclassifier.cpp:33
bool Overlaps(const Interval &other) const
Returns true if the intervals overlap.
Definition vrtreclassifier.cpp:23
double dfMax
maximum value of range
Definition vrtreclassifier.h:47
double dfMin
minimum value of range
Definition vrtreclassifier.h:44