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 
42 {
43  //clear kernel map A
44  KernelMap::iterator itA = m_kMapA.begin();
45 
46  while(itA != m_kMapA.end())
47  {
48  delete itA->second.first;
49  ++itA;
50  }
51 
52  m_kMapA.clear();
53 
54  //clear kernel map B
55  KernelMap::iterator itB = m_kMapB.begin();
56 
57  while(itB != m_kMapB.end())
58  {
59  delete itB->second.first;
60  ++itB;
61  }
62 
63  m_kMapB.clear();
64 }
65 
67 {
68  assert(m_inputParamsA.get());
69  assert(m_inputParamsB.get());
70  assert(m_outputParams.get());
71 
72  //build tree and map kernel
73  buildTree();
74 
75  //check the output storage mode
76  te::sa::KernelOutputType outType = m_outputParams->m_storageType;
77 
78  if(outType == te::sa::Grid)
79  {
80  //create raster A
81  std::unique_ptr<te::rst::Raster> rasterA = buildRaster(m_inputParamsA.get(), m_kTree, "MEM");
82 
83  //run kernel A
84  runRasterKernel(m_inputParamsA.get(), m_kTree, m_kMapA, rasterA.get());
85 
86  //create raster B
87  std::unique_ptr<te::rst::Raster> rasterB = buildRaster(m_inputParamsB.get(), m_kTree, "MEM");
88 
89  //run kernel B
90  runRasterKernel(m_inputParamsB.get(), m_kTree, m_kMapB, rasterB.get());
91 
92  //create raster out
93  std::unique_ptr<te::rst::Raster> rasterOut = buildRaster(m_inputParamsA.get(), m_kTree, "GDAL");
94 
95  //run kernel ratio
96  te::sa::GridRatioKernel(m_outputParams.get(), rasterA.get(), rasterB.get(), rasterOut.get());
97  }
98  else if(outType == te::sa::Attribute)
99  {
100  //create datasetype A
101  std::unique_ptr<te::da::DataSetType> dsTypeA = createDataSetType(m_inputParamsA->m_dsType.get());
102 
103  //run kernel A
104  std::unique_ptr<te::mem::DataSet> dataSetA = runDataSetKernel(m_inputParamsA.get(), m_kTree, m_kMapA, dsTypeA.get());
105 
106  //create datasetype B
107  std::unique_ptr<te::da::DataSetType> dsTypeB = createDataSetType(m_inputParamsB->m_dsType.get());
108 
109  //run kernel B
110  std::unique_ptr<te::mem::DataSet> dataSetB = runDataSetKernel(m_inputParamsB.get(), m_kTree, m_kMapB, dsTypeB.get());
111 
112  //create datasetype out
113  std::unique_ptr<te::da::DataSetType> dsTypeOut = createDataSetType(m_inputParamsA->m_dsType.get());
114 
115  //create dataset in memory
116  std::unique_ptr<te::mem::DataSet> dataSetOut = createDataSet(m_inputParamsA->m_ds.get(), dsTypeOut.get());
117 
118  //get kernel attr index
119  std::size_t kernelIdx = dsTypeOut->getPropertyPosition(m_outputParams->m_outputAttrName);
120 
121  //get geom attr index
122  std::size_t geomIdx = te::da::GetFirstPropertyPos(dataSetOut.get(), te::dt::GEOMETRY_TYPE);
123 
124  //run kernel ratio
125  te::sa::DataSetRatioKernel(m_outputParams.get(), dataSetA.get(), dataSetB.get(), dataSetOut.get(), static_cast<int>(kernelIdx), static_cast<int>(geomIdx));
126 
127  //save dataset
128  saveDataSet(dataSetOut.get(), dsTypeOut.get());
129  }
130 }
131 
133 {
134  m_inputParamsA.reset(inParamsA);
135  m_inputParamsB.reset(inParamsB);
136 }
137 
139 {
140  //get properties information
141  te::da::DataSetType* dataSetType = m_inputParamsA->m_dsType.get();
142 
143  te::da::PrimaryKey* pk = dataSetType->getPrimaryKey();
144 
145  if(!pk || pk->getProperties().empty())
146  throw;
147 
148  std::string idxName = pk->getProperties()[0]->getName();
149 
151 
152  if(!gmProp)
153  throw;
154 
155  te::da::DataSet* dataSet = m_inputParamsA->m_ds.get();
156 
157  dataSet->moveBeforeFirst();
158 
159  //create tree and kernel map
160  while(dataSet->moveNext())
161  {
162  std::string strId = dataSet->getAsString(idxName);
163 
164  int id = atoi(strId.c_str());
165 
166  std::unique_ptr<te::gm::Geometry> g = dataSet->getGeometry(gmProp->getName());
167 
168  const te::gm::Envelope* box = g->getMBR();
169 
170  m_kTree.insert(*box, id);
171 
172  double valueA = 1.; //If there is no properties, assume intensity of one
173 
174  if(!m_inputParamsA->m_intensityAttrName.empty())
175  {
176  valueA = te::sa::GetDataValue(dataSet->getValue(m_inputParamsA->m_intensityAttrName).get());
177  }
178 
179  std::pair<te::gm::Geometry*, double> pairA((te::gm::Geometry*)g->clone(), valueA);
180 
181  m_kMapA.insert(KernelMap::value_type(id, pairA));
182 
183  double valueB = 1.; //If there is no properties, assume intensity of one
184 
185  if(!m_inputParamsB->m_intensityAttrName.empty())
186  {
187  valueB = te::sa::GetDataValue(dataSet->getValue(m_inputParamsB->m_intensityAttrName).get());
188  }
189 
190  std::pair<te::gm::Geometry*, double> pairB((te::gm::Geometry*)g->clone(), valueB);
191 
192  m_kMapB.insert(KernelMap::value_type(id, pairB));
193  }
194 }
std::unique_ptr< te::sa::KernelInputParams > m_inputParamsB
Kernel input parameters B.
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.
virtual std::unique_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
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.
te::sa::KernelMap m_kMapB
Kernel map with input data B.
virtual void execute()
Function to execute the kernel operation.
Geometric property.
Class that represents the kernel input parameters.
Definition: KernelParams.h:54
te::sa::KernelTree m_kTree
Attribute used to locate near geometries.
A class that models the description of a dataset.
Definition: DataSetType.h:72
te::sa::KernelMap m_kMapA
Kernel map with input data A.
This file contains a class that represents the kernel ratio operation.
void saveDataSet(te::da::DataSet *dataSet, te::da::DataSetType *dsType)
std::unique_ptr< te::mem::DataSet > runDataSetKernel(te::sa::KernelInputParams *inputParams, te::sa::KernelTree &kTree, te::sa::KernelMap &kMap, te::da::DataSetType *dsType)
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
std::unique_ptr< te::sa::KernelInputParams > m_inputParamsA
Kernel input parameters A.
void runRasterKernel(te::sa::KernelInputParams *inputParams, te::sa::KernelTree &kTree, te::sa::KernelMap &kMap, te::rst::Raster *raster)
~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.
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
Utility functions for the data access module.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
KernelOutputType
Defines the kernel result storage mode.
A dataset is the unit of information manipulated by the data access module of TerraLib.
std::unique_ptr< te::sa::KernelOutputParams > m_outputParams
Attribute with the kernel output parameters.
void insert(const te::gm::Envelope &mbr, const DATATYPE &data)
It inserts an item into the tree.
virtual std::unique_ptr< te::dt::AbstractData > getValue(std::size_t i) const
Method for retrieving any other type of data value stored in the data source.
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.
std::unique_ptr< te::mem::DataSet > createDataSet(te::da::DataSet *inputDataSet, te::da::DataSetType *dsType)
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
TESAEXPORT double GetDataValue(te::dt::AbstractData *ad)
Function used to get the numeric value from a gpm property.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
std::unique_ptr< te::da::DataSetType > createDataSetType(te::da::DataSetType *dsType)
std::unique_ptr< te::rst::Raster > buildRaster(te::sa::KernelInputParams *inputParams, te::sa::KernelTree &kTree, std::string driver)
const std::string & getName() const
It returns the property name.
Definition: Property.h:127