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/se/Utils.cpp
22 
23  \brief Utility functions for Symbology Enconding module.
24 */
25 
26 // TerraLib
27 #include "../color/ColorTransform.h"
28 #include "../color/RGBAColor.h"
29 #include "../fe/Literal.h"
30 #include "../raster/BandProperty.h"
31 #include "ChannelSelection.h"
32 #include "CoverageStyle.h"
33 #include "Description.h"
34 #include "FeatureTypeStyle.h"
35 #include "Fill.h"
36 #include "Graphic.h"
37 #include "LineSymbolizer.h"
38 #include "Mark.h"
39 #include "ParameterValue.h"
40 #include "PointSymbolizer.h"
41 #include "PolygonSymbolizer.h"
42 #include "RasterSymbolizer.h"
43 #include "Rule.h"
44 #include "SelectedChannel.h"
45 #include "Stroke.h"
46 #include "SvgParameter.h"
47 #include "TextSymbolizer.h"
48 #include "Utils.h"
49 
50 // STL
51 #include <cassert>
52 #include <cstdlib>
53 
54 te::se::Stroke* te::se::CreateStroke(const std::string& color, const std::string& width)
55 {
56  return te::se::CreateStroke(color, width, "");
57 }
58 
59 te::se::Stroke* te::se::CreateStroke(const std::string& color, const std::string& width, const std::string& opacity)
60 {
61  return te::se::CreateStroke(color, width, opacity, "");
62 }
63 
64 te::se::Stroke* te::se::CreateStroke(const std::string& color, const std::string& width,
65  const std::string& opacity, const std::string& dasharray)
66 {
67  return te::se::CreateStroke(color, width, opacity, dasharray, "", "");
68 }
69 
70 te::se::Stroke* te::se::CreateStroke(const std::string& color, const std::string& width,
71  const std::string& opacity, const std::string& dasharray,
72  const std::string& linecap, const std::string& linejoin)
73 {
74  te::se::Stroke* stroke = te::se::CreateStroke(0, width, opacity, dasharray, linecap, linejoin);
75 
76  if(!color.empty())
77  stroke->setColor(color);
78 
79  return stroke;
80 }
81 
83  const std::string& width, const std::string& opacity,
84  const std::string& dasharray, const std::string& linecap, const std::string& linejoin)
85 {
86  te::se::Stroke* stroke = new te::se::Stroke;
87 
88  if(graphicFill)
89  stroke->setGraphicFill(graphicFill);
90 
91  if(!width.empty())
92  stroke->setWidth(width);
93 
94  if(!opacity.empty())
95  stroke->setOpacity(opacity);
96 
97  if(!dasharray.empty())
98  stroke->setDashArray(dasharray);
99 
100  if(!linecap.empty())
101  stroke->setLineCap(linecap);
102 
103  if(!linejoin.empty())
104  stroke->setLineJoin(linecap);
105 
106  return stroke;
107 }
108 
109 te::se::Fill* te::se::CreateFill(const std::string& color, const std::string& opacity)
110 {
111  te::se::Fill* fill = new te::se::Fill;
112 
113  if(!color.empty())
114  fill->setColor(color);
115 
116  if(!opacity.empty())
117  fill->setOpacity(opacity);
118 
119  return fill;
120 }
121 
123 {
124  te::se::Fill* fill = new te::se::Fill;
125  fill->setGraphicFill(graphicFill);
126 
127  return fill;
128 }
129 
130 te::se::Mark* te::se::CreateMark(const std::string& wellKnownName, te::se::Stroke* stroke, te::se::Fill* fill)
131 {
132  std::string* name = new std::string(wellKnownName);
133 
134  te::se::Mark* mark = new te::se::Mark;
135  mark->setWellKnownName(name);
136  mark->setStroke(stroke);
137  mark->setFill(fill);
138 
139  return mark;
140 }
141 
142 te::se::Graphic* te::se::CreateGraphic(te::se::Mark* mark, const std::string& size, const std::string& rotation, const std::string& opacity)
143 {
144  te::se::Graphic* graphic = new te::se::Graphic;
145  graphic->add(mark);
146 
147  if(!size.empty())
148  graphic->setSize(new te::se::ParameterValue(size));
149 
150  if(!rotation.empty())
151  graphic->setRotation(new te::se::ParameterValue(rotation));
152 
153  if(!opacity.empty())
154  graphic->setOpacity(new te::se::ParameterValue(opacity));
155 
156  return graphic;
157 }
158 
160 {
162 
163  if(fill)
164  symbolizer->setFill(fill);
165 
166  if(stroke)
167  symbolizer->setStroke(stroke);
168 
169  return symbolizer;
170 }
171 
173 {
175 
176  if(stroke)
177  symbolizer->setStroke(stroke);
178 
179  return symbolizer;
180 }
181 
183 {
185  symbolizer->setGraphic(graphic);
186 
187  return symbolizer;
188 }
189 
190 te::se::Font* te::se::CreateFont(const std::string& family, const std::string& size, const te::se::Font::FontStyleType& style, const te::se::Font::FontWeightType& weight)
191 {
192  te::se::Font* font = new te::se::Font;
193  font->setFamily(family);
194  font->setSize(size);
195  font->setStyle(style);
196  font->setWeight(weight);
197 
198  return font;
199 }
200 
202 {
204  symbolizer->setLabel(new te::se::ParameterValue(label));
205  symbolizer->setFill(fill);
206  symbolizer->setFont(font);
207 
208  return symbolizer;
209 }
210 
211 te::se::Description* te::se::CreateDescription(const std::string& title, const std::string& abst)
212 {
213  te::se::Description* description = new te::se::Description;
214  description->setTitle(title);
215  description->setAbstract(abst);
216 
217  return description;
218 }
219 
221 {
222  std::string color = GenerateRandomColor();
223 
224  return CreateSymbolizer(geomType, color);
225 }
226 
227 te::se::Symbolizer* te::se::CreateSymbolizer(const te::gm::GeomType& geomType, const std::string& color)
228 {
229  switch(geomType)
230  {
231  case te::gm::PolygonType:
239  {
240  te::se::Fill* fill = CreateFill(color, "1.0");
241  te::se::Stroke* stroke = CreateStroke("#000000", "1");
243  symbolizer->setFill(fill);
244  symbolizer->setStroke(stroke);
245  return symbolizer;
246  }
247 
256  {
257  te::se::Stroke* stroke = CreateStroke(color, "1");
259  symbolizer->setStroke(stroke);
260  return symbolizer;
261  }
262 
263  case te::gm::PointType:
264  case te::gm::PointMType:
265  case te::gm::PointZType:
266  case te::gm::PointZMType:
271  {
272  te::se::Fill* markFill = CreateFill(color, "1.0");
273  te::se::Stroke* markStroke = CreateStroke("#000000", "1");
274  te::se::Mark* mark = CreateMark("circle", markStroke, markFill);
275  te::se::Graphic* graphic = CreateGraphic(mark, "12", "", "");
276  return CreatePointSymbolizer(graphic);
277  }
278 
279  default:
280  return 0;
281  }
282 }
283 
285 {
286  te::se::Symbolizer* symbolizer = CreateSymbolizer(geomType);
287 
288  te::se::Rule* rule = new te::se::Rule;
289 
290  if(symbolizer != 0)
291  rule->push_back(symbolizer);
292 
294  style->push_back(rule);
295 
296  return style;
297 }
298 
299 te::se::Style* te::se::CreateCoverageStyle(const std::vector<te::rst::BandProperty*>& properties)
300 {
301  // TODO: Review this method in order to extract the maximum information about the given band properties.
302  return CreateCoverageStyle(properties.size());
303 }
304 
305 te::se::Style* te::se::CreateCoverageStyle(const std::size_t& nBands)
306 {
307  if(nBands == 0)
308  return 0;
309 
310  // Default raster symbolizer
311  te::se::RasterSymbolizer* rasterSymbolizer = CreateRasterSymbolizer(nBands);
312 
313  te::se::Rule* rule = new te::se::Rule();
314  rule->push_back(rasterSymbolizer);
315 
317  style->push_back(rule);
318 
319  return style;
320 }
321 
322 
324 {
325  // Default raster symbolizer
326  te::se::RasterSymbolizer* rasterSymbolizer = new te::se::RasterSymbolizer;
327 
328  // General parameters
329  rasterSymbolizer->setOpacity(new te::se::ParameterValue("1.0"));
330  rasterSymbolizer->setGain(new te::se::ParameterValue("0.0"));
331  rasterSymbolizer->setOffset(new te::se::ParameterValue("0.0"));
332 
333  // Channel selection
334  if((nBands == 1) || (nBands == 2))
335  {
337  sc->setSourceChannelName(std::string("0"));
338 
341  cs->setGrayChannel(sc);
342 
343  rasterSymbolizer->setChannelSelection(cs);
344  }
345  else if(nBands >= 3)
346  {
347  // Red Channel
349  scr->setSourceChannelName(std::string("0"));
350 
351  // Green Channel
353  scg->setSourceChannelName(std::string("1"));
354 
355  // Blue channel
357  scb->setSourceChannelName(std::string("2"));
358 
361  cs->setRedChannel(scr);
362  cs->setGreenChannel(scg);
363  cs->setBlueChannel(scb);
364 
365  rasterSymbolizer->setChannelSelection(cs);
366  }
367 
368  return rasterSymbolizer;
369 }
370 
372 {
373  assert(s);
374 
375  std::size_t nRules = s->getRules().size();
376 
377  if(nRules <= 0)
378  return 0;
379 
380  // for while, consider one rule
381  const te::se::Rule* r = s->getRule(0);
382 
383  const std::vector<te::se::Symbolizer*>& symbolizers = r->getSymbolizers();
384 
385  if(symbolizers.empty())
386  return 0;
387 
388  // for while, consider one raster symbolizer
389  te::se::RasterSymbolizer* rasterSymbolizer = dynamic_cast<te::se::RasterSymbolizer*>(symbolizers[0]);
390 
391  return rasterSymbolizer;
392 }
393 
395 {
397  t.setHsv(rand() % 360, 64 + (rand() % 192), 128 + (rand() % 128));
398 
399  te::color::RGBAColor color(t.getRgba());
400 
401  return color.getColor();
402 }
403 
405 {
406  if(stroke == 0)
407  return;
408 
409  te::se::GetColor(stroke->getColor(), stroke->getOpacity(), color);
410 }
411 
413 {
414  if(fill == 0)
415  return;
416  te::se::GetColor(fill->getColor(), fill->getOpacity(), color);
417 }
418 
420 {
421  if(color == 0 && opacity == 0)
422  return;
423 
424  int alpha = TE_OPAQUE;
425  if(opacity)
426  {
427  alpha = (int)(te::se::GetDouble(opacity) * TE_OPAQUE);
428  rgba.setColor(rgba.getRed(), rgba.getGreen(), rgba.getBlue(), alpha);
429  }
430 
431  if(color)
432  {
434  rgba.setColor(rgb.getRed(), rgb.getGreen(), rgb.getBlue(), rgba.getAlpha());
435  }
436 }
437 
439 {
441 }
442 
444 {
445  return atoi(te::se::GetString(param).c_str());
446 }
447 
449 {
450  return atof(te::se::GetString(param).c_str());
451 }
452 
453 std::string te::se::GetString(const te::se::ParameterValue* param)
454 {
455  assert(param->getNParameters() > 0);
456 
457  const te::se::ParameterValue::Parameter* p = param->getParameter(0);
458  assert(p);
459 
460  if(p->m_mixedData)
461  {
462  return *p->m_mixedData;
463  }
464  else //if(p->m_expression)
465  {
466  te::fe::Literal* l = dynamic_cast<te::fe::Literal*>(p->m_expression);
467  assert(l);
468  return l->getValue();
469  }
470 }
A TextSymbolizer is used to render text labels according to various graphical parameters.
void setRotation(ParameterValue *value)
The Rotation element gives the rotation of a graphic in the clockwise direction about its center poin...
Definition: Graphic.cpp:125
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
void setRedChannel(SelectedChannel *c)
A selected channel to be display.
void setGraphicFill(Graphic *fill)
Sets the GraphicFill element to this Stroke. GraphicFill defines that the pixels of the line will be ...
Definition: Stroke.cpp:57
A SvgParameter refers to an SVG/CSS graphical-formatting parameter.
TESEEXPORT void GetColor(const te::se::Stroke *stroke, te::color::RGBAColor &color)
It gets the RGBA color from the Stroke element.
Definition: Utils.cpp:404
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
A Description gives human-readable descriptive information for the object it is included within...
Definition: Description.h:56
ChannelSelection specifies the false-color channel selection for a multi-spectral raster source (such...
void setWidth(const std::string &width)
Definition: Stroke.cpp:98
void setColor(const std::string &hexColor)
It sets the color using a two hexadecimal RGB-encoded color.
Definition: RGBAColor.h:329
void setOpacity(ParameterValue *value)
The Opacity element gives the opacity to use for rendering the graphic. It has the same semantics as ...
Definition: Graphic.cpp:103
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
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:295
void setAbstract(const std::string &a)
Definition: Description.cpp:47
A PolygonSymbolizer is used to draw a polygon (or other area-type geometries), including filling its ...
TESEEXPORT Symbolizer * CreateSymbolizer(const te::gm::GeomType &geomType)
Try creates an appropriate symbolizer based on given geometry type.
Definition: Utils.cpp:220
TESEEXPORT RasterSymbolizer * CreateRasterSymbolizer(const std::size_t &nBands)
Try creates an appropriate raster symbolizer style based on given number of bands.
Definition: Utils.cpp:323
A Fill specifies the pattern for filling an area geometry.
A Symbolizer describes how a feature is to appear on a map.
Definition: Symbolizer.h:80
TESEEXPORT RasterSymbolizer * GetRasterSymbolizer(Style *s)
Try to get raster symbolizer from a style.
Definition: Utils.cpp:371
void setSize(ParameterValue *value)
The Size element gives the absolute size of the graphic in uoms encoded as a floating-point number...
Definition: Graphic.cpp:114
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
Definition: CoverageStyle.h:45
void setColor(const std::string &color)
Definition: Fill.cpp:66
TESEEXPORT Style * CreateCoverageStyle(const std::vector< te::rst::BandProperty * > &properties)
Try creates an appropriate coverage style based on given band properties.
Definition: Utils.cpp:299
void setGain(ParameterValue *p)
The ParameterValueType uses WFS-Filter expressions to give values for SE graphic parameters.
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:305
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:300
A PointSymbolizer specifies the rendering of a graphic Symbolizer at a point.
void setSize(const std::string &size)
Definition: Font.cpp:80
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
void setFill(Fill *f)
Definition: Mark.cpp:108
void push_back(const std::string &semanticTypeIdentifier)
Definition: Style.cpp:75
TESEEXPORT double GetDouble(const te::se::ParameterValue *param)
It gets the parameter value as a double.
Definition: Utils.cpp:448
void setGrayChannel(SelectedChannel *c)
TESEEXPORT LineSymbolizer * CreateLineSymbolizer(Stroke *stroke)
Creates a line symbolizer.
Definition: Utils.cpp:172
TESEEXPORT TextSymbolizer * CreateTextSymbolizer(const std::string &label, Fill *fill, Font *font)
Creates a text symbolizer.
Definition: Utils.cpp:201
void getRgba(int *r, int *g, int *b, int *a=0) const
It gets the color value.
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
void setDashArray(const std::string &dasharray)
Definition: Stroke.cpp:113
The "ParameterValueType" uses WFS-Filter expressions to give values for SE graphic parameters...
void setColor(const std::string &color)
Definition: Stroke.cpp:88
void add(ExternalGraphic *g)
Graphics can either be referenced from an external URL in a common format (such as GIF or SVG)...
Definition: Graphic.cpp:59
void setHsv(int h, int s, int v, int a=255)
It sets the h, s, v components of this color.
TESEEXPORT Style * CreateFeatureTypeStyle(const te::gm::GeomType &geomType)
Try creates an appropriate feature type style based on given geometry type.
Definition: Utils.cpp:284
const Parameter * getParameter(size_t i) const
A Description gives human-readable descriptive information for the object it is included within...
Rule * getRule(std::size_t i) const
Definition: Style.cpp:105
const std::vector< Rule * > & getRules() const
Definition: Style.cpp:94
A LineSymbolizer is used to style a stroke along a linear geometry type, such as a string of line seg...
void setOpacity(const std::string &opacity)
Definition: Stroke.cpp:93
const SvgParameter * getOpacity() const
Definition: Stroke.cpp:128
The FeatureTypeStyle defines the styling that is to be applied to a dataset that can be viewed as a f...
const SvgParameter * getColor() const
Definition: Stroke.cpp:123
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
void setColorCompositionType(ColorCompositionType cct)
void setChannelSelection(ChannelSelection *c)
A Font specifies the text font to use in a text symbolizer.
Definition: Font.h:63
void setStroke(Stroke *stroke)
A Stroke specifies the appearance of a linear geometry. The Stroke element is optional inside of Line...
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
void setTitle(const std::string &title)
Definition: Description.cpp:37
void setLineCap(const std::string &cap)
Definition: Stroke.cpp:108
TESEEXPORT Graphic * CreateGraphic(Mark *mark, const std::string &size, const std::string &rotation, const std::string &opacity)
Creates a graphic.
Definition: Utils.cpp:142
A PolygonSymbolizer is used to draw a polygon (or other area-type geometries), including filling its ...
size_t getNParameters() const
void setOpacity(const std::string &opacity)
Definition: Fill.cpp:71
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)
FontStyleType
It defines the style to use for a font.
Definition: Font.h:72
void setGraphicFill(Graphic *g)
Sets the GraphicFill element to this Fill. GraphicFill defines that the pixels of the area will be dr...
Definition: Fill.cpp:46
TESEEXPORT Mark * CreateMark(const std::string &wellKnownName, Stroke *stroke, Fill *fill)
Creates a mark.
Definition: Utils.cpp:130
A TextSymbolizer is used to render text labels according to various graphical parameters.
The RasterSymbolizer describes how to render raster/matrix-coverage data (e.g., satellite photos...
const std::vector< Symbolizer * > & getSymbolizers() const
Definition: Rule.cpp:158
TESEEXPORT Stroke * CreateStroke(const std::string &color, const std::string &width)
Creates a stroke.
Definition: Utils.cpp:54
void setWeight(const FontWeightType &weight)
Definition: Font.cpp:75
void setLabel(ParameterValue *l)
A Mark specifies a geometric shape and applies coloring to it.
void setLineJoin(const std::string &join)
Definition: Stroke.cpp:103
This class can be used to represent literal values.
Definition: Literal.h:56
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
void setFamily(const std::string &family)
Definition: Font.cpp:65
A Graphic is a graphic symbol with an inherent shape, color(s), and possibly size.
std::string getColor() const
It gets the color encoded using two hexadecimal digits per primary-color component, in the order Red, Green, Blue, prefixed with a hash (#) sign.
Definition: RGBAColor.h:349
void setStroke(Stroke *stroke)
A Stroke specifies the appearance of a linear geometry.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
TESEEXPORT int GetInt(const te::se::ParameterValue *param)
It gets the parameter value as an integer.
Definition: Utils.cpp:443
void setSourceChannelName(const std::string &name)
TESEEXPORT Font * CreateFont(const std::string &family, const std::string &size, const te::se::Font::FontStyleType &style=te::se::Font::StyleNormal, const te::se::Font::FontWeightType &weight=te::se::Font::WeightNormal)
Creates a font.
Definition: Utils.cpp:190
void setStroke(Stroke *s)
Definition: Mark.cpp:119
The FeatureTypeStyle defines the styling that is to be applied to a dataset that can be viewed as a f...
A LineSymbolizer is used to style a stroke along a linear geometry type, such as a string of line seg...
const SvgParameter * getColor() const
Definition: Fill.cpp:76
TESEEXPORT std::string GenerateRandomColor()
Creates a random RGB color encoded using two hexadecimal digits per primary-color component prefixed ...
Definition: Utils.cpp:394
TESEEXPORT Description * CreateDescription(const std::string &title, const std::string &abst)
Creates a description.
Definition: Utils.cpp:211
A PointSymbolizer specifies the rendering of a graphic Symbolizer at a point.
const std::string & getValue() const
It returns the literal value.
Definition: Literal.cpp:38
A class to transform RGBA color to HSV, HSL and CMYK.
void setFill(Fill *f)
A Fill specifies the pattern for filling an area geometry.
void setStyle(const FontStyleType &style)
Definition: Font.cpp:70
A selected channel to be display.
FontWeightType
It gives the amount of weight or boldness to use for a font.
Definition: Font.h:84
void setWellKnownName(std::string *name)
The WellKnownName element gives the well-known name of the shape of the mark.
Definition: Mark.cpp:54
void setBlueChannel(SelectedChannel *c)
void setGraphic(Graphic *graphic)
Utility functions for Symbology Enconding module.
TESEEXPORT std::string GetString(const te::se::ParameterValue *param)
It gets the parameter value as a string.
Definition: Utils.cpp:453
A Stroke specifies the appearance of a linear geometry.
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)
const SvgParameter * getOpacity() const
Definition: Fill.cpp:81
ChannelSelection specifies the false-color channel selection for a multi-spectral raster source (such...