All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GeostatisticalFunctions.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/sa/core/GeostatisticalFunctions.cpp
22 
23  \brief Auxiliary functions used to calculate the geostatistical methods.
24 
25  \reference Methods adapted from TerraLib4.
26 */
27 
28 // TerraLib
29 #include "../../common/STLUtils.h"
30 #include "../../dataaccess/dataset/DataSet.h"
31 #include "../../geometry/Geometry.h"
32 #include "../../geometry/Coord2D.h"
34 #include "Utils.h"
35 
36 // STL
37 #include <cassert>
38 
39 boost::numeric::ublas::matrix<double> te::sa::CreateMatrixFromDataSet(te::da::DataSet* dataSet, int attrIdx, int geomIdx)
40 {
41  assert(dataSet);
42 
43  //create geostatistical data
44  te::sa::GeostatisticalData geoData = CreateGeostatisticalData(dataSet, attrIdx, geomIdx);
45 
46  //create output matrix
47  boost::numeric::ublas::matrix<double> matrix(geoData.size(), geoData.size());
48 
49  //fill matrix
50  for(std::size_t i = 0; i < geoData.size(); ++i)
51  {
52  te::sa::GeostatisticalDataItem* itemI = geoData[i];
53 
54  //set diagonal value
55  matrix(i, i) = itemI->m_value;
56 
58 
59  for(std::size_t j = i + 1; j < geoData.size(); ++j)
60  {
61  te::sa::GeostatisticalDataItem* itemJ = geoData[j];
62 
64 
65  //calculate distance and set at the top of the matrix
66  double distance = sqrt(pow((coordI.x - coordJ.x), 2) + pow((coordI.y - coordJ.y), 2));
67 
68  matrix(i, j) = distance;
69 
70  //calculate angle and set at the bottom of the matrix
71  double angle = -( atan ((coordI.y - coordJ.y)/(coordI.x - coordJ.x)) * (180/3.14) ) + 90.0;
72 
73  matrix(j, i) = angle;
74  }
75  }
76 
77  //clear geostatistical data
78  te::common::FreeContents(geoData);
79 
80  return matrix;
81 }
82 
83 void te::sa::SetMainDiagonal(boost::numeric::ublas::matrix<double>& matrix, te::da::DataSet* dataSet, int attrIdx)
84 {
85  assert(dataSet);
86 
87  dataSet->moveBeforeFirst();
88 
89  std::size_t t = 0;
90 
91  //reset only the main diagonal values
92  while(dataSet->moveNext())
93  {
94  double value = te::sa::GetDataValue(dataSet->getValue(attrIdx).get());
95 
96  matrix(t, t) = value;
97 
98  ++t;
99  }
100 }
101 
102 void te::sa::CalculateMoments(const boost::numeric::ublas::matrix<double>& matrix, double& mean, double& variance)
103 {
104  double sum = 0.;
105  double sumSquare = 0.;
106 
107  for(std::size_t t = 0; t < matrix.size1(); ++t)
108  {
109  sum += matrix(t,t);
110  sumSquare += pow(matrix(t,t), 2);
111  }
112 
113  mean = sum / (double)matrix.size1();
114  variance = (sumSquare / (double)matrix.size1()) - pow(mean, 2);
115 }
116 
118 {
119  assert(dataSet);
120 
122 
123  dataSet->moveBeforeFirst();
124 
125  while(dataSet->moveNext())
126  {
128 
129  item->m_value = te::sa::GetDataValue(dataSet->getValue(attrIdx).get());
130  item->m_geom = dataSet->getGeometry(geomIdx).release();
131 
132  geoData.push_back(item);
133  }
134 
135  return geoData;
136 }
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
TESAEXPORT te::gm::Coord2D GetCentroidCoord(te::gm::Geometry *geom)
Function used to get centroid coord of a geometry.
Definition: Utils.cpp:243
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
Utilitary function for spatial analysis module.
TESAEXPORT void SetMainDiagonal(boost::numeric::ublas::matrix< double > &matrix, te::da::DataSet *dataSet, int attrIdx)
Function used to set new values in the matrix main diagonal.
std::vector< te::sa::GeostatisticalDataItem * > GeostatisticalData
TESAEXPORT boost::numeric::ublas::matrix< double > CreateMatrixFromDataSet(te::da::DataSet *dataSet, int attrIdx, int geomIdx)
Function used to create a matrix with values, distance and angle for each element from dataset...
TESAEXPORT void CalculateMoments(const boost::numeric::ublas::matrix< double > &matrix, double &mean, double &variance)
Function used to calculate mean and variance from a matrix.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
Auxiliary functions used to calculate the geostatistical methods.
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
#define TESAEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:136
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
TESAEXPORT double GetDataValue(te::dt::AbstractData *ad)
Function used to get the numeric value from a gpm property.
Definition: Utils.cpp:200
TESAEXPORT te::sa::GeostatisticalData CreateGeostatisticalData(te::da::DataSet *dataSet, int attrIdx, int geomIdx)
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
virtual std::auto_ptr< te::dt::AbstractData > getValue(std::size_t i) const
Method for retrieving any other type of data value stored in the data source.
Definition: DataSet.cpp:151