All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
KernelOperation.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/KernelOperation.cpp
22 
23  \brief This file contains a class that represents the kernel operation.
24 
25  \reference Adapted from TerraLib4.
26 */
27 
28 //TerraLib
29 #include "../../dataaccess/datasource/DataSource.h"
30 #include "../../dataaccess/datasource/DataSourceManager.h"
31 #include "../../dataaccess/utils/Utils.h"
32 #include "../../datatype/SimpleProperty.h"
33 #include "../../geometry/Geometry.h"
34 #include "../../geometry/GeometryProperty.h"
35 #include "../../memory/DataSet.h"
36 #include "../../memory/DataSetItem.h"
37 #include "../../raster/BandProperty.h"
38 #include "../../raster/Grid.h"
39 #include "../../raster/Raster.h"
40 #include "../../raster/RasterFactory.h"
41 #include "KernelOperation.h"
42 #include "Utils.h"
43 
44 //STL
45 #include <cassert>
46 
47 // Boost
48 #include <boost/uuid/random_generator.hpp>
49 #include <boost/uuid/uuid_io.hpp>
50 
52 {
53 }
54 
56 {
57 }
58 
60 {
61  m_outputParams.reset(outParams);
62 }
63 
65 {
66  if(!raster)
67  throw;
68 
69  //check if use adaptative radius or not
70  if(inputParams->m_useAdaptativeRadius)
71  {
72  te::sa::GridAdaptRadiusKernel(inputParams, kTree, kMap, raster);
73  }
74  else
75  {
76  double val = std::max<double>(raster->getGrid()->getExtent()->getWidth(), raster->getGrid()->getExtent()->getHeight());
77  double radius = (val * inputParams->m_radiusPercentValue) / 100.;
78 
79  te::sa::GridStatRadiusKernel(inputParams, kTree, kMap, raster, radius);
80  }
81 }
82 
84 {
85  if(!dsType)
86  throw;
87 
88  //create dataset in memory
89  std::auto_ptr<te::mem::DataSet> dataSet = createDataSet(inputParams->m_ds.get(), dsType);
90 
91  //get kernel attr index
92  std::size_t kernelIdx = dsType->getPropertyPosition(m_outputParams->m_outputAttrName);
93 
94  //get geom attr index
95  std::size_t geomIdx = te::da::GetFirstPropertyPos(dataSet.get(), te::dt::GEOMETRY_TYPE);
96 
97  //check if use adaptative radius or not
98  if(inputParams->m_useAdaptativeRadius)
99  {
100  te::sa::DataSetAdaptRadiusKernel(inputParams, kTree, kMap, dataSet.get(), kernelIdx, geomIdx);
101  }
102  else
103  {
104  double val = std::max<double>(kTree.getMBR().getWidth(), kTree.getMBR().getHeight());
105  double radius = (val * inputParams->m_radiusPercentValue) / 100.;
106 
107  te::sa::DataSetStatRadiusKernel(inputParams, kTree, kMap, dataSet.get(), kernelIdx, geomIdx, radius);
108  }
109 
110  return dataSet;
111 }
112 
113 std::auto_ptr<te::rst::Raster> te::sa::KernelOperation::buildRaster(te::sa::KernelInputParams* inputParams, te::sa::KernelTree& kTree, std::string driver)
114 {
115  //get extent of input data
116  te::gm::Envelope* env = new te::gm::Envelope(kTree.getMBR());
117 
118  //get srid
119  int srid = TE_UNKNOWN_SRS;
120 
121  te::gm::GeometryProperty* gmProp = te::da::GetFirstGeomProperty(inputParams->m_dsType.get());
122 
123  if(gmProp)
124  srid = gmProp->getSRID();
125 
126  //create grid
127  double val = std::max<double>(env->getWidth(), env->getHeight());
128 
129  double res = val / m_outputParams->m_nCols;
130 
131  te::rst::Grid* grid = new te::rst::Grid(res, res, env, srid);
132 
133  //create bands
135 
136  bProp->m_noDataValue = 0.;
137 
138  std::vector<te::rst::BandProperty*> vecBandProp;
139  vecBandProp.push_back(bProp);
140 
141  //create raster info
142  std::string fileName = m_outputParams->m_outputPath + "/" + m_outputParams->m_outputDataSetName + ".tif";
143 
144  std::map<std::string, std::string> rInfo;
145  rInfo["URI"] = fileName;
146 
147  //create raster
148  std::auto_ptr<te::rst::Raster> rst(te::rst::RasterFactory::make(driver, grid, vecBandProp, rInfo));
149 
150  return rst;
151 }
152 
154 {
155  //create dataset info
156  std::string fileName = m_outputParams->m_outputPath + "/" + m_outputParams->m_outputDataSetName + ".shp";
157 
158  //connection info
159  std::map<std::string, std::string> info;
160  info["URI"] = fileName;
161 
162  //data source id
163  boost::uuids::basic_random_generator<boost::mt19937> gen;
164  boost::uuids::uuid u = gen();
165  std::string id_ds = boost::uuids::to_string(u);
166 
167  //create data source
168  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(id_ds, "OGR", info);
169 
170  //save dataset
171  dataSet->moveBeforeFirst();
172 
173  std::map<std::string, std::string> options;
174 
175  ds->createDataSet(dsType, options);
176 
177  ds->add(m_outputParams->m_outputDataSetName, dataSet, options);
178 }
179 
180 std::auto_ptr<te::da::DataSetType> te::sa::KernelOperation::createDataSetType(te::da::DataSetType* dsType)
181 {
182  std::auto_ptr<te::da::DataSetType> dataSetType(new te::da::DataSetType(m_outputParams->m_outputDataSetName));
183 
184  //create all input dataset properties
185  std::vector<te::dt::Property*> propertyVec = dsType->getProperties();
186 
187  for(std::size_t t = 0; t < propertyVec.size(); ++t)
188  {
189  te::dt::Property* prop = propertyVec[t];
190 
191  te::dt::Property* newProp = prop->clone();
192  newProp->setId(0);
193  newProp->setParent(0);
194 
195  dataSetType->add(newProp);
196  }
197 
198  //create kernel property
199  te::dt::SimpleProperty* kernelProperty = new te::dt::SimpleProperty(m_outputParams->m_outputAttrName, te::dt::DOUBLE_TYPE);
200  dataSetType->add(kernelProperty);
201 
202  return dataSetType;
203 }
204 
205 std::auto_ptr<te::mem::DataSet> te::sa::KernelOperation::createDataSet(te::da::DataSet* inputDataSet, te::da::DataSetType* dsType)
206 {
207  std::auto_ptr<te::mem::DataSet> outDataset(new te::mem::DataSet(dsType));
208 
209  std::size_t nProp = inputDataSet->getNumProperties();
210 
211  inputDataSet->moveBeforeFirst();
212 
213  while(inputDataSet->moveNext())
214  {
215  //create dataset item
216  te::mem::DataSetItem* outDSetItem = new te::mem::DataSetItem(outDataset.get());
217 
218  for(std::size_t t = 0; t < nProp; ++t)
219  {
220  te::dt::AbstractData* ad = inputDataSet->getValue(t).release();
221 
222  outDSetItem->setValue(t, ad);
223  }
224 
225  //set kernel default value
226  outDSetItem->setDouble(m_outputParams->m_outputAttrName, 0.);
227 
228  //add item into dataset
229  outDataset->add(outDSetItem);
230  }
231 
232  return outDataset;
233 }
KernelOperation()
Default constructor.
std::auto_ptr< te::mem::DataSet > runDataSetKernel(te::sa::KernelInputParams *inputParams, te::sa::KernelTree &kTree, te::sa::KernelMap &kMap, te::da::DataSetType *dsType)
std::auto_ptr< te::da::DataSetType > createDataSetType(te::da::DataSetType *dsType)
Geometric property.
TESAEXPORT void GridAdaptRadiusKernel(te::sa::KernelInputParams *params, te::sa::KernelTree &kTree, te::sa::KernelMap &kMap, te::rst::Raster *raster)
Evaluates kernel value using a raster as output data and a adaptative value for radius.
Utility functions for the data access module.
void setDouble(std::size_t i, double value)
It sets the value of the i-th property.
Class that represents the kernel input parameters.
Definition: KernelParams.h:54
An atomic property like an integer or double.
TESAEXPORT void GridStatRadiusKernel(te::sa::KernelInputParams *params, te::sa::KernelTree &kTree, te::sa::KernelMap &kMap, te::rst::Raster *raster, double radius)
Evaluates kernel value using a raster as output data and a fixed value for radius.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
A raster band description.
Definition: BandProperty.h:61
A class that models the description of a dataset.
Definition: DataSetType.h:72
void saveDataSet(te::da::DataSet *dataSet, te::da::DataSetType *dsType)
virtual Property * clone() const =0
It returns a clone of the object.
std::map< int, std::pair< te::gm::Geometry *, double > > KernelMap
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
double getWidth() const
It returns the envelope width.
Definition: Envelope.h:443
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
Definition: BandProperty.h:136
std::auto_ptr< te::mem::DataSet > createDataSet(te::da::DataSet *inputDataSet, te::da::DataSetType *dsType)
Class that represents the kernel output parameters.
Definition: KernelParams.h:95
It models a property definition.
Definition: Property.h:59
void setOutputParameters(te::sa::KernelOutputParams *outParams)
TESAEXPORT void DataSetAdaptRadiusKernel(te::sa::KernelInputParams *params, te::sa::KernelTree &kTree, te::sa::KernelMap &kMap, te::mem::DataSet *ds, int kernelIdx, int geomIdx)
Evaluates kernel value using a dataset as output data and a adaptative value for radius.
int m_radiusPercentValue
Attribute with radius percent value (m_useAdaptativeRadius must be false)
Definition: KernelParams.h:84
void runRasterKernel(te::sa::KernelInputParams *inputParams, te::sa::KernelTree &kTree, te::sa::KernelMap &kMap, te::rst::Raster *raster)
void setId(unsigned int id)
It sets the property identifier.
Definition: Property.h:118
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
int getSRID() const
It returns the spatial reference system identifier associated to this property.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:44
An abstract class for raster data strucutures.
Definition: Raster.h:71
virtual ~KernelOperation()
Virtual destructor.
std::auto_ptr< te::da::DataSet > m_ds
Attribute with data set.
Definition: KernelParams.h:76
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
Grid * getGrid()
It returns the raster grid.
Definition: Raster.cpp:94
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
const te::gm::Envelope & getMBR(void) const
It returns the bounding box of all elements in the tree.
Definition: Index.h:343
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
bool m_useAdaptativeRadius
Attribute to indicate if a an adaptative radius has to be used.
Definition: KernelParams.h:83
static Raster * make()
It creates and returns an empty raster with default raster driver.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
Definition: Grid.cpp:275
std::auto_ptr< te::da::DataSetType > m_dsType
Attribute used to access the data set metadata.
Definition: KernelParams.h:75
This file contains a class that represents the kernel operation.
std::size_t getPropertyPosition(const std::string &name) const
It returns the property position based on its name.
std::auto_ptr< te::rst::Raster > buildRaster(te::sa::KernelInputParams *inputParams, te::sa::KernelTree &kTree, std::string driver)
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:481
TESAEXPORT void DataSetStatRadiusKernel(te::sa::KernelInputParams *params, te::sa::KernelTree &kTree, te::sa::KernelMap &kMap, te::mem::DataSet *ds, int kernelIdx, int geomIdx, double radius)
Evaluates kernel value using a dataset as output data and a fixed value for radius.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
double getHeight() const
It returns the envelope height.
Definition: Envelope.h:448
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 setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177