All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Utils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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 "../raster/BandProperty.h"
30 #include "ChannelSelection.h"
31 #include "CoverageStyle.h"
32 #include "Description.h"
33 #include "FeatureTypeStyle.h"
34 #include "Fill.h"
35 #include "Graphic.h"
36 #include "LineSymbolizer.h"
37 #include "Mark.h"
38 #include "ParameterValue.h"
39 #include "PointSymbolizer.h"
40 #include "PolygonSymbolizer.h"
41 #include "RasterSymbolizer.h"
42 #include "Rule.h"
43 #include "SelectedChannel.h"
44 #include "Stroke.h"
45 #include "TextSymbolizer.h"
46 #include "Utils.h"
47 
48 // STL
49 #include <cassert>
50 #include <cstdlib>
51 
52 te::se::Stroke* te::se::CreateStroke(const std::string& color, const std::string& width)
53 {
54  return te::se::CreateStroke(color, width, "");
55 }
56 
57 te::se::Stroke* te::se::CreateStroke(const std::string& color, const std::string& width, const std::string& opacity)
58 {
59  return te::se::CreateStroke(color, width, opacity, "");
60 }
61 
62 te::se::Stroke* te::se::CreateStroke(const std::string& color, const std::string& width,
63  const std::string& opacity, const std::string& dasharray)
64 {
65  return te::se::CreateStroke(color, width, opacity, dasharray, "", "");
66 }
67 
68 te::se::Stroke* te::se::CreateStroke(const std::string& color, const std::string& width,
69  const std::string& opacity, const std::string& dasharray,
70  const std::string& linecap, const std::string& linejoin)
71 {
72  te::se::Stroke* stroke = te::se::CreateStroke(0, width, opacity, dasharray, linecap, linejoin);
73 
74  if(!color.empty())
75  stroke->setColor(color);
76 
77  return stroke;
78 }
79 
81  const std::string& width, const std::string& opacity,
82  const std::string& dasharray, const std::string& linecap, const std::string& linejoin)
83 {
84  te::se::Stroke* stroke = new te::se::Stroke;
85 
86  if(graphicFill)
87  stroke->setGraphicFill(graphicFill);
88 
89  if(!width.empty())
90  stroke->setWidth(width);
91 
92  if(!opacity.empty())
93  stroke->setOpacity(opacity);
94 
95  if(!dasharray.empty())
96  stroke->setDashArray(dasharray);
97 
98  if(!linecap.empty())
99  stroke->setLineCap(linecap);
100 
101  if(!linejoin.empty())
102  stroke->setLineJoin(linecap);
103 
104  return stroke;
105 }
106 
107 te::se::Fill* te::se::CreateFill(const std::string& color, const std::string& opacity)
108 {
109  te::se::Fill* fill = new te::se::Fill;
110 
111  if(!color.empty())
112  fill->setColor(color);
113 
114  if(!opacity.empty())
115  fill->setOpacity(opacity);
116 
117  return fill;
118 }
119 
121 {
122  te::se::Fill* fill = new te::se::Fill;
123  fill->setGraphicFill(graphicFill);
124 
125  return fill;
126 }
127 
128 te::se::Mark* te::se::CreateMark(const std::string& wellKnownName, te::se::Stroke* stroke, te::se::Fill* fill)
129 {
130  std::string* name = new std::string(wellKnownName);
131 
132  te::se::Mark* mark = new te::se::Mark;
133  mark->setWellKnownName(name);
134  mark->setStroke(stroke);
135  mark->setFill(fill);
136 
137  return mark;
138 }
139 
140 te::se::Graphic* te::se::CreateGraphic(te::se::Mark* mark, const std::string& size, const std::string& rotation, const std::string& opacity)
141 {
142  te::se::Graphic* graphic = new te::se::Graphic;
143  graphic->add(mark);
144 
145  if(!size.empty())
146  graphic->setSize(new te::se::ParameterValue(size));
147 
148  if(!rotation.empty())
149  graphic->setRotation(new te::se::ParameterValue(rotation));
150 
151  if(!opacity.empty())
152  graphic->setOpacity(new te::se::ParameterValue(opacity));
153 
154  return graphic;
155 }
156 
158 {
160 
161  if(fill)
162  symbolizer->setFill(fill);
163 
164  if(stroke)
165  symbolizer->setStroke(stroke);
166 
167  return symbolizer;
168 }
169 
171 {
173 
174  if(stroke)
175  symbolizer->setStroke(stroke);
176 
177  return symbolizer;
178 }
179 
181 {
183  symbolizer->setGraphic(graphic);
184 
185  return symbolizer;
186 }
187 
188 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)
189 {
190  te::se::Font* font = new te::se::Font;
191  font->setFamily(family);
192  font->setSize(size);
193  font->setStyle(style);
194  font->setWeight(weight);
195 
196  return font;
197 }
198 
200 {
202  symbolizer->setLabel(new te::se::ParameterValue(label));
203  symbolizer->setFill(fill);
204  symbolizer->setFont(font);
205 
206  return symbolizer;
207 }
208 
209 te::se::Description* te::se::CreateDescription(const std::string& title, const std::string& abst)
210 {
211  te::se::Description* description = new te::se::Description;
212  description->setTitle(title);
213  description->setAbstract(abst);
214 
215  return description;
216 }
217 
219 {
220  std::string color = GenerateRandomColor();
221 
222  return CreateSymbolizer(geomType, color);
223 }
224 
225 te::se::Symbolizer* te::se::CreateSymbolizer(const te::gm::GeomType& geomType, const std::string& color)
226 {
227  switch(geomType)
228  {
229  case te::gm::PolygonType:
237  {
238  te::se::Fill* fill = CreateFill(color, "1.0");
239  te::se::Stroke* stroke = CreateStroke("#000000", "1");
241  symbolizer->setFill(fill);
242  symbolizer->setStroke(stroke);
243  return symbolizer;
244  }
245 
254  {
255  te::se::Stroke* stroke = CreateStroke(color, "1");
257  symbolizer->setStroke(stroke);
258  return symbolizer;
259  }
260 
261  case te::gm::PointType:
262  case te::gm::PointMType:
263  case te::gm::PointZType:
264  case te::gm::PointZMType:
269  {
270  te::se::Fill* markFill = CreateFill(color, "1.0");
271  te::se::Stroke* markStroke = CreateStroke("#000000", "1");
272  te::se::Mark* mark = CreateMark("circle", markStroke, markFill);
273  te::se::Graphic* graphic = CreateGraphic(mark, "12", "", "");
274  return CreatePointSymbolizer(graphic);
275  }
276 
277  default:
278  return 0;
279  }
280 }
281 
283 {
284  te::se::Symbolizer* symbolizer = CreateSymbolizer(geomType);
285 
286  te::se::Rule* rule = new te::se::Rule;
287 
288  if(symbolizer != 0)
289  rule->push_back(symbolizer);
290 
292  style->push_back(rule);
293 
294  return style;
295 }
296 
297 te::se::Style* te::se::CreateCoverageStyle(const std::vector<te::rst::BandProperty*>& properties)
298 {
299  // TODO: Review this method in order to extract the maximum information about the given band properties.
300  return CreateCoverageStyle(properties.size());
301 }
302 
303 te::se::Style* te::se::CreateCoverageStyle(const std::size_t& nBands)
304 {
305  if(nBands == 0)
306  return 0;
307 
308  // Default raster symbolizer
309  te::se::RasterSymbolizer* rasterSymbolizer = CreateRasterSymbolizer(nBands);
310 
311  te::se::Rule* rule = new te::se::Rule();
312  rule->push_back(rasterSymbolizer);
313 
315  style->push_back(rule);
316 
317  return style;
318 }
319 
320 
322 {
323  // Default raster symbolizer
324  te::se::RasterSymbolizer* rasterSymbolizer = new te::se::RasterSymbolizer;
325 
326  // General parameters
327  rasterSymbolizer->setOpacity(new te::se::ParameterValue("1.0"));
328  rasterSymbolizer->setGain(new te::se::ParameterValue("0.0"));
329  rasterSymbolizer->setOffset(new te::se::ParameterValue("0.0"));
330 
331  // Channel selection
332  if((nBands == 1) || (nBands == 2))
333  {
335  sc->setSourceChannelName(std::string("0"));
336 
339  cs->setGrayChannel(sc);
340 
341  rasterSymbolizer->setChannelSelection(cs);
342  }
343  else if(nBands >= 3)
344  {
345  // Red Channel
347  scr->setSourceChannelName(std::string("0"));
348 
349  // Green Channel
351  scg->setSourceChannelName(std::string("1"));
352 
353  // Blue channel
355  scb->setSourceChannelName(std::string("2"));
356 
359  cs->setRedChannel(scr);
360  cs->setGreenChannel(scg);
361  cs->setBlueChannel(scb);
362 
363  rasterSymbolizer->setChannelSelection(cs);
364  }
365 
366  return rasterSymbolizer;
367 }
368 
370 {
371  assert(s);
372 
373  std::size_t nRules = s->getRules().size();
374 
375  if(nRules <= 0)
376  return 0;
377 
378  // for while, consider one rule
379  const te::se::Rule* r = s->getRule(0);
380 
381  const std::vector<te::se::Symbolizer*>& symbolizers = r->getSymbolizers();
382 
383  if(symbolizers.empty())
384  return 0;
385 
386  // for while, consider one raster symbolizer
387  te::se::RasterSymbolizer* rasterSymbolizer = dynamic_cast<te::se::RasterSymbolizer*>(symbolizers[0]);
388 
389  return rasterSymbolizer;
390 }
391 
393 {
395  t.setHsv(rand() % 360, 64 + (rand() % 192), 128 + (rand() % 128));
396 
397  te::color::RGBAColor color(t.getRgba());
398 
399  return color.getColor();
400 }
A LineSymbolizer is used to style a stroke along a linear geometry type, such as a string of line seg...
void setStroke(Stroke *stroke)
A Stroke specifies the appearance of a linear geometry.
void setSourceChannelName(const std::string &name)
The RasterSymbolizer describes how to render raster/matrix-coverage data (e.g., satellite photos...
const std::vector< Rule * > & getRules() const
Definition: Style.cpp:94
TESEEXPORT Fill * CreateFill(const std::string &color, const std::string &opacity)
Creates a fill.
Definition: Utils.cpp:107
A Mark specifies a geometric shape and applies coloring to it.
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 setStroke(Stroke *s)
Definition: Mark.cpp:119
A Fill specifies the pattern for filling an area geometry.
Definition: Fill.h:59
void push_back(Symbolizer *s)
Definition: Rule.cpp:138
ChannelSelection specifies the false-color channel selection for a multi-spectral raster source (such...
The FeatureTypeStyle defines the styling that is to be applied to a dataset that can be viewed as a f...
void setLineCap(const std::string &cap)
Definition: Stroke.cpp:108
Rule * getRule(std::size_t i) const
Definition: Style.cpp:105
void setDashArray(const std::string &dasharray)
Definition: Stroke.cpp:113
void setWidth(const std::string &width)
Definition: Stroke.cpp:98
FontStyleType
It defines the style to use for a font.
Definition: Font.h:72
TESEEXPORT TextSymbolizer * CreateTextSymbolizer(const std::string &label, Fill *fill, Font *font)
Creates a text symbolizer.
Definition: Utils.cpp:199
The FeatureTypeStyle defines the styling that is to be applied to a dataset that can be viewed as a f...
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
void setTitle(const std::string &title)
Definition: Description.cpp:37
TESEEXPORT PointSymbolizer * CreatePointSymbolizer(Graphic *graphic)
Creates a point symbolizer.
Definition: Utils.cpp:180
void setSize(const std::string &size)
Definition: Font.cpp:80
TESEEXPORT Graphic * CreateGraphic(Mark *mark, const std::string &size, const std::string &rotation, const std::string &opacity)
Creates a graphic.
Definition: Utils.cpp:140
A Symbolizer describes how a feature is to appear on a map.
Definition: Symbolizer.h:80
A PolygonSymbolizer is used to draw a polygon (or other area-type geometries), including filling its ...
void setGraphic(Graphic *graphic)
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
A TextSymbolizer is used to render text labels according to various graphical parameters.
A PointSymbolizer specifies the rendering of a graphic Symbolizer at a point.
TESEEXPORT LineSymbolizer * CreateLineSymbolizer(Stroke *stroke)
Creates a line symbolizer.
Definition: Utils.cpp:170
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
Definition: CoverageStyle.h:45
const std::vector< Symbolizer * > & getSymbolizers() const
Definition: Rule.cpp:152
void setLineJoin(const std::string &join)
Definition: Stroke.cpp:103
TESEEXPORT Mark * CreateMark(const std::string &wellKnownName, Stroke *stroke, Fill *fill)
Creates a mark.
Definition: Utils.cpp:128
void setHsv(int h, int s, int v, int a=255)
It sets the h, s, v components of this color.
void push_back(const std::string &semanticTypeIdentifier)
Definition: Style.cpp:75
void setFamily(const std::string &family)
Definition: Font.cpp:65
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
A selected channel to be display.
A Fill specifies the pattern for filling an area geometry.
void setChannelSelection(ChannelSelection *c)
void setBlueChannel(SelectedChannel *c)
A Stroke specifies the appearance of a linear geometry.
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:78
void setColor(const std::string &color)
Definition: Fill.cpp:66
FontWeightType
It gives the amount of weight or boldness to use for a font.
Definition: Font.h:84
A Description gives human-readable descriptive information for the object it is included within...
A LineSymbolizer is used to style a stroke along a linear geometry type, such as a string of line seg...
void setFill(Fill *f)
Definition: Mark.cpp:108
TESEEXPORT Style * CreateFeatureTypeStyle(const te::gm::GeomType &geomType)
Try creates an appropriate feature type style based on given geometry type.
Definition: Utils.cpp:282
void setLabel(ParameterValue *l)
TESEEXPORT Stroke * CreateStroke(const std::string &color, const std::string &width)
Creates a stroke.
Definition: Utils.cpp:52
A PointSymbolizer specifies the rendering of a graphic Symbolizer at a point.
The RasterSymbolizer describes how to render raster/matrix-coverage data (e.g., satellite photos...
TESEEXPORT Style * CreateCoverageStyle(const std::vector< te::rst::BandProperty * > &properties)
Try creates an appropriate coverage style based on given band properties.
Definition: Utils.cpp:297
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
A PolygonSymbolizer is used to draw a polygon (or other area-type geometries), including filling its ...
void setStyle(const FontStyleType &style)
Definition: Font.cpp:70
TESEEXPORT Symbolizer * CreateSymbolizer(const te::gm::GeomType &geomType)
Try creates an appropriate symbolizer based on given geometry type.
Definition: Utils.cpp:218
A Font specifies the text font to use in a text symbolizer.
Definition: Font.h:63
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
A TextSymbolizer is used to render text labels according to various graphical parameters.
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
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
void setOpacity(const std::string &opacity)
Definition: Stroke.cpp:93
void setGain(ParameterValue *p)
void setRedChannel(SelectedChannel *c)
TESEEXPORT Description * CreateDescription(const std::string &title, const std::string &abst)
Creates a description.
Definition: Utils.cpp:209
void setAbstract(const std::string &a)
Definition: Description.cpp:47
TESEEXPORT RasterSymbolizer * GetRasterSymbolizer(Style *s)
Try to get raster symbolizer from a style.
Definition: Utils.cpp:369
void setWellKnownName(std::string *name)
The WellKnownName element gives the well-known name of the shape of the mark.
Definition: Mark.cpp:54
void setFill(Fill *f)
A Fill specifies the pattern for filling an area geometry.
TESEEXPORT PolygonSymbolizer * CreatePolygonSymbolizer(Stroke *stroke, Fill *fill)
Creates a polygon symbolizer.
Definition: Utils.cpp:157
void setOpacity(ParameterValue *p)
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
void setGrayChannel(SelectedChannel *c)
Utility functions for Symbology Enconding module.
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
ChannelSelection specifies the false-color channel selection for a multi-spectral raster source (such...
The ParameterValueType uses WFS-Filter expressions to give values for SE graphic parameters.
void setColorCompositionType(ColorCompositionType cct)
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:188
TESEEXPORT std::string GenerateRandomColor()
Creates a random RGB color encoded using two hexadecimal digits per primary-color component prefixed ...
Definition: Utils.cpp:392
A Description gives human-readable descriptive information for the object it is included within...
Definition: Description.h:56
void setOpacity(const std::string &opacity)
Definition: Fill.cpp:71
A selected channel to be display.
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
A Graphic is a graphic symbol with an inherent shape, color(s), and possibly size.
void setGreenChannel(SelectedChannel *c)
void setStroke(Stroke *stroke)
A Stroke specifies the appearance of a linear geometry. The Stroke element is optional inside of Line...
void setWeight(const FontWeightType &weight)
Definition: Font.cpp:75
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
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 Graphic is a graphic symbol with an inherent shape, color(s), and possibly size.
Definition: Graphic.h:66
The &quot;ParameterValueType&quot; uses WFS-Filter expressions to give values for SE graphic parameters...
TESEEXPORT RasterSymbolizer * CreateRasterSymbolizer(const std::size_t &nBands)
Try creates an appropriate raster symbolizer style based on given number of bands.
Definition: Utils.cpp:321
void setOffset(ParameterValue *p)
void getRgba(int *r, int *g, int *b, int *a=0) const
It gets the color value.
void setColor(const std::string &color)
Definition: Stroke.cpp:88
A class to transform RGBA color to HSV, HSL and CMYK.
A Mark specifies a geometric shape and applies coloring to it.
Definition: Mark.h:84