Styling.cpp
Go to the documentation of this file.
1 #include "MapToolsExamples.h"
2 
3 // TerraLib
4 #include <terralib/common.h>
5 #include <terralib/geometry.h>
6 #include <terralib/dataaccess.h>
7 #include <terralib/maptools.h>
8 #include <terralib/se.h>
10 
11 // STL
12 #include <cassert>
13 #include <iostream>
14 #include <memory>
15 #include <vector>
16 
17 // Qt
18 #include <QApplication>
19 #include <QDialog>
20 #include <QLabel>
21 
22 /** @name Polygon Styles */
23 //@{
25 {
26  te::se::Fill* fill = te::se::CreateFill("#FF8C00", "1.0");
27 
29  symbolizer->setFill(fill);
30 
31  te::se::Rule* rule = new te::se::Rule;
32  rule->push_back(symbolizer);
33 
35  style->push_back(rule);
36 
37  return style;
38 }
39 
41 {
42  te::se::Fill* fill = te::se::CreateFill("#FF8C00", "1.0");
43  te::se::Stroke* stroke = te::se::CreateStroke("#FFFFFF", "2");
44 
46 
47  te::se::Rule* rule = new te::se::Rule;
48  rule->push_back(symbolizer);
49 
51  style->push_back(rule);
52 
53  return style;
54 }
55 
57 {
58  te::se::Fill* fill = te::se::CreateFill("#FF8C00", "0.5");
59  te::se::Stroke* stroke = te::se::CreateStroke("#FFFFFF", "2");
60 
61  te::se::PolygonSymbolizer* symbolizer = CreatePolygonSymbolizer(stroke, fill);
62 
63  te::se::Rule* rule = new te::se::Rule;
64  rule->push_back(symbolizer);
65 
67  style->push_back(rule);
68 
69  return style;
70 }
71 
72 te::se::Style* GraphicFillPolygonStyle(const std::string& markName)
73 {
74  te::se::Fill* markFill = te::se::CreateFill("#FF8C00", "1.0");
75  te::se::Stroke* markStroke = te::se::CreateStroke("#000000", "1");
76 
77  te::se::Mark* mark = te::se::CreateMark(markName, markStroke, markFill);
78 
79  te::se::Graphic* graphic = te::se::CreateGraphic(mark, "16", "", "");
80 
81  te::se::Fill* fill = te::se::CreateFill(graphic);
82  te::se::Stroke* stroke = te::se::CreateStroke("#000000", "2");
83 
85 
86  te::se::Rule* rule = new te::se::Rule;
87  rule->push_back(symbolizer);
88 
90  style->push_back(rule);
91 
92  return style;
93 }
94 
96 {
97  te::se::Fill* markFill = te::se::CreateFill("#00FF00", "1.0");
98  te::se::Stroke* markStroke = te::se::CreateStroke("#900020", "2");
99 
100  te::se::Mark* mark = te::se::CreateMark("triangle", markStroke, markFill);
101 
102  te::se::Graphic* graphic = te::se::CreateGraphic(mark, "24", "", "");
103 
104  te::se::Fill* fillOne = te::se::CreateFill(graphic);
105 
106  te::se::PolygonSymbolizer* symbolizerOne = te::se::CreatePolygonSymbolizer(0, fillOne);
107 
108  te::se::Fill* fillTwo = te::se::CreateFill("#00FF00", "0.5");
109  te::se::Stroke* stroke = te::se::CreateStroke("#900020", "2");
110 
111  te::se::PolygonSymbolizer* symbolizerTwo = te::se::CreatePolygonSymbolizer(stroke, fillTwo);
112 
113  te::se::Rule* rule = new te::se::Rule;
114  rule->push_back(symbolizerOne);
115  rule->push_back(symbolizerTwo);
116 
118  style->push_back(rule);
119 
120  return style;
121 }
122 
123 te::se::Style* GraphicFillContourPolygonStyle(const std::string& markName)
124 {
125  te::se::Fill* markFill = te::se::CreateFill("#FF0000", "1.0");
126  te::se::Stroke* markStroke = te::se::CreateStroke("#000000", "1");
127 
128  te::se::Mark* mark = te::se::CreateMark(markName, markStroke, markFill);
129 
130  te::se::Graphic* graphic = te::se::CreateGraphic(mark, "16", "-45", "");
131 
132  te::se::Stroke* stroke = te::se::CreateStroke(graphic, "16", "", "", "", "");
133 
134  te::se::Fill* fill = te::se::CreateFill("#000000", "1.0");
135 
137 
138  te::se::Rule* rule = new te::se::Rule;
139  rule->push_back(symbolizer);
140 
142  style->push_back(rule);
143 
144  return style;
145 }
146 //@}
147 
148 /** @name Line Styles */
149 //@{
151 {
152  te::se::Stroke* stroke = te::se::CreateStroke("#000000", "3.0");
153 
155 
156  te::se::Rule* rule = new te::se::Rule;
157  rule->push_back(symbolizer);
158 
160  style->push_back(rule);
161 
162  return style;
163 }
164 
166 {
167  te::se::Stroke* stroke = te::se::CreateStroke("#0000FF", "3", "1.0", "5 2");
169 
170  te::se::Rule* rule = new te::se::Rule;
171  rule->push_back(symbolizer);
172 
174  style->push_back(rule);
175 
176  return style;
177 }
178 
180 {
181  te::se::Stroke* strokeOne = te::se::CreateStroke("#333333", "5");
182  te::se::LineSymbolizer* symbolizerOne = te::se::CreateLineSymbolizer(strokeOne);
183 
184  te::se::Stroke* strokeTwo = te::se::CreateStroke("#6699FF", "3");
185  te::se::LineSymbolizer* symbolizerTwo = te::se::CreateLineSymbolizer(strokeTwo);
186 
187  te::se::Rule* rule = new te::se::Rule;
188  rule->push_back(symbolizerOne);
189  rule->push_back(symbolizerTwo);
190 
192  style->push_back(rule);
193 
194  return style;
195 }
196 
198 {
199  te::se::Stroke* strokeOne = te::se::CreateStroke("#000000", "12");
200  te::se::LineSymbolizer* symbolizerOne = te::se::CreateLineSymbolizer(strokeOne);
201 
202  te::se::Stroke* strokeTwo = te::se::CreateStroke("#888888", "8");
203  te::se::LineSymbolizer* symbolizerTwo = te::se::CreateLineSymbolizer(strokeTwo);
204 
205  te::se::Stroke* strokeThree = te::se::CreateStroke("#FFFF00", "2", "1.0", "5 5");
206  te::se::LineSymbolizer* symbolizerThree = te::se::CreateLineSymbolizer(strokeThree);
207 
208  te::se::Rule* rule = new te::se::Rule;
209  rule->push_back(symbolizerOne);
210  rule->push_back(symbolizerTwo);
211  rule->push_back(symbolizerThree);
212 
214  style->push_back(rule);
215 
216  return style;
217 }
218 
220 {
221  te::se::Stroke* strokeOne = te::se::CreateStroke("#000000", "16", "1.0", "", "butt", "miter");
222  te::se::LineSymbolizer* symbolizerOne = te::se::CreateLineSymbolizer(strokeOne);
223 
224  te::se::Stroke* strokeTwo = te::se::CreateStroke("#666666", "10", "1.0", "", "butt", "miter");
225  te::se::LineSymbolizer* symbolizerTwo = te::se::CreateLineSymbolizer(strokeTwo);
226 
227  te::se::Stroke* strokeThree = te::se::CreateStroke("#FFFFFF", "2", "1.0", "2 2", "butt", "miter");
228  te::se::LineSymbolizer* symbolizerThree = te::se::CreateLineSymbolizer(strokeThree);
229 
230  te::se::Rule* rule = new te::se::Rule;
231  rule->push_back(symbolizerOne);
232  rule->push_back(symbolizerTwo);
233  rule->push_back(symbolizerThree);
234 
236  style->push_back(rule);
237 
238  return style;
239 }
240 //@}
241 
242 /** @name Point Styles */
243 //@{
244 te::se::Style* MarkPointStyle(const std::string& markName)
245 {
246  te::se::Fill* markFill = te::se::CreateFill("#009900", "1.0");
247  te::se::Stroke* markStroke = te::se::CreateStroke("#000000", "1");
248  te::se::Mark* mark = te::se::CreateMark(markName, markStroke, markFill);
249 
250  te::se::Graphic* graphic = te::se::CreateGraphic(mark, "16", "", "");
251 
253 
254  te::se::Rule* rule = new te::se::Rule;
255  rule->push_back(symbolizer);
256 
258  style->push_back(rule);
259 
260  return style;
261 }
262 //@}
263 
264 te::map::DataSetLayer* CreateDataSetLayer(const std::string& path)
265 {
266  // Connection string to a shape file
267  std::string connInfo("file://" + path);
268 
269  // Creates and connects data source
271 
272  // Get the number of data set types that belongs to the data source
273  std::vector<std::string> datasets = datasource->getDataSetNames();
274  assert(!datasets.empty());
275 
276  // Gets the first dataset
277  std::string datasetName(datasets[0]);
278  std::unique_ptr<te::da::DataSetType> dt(datasource->getDataSetType(datasetName));
279  assert(dt->hasGeom());
280 
282  assert(geomProperty);
283 
284  // Box
285  std::unique_ptr<te::gm::Envelope> extent(datasource->getExtent(datasetName, geomProperty->getName()));
286 
287  // Creates a DataSetLayer
289  layer->setDataSourceId(datasource->getId());
290  layer->setDataSetName(datasetName);
291  layer->setExtent(*extent);
292  layer->setRendererType("ABSTRACT_LAYER_RENDERER");
293 
294  return layer;
295 }
296 
298 {
299  const te::gm::Envelope& extent = layer->getExtent();
300  double llx = extent.m_llx;
301  double lly = extent.m_lly;
302  double urx = extent.m_urx;
303  double ury = extent.m_ury;
304 
305  std::unique_ptr<te::qt::widgets::Canvas> canvas(new te::qt::widgets::Canvas(512, 512));
306  canvas->calcAspectRatio(llx, lly, urx, ury);
307  canvas->setWindow(llx, lly, urx, ury);
308  canvas->setBackgroundColor(te::color::RGBAColor(255, 255, 255, TE_OPAQUE));
309 
310  bool cancel = false;
311 
312  layer->draw(canvas.get(), extent, 4326, 0, &cancel);
313 
314  QPixmap* pixmap = canvas->getPixmap();
315 
316  QDialog dialog;
317  dialog.setFixedSize(pixmap->size());
318 
319  QLabel preview(&dialog);
320  preview.setPixmap(*pixmap);
321 
322  dialog.exec();
323 }
324 
326 {
327  // If you want to use any Qt resource you must initialize a QApplication in some place!
328  int argc = 0;
329  QApplication app(argc, 0);
330 
331  try
332  {
333  // Gets supported marks
334  std::vector<std::string> marks;
335  //te::map::MarkRendererManager::getAllSupportedMarks(marks); // AbstractMarkFactory::SupportedMarks(marks);
336  te::map::MarkRendererManager::getInstance().getAllSupportedMarks(marks);
337  // Creates a layer of polygons
338  te::map::DataSetLayer* polygons = CreateDataSetLayer("./shape/style/polygons.shp");
339 
340  // Polygon Styles
341  std::vector<te::se::Style*> polygonStyles;
342  polygonStyles.push_back(SimplePolygonStyle());
343  polygonStyles.push_back(SimplePolygonStyleWithStroke());
344  polygonStyles.push_back(TransparentPolygonStyle());
345  for(std::size_t i = 0; i < marks.size(); ++i)
346  polygonStyles.push_back(GraphicFillPolygonStyle(marks[i]));
347  polygonStyles.push_back(TreePolygonStyle());
348  polygonStyles.push_back(GraphicFillContourPolygonStyle("star"));
349 
350  // Draw!
351  for(std::size_t i = 0; i < polygonStyles.size(); ++i)
352  {
353  polygons->setStyle(polygonStyles[i]);
354  Draw(polygons);
355  }
356 
357  // Creates a layer of lines
358  te::map::DataSetLayer* lines = CreateDataSetLayer("./shape/style/lines.shp");
359 
360  // Line Styles
361  std::vector<te::se::Style*> lineStyles;
362  lineStyles.push_back(SimpleLineStyle());
363  lineStyles.push_back(DashedLineStyle());
364  lineStyles.push_back(LineWithBorderStyle());
365  lineStyles.push_back(RoadStyleOne());
366  lineStyles.push_back(RoadStyleTwo());
367 
368  // Draw!
369  for(std::size_t i = 0; i < lineStyles.size(); ++i)
370  {
371  lines->setStyle(lineStyles[i]);
372  Draw(lines);
373  }
374 
375  // Creates a layer of points
376  te::map::DataSetLayer* points = CreateDataSetLayer("./shape/style/points.shp");
377 
378  // Point Styles
379  std::vector<te::se::Style*> pointStyles;
380  for(std::size_t i = 0; i < marks.size(); ++i)
381  pointStyles.push_back(MarkPointStyle(marks[i]));
382 
383  // Draw!
384  for(std::size_t i = 0; i < pointStyles.size(); ++i)
385  {
386  points->setStyle(pointStyles[i]);
387  Draw(points);
388  }
389 
390  delete points;
391  delete lines;
392  delete polygons;
393  }
394  catch(const std::exception& e)
395  {
396  std::cout << std::endl << "An exception has occurred in Styling example: " << e.what() << std::endl;
397  }
398  catch(...)
399  {
400  std::cout << std::endl << "An unexpected exception has occurred in Styling example!" << std::endl;
401  }
402 }
A canvas built on top of Qt.
Geometric property.
virtual void setDataSourceId(const std::string &id)
te::se::Style * SimplePolygonStyle()
Definition: Styling.cpp:24
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
virtual const te::gm::Envelope & getExtent() const
It returns the Layer extent (or minimum bounding box).
A PolygonSymbolizer is used to draw a polygon (or other area-type geometries), including filling its ...
boost::shared_ptr< DataSource > DataSourcePtr
te::se::Style * TransparentPolygonStyle()
Definition: Styling.cpp:56
te::se::Style * GraphicFillContourPolygonStyle(const std::string &markName)
Definition: Styling.cpp:123
te::se::Style * LineWithBorderStyle()
Definition: Styling.cpp:179
void setDataSetName(const std::string &name)
te::se::Style * RoadStyleOne()
Definition: Styling.cpp:197
double m_urx
Upper right corner x-coordinate.
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
void DrawStyledLayers()
It draws a set of layers using styles encoded using OGC Symbology Enconding specification.
Definition: Styling.cpp:325
te::se::Style * MarkPointStyle(const std::string &markName)
Definition: Styling.cpp:244
A PointSymbolizer specifies the rendering of a graphic Symbolizer at a point.
This file contains include headers for TerraLib Symbology Encoding module.
void push_back(const std::string &semanticTypeIdentifier)
Definition: Style.cpp:75
TESEEXPORT LineSymbolizer * CreateLineSymbolizer(Stroke *stroke)
Creates a line symbolizer.
te::se::Style * SimplePolygonStyleWithStroke()
Definition: Styling.cpp:40
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.
te::se::Style * GraphicFillPolygonStyle(const std::string &markName)
Definition: Styling.cpp:72
void push_back(Symbolizer *s)
Definition: Rule.cpp:138
te::se::Style * TreePolygonStyle()
Definition: Styling.cpp:95
void setRendererType(const std::string &t)
double m_llx
Lower left corner x-coordinate.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
The FeatureTypeStyle defines the styling that is to be applied to a dataset that can be viewed as a f...
te::se::Style * SimpleLineStyle()
Definition: Styling.cpp:150
static te::dt::TimeDuration dt(20, 30, 50, 11)
TESEEXPORT Graphic * CreateGraphic(Mark *mark, const std::string &size, const std::string &rotation, const std::string &opacity)
Creates a graphic.
virtual void setExtent(const te::gm::Envelope &mbr)
It sets the Layer extent (or minimum bounding box).
A Fill specifies the pattern for filling an area geometry.
Definition: Fill.h:59
TESEEXPORT Mark * CreateMark(const std::string &wellKnownName, Stroke *stroke, Fill *fill)
Creates a mark.
double m_lly
Lower left corner y-coordinate.
TESEEXPORT Stroke * CreateStroke(const std::string &color, const std::string &width)
Creates a stroke.
te::se::Style * DashedLineStyle()
Definition: Styling.cpp:165
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:76
double m_ury
Upper right corner y-coordinate.
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
virtual void setStyle(te::se::Style *style)
It sets the Style associated to the layer.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
void Draw(te::map::DataSetLayer *layer)
Definition: Styling.cpp:297
A LineSymbolizer is used to style a stroke along a linear geometry type, such as a string of line seg...
te::se::Style * RoadStyleTwo()
Definition: Styling.cpp:219
This file contains include headers for the TerraLib Common Runtime module.
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
void setFill(Fill *f)
A Fill specifies the pattern for filling an area geometry.
unsigned int G_ID
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56
This file contains include headers for the Vector Geometry model of TerraLib.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
This file contains include headers for the Data Access module of TerraLib.
This file contains include headers for the Map Tools module of TerraLib.
void draw(Canvas *canvas, const te::gm::Envelope &bbox, int srid, const double &scale, bool *cancel)
It draws the layer geographic objects in the given canvas using the informed SRS. ...
te::map::DataSetLayer * CreateDataSetLayer(const std::string &path)
Definition: Styling.cpp:264
TESEEXPORT PointSymbolizer * CreatePointSymbolizer(Graphic *graphic)
Creates a point symbolizer.
TESEEXPORT Fill * CreateFill(const std::string &color, const std::string &opacity)
Creates a fill.