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 "../../raster/Utils.h"
42 #include "KernelOperation.h"
43 #include "Utils.h"
44 
45 //STL
46 #include <cassert>
47 
48 // Boost
49 #include <boost/uuid/random_generator.hpp>
50 #include <boost/uuid/uuid_io.hpp>
51 
53 
55 
57 {
58  m_outputParams.reset(outParams);
59 }
60 
62 {
63  if(!raster)
64  throw;
65 
66  //check if use adaptative radius or not
67  if(inputParams->m_useAdaptativeRadius)
68  {
69  te::sa::GridAdaptRadiusKernel(inputParams, kTree, kMap, raster);
70  }
71  else
72  {
73  double val = std::max<double>(raster->getGrid()->getExtent()->getWidth(), raster->getGrid()->getExtent()->getHeight());
74  double radius = (val * inputParams->m_radiusPercentValue) / 100.;
75 
76  te::sa::GridStatRadiusKernel(inputParams, kTree, kMap, raster, radius);
77  }
78 }
79 
81 {
82  if(!dsType)
83  throw;
84 
85  //create dataset in memory
86  std::unique_ptr<te::mem::DataSet> dataSet = createDataSet(inputParams->m_ds.get(), dsType);
87 
88  //get kernel attr index
89  std::size_t kernelIdx = dsType->getPropertyPosition(m_outputParams->m_outputAttrName);
90 
91  //get geom attr index
92  std::size_t geomIdx = te::da::GetFirstPropertyPos(dataSet.get(), te::dt::GEOMETRY_TYPE);
93 
94  //check if use adaptative radius or not
95  if(inputParams->m_useAdaptativeRadius)
96  {
97  te::sa::DataSetAdaptRadiusKernel(inputParams, kTree, kMap, dataSet.get(), static_cast<int>(kernelIdx), static_cast<int>(geomIdx));
98  }
99  else
100  {
101  double val = std::max<double>(kTree.getMBR().getWidth(), kTree.getMBR().getHeight());
102  double radius = (val * inputParams->m_radiusPercentValue) / 100.;
103 
104  te::sa::DataSetStatRadiusKernel(inputParams, kTree, kMap, dataSet.get(), static_cast<int>(kernelIdx), static_cast<int>(geomIdx), radius);
105  }
106 
107  return dataSet;
108 }
109 
110 std::unique_ptr<te::rst::Raster> te::sa::KernelOperation::buildRaster(te::sa::KernelInputParams* inputParams, te::sa::KernelTree& kTree, std::string driver)
111 {
112  //get extent of input data
113  te::gm::Envelope* env = new te::gm::Envelope(kTree.getMBR());
114 
115  //get srid
116  int srid = TE_UNKNOWN_SRS;
117 
118  te::gm::GeometryProperty* gmProp = te::da::GetFirstGeomProperty(inputParams->m_dsType.get());
119 
120  if(gmProp)
121  srid = gmProp->getSRID();
122 
123  //create grid
124  double val = std::max<double>(env->getWidth(), env->getHeight());
125 
126  double res = val / m_outputParams->m_nCols;
127 
128  te::rst::Grid* grid = new te::rst::Grid(res, res, env, srid);
129 
130  //create bands
132 
133  bProp->m_noDataValue = 0.;
134 
135  std::vector<te::rst::BandProperty*> vecBandProp;
136  vecBandProp.push_back(bProp);
137 
138  //create raster info
139  std::string fileName = m_outputParams->m_outputPath + "/" + m_outputParams->m_outputDataSetName + ".tif";
140 
141  std::map<std::string, std::string> rInfo;
142  rInfo["URI"] = fileName;
143 
144  //create raster
145  std::unique_ptr<te::rst::Raster> rst(te::rst::RasterFactory::make(driver, grid, vecBandProp, rInfo));
146 
147  //fill raster if dummy value
148  te::rst::FillRaster(rst.get(), rst->getBand(0)->getProperty()->m_noDataValue);
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  //data source id
159  boost::uuids::basic_random_generator<boost::mt19937> gen;
160  boost::uuids::uuid u = gen();
161  std::string id_ds = boost::uuids::to_string(u);
162 
163  //create data source
164  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(id_ds, "OGR", ("file://" + fileName));
165 
166  //save dataset
167  dataSet->moveBeforeFirst();
168 
169  std::map<std::string, std::string> options;
170 
171  ds->createDataSet(dsType, options);
172 
173  ds->add(m_outputParams->m_outputDataSetName, dataSet, options);
174 }
175 
176 std::unique_ptr<te::da::DataSetType> te::sa::KernelOperation::createDataSetType(te::da::DataSetType* dsType)
177 {
178  std::unique_ptr<te::da::DataSetType> dataSetType(new te::da::DataSetType(m_outputParams->m_outputDataSetName));
179 
180  //create all input dataset properties
181  std::vector<te::dt::Property*> propertyVec = dsType->getProperties();
182 
183  for(std::size_t t = 0; t < propertyVec.size(); ++t)
184  {
185  te::dt::Property* prop = propertyVec[t];
186 
187  te::dt::Property* newProp = prop->clone();
188  newProp->setId(0);
189  newProp->setParent(nullptr);
190 
191  dataSetType->add(newProp);
192  }
193 
194  //create kernel property
195  te::dt::SimpleProperty* kernelProperty = new te::dt::SimpleProperty(m_outputParams->m_outputAttrName, te::dt::DOUBLE_TYPE);
196  dataSetType->add(kernelProperty);
197 
198  return dataSetType;
199 }
200 
201 std::unique_ptr<te::mem::DataSet> te::sa::KernelOperation::createDataSet(te::da::DataSet* inputDataSet, te::da::DataSetType* dsType)
202 {
203  std::unique_ptr<te::mem::DataSet> outDataset(new te::mem::DataSet(dsType));
204 
205  std::size_t nProp = inputDataSet->getNumProperties();
206 
207  inputDataSet->moveBeforeFirst();
208 
209  while(inputDataSet->moveNext())
210  {
211  //create dataset item
212  te::mem::DataSetItem* outDSetItem = new te::mem::DataSetItem(outDataset.get());
213 
214  for(std::size_t t = 0; t < nProp; ++t)
215  {
216  te::dt::AbstractData* ad = inputDataSet->getValue(t).release();
217 
218  outDSetItem->setValue(t, ad);
219  }
220 
221  //set kernel default value
222  outDSetItem->setDouble(m_outputParams->m_outputAttrName, 0.);
223 
224  //add item into dataset
225  outDataset->add(outDSetItem);
226  }
227 
228  return outDataset;
229 }
KernelOperation()
Default constructor.
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.
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
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
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)
std::unique_ptr< te::mem::DataSet > runDataSetKernel(te::sa::KernelInputParams *inputParams, te::sa::KernelTree &kTree, te::sa::KernelMap &kMap, te::da::DataSetType *dsType)
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.
TERASTEREXPORT void FillRaster(te::rst::Raster *rin, const std::complex< double > &value)
Fill a Raster with provided value (all bands).
std::unique_ptr< te::da::DataSetType > m_dsType
Attribute used to access the data set metadata.
Definition: KernelParams.h:75
static te::dt::Date ds(2010, 01, 01)
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
Definition: BandProperty.h:136
virtual Property * clone() const =0
It returns a clone of the object.
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...
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.
An abstract class for raster data strucutures.
virtual ~KernelOperation()
Virtual destructor.
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.
Utility functions for the data access module.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
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.
const te::gm::Envelope & getMBR(void) const
It returns the bounding box of all elements in 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.
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.
std::unique_ptr< te::mem::DataSet > createDataSet(te::da::DataSet *inputDataSet, te::da::DataSetType *dsType)
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
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.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
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)
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
std::unique_ptr< te::da::DataSetType > createDataSetType(te::da::DataSetType *dsType)
double getHeight() const
It returns the envelope height.
std::unique_ptr< te::rst::Raster > buildRaster(te::sa::KernelInputParams *inputParams, te::sa::KernelTree &kTree, std::string driver)
std::unique_ptr< te::da::DataSet > m_ds
Attribute with data set.
Definition: KernelParams.h:76
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177