All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
KernelRatioOperation.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/KernelRatioOperation.cpp
22 
23  \brief This file contains a class that represents the kernel ratio operation.
24 
25  \reference Adapted from TerraLib4.
26 */
27 
28 //TerraLib
29 #include "../../dataaccess/utils/Utils.h"
30 #include "../../geometry/GeometryProperty.h"
31 #include "../../memory/DataSet.h"
32 #include "../../raster/Raster.h"
33 #include "KernelRatioOperation.h"
34 #include "Utils.h"
35 
36 //STL
37 #include <cassert>
38 
40 {
41 
42 }
43 
45 {
46  //clear kernel map A
47  KernelMap::iterator itA = m_kMapA.begin();
48 
49  while(itA != m_kMapA.end())
50  {
51  delete itA->second.first;
52  ++itA;
53  }
54 
55  m_kMapA.clear();
56 
57  //clear kernel map B
58  KernelMap::iterator itB = m_kMapB.begin();
59 
60  while(itB != m_kMapB.end())
61  {
62  delete itB->second.first;
63  ++itB;
64  }
65 
66  m_kMapB.clear();
67 }
68 
70 {
71  assert(m_inputParamsA.get());
72  assert(m_inputParamsB.get());
73  assert(m_outputParams.get());
74 
75  //build tree and map kernel
76  buildTree();
77 
78  //check the output storage mode
79  te::sa::KernelOutputType outType = m_outputParams->m_storageType;
80 
81  if(outType == te::sa::Grid)
82  {
83  //create raster A
84  std::auto_ptr<te::rst::Raster> rasterA = buildRaster(m_inputParamsA.get(), m_kTree, "MEM");
85 
86  //run kernel A
87  runRasterKernel(m_inputParamsA.get(), m_kTree, m_kMapA, rasterA.get());
88 
89  //create raster B
90  std::auto_ptr<te::rst::Raster> rasterB = buildRaster(m_inputParamsB.get(), m_kTree, "MEM");
91 
92  //run kernel B
93  runRasterKernel(m_inputParamsB.get(), m_kTree, m_kMapB, rasterB.get());
94 
95  //create raster out
96  std::auto_ptr<te::rst::Raster> rasterOut = buildRaster(m_inputParamsA.get(), m_kTree, "GDAL");
97 
98  //run kernel ratio
99  te::sa::GridRatioKernel(m_outputParams.get(), rasterA.get(), rasterB.get(), rasterOut.get());
100  }
101  else if(outType == te::sa::Attribute)
102  {
103  //create datasetype A
104  std::auto_ptr<te::da::DataSetType> dsTypeA = createDataSetType(m_inputParamsA->m_dsType.get());
105 
106  //run kernel A
107  std::auto_ptr<te::mem::DataSet> dataSetA = runDataSetKernel(m_inputParamsA.get(), m_kTree, m_kMapA, dsTypeA.get());
108 
109  //create datasetype B
110  std::auto_ptr<te::da::DataSetType> dsTypeB = createDataSetType(m_inputParamsB->m_dsType.get());
111 
112  //run kernel B
113  std::auto_ptr<te::mem::DataSet> dataSetB = runDataSetKernel(m_inputParamsB.get(), m_kTree, m_kMapB, dsTypeB.get());
114 
115  //create datasetype out
116  std::auto_ptr<te::da::DataSetType> dsTypeOut = createDataSetType(m_inputParamsA->m_dsType.get());
117 
118  //create dataset in memory
119  std::auto_ptr<te::mem::DataSet> dataSetOut = createDataSet(m_inputParamsA->m_ds.get(), dsTypeOut.get());
120 
121  //get kernel attr index
122  std::size_t kernelIdx = dsTypeOut->getPropertyPosition(m_outputParams->m_outputAttrName);
123 
124  //get geom attr index
125  std::size_t geomIdx = te::da::GetFirstPropertyPos(dataSetOut.get(), te::dt::GEOMETRY_TYPE);
126 
127  //run kernel ratio
128  te::sa::DataSetRatioKernel(m_outputParams.get(), dataSetA.get(), dataSetB.get(), dataSetOut.get(), kernelIdx, geomIdx);
129 
130  //save dataset
131  saveDataSet(dataSetOut.get(), dsTypeOut.get());
132  }
133 }
134 
136 {
137  m_inputParamsA.reset(inParamsA);
138  m_inputParamsB.reset(inParamsB);
139 }
140 
142 {
143  //get properties information
144  te::da::DataSetType* dataSetType = m_inputParamsA->m_dsType.get();
145 
146  te::da::PrimaryKey* pk = dataSetType->getPrimaryKey();
147 
148  if(!pk || pk->getProperties().empty())
149  throw;
150 
151  std::string idxName = pk->getProperties()[0]->getName();
152 
154 
155  if(!gmProp)
156  throw;
157 
158  te::da::DataSet* dataSet = m_inputParamsA->m_ds.get();
159 
160  dataSet->moveBeforeFirst();
161 
162  //create tree and kernel map
163  while(dataSet->moveNext())
164  {
165  std::string strId = dataSet->getAsString(idxName);
166 
167  int id = atoi(strId.c_str());
168 
169  std::auto_ptr<te::gm::Geometry> g = dataSet->getGeometry(gmProp->getName());
170 
171  const te::gm::Envelope* box = g->getMBR();
172 
173  m_kTree.insert(*box, id);
174 
175  double valueA = 1.; //If there is no properties, assume intensity of one
176 
177  if(!m_inputParamsA->m_intensityAttrName.empty())
178  {
179  valueA = te::sa::GetDataValue(dataSet->getValue(m_inputParamsA->m_intensityAttrName).get());
180  }
181 
182  std::pair<te::gm::Geometry*, double> pairA((te::gm::Geometry*)g->clone(), valueA);
183 
184  m_kMapA.insert(KernelMap::value_type(id, pairA));
185 
186  double valueB = 1.; //If there is no properties, assume intensity of one
187 
188  if(!m_inputParamsB->m_intensityAttrName.empty())
189  {
190  valueB = te::sa::GetDataValue(dataSet->getValue(m_inputParamsB->m_intensityAttrName).get());
191  }
192 
193  std::pair<te::gm::Geometry*, double> pairB((te::gm::Geometry*)g->clone(), valueB);
194 
195  m_kMapB.insert(KernelMap::value_type(id, pairB));
196  }
197 }
TESAEXPORT void GridRatioKernel(te::sa::KernelOutputParams *params, te::rst::Raster *rasterA, te::rst::Raster *rasterB, te::rst::Raster *rasterOut)
Evaluates kernel ratio value using a raster as output data.
TESAEXPORT void DataSetRatioKernel(te::sa::KernelOutputParams *params, te::mem::DataSet *dsA, te::mem::DataSet *dsB, te::mem::DataSet *dsOut, int kernelIdx, int geomIdx)
Evaluates kernel ratio value using a dataset as output data.
virtual void execute()
Function to execute the kernel operation.
Geometric property.
Utility functions for the data access module.
Class that represents the kernel input parameters.
Definition: KernelParams.h:54
A class that models the description of a dataset.
Definition: DataSetType.h:72
This file contains a class that represents the kernel ratio operation.
void setInputParameters(te::sa::KernelInputParams *inParamsA, te::sa::KernelInputParams *inParamsB)
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the primary key.
Definition: PrimaryKey.h:109
~KernelRatioOperation()
Virtual destructor.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
Definition: DataSet.cpp:218
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
KernelOutputType
Defines the kernel result storage mode.
Definition: Enums.h:104
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
KernelRatioOperation()
Default constructor.
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:481
TESAEXPORT double GetDataValue(te::dt::AbstractData *ad)
Function used to get the numeric value from a gpm property.
Definition: Utils.cpp:200
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
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
const std::string & getName() const
It returns the property name.
Definition: Property.h:127