/usr/include/terralib/functions/TeInterpolation.h is in libterralib-dev 4.0.0-5ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | /************************************************************************************
TerraLib - a library for developing GIS applications.
Copyright © 2001-2004 INPE and Tecgraf/PUC-Rio.
This code is part of the TerraLib library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
You should have received a copy of the GNU Lesser General Public
License along with this library.
The authors reassure the license terms regarding the warranties.
They specifically disclaim any warranties, including, but not limited to,
the implied warranties of merchantability and fitness for a particular purpose.
The library provided hereunder is on an "as is" basis, and the authors have no
obligation to provide maintenance, support, updates, enhancements, or modifications.
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
indirect, special, incidental, or consequential damages arising out of the use
of this library and its documentation.
*************************************************************************************/
/*! \file TeInterpolation.h
This file contains routines for interpolation of cell themes and raster themes.
*/
#ifndef __TERRALIB_INTERNAL_INTERPOLATION_H
#define __TERRALIB_INTERNAL_INTERPOLATION_H
#include "TeTheme.h"
/*
* NOTE: You should call TeCellInterpolate or TeRasterInterpolate with the required parameters. Don't use
* the class TeInterpolationAlgorithms, it is for internal use only!
*
*/
/** @defgroup InterpolationDataStructure Interpolation Algorithms and Data Structures
* TerraLib Interpolation Data Structure.
* @{
*/
/*! \enum TeInterpolationAlgorithm
Methods of interpolation, may be:
- TeNNInterpolation Interpolation that uses the nearest neighbor value
- TeAvgInterpolation Interpolation that uses the average of k-nearest neighbors values
- TeDistWeightAvgInterpolation Interpolation with weight average (inverse of square distance or other) of k-nearest neighbors values
- TeAvgInBoxInterpolation Interpolation with simple average of elements in box
- TeDistWeightAvgInBoxInterpolation Interpolation with weight average of elements in box
*/
enum TeInterpolationAlgorithm { TeNNInterpolation, TeAvgInterpolation, TeDistWeightAvgInterpolation,
TeAvgInBoxInterpolation, TeDistWeightAvgInBoxInterpolation };
//! Class that supports several types of interpolation methods
/*!
Given a KdTree and a key, choose a method of interpolation.
WARNING: 1. This class MUST BE USED ONLY BY in the routines below (TeCellInterpolate and TeRasterInterpolate)
and should NOT be used by anyone because the support and interfaces can be changed in future.
THIS IS FOR INTERNAL USE ONLY.
2. As in simple average as in weight average, if all neighbours aren't find the calculus considerates
the number of found neighbours.
3. If an unexpected situation occurs so -TeMAXFLOAT is returned.
4. If a sample exist in the same position of a key, so the sample value is returned, i.e.,
not all neighbours are considered.
*/
template<class ADAPTATIVEKDTREE> class TeInterpolationAlgorithms
{
protected:
typedef typename ADAPTATIVEKDTREE::kdKey kdKey;
typedef typename ADAPTATIVEKDTREE::kdDataItem kdDataItem;
typedef typename ADAPTATIVEKDTREE::kdNode kdNode;
//! reference to an adptative kdtree
const ADAPTATIVEKDTREE& kd_;
public:
//! Constructor
TeInterpolationAlgorithms(const ADAPTATIVEKDTREE& kd)
: kd_(kd)
{
}
//! Destructor
~TeInterpolationAlgorithms()
{
}
//! Returns the nearest neighbor value
double nearestNeighbor(const kdKey& key)
{
vector<kdDataItem> report;
vector<double> sqrDists;
fillNNVector(report, 1);
kd_.nearestNeighborSearch(key, report, sqrDists, 1);
if(sqrDists[0] >= TeMAXFLOAT)
return -TeMAXFLOAT;
else
return report[0].value();
}
//! Simple Average of Nearest Neighbors. If an error occur returns -TeMAXFLOAT
double avgNearestNeighbor(const kdKey& key, const unsigned int& numberOfNeighbors)
{
vector<kdDataItem> report;
vector<double> sqrDists;
fillNNVector(report, numberOfNeighbors);
kd_.nearestNeighborSearch(key, report, sqrDists, numberOfNeighbors);
double numElements = numberOfNeighbors;
double sum = 0.0;
for(unsigned int i = 0; i < numberOfNeighbors; ++i)
{
if(sqrDists[i] >= TeMAXFLOAT)
{
--numElements;
continue;
}
if(sqrDists[i] == 0.0)
return report[i].value();
sum += report[i].value();
}
if(numElements > 0.0)
return sum / numElements;
else
return -TeMAXFLOAT;
}
//! Weight Average of Nearest Neighbors. If an error occur returns -TeMAXFLOAT
double distWeightAvgNearestNeighbor(const kdKey& key, const unsigned int& numberOfNeighbors, const int& powValue)
{
vector<kdDataItem> report;
vector<double> sqrDists;
fillNNVector(report, numberOfNeighbors);
kd_.nearestNeighborSearch(key, report, sqrDists, numberOfNeighbors);
double num = 0.0;
double den = 0.0;
if(powValue == 1.0)
{
for(unsigned int i = 0; i < numberOfNeighbors; ++i)
{
if(sqrDists[i] >= TeMAXFLOAT)
continue;
if(sqrDists[i] == 0.0)
return report[i].value();
double wi = 1.0 / (sqrt(sqrDists[i]));
num += (wi * report[i].value());
den += wi;
}
}
else if(powValue == 2.0)
{
for(unsigned int i = 0; i < numberOfNeighbors; ++i)
{
if(sqrDists[i] >= TeMAXFLOAT)
continue;
if(sqrDists[i] == 0.0)
return report[i].value();
double wi = 1.0 / (sqrDists[i]);
num += (wi * report[i].value());
den += wi;
}
}
else
{
for(unsigned int i = 0; i < numberOfNeighbors; ++i)
{
if(sqrDists[i] >= TeMAXFLOAT)
continue;
if(sqrDists[i] == 0.0)
return report[i].value();
double wi = 1.0 / pow(sqrt(sqrDists[i]), powValue);
num += (wi * report[i].value());
den += wi;
}
}
if(den != 0.0)
return num / den;
else
return -TeMAXFLOAT;
}
//! Simple Average of Elements in Box. If an error occur returns -TeMAXFLOAT
double boxAvg(const TeBox& box)
{
vector<kdNode*> report;
kd_.search(box, report);
unsigned int numberOfNodes = report.size();
unsigned int numElements = 0;
double sum = 0.0;
for(unsigned int i = 0; i < numberOfNodes; ++i)
{
unsigned int nodeSize = report[i]->getData().size();
for(unsigned int j = 0; j < nodeSize; ++j)
{
if(TeIntersects(report[i]->getData()[j].location(), box))
{
sum += report[i]->getData()[j].value();
++numElements;
}
}
}
if(numElements > 0)
return sum / double(numElements);
else
return -TeMAXFLOAT;
}
//! Distance Weight Average of Elements in Box. If an error occur returns -TeMAXFLOAT
double boxDistWeightAvg(const kdKey& key, const TeBox& box, const int& powValue)
{
vector<kdNode*> report;
kd_.search(box, report);
unsigned int numberOfNodes = report.size();
double num = 0.0;
double den = 0.0;
for(unsigned int i = 0; i < numberOfNodes; ++i)
{
unsigned int nodeSize = report[i]->getData().size();
for(unsigned int j = 0; j < nodeSize; ++j)
{
if(TeIntersects(report[i]->getData()[j].location(), box))
{
double wi = 1 / pow(TeDistance(key, report[i]->getData()[j].location()), powValue);
num += wi * report[i]->getData()[j].value();
den += wi;
}
}
}
if(den != 0.0)
return num / den;
else
return -TeMAXFLOAT;
}
protected:
//! Fills the nearest neighbour vector with default values
void fillNNVector(vector<kdDataItem>& report, const unsigned int& numberOfNeighbors) const
{
for(unsigned int i = 0; i < numberOfNeighbors; ++i)
{
TeCoord2D c(TeMAXFLOAT, TeMAXFLOAT);
kdDataItem item(c, -TeMAXFLOAT);
report.push_back(item);
}
}
private:
//! No copy allowed
TeInterpolationAlgorithms(const TeInterpolationAlgorithms& rhs);
//! No copy allowed
TeInterpolationAlgorithms& operator=(const TeInterpolationAlgorithms& rhs);
};
//! Interpolates a cell theme
/*
\param inputTheme The theme with samples to be used: samples are treated as a layer of points with a column with a value (double or integer)
\param inputAttrTableName Theme table with samples
\param sampleColumnName Table column with sample values: must be int or double values
\param outputTheme The theme where output will be stored: this theme must already exist
\param outputAttrTableName Theme table where output will be stored: this table must already exist
\param outputColumnName Table column where data will be stored: this column must already exist
\param algorithm Type of algorithm to be used: see enum TeInterpolationAlgorithm
\param numberOfNeighbors Number of nearest neighbor to be used in interpolation
\param powValue Pow parameter for inverse distance function
\param boxRatio Ratio of the box used to search samples
\return TRUE if there isn't an error during interpolation otherwise return FALSE
*/
bool TeCellInterpolate(TeTheme* inputTheme, const string& inputAttrTableName, const string& sampleColumnName,
TeTheme* outputTheme, const string& outputAttrTableName, const string& outputColumnName,
const TeInterpolationAlgorithm& algorithm = TeNNInterpolation,
const unsigned int& numberOfNeighbors = 1, const int& powValue = 1, const double& boxRatio = 1.0);
//! Interpolates a a raster
/*
\param inputTheme The theme with samples to be used: samples are treated as a layer of points with a column with a value (double or integer)
\param inputAttrTableName Theme table with samples
\param sampleColumnName Table column with sample values
\param outputRaster A pointer to a raster where interpolated data will be stored: this raster mus already exist
\param band The band to be interpolated
\param algorithm Type of algorithm to be used: see enum TeInterpolationAlgorithm
\param numberOfNeighbors Number of nearest neighbor to be used in interpolation
\param powValue Pow parameter for inverse distance function
\param boxRatio Ratio of the box used to search samples
\return TRUE if there isn't an error during interpolation otherwise return FALSE
*/
bool TeRasterInterpolate(TeTheme* inputTheme, const string& inputAttrTableName, const string& sampleColumnName,
TeRaster* outputRaster, const int& band = 0,
const TeInterpolationAlgorithm& algorithm = TeNNInterpolation,
const unsigned int& numberOfNeighbors = 1, const int& powValue = 1, const double& boxRatio = 1.0);
/** @} */
#endif // __TERRALIB_INTERNAL_INTERPOLATION_H
|