All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
KernelMapOperation.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/KernelMapOperation.cpp
22 
23  \brief This file contains a class that represents the kernel map 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 "KernelMapOperation.h"
34 #include "Utils.h"
35 
36 //STL
37 #include <cassert>
38 
40 {
41 
42 }
43 
45 {
46  //clear kernel map
47  KernelMap::iterator it = m_kMap.begin();
48 
49  while(it != m_kMap.end())
50  {
51  delete it->second.first;
52  ++it;
53  }
54 
55  m_kMap.clear();
56 }
57 
59 {
60  assert(m_inputParams.get());
61  assert(m_outputParams.get());
62 
63  //build tree and map kernel
64  buildTree();
65 
66  //check the output storage mode
67  te::sa::KernelOutputType outType = m_outputParams->m_storageType;
68 
69  if(outType == te::sa::Grid)
70  {
71  //create raster
72  std::auto_ptr<te::rst::Raster> raster = buildRaster(m_inputParams.get(), m_kTree, "GDAL");
73 
74  //run kernel
75  runRasterKernel(m_inputParams.get(), m_kTree, m_kMap, raster.get());
76  }
77  else if(outType == te::sa::Attribute)
78  {
79  //create datasetype
80  std::auto_ptr<te::da::DataSetType> dsType = createDataSetType(m_inputParams->m_dsType.get());
81 
82  //run kernel
83  std::auto_ptr<te::mem::DataSet> dataSet = runDataSetKernel(m_inputParams.get(), m_kTree, m_kMap, dsType.get());
84 
85  //save dataset
86  saveDataSet(dataSet.get(), dsType.get());
87  }
88 }
89 
91 {
92  m_inputParams.reset(inParams);
93 }
94 
96 {
97  //get properties information
98  te::da::DataSetType* dataSetType = m_inputParams->m_dsType.get();
99 
100  te::da::PrimaryKey* pk = dataSetType->getPrimaryKey();
101 
102  if(!pk || pk->getProperties().empty())
103  throw;
104 
105  std::string idxName = pk->getProperties()[0]->getName();
106 
108 
109  if(!gmProp)
110  throw;
111 
112  te::da::DataSet* dataSet = m_inputParams->m_ds.get();
113 
114  dataSet->moveBeforeFirst();
115 
116  //create tree and kernel map
117  while(dataSet->moveNext())
118  {
119  std::string strId = dataSet->getAsString(idxName);
120 
121  int id = atoi(strId.c_str());
122 
123  te::gm::Geometry* g = dataSet->getGeometry(gmProp->getName()).release();
124 
125  const te::gm::Envelope* box = g->getMBR();
126 
127  m_kTree.insert(*box, id);
128 
129  double value = 1.; //If there is no properties, assume intensity of one
130 
131  if(!m_inputParams->m_intensityAttrName.empty())
132  {
133  value = te::sa::GetDataValue(dataSet->getValue(m_inputParams->m_intensityAttrName).get());
134  }
135 
136  std::pair<te::gm::Geometry*, double> pair(g, value);
137 
138  m_kMap.insert(KernelMap::value_type(id, pair));
139  }
140 }
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
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
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
This file contains a class that represents the kernel map operation.
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
virtual void execute()
Function to execute the kernel operation.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
KernelMapOperation()
Default constructor.
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
~KernelMapOperation()
Virtual destructor.
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
void setInputParameters(te::sa::KernelInputParams *inParams)
const std::string & getName() const
It returns the property name.
Definition: Property.h:127