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/qt/plugins/terralib5/Utils.h
22 
23  \brief
24 */
25 
26 // TerraLib
27 #include "../../../../color/RGBAColor.h"
28 #include "../../../../common/StringUtils.h"
29 #include "../../../../geometry/GeometryProperty.h"
30 #include "../../../../maptools/Enums.h"
31 #include "../../../../maptools/Grouping.h"
32 #include "../../../../maptools/GroupingItem.h"
33 #include "../../../../se/Categorize.h"
34 #include "../../../../se/ChannelSelection.h"
35 #include "../../../../se/ColorMap.h"
36 #include "../../../../se/ContrastEnhancement.h"
37 #include "../../../../se/CoverageStyle.h"
38 #include "../../../../se/FeatureTypeStyle.h"
39 #include "../../../../se/Fill.h"
40 #include "../../../../se/LineSymbolizer.h"
41 #include "../../../../se/ParameterValue.h"
42 #include "../../../../se/PolygonSymbolizer.h"
43 #include "../../../../se/PointSymbolizer.h"
44 #include "../../../../se/RasterSymbolizer.h"
45 #include "../../../../se/Rule.h"
46 #include "../../../../se/SelectedChannel.h"
47 #include "../../../../se/Style.h"
48 #include "../../../../se/Stroke.h"
49 #include "../../../../se/Utils.h"
50 #include "../../../../terralib4/Utils.h"
51 #include "Utils.h"
52 
53 // TerraLib 4
54 #include <terralib4/kernel/TeAbstractTheme.h>
55 #include <terralib4/kernel/TeDataTypes.h>
56 #include <terralib4/kernel/TeLegendEntry.h>
57 #include <terralib4/kernel/TeRaster.h>
58 #include <terralib4/kernel/TeRasterTransform.h>
59 #include <terralib4/kernel/TeTheme.h>
60 #include <terralib4/kernel/TeVisual.h>
61 
62 // Boost
63 #include <boost/lexical_cast.hpp>
64 
65 // Qt
66 #include <QtGui/QColor>
67 
69 {
70  te::color::RGBAColor tl5Color(color.red_, color.green_, color.blue_, 0);
71 
72  return tl5Color;
73 }
74 
76 {
77 
78  switch(mode)
79  {
80  case TeEqualSteps:
81  return te::map::EQUAL_STEPS;
82 
83  case TeQuantil:
84  return te::map::QUANTIL;
85 
86  case TeStdDeviation:
88 
89  case TeUniqueValue:
90  return te::map::UNIQUE_VALUE;
91 
92  default:
93  return te::map::EQUAL_STEPS;
94  }
95 }
96 
98 {
99  switch(type)
100  {
101  case TeLnTypeDashed:
102  return"4 2";
103  case TeLnTypeDotted:
104  return "1 2";
105  case TeLnTypeDashDot:
106  return "4 2 1 2";
107  case TeLnTypeDashDotDot:
108  return "4 2 1 2 1 2";
109  case TeLnTypeNone:
110  return "NONE";
111  case TeLnTypeContinuous:
112  default:
113  return "";
114  }
115 }
116 
118 {
119  switch(type)
120  {
121  case TePtTypePlus:
122  return "cross";
123  case TePtTypeStar:
124  return "star";
125  case TePtTypeCircle:
126  case TePtTypeHollowCircle:
127  return "circle";
128  case TePtTypeX:
129  return "x";
130  case TePtTypeBox:
131  case TePtTypeHollowBox:
132  return "square";
133  case TePtTypeDiamond:
134  case TePtTypeHollowDiamond:
135  return "diamond";
136  default:
137  return "";
138  }
139 }
140 
142 {
143  switch(type)
144  {
145  case TePtTypeHollowCircle:
146  return true;
147  case TePtTypeHollowBox:
148  return true;
149  case TePtTypeHollowDiamond:
150  return true;
151  default:
152  return false;
153  }
154 }
155 
156 te::se::Style* te::qt::plugins::terralib4::Convert2TerraLib5(int geometryType, TeTheme* theme, bool isRaster)
157 {
158  te::se::Symbolizer* symb = 0;
159 
160  if(isRaster)
161  {
162  TeRasterVisual* vis = 0;
163  vis = theme->rasterVisual();
164  if(!vis)
165  {
166  TeRaster* raster = theme->layer()->raster();
167  theme->createRasterVisual(raster);
168  vis = theme->rasterVisual();
169  }
170 
171  symb = GetRasterSymbolizer(vis);
172  }
173  else
174  {
175 
176  TeLegendEntry legend = theme->defaultLegend();
177 
178  TeGeomRepVisualMap visualMap = legend.getVisualMap();
179 
180  TeVisual* visual = 0;
181 
182  switch(geometryType)
183  {
184  case te::gm::PolygonType:
185  {
186  visual = visualMap[TePOLYGONS];
187  symb = GetPolygonSymbolizer(visual);
188  break;
189  }
191  {
192  visual = visualMap[TeLINES];
193  symb = GetLineSymbolizer(visual);
194  break;
195  }
196  case te::gm::PointType:
197  {
198  visual = visualMap[TePOINTS];
199  symb = GetPointSymbolizer(visual);
200  break;
201  }
202  default:
203  {
204  break;
205  }
206  }
207  }
208 
209  te::se::Rule* rule = new te::se::Rule;
210 
211  if(symb != 0)
212  rule->push_back(symb);
213 
214  te::se::Style* style = 0;
215 
216  if(isRaster)
217  style = new te::se::CoverageStyle;
218  else
219  style = new te::se::FeatureTypeStyle;
220 
221  style->push_back(rule);
222 
223  return style;
224 }
225 
227 {
228  switch(geometryType)
229  {
230  case te::gm::PolygonType:
231  return GetPolygonSymbolizer(visual);
232 
234  return GetLineSymbolizer(visual);
235 
236  case te::gm::PointType:
237  return GetPointSymbolizer(visual);
238 
239  default:
240  return 0;
241  }
242 }
243 
245 {
246  TeColor tl4Color = visual->color();
247 
248  // Stroke
249  std::string color = Convert2TerraLib5(visual->contourColor()).getColor();
250  std::string width = boost::lexical_cast<std::string>(visual->contourWidth());
251 
252  te::se::Stroke* stroke = te::se::CreateStroke(color, width, "1.0");
253 
254  std::string style = GetLineStyle(visual->contourStyle());
255 
256  if(!style.empty())
257  {
258  if(style == "NOME")
259  stroke = 0;
260  else
261  stroke->setDashArray(style);
262  }
263 
264  // Fill
265  color = Convert2TerraLib5(visual->color()).getColor();
266  double numericOpacity = (100-visual->transparency())/100.0;
267  std::string opacity = boost::lexical_cast<std::string>(numericOpacity);
268 
269  te::se::Fill* fill = te::se::CreateFill(color, opacity);
270 
271  return te::se::CreatePolygonSymbolizer(stroke, fill);
272 }
273 
275 {
276  std::string color = Convert2TerraLib5(visual->color()).getColor();
277  std::string width = boost::lexical_cast<std::string>(visual->width());
278 
279  te::se::Stroke* stroke = te::se::CreateStroke(color, width);
280 
281  std::string style = GetLineStyle(visual->style());
282 
283  if(!style.empty())
284  {
285  if(style == "NOME")
286  stroke = 0;
287  else
288  stroke->setDashArray(style);
289  }
290 
291  return te::se::CreateLineSymbolizer(stroke);
292 }
293 
295 {
296  std::string color = Convert2TerraLib5(visual->color()).getColor();
297 
298  te::se::Fill* fill = te::se::CreateFill(color, "1.0");
299 
300  std::string style = GetPointStyle(visual->style());
301 
302  if(IsHollow(visual->style()))
303  fill = 0;
304 
305  te::se::Mark* mark = te::se::CreateMark(style, 0, fill);
306 
307  std::string size = boost::lexical_cast<std::string>(visual->size());
308 
309  te::se::Graphic* gr = te::se::CreateGraphic(mark, size, "0", "1.0");
310 
312 }
313 
315 {
316  TeLegendEntryVector leg = theme->legend();
317  TeGrouping group = theme->grouping();
318 
319  TeAttributeRep attr = group.groupAttribute_;
320  std::string propertyName = attr.name_;
321  std::vector<std::string> tokens;
322  te::common::Tokenize(propertyName, tokens, ".");
323  propertyName = tokens[1];
324 
325  TeGroupingMode mode = group.groupMode_;
326  int tl5Type = ::terralib4::Convert2T5(attr.type_);
327  std::size_t precision = static_cast<std::size_t>(group.groupPrecision_);
328  int slices = group.groupNumSlices_;
329  double stdDeviation = group.groupStdDev_;
330 
331  te::map::Grouping* grouping = new te::map::Grouping(propertyName, GetGroupingType(mode), precision);
332  grouping->setPropertyType(tl5Type);
333  grouping->setStdDeviation(stdDeviation);
334 
335  std::vector<te::map::GroupingItem*> items;
336 
337  for(int i = 0; i < slices; ++i)
338  {
340 
341  TeLegendEntry le = leg[i];
342 
343  std::string title = le.label();
344 
345  item->setTitle(title);
346 
347  std::string fromValue = le.from();
348  std::string toValue = le.to();
349 
350  if(mode == TeUniqueValue)
351  {
352  item->setValue(fromValue);
353  }
354  else
355  {
356  item->setLowerLimit(fromValue);
357  item->setUpperLimit(toValue);
358  }
359 
360  std::vector<te::se::Symbolizer*> symbs;
361 
362  TeGeomRepVisualMap map = le.getVisualMap();
363 
364  TeGeomRep geomRep = map.begin()->first;
365  TeVisual* visual = map.begin()->second;
366 
367  symbs.push_back(GetSymbolizer(::terralib4::Convert2T5GeomType(geomRep), visual));
368 
369  item->setSymbolizers(symbs);
370 
371  items.push_back(item);
372  }
373 
374  grouping->setGroupingItems(items);
375 
376  return grouping;
377 }
378 
380 {
381  TeLegendEntryVector leg = theme->legend();
382  TeGrouping group = theme->grouping();
383 
384  int slices = group.groupNumSlices_;
385 
387 
388  c->setFallbackValue("#000000");
389  c->setLookupValue(new te::se::ParameterValue("Rasterdata"));
390 
391  QColor cWhite(Qt::white);
392  std::string colorWhiteStr = cWhite.name().toLatin1().data();
393 
394  //added dummy color for values < than min values...
395  c->addValue(new te::se::ParameterValue(colorWhiteStr));
396 
397  for(int i = 0; i < slices; ++i)
398  {
399  TeLegendEntry le = leg[i];
400 
401  std::string title = le.label();
402 
403  TeGeomRepVisualMap map = le.getVisualMap();
404 
405  TeGeomRep geomRep = map.begin()->first;
406  TeVisual* visual = map.begin()->second;
407 
408  QColor color(visual->color().red_, visual->color().green_, visual->color().blue_, 0);
409 
410  std::string rangeStr = le.from();
411  std::string colorStr = color.name().toStdString();
412 
413  c->addThreshold(new te::se::ParameterValue(rangeStr));
414  c->addValue(new te::se::ParameterValue(colorStr));
415 
416  if(i == slices - 1)
417  {
418  rangeStr = le.to();
419  c->addThreshold(new te::se::ParameterValue(rangeStr));
420  }
421  }
422 
423  //added dummy color for values > than max values...
424  c->addValue(new te::se::ParameterValue(colorWhiteStr));
425 
427 
429  cm->setCategorize(c);
430 
431  return cm;
432 }
433 
435 {
437 
438  double opacity = ((visual->getTransparency()*100.0)/255)/100.0;
439  double offset = visual->getOffset();
440  double gain = visual->getGain();
441 
442  symb->setOpacity(new te::se::ParameterValue(boost::lexical_cast<std::string>(opacity)));
443  symb->setOffset(new te::se::ParameterValue(boost::lexical_cast<std::string>(offset)));
444  symb->setGain(new te::se::ParameterValue(boost::lexical_cast<std::string>(gain)));
445 
446  TeRasterTransform::TeRasterTransfFunctions func = visual->getTransfFunction();
447 
449 
450  te::se::SelectedChannel* scRed = 0;
451  te::se::SelectedChannel* scGreen = 0;
452  te::se::SelectedChannel* scBlue = 0;
453  te::se::SelectedChannel* scMono = 0;
454 
455  if (func == TeRasterTransform::TeBand2Band)
456  {
457  scRed = new te::se::SelectedChannel;
458  scRed->setSourceChannelName("0");
459 
460  scGreen = new te::se::SelectedChannel;
461  scGreen->setSourceChannelName("1");
462 
463  scBlue = new te::se::SelectedChannel;
464  scBlue->setSourceChannelName("2");
465 
467 
468  }
469  else if (func == TeRasterTransform::TeExtractBand)
470  {
471 
472  short db = visual->getDestBand();
473 
474  if (db == TeRasterTransform::TeREDCHANNEL)
475  {
476 
477  scRed = new te::se::SelectedChannel;
478  scRed->setSourceChannelName(boost::lexical_cast<std::string>(visual->getSrcBand()));
479 
481 
482  }
483  else if (db == TeRasterTransform::TeGREENCHANNEL)
484  {
485 
486  scGreen = new te::se::SelectedChannel;
487  scGreen->setSourceChannelName(boost::lexical_cast<std::string>(visual->getSrcBand()));
488 
490 
491  }
492  else if (db == TeRasterTransform::TeBLUECHANNEL)
493  {
494 
495  scBlue = new te::se::SelectedChannel;
496  scBlue->setSourceChannelName(boost::lexical_cast<std::string>(visual->getSrcBand()));
497 
499 
500  }
501  }
502  else if (func == TeRasterTransform::TeMono2Three)
503  {
504 
505  scMono = new te::se::SelectedChannel;
506  scMono->setSourceChannelName(boost::lexical_cast<std::string>(visual->getSrcBand()));
507 
509 
510  }
511  else if (func == TeRasterTransform::TeExtractRGB)
512  {
513  std::map<TeRasterTransform::TeRGBChannels,short>& RGBmap = visual->getRGBMap();
514 
515  scRed = new te::se::SelectedChannel;
516  scRed->setSourceChannelName(boost::lexical_cast<std::string>(RGBmap[TeRasterTransform::TeREDCHANNEL]));
517 
518  scGreen = new te::se::SelectedChannel;
519  scGreen->setSourceChannelName(boost::lexical_cast<std::string>(RGBmap[TeRasterTransform::TeGREENCHANNEL]));
520 
521  scBlue = new te::se::SelectedChannel;
522  scBlue->setSourceChannelName(boost::lexical_cast<std::string>(RGBmap[TeRasterTransform::TeBLUECHANNEL]));
523 
525 
526  }
527  else if (func == TeRasterTransform::TeThreeBand2RGB)
528  {
529  scRed = new te::se::SelectedChannel;
530  scRed->setSourceChannelName("0");
531 
532  scGreen = new te::se::SelectedChannel;
533  scGreen->setSourceChannelName("1");
534 
535  scBlue = new te::se::SelectedChannel;
536  scBlue->setSourceChannelName("2");
537 
539  }
540 
541  else if (func == TeRasterTransform::TeExtractBands)
542  {
543 
544  std::map<TeRasterTransform::TeRGBChannels,short>& RGBmap = visual->getRGBMap();
545 
546  if (RGBmap[TeRasterTransform::TeREDCHANNEL] != -1)
547  scRed->setSourceChannelName(boost::lexical_cast<std::string>(RGBmap[TeRasterTransform::TeREDCHANNEL]));
548  if (RGBmap[TeRasterTransform::TeGREENCHANNEL] != -1)
549  scGreen->setSourceChannelName(boost::lexical_cast<std::string>(RGBmap[TeRasterTransform::TeGREENCHANNEL]));
550  if (RGBmap[TeRasterTransform::TeBLUECHANNEL] != -1)
551  scBlue->setSourceChannelName(boost::lexical_cast<std::string>(RGBmap[TeRasterTransform::TeBLUECHANNEL]));
552 
554  }
555 
556  if(scRed)
557  cs->setRedChannel(scRed);
558  if(scGreen)
559  cs->setGreenChannel(scGreen);
560  if(scBlue)
561  cs->setBlueChannel(scBlue);
562  if(scMono)
563  cs->setGrayChannel(scMono);
564 
565  symb->setChannelSelection(cs);
566 
567  return symb;
568 }
void setValue(const std::string &value)
It sets value of the legend item.
te::se::Symbolizer * GetSymbolizer(int geometryType, TeVisual *visual)
Definition: Utils.cpp:226
void setRedChannel(SelectedChannel *c)
A selected channel to be display.
te::color::RGBAColor Convert2TerraLib5(TeColor color)
Definition: Utils.cpp:68
The transformation of continuous values to distinct values (Categorize function). ...
Definition: Categorize.h:90
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
A Mark specifies a geometric shape and applies coloring to it.
Definition: Mark.h:84
te::map::Grouping * GetGrouping(TeTheme *theme)
Definition: Utils.cpp:314
A PolygonSymbolizer is used to draw a polygon (or other area-type geometries), including filling its ...
void setGroupingItems(std::vector< te::map::GroupingItem * > &items)
It sets the vector of grouping items.
Definition: Grouping.cpp:135
te::se::PointSymbolizer * GetPointSymbolizer(TeVisual *visual)
Definition: Utils.cpp:294
A Symbolizer describes how a feature is to appear on a map.
Definition: Symbolizer.h:80
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
Definition: CoverageStyle.h:45
ParameterValue * getOffset() const
void setGain(ParameterValue *p)
void setLookupValue(ParameterValue *v)
Definition: Categorize.cpp:81
A PointSymbolizer specifies the rendering of a graphic Symbolizer at a point.
void setCategorize(Categorize *c)
Definition: ColorMap.cpp:58
void push_back(const std::string &semanticTypeIdentifier)
Definition: Style.cpp:75
void setGrayChannel(SelectedChannel *c)
void setStdDeviation(double stdDeviation)
It sets the standard deviation for the Standard Deviation grouping.
Definition: Grouping.cpp:125
TESEEXPORT LineSymbolizer * CreateLineSymbolizer(Stroke *stroke)
Creates a line symbolizer.
Definition: Utils.cpp:172
void setTitle(const std::string &title)
It sets the title of the legend item.
A Graphic is a graphic symbol with an inherent shape, color(s), and possibly size.
Definition: Graphic.h:66
TESEEXPORT PolygonSymbolizer * CreatePolygonSymbolizer(Stroke *stroke, Fill *fill)
Creates a polygon symbolizer.
Definition: Utils.cpp:159
void push_back(Symbolizer *s)
Definition: Rule.cpp:138
The "ParameterValueType" uses WFS-Filter expressions to give values for SE graphic parameters...
std::auto_ptr< te::dt::Property > Convert2T5(const TeAttributeRep &attRep)
It creates a valid TerraLib 5 property given a valid TerraLib 4.x attribute representation.
Definition: Utils.cpp:53
This class contains the parameters needed for grouping the values of a Property.
Definition: Grouping.h:59
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:216
te::gm::GeomType Convert2T5GeomType(TeAttrDataType type)
Definition: Utils.cpp:197
A GroupingItem contains information about a grouping item associated to a layer.
Definition: GroupingItem.h:48
The FeatureTypeStyle defines the styling that is to be applied to a dataset that can be viewed as a f...
void setColorCompositionType(ColorCompositionType cct)
void setChannelSelection(ChannelSelection *c)
te::se::PolygonSymbolizer * GetPolygonSymbolizer(TeVisual *visual)
Definition: Utils.cpp:244
void setPropertyType(const int &type)
It sets the property type whose values will be grouped.
Definition: Grouping.cpp:84
TESEEXPORT Graphic * CreateGraphic(Mark *mark, const std::string &size, const std::string &rotation, const std::string &opacity)
Creates a graphic.
Definition: Utils.cpp:142
The RasterSymbolizer describes how to render raster/matrix-coverage data (e.g., satellite photos...
A Fill specifies the pattern for filling an area geometry.
Definition: Fill.h:59
void setOpacity(ParameterValue *p)
TESEEXPORT Mark * CreateMark(const std::string &wellKnownName, Stroke *stroke, Fill *fill)
Creates a mark.
Definition: Utils.cpp:130
TESEEXPORT Stroke * CreateStroke(const std::string &color, const std::string &width)
Creates a stroke.
Definition: Utils.cpp:54
te::map::GroupingType GetGroupingType(TeGroupingMode mode)
Definition: Utils.cpp:75
te::se::RasterSymbolizer * GetRasterSymbolizer(TeRasterTransform *visual)
Definition: Utils.cpp:434
GroupingType
The grouping type associated to the layer.
Definition: Enums.h:150
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:78
void setOffset(ParameterValue *p)
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
te::se::LineSymbolizer * GetLineSymbolizer(TeVisual *visual)
Definition: Utils.cpp:274
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
void setUpperLimit(const std::string &to)
It sets the upper limit value of the legend item.
void setSourceChannelName(const std::string &name)
std::string GetLineStyle(int type)
Definition: Utils.cpp:97
A LineSymbolizer is used to style a stroke along a linear geometry type, such as a string of line seg...
te::se::ColorMap * GetRasterGrouping(TeTheme *theme)
Definition: Utils.cpp:379
void setBlueChannel(SelectedChannel *c)
std::string GetPointStyle(int type)
Definition: Utils.cpp:117
void setThresholdsBelongTo(ThresholdsBelongToType t)
Definition: Categorize.cpp:104
void addThreshold(ParameterValue *v)
Definition: Categorize.cpp:94
A ColorMap defines either the colors of a pallette-type raster source or the mapping of numeric pixel...
Definition: ColorMap.h:60
void addValue(ParameterValue *v)
Definition: Categorize.cpp:99
void setFallbackValue(const std::string &v)
Definition: Function.cpp:37
bool IsHollow(int type)
Definition: Utils.cpp:141
TESEEXPORT PointSymbolizer * CreatePointSymbolizer(Graphic *graphic)
Creates a point symbolizer.
Definition: Utils.cpp:182
TESEEXPORT Fill * CreateFill(const std::string &color, const std::string &opacity)
Creates a fill.
Definition: Utils.cpp:109
void setGreenChannel(SelectedChannel *c)
void setSymbolizers(const std::vector< te::se::Symbolizer * > &symbolizers)
It sets the symbolizers of the legend item.
ChannelSelection specifies the false-color channel selection for a multi-spectral raster source (such...
void setLowerLimit(const std::string &from)
It sets the lower limit value of the legend item.