RasterStyling.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/raster.h>
9 #include <terralib/se.h>
11 
12 // STL
13 #include <cassert>
14 #include <iostream>
15 #include <memory>
16 #include <vector>
17 #include <string>
18 
19 // Qt
20 #include <QApplication>
21 #include <QDialog>
22 #include <QLabel>
23 
24 bool generatePNG = true;
25 
26 te::map::DataSetLayer* CreateRasterLayer(const std::string& path)
27 {
28  // Connection string to a raster file
29  std::string connInfo("file://" + path);
30 
31  // Creates and connects data source
33 
34  // Get the number of data set types that belongs to the data source
35  std::vector<std::string> datasets = datasource->getDataSetNames();
36  assert(!datasets.empty());
37 
38  // Gets the first dataset
39  std::string dataSetName(datasets[0]);
40  std::unique_ptr<te::da::DataSet> ds(datasource->getDataSet(dataSetName));
41 
42  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
43  std::unique_ptr<te::rst::Raster> raster(ds->getRaster(rpos));
44 
45  te::gm::Envelope extent(*raster->getExtent());
46 
47  // Creates a DataSetLayer
49  layer->setDataSourceId(datasource->getId());
50  layer->setDataSetName(dataSetName);
51  layer->setExtent(extent);
52  layer->setRendererType("ABSTRACT_LAYER_RENDERER");
53  layer->setSRID(raster->getSRID());
54 
55  return layer;
56 }
57 
59 {
60  te::gm::Envelope extent(layer->getExtent());
61 
62  std::unique_ptr<te::rst::Raster> raster(te::map::GetRaster(layer));
63 
64  if(srid != raster->getSRID())
65  extent.transform(raster->getSRID(), srid);
66 
67  double llx = extent.m_llx;
68  double lly = extent.m_lly;
69  double urx = extent.m_urx;
70  double ury = extent.m_ury;
71 
72  te::qt::widgets::Canvas* canvas = new te::qt::widgets::Canvas(800, 600);
73  canvas->calcAspectRatio(llx, lly, urx, ury);
74  canvas->setWindow(llx, lly, urx, ury);
75  canvas->setBackgroundColor(te::color::RGBAColor(255, 255, 255, TE_OPAQUE));
76 
77  return canvas;
78 }
79 
80 void showPixmap(te::qt::widgets::Canvas* c, std::string fileName)
81 {
82  QPixmap* pixmap = c->getPixmap();
83 
84  QDialog dialog;
85  dialog.setWindowTitle(fileName.c_str());
86  dialog.setFixedSize(pixmap->size());
87 
88  QLabel preview(&dialog);
89  preview.setPixmap(*pixmap);
90 
91  dialog.exec();
92 }
93 
94 void paint(te::qt::widgets::Canvas* c, bool generatePNG, std::string fileName)
95 {
96  showPixmap(c, fileName);
97 
98  if(generatePNG)
99  {
100  fileName += ".tif";
101  c->save(fileName.c_str(), te::map::PNG);
102  }
103 
104  c->clear();
105 }
106 
108  {
109  //create default raster symbolizer
111 
112  //set transparency
113  rs->setOpacity(new te::se::ParameterValue("1.0"));
114 
115  //set channel selection
117 
118  //channel R
120  scR->setSourceChannelName("0");
121  cs->setRedChannel(scR);
122 
123  //channel G
125  scG->setSourceChannelName("1");
126  cs->setGreenChannel(scG);
127 
128  //channel B
130  scB->setSourceChannelName("2");
131  cs->setBlueChannel(scB);
132 
133  rs->setChannelSelection(cs);
134 
135  //add symbolizer to a layer style
136  te::se::Rule* r = new te::se::Rule();
137  r->push_back(rs);
138 
140  s->push_back(r);
141 
142  l->setStyle(s);
143 
144  bool cancel = false;
145 
146  l->draw(c, *e, srid, 0, &cancel);
147 
148  paint(c, generatePNG, "RGB_012_Style");
149  }
150 
152  {
153  //create default raster symbolizer
155 
156  //set transparency
157  rs->setOpacity(new te::se::ParameterValue("0.5"));
158 
159  //set channel selection
162 
163  //channel R
165  scR->setSourceChannelName("0");
166  cs->setRedChannel(scR);
167 
168  //channel G
170  scG->setSourceChannelName("1");
171  cs->setGreenChannel(scG);
172 
173  //channel B
175  scB->setSourceChannelName("2");
176  cs->setBlueChannel(scB);
177 
178  rs->setChannelSelection(cs);
179 
180  //add symbolizer to a layer style
181  te::se::Rule* r = new te::se::Rule();
182  r->push_back(rs);
183 
185  s->push_back(r);
186 
187  l->setStyle(s);
188 
189  bool cancel = false;
190 
191  l->draw(c, *e, srid, 0, &cancel);
192 
193  paint(c, generatePNG, "RGB_012_Transp_Style");
194  }
195 
197  {
198  //create default raster symbolizer
200 
201  //set transparency
202  rs->setOpacity(new te::se::ParameterValue("1.0"));
203 
204  //set channel selection
207 
208  //channel R
210  scR->setSourceChannelName("1");
211  cs->setRedChannel(scR);
212 
213  //channel G
215  scG->setSourceChannelName("0");
216  cs->setGreenChannel(scG);
217 
218  //channel B
220  scB->setSourceChannelName("2");
221  cs->setBlueChannel(scB);
222 
223  rs->setChannelSelection(cs);
224 
225  //add symbolizer to a layer style
226  te::se::Rule* r = new te::se::Rule();
227  r->push_back(rs);
228 
230  s->push_back(r);
231 
232  l->setStyle(s);
233 
234  bool cancel = false;
235 
236  l->draw(c, *e, srid, 0, &cancel);
237 
238  paint(c, generatePNG, "RGB_102_Style");
239  }
240 
242  {
243  //create default raster symbolizer
245 
246  //set transparency
247  rs->setOpacity(new te::se::ParameterValue("1.0"));
248 
249  //set channel selection
252 
253  //channel R
255  scR->setSourceChannelName("0");
256  cs->setRedChannel(scR);
257 
258  //channel G
260  scG->setSourceChannelName("1");
261 
263  cG->setGammaValue(0.5);
264  scG->setContrastEnhancement(cG);
265  cs->setGreenChannel(scG);
266 
267  //channel B
269  scB->setSourceChannelName("2");
270  cs->setBlueChannel(scB);
271 
272  rs->setChannelSelection(cs);
273 
274  //add symbolizer to a layer style
275  te::se::Rule* r = new te::se::Rule();
276  r->push_back(rs);
277 
279  s->push_back(r);
280 
281  l->setStyle(s);
282 
283  bool cancel = false;
284 
285  l->draw(c, *e, srid, 0, &cancel);
286 
287  paint(c, generatePNG, "RGB_012_G_Contrast_Style");
288  }
289 
291  {
292  //create default raster symbolizer
294 
295  //set transparency
296  rs->setOpacity(new te::se::ParameterValue("1.0"));
297 
298  //set channel selection
301 
302  //channel R
304  scR->setSourceChannelName("0");
305 
307  cR->setGammaValue(0.5);
308  scR->setContrastEnhancement(cR);
309  cs->setRedChannel(scR);
310 
311  //channel G
313  scG->setSourceChannelName("1");
314 
316  cG->setGammaValue(0.5);
317  scG->setContrastEnhancement(cG);
318  cs->setGreenChannel(scG);
319 
320  //channel B
322  scB->setSourceChannelName("2");
323 
325  cB->setGammaValue(0.5);
326  scB->setContrastEnhancement(cB);
327  cs->setBlueChannel(scB);
328 
329  rs->setChannelSelection(cs);
330 
331  //add symbolizer to a layer style
332  te::se::Rule* r = new te::se::Rule();
333  r->push_back(rs);
334 
336  s->push_back(r);
337 
338  l->setStyle(s);
339 
340  bool cancel = false;
341 
342  l->draw(c, *e, srid, 0, &cancel);
343 
344  paint(c, generatePNG, "RGB_012_RGB_Contrast_Style");
345  }
346 
348  {
349  //create default raster symbolizer
351 
352  //set transparency
353  rs->setOpacity(new te::se::ParameterValue("1.0"));
354 
355  //set channel selection
358 
359  //channel M
361  scM->setSourceChannelName("0");
362  cs->setGrayChannel(scM);
363 
364  rs->setChannelSelection(cs);
365 
366  //add symbolizer to a layer style
367  te::se::Rule* r = new te::se::Rule();
368  r->push_back(rs);
369 
371  s->push_back(r);
372 
373  l->setStyle(s);
374 
375  bool cancel = false;
376 
377  l->draw(c, *e, srid, 0, &cancel);
378 
379  paint(c, generatePNG, "MONO_0_Style");
380  }
381 
383  {
384  //create default raster symbolizer
386 
387  //set transparency
388  rs->setOpacity(new te::se::ParameterValue("1.0"));
389 
390  //set channel selection
393 
394  //channel M
396  scM->setSourceChannelName("2");
397  cs->setGrayChannel(scM);
398 
399  rs->setChannelSelection(cs);
400 
401  //add symbolizer to a layer style
402  te::se::Rule* r = new te::se::Rule();
403  r->push_back(rs);
404 
406  s->push_back(r);
407 
408  l->setStyle(s);
409 
410  bool cancel = false;
411 
412  l->draw(c, *e, srid, 0, &cancel);
413 
414  paint(c, generatePNG, "MONO_2_Style");
415  }
416 
418  {
419  //create default raster symbolizer
421 
422  //set transparency
423  rs->setOpacity(new te::se::ParameterValue("1.0"));
424 
425  //set channel selection
428 
429  //channel R
431  scR->setSourceChannelName("0");
432  cs->setRedChannel(scR);
433 
434  rs->setChannelSelection(cs);
435 
436  //add symbolizer to a layer style
437  te::se::Rule* r = new te::se::Rule();
438  r->push_back(rs);
439 
441  s->push_back(r);
442 
443  l->setStyle(s);
444 
445  bool cancel = false;
446 
447  l->draw(c, *e, srid, 0, &cancel);
448 
449  paint(c, generatePNG, "RED_Style");
450  }
451 
453 {
454  // If you want to use any Qt resource you must initialize a QApplication in some place!
455  int argc = 0;
456  QApplication app(argc, 0);
457 
458  try
459  {
460  // Creates a layer of raster
461  std::unique_ptr<te::map::DataSetLayer> rasterLayer(CreateRasterLayer(TERRALIB_DATA_DIR "/rasters/cbers2b_rgb342_crop.tif"));
462 
463  // Get the box to be painted
464  te::gm::Envelope* extent = new te::gm::Envelope(rasterLayer->getExtent());
465 
466  // Get the projection used to be paint the raster
467  std::unique_ptr<te::rst::Raster> raster(te::map::GetRaster(rasterLayer.get()));
468  int srid = raster->getSRID();
469  //int srid = 4618; // LL SAD69
470 
471  // Creates a canvas
472  te::qt::widgets::Canvas* canvas = CreateCanvas(rasterLayer.get(), extent, srid);
473 
474  // RGB 012 Style
475  //RGB_012_Style(canvas, rasterLayer.get(), extent, srid);
476 
477  // RGB 012 with transparency Style
478  RGB_012_Transp_Style(canvas, rasterLayer.get(), extent, srid);
479 
480  // RGB 012 Style
481  RGB_102_Style(canvas, rasterLayer.get(), extent, srid);
482 
483  // RGB 012 with contrast in green band Style
484  RGB_012_G_Contrast_Style(canvas, rasterLayer.get(), extent, srid);
485 
486  // RGB 012 with contrast in RGB bands Style
487  RGB_012_RGB_Contrast_Style(canvas, rasterLayer.get(), extent, srid);
488 
489  // Mono band 0 Style
490  MONO_0_Style(canvas, rasterLayer.get(), extent, srid);
491 
492  // Mono band 2 Style
493  MONO_2_Style(canvas, rasterLayer.get(), extent, srid);
494 
495  delete extent;
496  delete canvas;
497  }
498  catch(const std::exception& e)
499  {
500  std::cout << std::endl << "An exception has occurred in Styling example: " << e.what() << std::endl;
501  }
502  catch(...)
503  {
504  std::cout << std::endl << "An unexpected exception has occurred in Styling example!" << std::endl;
505  }
506 }
void setRedChannel(SelectedChannel *c)
A canvas built on top of Qt.
void clear()
It clears the canvas content and fills with the background color.
A selected channel to be display.
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
virtual const te::gm::Envelope & getExtent() const
It returns the Layer extent (or minimum bounding box).
boost::shared_ptr< DataSource > DataSourcePtr
void MONO_2_Style(te::qt::widgets::Canvas *c, te::map::DataSetLayer *l, te::gm::Envelope *e, int srid)
void RGB_102_Style(te::qt::widgets::Canvas *c, te::map::DataSetLayer *l, te::gm::Envelope *e, int srid)
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
Definition: CoverageStyle.h:45
void RGB_012_Transp_Style(te::qt::widgets::Canvas *c, te::map::DataSetLayer *l, te::gm::Envelope *e, int srid)
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
This file contains include headers for TerraLib Symbology Encoding module.
void push_back(const std::string &semanticTypeIdentifier)
Definition: Style.cpp:75
void setGammaValue(const double &v)
void setGrayChannel(SelectedChannel *c)
static te::dt::Date ds(2010, 01, 01)
void RGB_012_G_Contrast_Style(te::qt::widgets::Canvas *c, te::map::DataSetLayer *l, te::gm::Envelope *e, int srid)
void push_back(Symbolizer *s)
Definition: Rule.cpp:138
The "ParameterValueType" uses WFS-Filter expressions to give values for SE graphic parameters...
void MONO_0_Style(te::qt::widgets::Canvas *c, te::map::DataSetLayer *l, te::gm::Envelope *e, int srid)
bool generatePNG
void setWindow(const double &llx, const double &lly, const double &urx, const double &ury)
It sets the world (or window) coordinates area (supposing a cartesian reference system).
te::map::DataSetLayer * CreateRasterLayer(const std::string &path)
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
te::qt::widgets::Canvas * CreateCanvas(te::map::DataSetLayer *layer, te::gm::Envelope *e, int srid)
void calcAspectRatio(double &llx, double &lly, double &urx, double &ury, const te::map::AlignType hAlign=te::map::Center, const te::map::AlignType vAlign=te::map::Center)
It calculates the best aspect ratio for world (or window) coordinates area (supposing a cartesian ref...
void setColorCompositionType(ColorCompositionType cct)
ContrastEnhancement defines the &#39;stretching&#39; of contrast for a channel of a false-color image or for ...
void setChannelSelection(ChannelSelection *c)
void RED_Style(te::qt::widgets::Canvas *c, te::map::DataSetLayer *l, te::gm::Envelope *e, int srid)
void RGB_012_RGB_Contrast_Style(te::qt::widgets::Canvas *c, te::map::DataSetLayer *l, te::gm::Envelope *e, int srid)
The RasterSymbolizer describes how to render raster/matrix-coverage data (e.g., satellite photos...
void setOpacity(ParameterValue *p)
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:76
QPixmap * getPixmap() const
It returns the internal pixmap used to draw geographical objects.
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 setContrastEnhancement(ContrastEnhancement *c)
void setSourceChannelName(const std::string &name)
This file contains include headers for the TerraLib Common Runtime module.
void DrawRasterStyledLayers()
It draws a set of raster layers using styles encoded using OGC RasterSymbology Enconding specificatio...
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
unsigned int G_ID
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
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.
void save(const char *fileName, te::map::ImageType t, int quality=75, int fg=0) const
It saves the canvas content to a file image in the specified format type.
This file contains include headers for the Data Access module of TerraLib.
void setBlueChannel(SelectedChannel *c)
void RGB_012_Style(te::qt::widgets::Canvas *c, te::map::DataSetLayer *l, te::gm::Envelope *e, int srid)
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. ...
void setBackgroundColor(const te::color::RGBAColor &color)
It sets the canvas background color.
void showPixmap(te::qt::widgets::Canvas *c, std::string fileName)
void setGreenChannel(SelectedChannel *c)
ChannelSelection specifies the false-color channel selection for a multi-spectral raster source (such...
void paint(te::qt::widgets::Canvas *c, bool generatePNG, std::string fileName)