src/terralib/sa/qt/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/Utils.h"
42 #include "../../raster/RasterSummary.h"
43 #include "../../raster/RasterSummaryManager.h"
44 #include "../../se/ColorMap.h"
45 #include "../../se/Enums.h"
46 #include "../../se/Interpolate.h"
47 #include "../../se/InterpolationPoint.h"
48 #include "../../se/ParameterValue.h"
49 #include "../../se/RasterSymbolizer.h"
50 #include "../../se/Style.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::string dsInfo("file://" + uri.string());
69 
70  boost::uuids::basic_random_generator<boost::mt19937> gen;
71  boost::uuids::uuid u = gen();
72  std::string id_ds = boost::uuids::to_string(u);
73 
75  dsInfoPtr->setConnInfo(dsInfo);
76  dsInfoPtr->setTitle(uri.stem().string());
77  dsInfoPtr->setAccessDriver("OGR");
78  dsInfoPtr->setType("OGR");
79  dsInfoPtr->setDescription(uri.string());
80  dsInfoPtr->setId(id_ds);
81 
83 
84  return te::da::DataSourceManager::getInstance().get(id_ds, "OGR", dsInfoPtr->getConnInfo());
85 }
86 
87 te::da::DataSourcePtr te::sa::CreateOGRDataSource(std::string path, std::string dataSetName)
88 {
89  std::string name = path + "/" + dataSetName + ".shp";
90 
91  return CreateOGRDataSource(name);
92 }
93 
94 te::da::DataSourcePtr te::sa::CreateGDALDataSource(std::string path, std::string dataSetName)
95 {
96  std::string name = path + "/" + dataSetName + ".tif";
97 
98  //create new data source
99  boost::filesystem::path uri(name);
100 
101  std::string dsInfo("file://" + uri.string());
102 
103  boost::uuids::basic_random_generator<boost::mt19937> gen;
104  boost::uuids::uuid u = gen();
105  std::string id_ds = boost::uuids::to_string(u);
106 
108  dsInfoPtr->setConnInfo(dsInfo);
109  dsInfoPtr->setTitle(uri.stem().string());
110  dsInfoPtr->setAccessDriver("GDAL");
111  dsInfoPtr->setType("GDAL");
112  dsInfoPtr->setDescription(uri.string());
113  dsInfoPtr->setId(id_ds);
114 
116 
117  return te::da::DataSourceManager::getInstance().get(id_ds, "GDAL", dsInfoPtr->getConnInfo());
118 }
119 
121 {
122  te::qt::widgets::DataSet2Layer converter(ds->getId());
123 
124  te::da::DataSetTypePtr dt(ds->getDataSetType(dataSetName).release());
125 
126  return converter(dt);
127 }
128 
130 {
131  std::string bayesAttr = TE_SA_BAYES_ATTR_NAME;
132  int attrType = te::dt::DOUBLE_TYPE;
133  int slices = 10;
134  int prec = 6;
135  int nullValues = 0;
136  std::vector<double> vec;
137 
138  //get data
139  std::unique_ptr<te::da::DataSet> ds(layer->getData());
140  ds->moveBeforeFirst();
141 
142  while(ds->moveNext())
143  {
144  if(ds->isNull(bayesAttr))
145  {
146  ++nullValues;
147  continue;
148  }
149 
150  vec.push_back(ds->getDouble(bayesAttr));
151  }
152 
153  //create grouping items
154  std::vector<te::se::Rule*> legend;
155 
156  te::map::GroupingByEqualSteps(bayesAttr, vec.begin(), vec.end(), slices, legend, prec);
157 
158  std::unique_ptr<te::color::ColorBar> cb(GetColorBar("Default", "Basic", "Red"));
159 
160  int legendSize = static_cast<int>(legend.size());
161 
162  std::vector<te::color::RGBAColor> colorVec;
163 
164  colorVec = cb->getSlices(legendSize);
165 
166  //create symbolizer
167  int geomType = te::map::GetGeomType(layer);
168 
169  for(size_t t = 0; t < colorVec.size(); ++t)
170  {
171  std::vector<te::se::Symbolizer*> symbVec;
172 
173  te::se::Symbolizer* s = te::se::CreateSymbolizer((te::gm::GeomType)geomType, colorVec[t].getColor());
174 
175  symbVec.push_back(s);
176 
177  legend[t]->setSymbolizers(symbVec);
178  }
179 
180  //create null grouping item
181  if(nullValues != 0)
182  {
183  std::string attrName = bayesAttr;
184 
185  std::string* ruleName = new std::string("No Value");
186 
187  te::se::Rule* rule = new te::se::Rule;
188  rule->setName(ruleName);
190 
191  std::vector<te::se::Symbolizer*> symbVec;
193  symbVec.push_back(s);
194  rule->setSymbolizers(symbVec);
195 
196  legend.push_back(rule);
197  }
198 
199  //create grouping
201  group->setPropertyType(attrType);
202  group->setNumSlices(slices);
203  group->setPrecision(prec);
204  group->setStdDeviation(0.);
205 
206  //fill rules into layer style
207  te::se::Style* style = layer->getStyle();
208 
209  style->removeRules();
210 
211  for (std::size_t t = 0; t < legend.size(); ++t)
212  style->push_back(legend[t]);
213 
214  layer->setGrouping(group);
215 }
216 
217 void te::sa::CreateKernelGrouping(te::map::AbstractLayerPtr layer, std::string kernelAttr)
218 {
219  int attrType = te::dt::DOUBLE_TYPE;
220  int slices = 20;
221  int prec = 15;
222  int nullValues = 0;
223  std::vector<double> vec;
224 
225  //get data
226  std::unique_ptr<te::da::DataSet> ds(layer->getData());
227  ds->moveBeforeFirst();
228 
229  while(ds->moveNext())
230  {
231  if(ds->isNull(kernelAttr))
232  {
233  ++nullValues;
234  continue;
235  }
236 
237  vec.push_back(ds->getDouble(kernelAttr));
238  }
239 
240  //create grouping items
241  std::vector<te::se::Rule*> legend;
242 
243  te::map::GroupingByEqualSteps(kernelAttr, vec.begin(), vec.end(), slices, legend, prec);
244 
245  std::unique_ptr<te::color::ColorBar> cb(GetColorBar("Default", "Classification", "Kernel"));
246 
247  int legendSize = static_cast<int>(legend.size());
248 
249  std::vector<te::color::RGBAColor> colorVec;
250 
251  colorVec = cb->getSlices(legendSize);
252 
253  //create symbolizer
254  int geomType = te::map::GetGeomType(layer);
255 
256  for(size_t t = 0; t < colorVec.size(); ++t)
257  {
258  std::vector<te::se::Symbolizer*> symbVec;
259 
260  te::se::Symbolizer* s = te::se::CreateSymbolizer((te::gm::GeomType)geomType, colorVec[t].getColor());
261 
262  symbVec.push_back(s);
263 
264  legend[t]->setSymbolizers(symbVec);
265  }
266 
267  //create null grouping item
268  if(nullValues != 0)
269  {
270  std::string attrName = kernelAttr;
271 
272  std::string* ruleName = new std::string("No Value");
273 
274  te::se::Rule* rule = new te::se::Rule;
275  rule->setName(ruleName);
277 
278  std::vector<te::se::Symbolizer*> symbVec;
280  symbVec.push_back(s);
281  rule->setSymbolizers(symbVec);
282 
283  legend.push_back(rule);
284  }
285 
286  //create grouping
287  te::map::Grouping* group = new te::map::Grouping(kernelAttr, te::map::EQUAL_STEPS);
288  group->setPropertyType(attrType);
289  group->setNumSlices(slices);
290  group->setPrecision(prec);
291  group->setStdDeviation(0.);
292 
293  //fill rules into layer style
294  te::se::Style* style = layer->getStyle();
295 
296  style->removeRules();
297 
298  for (std::size_t t = 0; t < legend.size(); ++t)
299  style->push_back(legend[t]);
300 
301  layer->setGrouping(group);
302 }
303 
305 {
306  int slices = 20;
307  int prec = 15;
308 
309  //get raster
311 
312  if(!raster)
313  return;
314 
317  const std::complex<double>* cmin = rsMin->at(0).m_minVal;
318  const std::complex<double>* cmax = rsMax->at(0).m_maxVal;
319  double min = cmin->real();
320  double max = cmax->real();
321 
322  delete raster;
323 
324  //get color bar
325  std::unique_ptr<te::color::ColorBar> cb(GetColorBar("Default", "Classification", "Kernel"));
326 
327  std::vector<te::color::RGBAColor> colorVec = cb->getSlices(slices + 1);
328 
329  std::vector<te::se::Rule*> legVec;
330 
331  std::vector<double> vec;
332  vec.push_back(min);
333  vec.push_back(max);
334 
335  //create grouping
336  te::map::GroupingByEqualSteps("raster", vec.begin(), vec.end(), slices, legVec, prec);
337 
338  te::se::Interpolate* interpolate = new te::se::Interpolate();
339 
340  interpolate->setFallbackValue("#000000");
341  interpolate->setLookupValue(new te::se::ParameterValue("Rasterdata"));
342  interpolate->setMethodType(te::se::Interpolate::COLOR);
343 
344  for(std::size_t i = 0; i < colorVec.size(); ++i)
345  {
346  QColor color(colorVec[i].getRed(), colorVec[i].getGreen(), colorVec[i].getBlue(), colorVec[i].getAlpha());
347 
348  if(i == colorVec.size() - 1)
349  {
350  std::string valueMin;
351  std::string valueMax;
352  const te::fe::Filter* ruleFilter = legVec[i - 1]->getFilter();
353  te::fe::GetFilterStepValues(ruleFilter, valueMin, valueMax);
354 
355  QString rangeStr = valueMax.c_str();
356  std::string colorStr = color.name().toUtf8().data();
357 
359 
360  ip->setData(rangeStr.toDouble());
361  ip->setValue(new te::se::ParameterValue(colorStr));
362 
363  interpolate->add(ip);
364  }
365  else
366  {
367  std::string valueMin;
368  std::string valueMax;
369  const te::fe::Filter* ruleFilter = legVec[i]->getFilter();
370  te::fe::GetFilterStepValues(ruleFilter, valueMin, valueMax);
371 
372  QString rangeStr = valueMin.c_str();
373  std::string colorStr = color.name().toUtf8().data();
374 
376 
377  ip->setData(rangeStr.toDouble());
378  ip->setValue(new te::se::ParameterValue(colorStr));
379 
380  interpolate->add(ip);
381  }
382  }
383 
385  cm->setInterpolate(interpolate);
386 
387  te::se::RasterSymbolizer* rasterSymb = te::se::GetRasterSymbolizer(layer->getStyle());
388  rasterSymb->setColorMap(cm);
389 }
390 
392 {
393  std::string stratifiedAttr = TE_SA_SPG_ATTR_CLASS_NAME;
394  int attrType = te::dt::STRING_TYPE;
395  int prec = 0;
396 
397  //create grouping items
398  std::vector<te::se::Rule*> legend;
399 
400  te::map::GroupingByUniqueValues(stratifiedAttr, strVec, attrType, legend, prec);
401 
402  std::unique_ptr<te::color::ColorBar> cb(GetColorBar("Default", "Classification", "Circular"));
403 
404  int legendSize = static_cast<int>(legend.size());
405 
406  std::vector<te::color::RGBAColor> colorVec;
407 
408  colorVec = cb->getSlices(legendSize);
409 
410  //create symbolizer
411  int geomType = te::map::GetGeomType(layer);
412 
413  for(size_t t = 0; t < colorVec.size(); ++t)
414  {
415  std::vector<te::se::Symbolizer*> symbVec;
416 
417  te::se::Symbolizer* s = te::se::CreateSymbolizer((te::gm::GeomType)geomType, colorVec[t].getColor());
418 
419  symbVec.push_back(s);
420 
421  legend[t]->setSymbolizers(symbVec);
422  }
423 
424  //create grouping
425  te::map::Grouping* group = new te::map::Grouping(stratifiedAttr, te::map::UNIQUE_VALUE);
426  group->setPropertyType(attrType);
427  group->setPrecision(prec);
428  group->setStdDeviation(0.);
429 
430  //fill rules into layer style
431  te::se::Style* style = layer->getStyle();
432 
433  style->removeRules();
434 
435  for (std::size_t t = 0; t < legend.size(); ++t)
436  style->push_back(legend[t]);
437 
438  layer->setGrouping(group);
439 }
440 
442 {
443  std::string skaterAttr = TE_SA_SKATER_ATTR_CLASS_NAME;
444  int attrType = te::dt::INT32_TYPE;
445  int prec = 0;
446 
447  //create grouping items
448  std::vector<te::se::Rule*> legend;
449 
450  std::vector<std::string> strVec;
451 
452  for(int i = 0; i < nClasses; ++i)
453  {
454  strVec.push_back(te::common::Convert2String(i));
455  }
456 
457  te::map::GroupingByUniqueValues(skaterAttr, strVec, attrType, legend, prec);
458 
459  //create symbolizer
460  int geomType = te::map::GetGeomType(layer);
461 
462  for(size_t t = 0; t < legend.size(); ++t)
463  {
464  std::vector<te::se::Symbolizer*> symbVec;
465 
467 
468  symbVec.push_back(s);
469 
470  legend[t]->setSymbolizers(symbVec);
471  }
472 
473  //create grouping
475  group->setPropertyType(attrType);
476  group->setPrecision(prec);
477  group->setStdDeviation(0.);
478 
479 
480  //fill rules into layer style
481  te::se::Style* style = layer->getStyle();
482 
483  style->removeRules();
484 
485  for (std::size_t t = 0; t < legend.size(); ++t)
486  style->push_back(legend[t]);
487 
488  layer->setGrouping(group);
489 }
490 
491 te::color::ColorBar* te::sa::GetColorBar(std::string catalog, std::string group, std::string schema)
492 {
493  te::color::ColorBar* cb = nullptr;
494 
496 
497  if(csc)
498  {
499  std::vector<te::color::ColorSchemeGroup*> csgVec = csc->getColorSchemeGroups();
500 
501  for(std::size_t t = 0; t < csgVec.size(); ++t)
502  {
503  te::color::ColorSchemeGroup* csg = csgVec[t];
504 
505  if(csg->getName() == group)
506  {
507  std::vector<te::color::ColorScheme*> csVec = csg->getColorSchemes();
508 
509  for(std::size_t p = 0; p < csVec.size(); ++p)
510  {
511  te::color::ColorScheme* cs = csVec[p];
512 
513  if(cs->getName() == schema)
514  {
515  std::vector<te::color::RGBAColor>* colors = cs->getColors()[0];
516 
517  std::vector<te::color::RGBAColor>::iterator it = colors->begin();
518 
519  // create color bar
520  cb = new te::color::ColorBar(*(colors->begin()), *(colors->end() - 1), 256);
521 
522  int count = 0;
523 
524  //fill color bar
525  while(it != colors->end())
526  {
527  if(count != 0 && count != static_cast<int>(colors->size()) - 1)
528  {
529  double pos = (1. / (colors->size() - 1)) * count;
530 
531  cb->addColor(*it, pos);
532  }
533 
534  ++count;
535  ++it;
536  }
537  }
538  }
539  }
540  }
541  }
542 
543  return cb;
544 }
void setPrecision(size_t precision)
It sets the precision to be used for the property values.
Definition: Grouping.cpp:104
#define TE_SA_SKATER_ATTR_CLASS_NAME
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
TESAEXPORT void CreateKernelColorMaping(te::map::AbstractLayerPtr layer)
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
TESEEXPORT Symbolizer * CreateSymbolizer(const te::gm::GeomType &geomType)
Try creates an appropriate symbolizer based on given geometry type.
boost::shared_ptr< DataSource > DataSourcePtr
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)
const std::vector< ColorSchemeGroup * > & getColorSchemeGroups() const
It returns the list of color scheme groups in the catalog.
void setData(const double &d)
TESAEXPORT void CreateSampleGeneratorStratifiedGrouping(te::map::AbstractLayerPtr layer, std::vector< std::string > strVec)
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.
TEMAPEXPORT void GroupingByUniqueValues(std::string attrName, std::vector< std::string > &inputValues, int dataType, std::vector< te::se::Rule * > &rules, int precision)
It groups the values using the unique value algorithm.
It models the concept of color scheme.
Definition: ColorScheme.h:58
void setName(std::string *name)
Definition: Rule.cpp:58
TEMAPEXPORT te::gm::GeomType GetGeomType(const te::map::AbstractLayerPtr &layer)
It gets the geometry type of the given layer.
const std::vector< std::vector< RGBAColor > * > & getColors() const
It returns all color lists.
Definition: ColorScheme.cpp:68
void push_back(const std::string &semanticTypeIdentifier)
Definition: Style.cpp:75
TESAEXPORT te::da::DataSourcePtr CreateGDALDataSource(std::string path, std::string dataSetName)
void setStdDeviation(double stdDeviation)
It sets the standard deviation for the Standard Deviation grouping.
Definition: Grouping.cpp:124
static te::dt::Date ds(2010, 01, 01)
void GroupingByEqualSteps(std::string attrName, iterator begin, iterator end, int nSteps, std::vector< te::se::Rule * > &rules, int precision=0)
It groups the values defined by a range of iterators using the equal steps algorithm.
void setFilter(te::fe::Filter *f)
Definition: Rule.cpp:91
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:57
TEFEEXPORT void GetFilterStepValues(const te::fe::Filter *filter, std::string &valueMin, std::string &valueMax)
TEFEEXPORT te::fe::Filter * CreateFilterByStep(const std::string &attrName, const std::string &minValue, const std::string &maxValue)
#define TE_SA_BAYES_ATTR_NAME
void setNumSlices(size_t numSlices)
It sets the number of slices for the EqualSteps and Quantil groupings.
Definition: Grouping.cpp:114
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
TESAEXPORT void CreateBayesGrouping(te::map::AbstractLayerPtr layer)
An abstract class for raster data strucutures.
static te::dt::TimeDuration dt(20, 30, 50, 11)
void removeRules()
Definition: Style.cpp:120
void setPropertyType(const int &type)
It sets the property type whose values will be grouped.
Definition: Grouping.cpp:83
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
Definition: RasterSummary.h:44
te::gm::Polygon * p
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
A filter is any valid predicate expression.
Definition: fe/Filter.h:55
void setSymbolizers(const std::vector< Symbolizer * > &symbs)
Definition: Rule.cpp:152
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
void addColor(const RGBAColor &color, const double &pos)
It adds a color in the color bar.
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:76
This class represents a group of color schemes.
They are used to define a graph of points.
A catalog for color schemes.
TESAEXPORT te::map::AbstractLayerPtr CreateLayer(te::da::DataSourcePtr ds, std::string dataSetName)
void setColorMap(ColorMap *c)
TESAEXPORT te::color::ColorBar * GetColorBar(std::string catalog, std::string group, std::string schema)
It models the concept of color bar.
A class that represents a data source component.
#define TE_SA_SPG_ATTR_CLASS_NAME
TESEEXPORT std::string GenerateRandomColor()
Creates a random RGB color encoded using two hexadecimal digits per primary-color component prefixed ...
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
TESAEXPORT void CreateKernelGrouping(te::map::AbstractLayerPtr layer, std::string kernelAttr)
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56
TESAEXPORT void CreateSkaterGrouping(te::map::AbstractLayerPtr layer, int nClasses)
Calculate the min value.
Utility functions for MapTools module.
Calculate the max value.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void setInterpolate(Interpolate *i)
Definition: ColorMap.cpp:78
A ColorMap defines either the colors of a pallette-type raster source or the mapping of numeric pixel...
Definition: ColorMap.h:61
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
static const std::string sm_nanStr
Not a number string value.