All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.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/qt/Utils.cpp
22 
23  \brief Utilitary interface function for spatial analysis module.
24 */
25 
26 //TerraLib
27 #include "../../color/ColorBar.h"
28 #include "../../color/ColorScheme.h"
29 #include "../../color/ColorSchemeCatalog.h"
30 #include "../../color/ColorSchemeCatalogManager.h"
31 #include "../../color/ColorSchemeGroup.h"
32 #include "../../color/RGBAColor.h"
33 #include "../../common/Globals.h"
34 #include "../../common/StringUtils.h"
35 #include "../../dataaccess/datasource/DataSource.h"
36 #include "../../dataaccess/datasource/DataSourceInfo.h"
37 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
38 #include "../../dataaccess/datasource/DataSourceManager.h"
39 #include "../../maptools/Grouping.h"
40 #include "../../maptools/GroupingAlgorithms.h"
41 #include "../../maptools/GroupingItem.h"
42 #include "../../maptools/Utils.h"
43 #include "../../raster/RasterSummary.h"
44 #include "../../raster/RasterSummaryManager.h"
45 #include "../../se/ColorMap.h"
46 #include "../../se/Enums.h"
47 #include "../../se/Interpolate.h"
48 #include "../../se/InterpolationPoint.h"
49 #include "../../se/ParameterValue.h"
50 #include "../../se/RasterSymbolizer.h"
51 #include "../../se/Utils.h"
52 #include "../../qt/widgets/layer/utils/DataSet2Layer.h"
53 #include "Utils.h"
54 
55 //Boost
56 #include <boost/filesystem.hpp>
57 #include <boost/uuid/random_generator.hpp>
58 #include <boost/uuid/uuid_io.hpp>
59 
60 //Qt
61 #include <QColor>
62 
64 {
65  //create new data source
66  boost::filesystem::path uri(repository);
67 
68  std::map<std::string, std::string> dsInfo;
69  dsInfo["URI"] = uri.string();
70 
71  boost::uuids::basic_random_generator<boost::mt19937> gen;
72  boost::uuids::uuid u = gen();
73  std::string id_ds = boost::uuids::to_string(u);
74 
76  dsInfoPtr->setConnInfo(dsInfo);
77  dsInfoPtr->setTitle(uri.stem().string());
78  dsInfoPtr->setAccessDriver("OGR");
79  dsInfoPtr->setType("OGR");
80  dsInfoPtr->setDescription(uri.string());
81  dsInfoPtr->setId(id_ds);
82 
84 
85  return te::da::DataSourceManager::getInstance().get(id_ds, "OGR", dsInfoPtr->getConnInfo());
86 }
87 
88 te::da::DataSourcePtr te::sa::CreateOGRDataSource(std::string path, std::string dataSetName)
89 {
90  std::string name = path + "/" + dataSetName + ".shp";
91 
92  return CreateOGRDataSource(name);
93 }
94 
95 te::da::DataSourcePtr te::sa::CreateGDALDataSource(std::string path, std::string dataSetName)
96 {
97  std::string name = path + "/" + dataSetName + ".tif";
98 
99  //create new data source
100  boost::filesystem::path uri(name);
101 
102  std::map<std::string, std::string> dsInfo;
103  dsInfo["URI"] = uri.string();
104 
105  boost::uuids::basic_random_generator<boost::mt19937> gen;
106  boost::uuids::uuid u = gen();
107  std::string id_ds = boost::uuids::to_string(u);
108 
110  dsInfoPtr->setConnInfo(dsInfo);
111  dsInfoPtr->setTitle(uri.stem().string());
112  dsInfoPtr->setAccessDriver("GDAL");
113  dsInfoPtr->setType("GDAL");
114  dsInfoPtr->setDescription(uri.string());
115  dsInfoPtr->setId(id_ds);
116 
118 
119  return te::da::DataSourceManager::getInstance().get(id_ds, "GDAL", dsInfoPtr->getConnInfo());
120 }
121 
123 {
124  te::qt::widgets::DataSet2Layer converter(ds->getId());
125 
126  te::da::DataSetTypePtr dt(ds->getDataSetType(dataSetName).release());
127 
128  return converter(dt);
129 }
130 
132 {
133  std::string bayesAttr = TE_SA_BAYES_ATTR_NAME;
134  int attrType = te::dt::DOUBLE_TYPE;
135  int slices = 10;
136  int prec = 6;
137  int nullValues = 0;
138  std::vector<double> vec;
139 
140  //get data
141  std::auto_ptr<te::da::DataSet> ds(layer->getData());
142  ds->moveBeforeFirst();
143 
144  while(ds->moveNext())
145  {
146  if(ds->isNull(bayesAttr))
147  {
148  ++nullValues;
149  continue;
150  }
151 
152  vec.push_back(ds->getDouble(bayesAttr));
153  }
154 
155  //create grouping items
156  std::vector<te::map::GroupingItem*> legend;
157 
158  te::map::GroupingByEqualSteps(vec.begin(), vec.end(), slices, legend, prec);
159 
160  std::auto_ptr<te::color::ColorBar> cb(GetColorBar("Default", "Basic", "Vermelho"));
161 
162  int legendSize = legend.size();
163 
164  std::vector<te::color::RGBAColor> colorVec;
165 
166  colorVec = cb->getSlices(legendSize);
167 
168  //create symbolizer
169  int geomType = te::map::GetGeomType(layer);
170 
171  for(size_t t = 0; t < colorVec.size(); ++t)
172  {
173  std::vector<te::se::Symbolizer*> symbVec;
174 
175  te::se::Symbolizer* s = te::se::CreateSymbolizer((te::gm::GeomType)geomType, colorVec[t].getColor());
176 
177  symbVec.push_back(s);
178 
179  legend[t]->setSymbolizers(symbVec);
180  }
181 
182  //create null grouping item
183  if(nullValues != 0)
184  {
188  legendItem->setTitle("No Value");
189  legendItem->setCount(nullValues);
190 
191  std::vector<te::se::Symbolizer*> symbVec;
193  symbVec.push_back(s);
194  legendItem->setSymbolizers(symbVec);
195 
196  legend.push_back(legendItem);
197  }
198 
199  //create grouping
201  group->setPropertyType(attrType);
202  group->setNumSlices(slices);
203  group->setPrecision(prec);
204  group->setStdDeviation(0.);
205  group->setGroupingItems(legend);
206 
207  layer->setGrouping(group);
208 }
209 
210 void te::sa::CreateKernelGrouping(te::map::AbstractLayerPtr layer, std::string kernelAttr)
211 {
212  int attrType = te::dt::DOUBLE_TYPE;
213  int slices = 20;
214  int prec = 15;
215  int nullValues = 0;
216  std::vector<double> vec;
217 
218  //get data
219  std::auto_ptr<te::da::DataSet> ds(layer->getData());
220  ds->moveBeforeFirst();
221 
222  while(ds->moveNext())
223  {
224  if(ds->isNull(kernelAttr))
225  {
226  ++nullValues;
227  continue;
228  }
229 
230  vec.push_back(ds->getDouble(kernelAttr));
231  }
232 
233  //create grouping items
234  std::vector<te::map::GroupingItem*> legend;
235 
236  te::map::GroupingByEqualSteps(vec.begin(), vec.end(), slices, legend, prec);
237 
238  std::auto_ptr<te::color::ColorBar> cb(GetColorBar("Default", "Classification", "Kernel"));
239 
240  int legendSize = legend.size();
241 
242  std::vector<te::color::RGBAColor> colorVec;
243 
244  colorVec = cb->getSlices(legendSize);
245 
246  //create symbolizer
247  int geomType = te::map::GetGeomType(layer);
248 
249  for(size_t t = 0; t < colorVec.size(); ++t)
250  {
251  std::vector<te::se::Symbolizer*> symbVec;
252 
253  te::se::Symbolizer* s = te::se::CreateSymbolizer((te::gm::GeomType)geomType, colorVec[t].getColor());
254 
255  symbVec.push_back(s);
256 
257  legend[t]->setSymbolizers(symbVec);
258  }
259 
260  //create null grouping item
261  if(nullValues != 0)
262  {
266  legendItem->setTitle("No Value");
267  legendItem->setCount(nullValues);
268 
269  std::vector<te::se::Symbolizer*> symbVec;
271  symbVec.push_back(s);
272  legendItem->setSymbolizers(symbVec);
273 
274  legend.push_back(legendItem);
275  }
276 
277  //create grouping
278  te::map::Grouping* group = new te::map::Grouping(kernelAttr, te::map::EQUAL_STEPS);
279  group->setPropertyType(attrType);
280  group->setNumSlices(slices);
281  group->setPrecision(prec);
282  group->setStdDeviation(0.);
283  group->setGroupingItems(legend);
284 
285  layer->setGrouping(group);
286 }
287 
289 {
290  int slices = 20;
291  int prec = 15;
292 
293  //get raster
295 
296  if(!raster)
297  return;
298 
301  const std::complex<double>* cmin = rsMin->at(0).m_minVal;
302  const std::complex<double>* cmax = rsMax->at(0).m_maxVal;
303  double min = cmin->real();
304  double max = cmax->real();
305 
306  delete raster;
307 
308  //get color bar
309  std::auto_ptr<te::color::ColorBar> cb(GetColorBar("Default", "Classification", "Kernel"));
310 
311  std::vector<te::color::RGBAColor> colorVec = cb->getSlices(slices + 1);
312 
313  std::vector<te::map::GroupingItem*> legVec;
314 
315  std::vector<double> vec;
316  vec.push_back(min);
317  vec.push_back(max);
318 
319  //create grouping
320  te::map::GroupingByEqualSteps(vec.begin(), vec.end(), slices, legVec, prec);
321 
322  te::se::Interpolate* interpolate = new te::se::Interpolate();
323 
324  interpolate->setFallbackValue("#000000");
325  interpolate->setLookupValue(new te::se::ParameterValue("Rasterdata"));
326  interpolate->setMethodType(te::se::Interpolate::COLOR);
327 
328  for(std::size_t i = 0; i < colorVec.size(); ++i)
329  {
330  QColor color(colorVec[i].getRed(), colorVec[i].getGreen(), colorVec[i].getBlue(), colorVec[i].getAlpha());
331 
332  if(i == colorVec.size() - 1)
333  {
334  QString rangeStr = legVec[i - 1]->getUpperLimit().c_str();
335  std::string colorStr = color.name().toLatin1().data();
336 
338 
339  ip->setData(rangeStr.toDouble());
340  ip->setValue(new te::se::ParameterValue(colorStr));
341 
342  interpolate->add(ip);
343  }
344  else
345  {
346  QString rangeStr = legVec[i]->getLowerLimit().c_str();
347  std::string colorStr = color.name().toLatin1().data();
348 
350 
351  ip->setData(rangeStr.toDouble());
352  ip->setValue(new te::se::ParameterValue(colorStr));
353 
354  interpolate->add(ip);
355  }
356  }
357 
359  cm->setInterpolate(interpolate);
360 
361  te::se::RasterSymbolizer* rasterSymb = te::se::GetRasterSymbolizer(layer->getStyle());
362  rasterSymb->setColorMap(cm);
363 }
364 
366 {
367  std::string stratifiedAttr = TE_SA_SPG_ATTR_CLASS_NAME;
368  int attrType = te::dt::STRING_TYPE;
369  int prec = 0;
370 
371  //create grouping items
372  std::vector<te::map::GroupingItem*> legend;
373 
374  te::map::GroupingByUniqueValues(strVec, attrType, legend, prec);
375 
376  std::auto_ptr<te::color::ColorBar> cb(GetColorBar("Default", "Classification", "Circular"));
377 
378  int legendSize = legend.size();
379 
380  std::vector<te::color::RGBAColor> colorVec;
381 
382  colorVec = cb->getSlices(legendSize);
383 
384  //create symbolizer
385  int geomType = te::map::GetGeomType(layer);
386 
387  for(size_t t = 0; t < colorVec.size(); ++t)
388  {
389  std::vector<te::se::Symbolizer*> symbVec;
390 
391  te::se::Symbolizer* s = te::se::CreateSymbolizer((te::gm::GeomType)geomType, colorVec[t].getColor());
392 
393  symbVec.push_back(s);
394 
395  legend[t]->setSymbolizers(symbVec);
396  }
397 
398  //create grouping
399  te::map::Grouping* group = new te::map::Grouping(stratifiedAttr, te::map::UNIQUE_VALUE);
400  group->setPropertyType(attrType);
401  group->setPrecision(prec);
402  group->setStdDeviation(0.);
403  group->setGroupingItems(legend);
404 
405  layer->setGrouping(group);
406 }
407 
409 {
410  std::string skaterAttr = TE_SA_SKATER_ATTR_CLASS_NAME;
411  int attrType = te::dt::INT32_TYPE;
412  int prec = 0;
413 
414  //create grouping items
415  std::vector<te::map::GroupingItem*> legend;
416 
417  std::vector<std::string> strVec;
418 
419  for(int i = 0; i < nClasses; ++i)
420  {
421  strVec.push_back(te::common::Convert2String(i));
422  }
423 
424  te::map::GroupingByUniqueValues(strVec, attrType, legend, prec);
425 
426  //std::auto_ptr<te::color::ColorBar> cb(GetColorBar("Default", "Classification", "Circular"));
427 
428  //int legendSize = legend.size();
429 
430  //std::vector<te::color::RGBAColor> colorVec;
431 
432  //colorVec = cb->getSlices(legendSize);
433 
434  //create symbolizer
435  int geomType = te::map::GetGeomType(layer);
436 
437  for(size_t t = 0; t < legend.size(); ++t)
438  {
439  std::vector<te::se::Symbolizer*> symbVec;
440 
442 
443  symbVec.push_back(s);
444 
445  legend[t]->setSymbolizers(symbVec);
446  }
447 
448  //create grouping
450  group->setPropertyType(attrType);
451  group->setPrecision(prec);
452  group->setStdDeviation(0.);
453  group->setGroupingItems(legend);
454 
455  layer->setGrouping(group);
456 }
457 
458 te::color::ColorBar* te::sa::GetColorBar(std::string catalog, std::string group, std::string schema)
459 {
460  te::color::ColorBar* cb = 0;
461 
463 
464  if(csc)
465  {
466  std::vector<te::color::ColorSchemeGroup*> csgVec = csc->getColorSchemeGroups();
467 
468  for(std::size_t t = 0; t < csgVec.size(); ++t)
469  {
470  te::color::ColorSchemeGroup* csg = csgVec[t];
471 
472  if(csg->getName() == group)
473  {
474  std::vector<te::color::ColorScheme*> csVec = csg->getColorSchemes();
475 
476  for(std::size_t p = 0; p < csVec.size(); ++p)
477  {
478  te::color::ColorScheme* cs = csVec[p];
479 
480  if(cs->getName() == schema)
481  {
482  std::vector<te::color::RGBAColor>* colors = cs->getColors()[0];
483 
484  std::vector<te::color::RGBAColor>::iterator it = colors->begin();
485 
486  // create color bar
487  cb = new te::color::ColorBar(*(colors->begin()), *(colors->end() - 1), 256);
488 
489  int count = 0;
490 
491  //fill color bar
492  while(it != colors->end())
493  {
494  if(count != 0 && count != colors->size() - 1)
495  {
496  double pos = (1. / (colors->size() - 1)) * count;
497 
498  cb->addColor(*it, pos);
499  }
500 
501  ++count;
502  ++it;
503  }
504  }
505  }
506  }
507  }
508  }
509 
510  return cb;
511 }
void setPrecision(size_t precision)
It sets the precision to be used for the property values.
Definition: Grouping.cpp:105
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
TESAEXPORT void CreateKernelColorMaping(te::map::AbstractLayerPtr layer)
Definition: Utils.cpp:288
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
TESEEXPORT Symbolizer * CreateSymbolizer(const te::gm::GeomType &geomType)
Try creates an appropriate symbolizer based on given geometry type.
Definition: Utils.cpp:220
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
const std::vector< ColorScheme * > & getColorSchemes() const
It returns a reference to the list of color schemes belonging to this group.
TESAEXPORT te::da::DataSourcePtr CreateOGRDataSource(std::string repository)
Definition: Utils.cpp:63
const std::vector< ColorSchemeGroup * > & getColorSchemeGroups() const
It returns the list of color scheme groups in the catalog.
void setData(const double &d)
void setGroupingItems(std::vector< te::map::GroupingItem * > &items)
It sets the vector of grouping items.
Definition: Grouping.cpp:135
TESAEXPORT void CreateSampleGeneratorStratifiedGrouping(te::map::AbstractLayerPtr layer, std::vector< std::string > strVec)
Definition: Utils.cpp:365
A Symbolizer describes how a feature is to appear on a map.
Definition: Symbolizer.h:80
const std::string & getName() const
It returns the group name.
TESEEXPORT RasterSymbolizer * GetRasterSymbolizer(Style *s)
Try to get raster symbolizer from a style.
Definition: Utils.cpp:371
It models the concept of color scheme.
Definition: ColorScheme.h:58
TEMAPEXPORT te::gm::GeomType GetGeomType(const te::map::AbstractLayerPtr &layer)
It gets the geometry type of the given layer.
Definition: Utils.cpp:748
void addColor(const RGBAColor &color, const double &pos)
It adds a color in the color bar.
Definition: ColorBar.cpp:264
const std::vector< std::vector< RGBAColor > * > & getColors() const
It returns all color lists.
Definition: ColorScheme.cpp:68
TESAEXPORT te::da::DataSourcePtr CreateGDALDataSource(std::string path, std::string dataSetName)
Definition: Utils.cpp:95
void setStdDeviation(double stdDeviation)
It sets the standard deviation for the Standard Deviation grouping.
Definition: Grouping.cpp:125
void setTitle(const std::string &title)
It sets the title of the legend item.
void GroupingByEqualSteps(iterator begin, iterator end, int nSteps, std::vector< te::map::GroupingItem * > &legend, int precision=0, bool countElements=true)
It groups the values defined by a range of iterators using the equal steps algorithm.
The transformation of continuous values to a number of values (Interpolate function).
Definition: Interpolate.h:88
The "ParameterValueType" uses WFS-Filter expressions to give values for SE graphic parameters...
void setValue(ParameterValue *v)
This class contains the parameters needed for grouping the values of a Property.
Definition: Grouping.h:59
void setNumSlices(size_t numSlices)
It sets the number of slices for the EqualSteps and Quantil groupings.
Definition: Grouping.cpp:115
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
A GroupingItem contains information about a grouping item associated to a layer.
Definition: GroupingItem.h:48
TESAEXPORT void CreateBayesGrouping(te::map::AbstractLayerPtr layer)
Definition: Utils.cpp:131
An abstract class for raster data strucutures.
Definition: Raster.h:71
void setPropertyType(const int &type)
It sets the property type whose values will be grouped.
Definition: Grouping.cpp:84
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
Definition: RasterSummary.h:44
The RasterSymbolizer describes how to render raster/matrix-coverage data (e.g., satellite photos...
const std::string & getName() const
It returns the color schema name.
Definition: ColorScheme.cpp:42
TEMAPEXPORT te::rst::Raster * GetRaster(DataSetLayer *layer)
It gets the raster referenced by the given data set layer.
Definition: Utils.cpp:192
Utility functions for MapTools module.
This class represents a group of color schemes.
They are used to define a graph of points.
TEMAPEXPORT void GroupingByUniqueValues(std::vector< std::string > &inputValues, int dataType, std::vector< te::map::GroupingItem * > &legend, int precision)
It groups the values using the unique value algorithm.
void setCount(std::size_t count)
It It sets the number of objects whose values are between the lower and upper limits.
A catalog for color schemes.
TESAEXPORT te::map::AbstractLayerPtr CreateLayer(te::da::DataSourcePtr ds, std::string dataSetName)
Definition: Utils.cpp:122
void setColorMap(ColorMap *c)
void setUpperLimit(const std::string &to)
It sets the upper limit value of the legend item.
TESAEXPORT te::color::ColorBar * GetColorBar(std::string catalog, std::string group, std::string schema)
Definition: Utils.cpp:458
It models the concept of color bar.
Definition: ColorBar.h:49
A class that represents a data source component.
#define TE_SA_SKATER_ATTR_CLASS_NAME
Definition: Config.h:104
TESEEXPORT std::string GenerateRandomColor()
Creates a random RGB color encoded using two hexadecimal digits per primary-color component prefixed ...
Definition: Utils.cpp:394
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
TESAEXPORT void CreateKernelGrouping(te::map::AbstractLayerPtr layer, std::string kernelAttr)
Definition: Utils.cpp:210
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:51
TESAEXPORT void CreateSkaterGrouping(te::map::AbstractLayerPtr layer, int nClasses)
Definition: Utils.cpp:408
Calculate the min value.
Definition: Enums.h:40
Calculate the max value.
Definition: Enums.h:41
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
#define TE_SA_SPG_ATTR_CLASS_NAME
Definition: Config.h:92
#define TE_SA_BAYES_ATTR_NAME
Definition: Config.h:80
void setInterpolate(Interpolate *i)
Definition: ColorMap.cpp:69
A ColorMap defines either the colors of a pallette-type raster source or the mapping of numeric pixel...
Definition: ColorMap.h:60
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
void setSymbolizers(const std::vector< te::se::Symbolizer * > &symbolizers)
It sets the symbolizers of the legend item.
static const std::string sm_nanStr
Not a number string value.
Definition: Globals.h:56
void setLowerLimit(const std::string &from)
It sets the lower limit value of the legend item.