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
23 namespace gdal
24{
25
29 class Reclassifier
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
146 CPLErr Finalize();
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
gdal::Reclassifier
Class to manage reclassification of pixel values.
Definition vrtreclassifier.h:30
gdal::Reclassifier::SetNaNValue
void SetNaNValue(double value)
Sets a value for an input NaN value.
Definition vrtreclassifier.h:139
gdal::Reclassifier::MAPPING_INTERVAL_SEP_CHAR
static constexpr char MAPPING_INTERVAL_SEP_CHAR
Character separating elements in a list of mapping.
Definition vrtreclassifier.h:33
gdal::Reclassifier::SetDefaultValue
void SetDefaultValue(double value)
Sets a default value for any value not matched by any interval.
Definition vrtreclassifier.h:132
gdal::Reclassifier::AddMapping
void AddMapping(const Interval &interval, std::optional< double > dfDstVal)
Set a mapping between an interval and (optionally) a destination value.
Definition vrtreclassifier.cpp:196
gdal::Reclassifier::Reclassify
double Reclassify(double srcVal, bool &bFoundInterval) const
Reclassify a value.
Definition vrtreclassifier.cpp:398
gdal::Reclassifier::Finalize
CPLErr Finalize()
Prepare reclassifier for use.
Definition vrtreclassifier.cpp:166
gdal::Reclassifier::SetDefaultPassThrough
void SetDefaultPassThrough(bool value)
If true, values not matched by any interval will be returned unmodified.
Definition vrtreclassifier.h:125
gdal::Reclassifier::MAPPING_FROMTO_SEP_CHAR
static constexpr char MAPPING_FROMTO_SEP_CHAR
Character separating source interval from target value.
Definition vrtreclassifier.h:36
gdal::Reclassifier::Init
CPLErr Init(const char *pszText, std::optional< double > noDataValue, GDALDataType eBufType)
Initialize a Reclassifier from text.
Definition vrtreclassifier.cpp:202
cpl_error.h
CPL error handling services.
CPLErr
CPLErr
Error category.
Definition cpl_error.h:37
gdal.h
Public (C callable) GDAL entry points.
GDALDataType
GDALDataType
Definition gdal.h:48
end
OGRLayer::FeatureIterator end(OGRLayer *poLayer)
Return end of feature iterator.
Definition ogrsf_frmts.h:478
gdal::Reclassifier::Interval
Internal struct to hold an interval of values to be reclassified.
Definition vrtreclassifier.h:42
gdal::Reclassifier::Interval::Contains
bool Contains(double x) const
Returns true if the interval contains a value.
Definition vrtreclassifier.h:69
gdal::Reclassifier::Interval::IsConstant
bool IsConstant() const
Returns true of the interval represents a single value [x,x].
Definition vrtreclassifier.h:63
gdal::Reclassifier::Interval::SetToConstant
void SetToConstant(double dfVal)
Set the interval to represent a single value [x,x].
Definition vrtreclassifier.cpp:160
gdal::Reclassifier::Interval::Parse
CPLErr Parse(const char *pszText, char **end)
Parse an interval.
Definition vrtreclassifier.cpp:33
gdal::Reclassifier::Interval::Overlaps
bool Overlaps(const Interval &other) const
Returns true if the intervals overlap.
Definition vrtreclassifier.cpp:23
gdal::Reclassifier::Interval::dfMax
double dfMax
maximum value of range
Definition vrtreclassifier.h:47
gdal::Reclassifier::Interval::dfMin
double dfMin
minimum value of range
Definition vrtreclassifier.h:44