RasterSlicingWizardPage.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 //Terralib
21 #include "../../../dataaccess/utils/Utils.h"
22 #include "../../../raster/Band.h"
23 #include "../../../raster/Raster.h"
24 #include "../../../geometry/Utils.h"
25 #include "../../../rp/Functions.h"
26 #include "../../../maptools/Utils.h"
27 #include "../../../se/Categorize.h"
28 #include "../../../se/ColorMap.h"
29 #include "../../../se/CoverageStyle.h"
30 #include "../../../se/Utils.h"
31 #include "../../../se/ParameterValue.h"
32 #include "../../../common/STLUtils.h"
33 #include "../charts/ChartDisplay.h"
34 #include "../charts/ChartStyle.h"
35 #include "../charts/Histogram.h"
36 #include "../charts/HistogramChart.h"
37 #include "../charts/HistogramStyle.h"
38 #include "../charts/Utils.h"
39 #include "../se/SlicingColorMapWidget.h"
40 #include "../utils/ScopedCursor.h"
41 #include "../canvas/Canvas.h"
42 #include "../canvas/MapDisplay.h"
43 #include "../progress/ProgressViewerDialog.h"
45 #include "ui_SlicingColorMapForm.h"
46 #include "ui_RasterSlicingWizardPageForm.h"
47 
48 // QT
49 #include <QComboBox>
50 #include <QString>
51 #include <QMessageBox>
52 
54  QWizardPage(parent)
55 {
56  m_ui.reset(new Ui::RasterSlicingWizardPageForm);
57  m_mapDisplay = nullptr;
58 
59  m_ui->setupUi(this);
60  m_ui->m_histogramToolButton->setIcon(QIcon::fromTheme("chart-bar"));
61 
62  //configure page
63  this->setTitle(tr("Raster Slicing"));
64  this->setSubTitle(tr("Define the raster slicing parameters."));
65 
66  //Creating and adjusting the chart Display's style.
68  chartStyle->setTitle(tr(""));
69  chartStyle->setAxisX(tr(""));
70  chartStyle->setAxisY(tr(""));
71  chartStyle->setGridChecked(true);
72 
73  QGridLayout* displayLayout = new QGridLayout(m_ui->m_toolWidget);
74  m_tool.reset(new te::qt::widgets::RpToolsWidget(m_ui->m_toolWidget));
75  displayLayout->addWidget(m_tool.get());
76  displayLayout->setContentsMargins(0,0,0,0);
77 
78  //Creating and adjusting the chart Display
79  QGridLayout* chartLayout = new QGridLayout(m_ui->m_histWidget);
80 
81  m_chartDisplay = new te::qt::widgets::ChartDisplay(m_ui->m_histWidget, QString::fromUtf8(""), chartStyle);
83  m_chartDisplay->show();
84  m_chartDisplay->replot();
85 
86  chartLayout->addWidget(m_chartDisplay, 0, 0);
87  chartLayout->setContentsMargins(0, 0, 0, 0);
88 
91 
94 
95  QGridLayout* slicesWidgetLayout = new QGridLayout(m_ui->m_slicesWidget);
96 
97  slicesWidgetLayout->addWidget(m_colorMapWidget);
98  slicesWidgetLayout->setContentsMargins(0, 0, 0, 0);
99 
101  m_srid = 0;
102  m_ui->m_visibleAreaCheckBox->setEnabled(false);
103 
104  connect(m_ui->m_histogramToolButton, SIGNAL(clicked()), this, SLOT(updateHistogram()));
105  connect(m_ui->m_bandComboBox, SIGNAL(currentIndexChanged(int)), m_colorMapWidget, SLOT(onBandSelected(int)));
106  connect(m_colorMapWidget->getForm()->m_tableWidget, SIGNAL(cellChanged(int, int)), this, SIGNAL(completeChanged()));
107  connect(m_tool.get(), SIGNAL(envelopeAcquired(te::gm::Envelope)), this, SLOT(onEnvelopeAcquired(te::gm::Envelope)));
108 }
109 
111 
113 {
114  return m_colorMapWidget->getColorMap();
115 }
116 
118 {
119  return m_ui->m_bandComboBox->currentText().toUtf8().data();
120 }
121 
123 {
124  return m_extent;
125 }
126 
128 {
129  return m_srid;
130 }
131 
133 {
134  m_tool->setMapDisplay(mapDisplay);
135  m_mapDisplay = mapDisplay;
136 }
137 
139 {
140  m_tool->setActionGroup(actionGroup);
141  m_tool->enableBoxAction();
142 }
143 
145 {
146  bool useVisibleArea = m_ui->m_visibleAreaCheckBox->isChecked();
147  bool extValid = m_extent.isValid();
148  bool isWithinRaster = m_raster->getExtent()->contains(m_extent);
149 
150  if (useVisibleArea && extValid && isWithinRaster)
151  return true;
152  else
153  return false;
154 }
155 
157 {
158  m_rasterLayer = layer;
159 
160  //get input raster
161  std::unique_ptr<te::da::DataSet> ds = m_rasterLayer->getData();
162 
163  if (ds.get())
164  {
165  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
166 
167  std::unique_ptr<te::rst::Raster> inputRst = ds->getRaster(rpos);
168 
169  if (inputRst.get())
170  {
171  m_raster.reset(inputRst.release());
172 
174 
175  size_t bandsNumber = m_raster->getNumberOfBands();
176 
177  m_ui->m_bandComboBox->clear();
178  for (size_t band = 0; band < bandsNumber; ++band)
179  {
180  m_ui->m_bandComboBox->addItem(QString::number(band));
181  }
182  }
183  }
184 }
185 
187 {
188  m_extent = extent;
189 
190  if (m_extent.isValid())
191  m_ui->m_visibleAreaCheckBox->setEnabled(true);
192  else
193  m_ui->m_visibleAreaCheckBox->setEnabled(false);
194 }
195 
197 {
198  m_srid = srid;
199 }
200 
202 {
203  te::qt::widgets::ScopedCursor cursor(Qt::WaitCursor);
204 
205  std::unique_ptr<te::da::DataSet> dataset(m_rasterLayer->getData().release());
206  std::unique_ptr<te::da::DataSetType> dsType(m_rasterLayer->getSchema().release());
207 
208  if (dataset.get())
209  {
210  std::unique_ptr<te::rst::Raster> inputRst(te::map::GetRaster(m_rasterLayer.get()));
211 
212  if (inputRst.get())
213  {
214  int band = m_ui->m_bandComboBox->currentIndex();
215 
218  }
219  }
220 
221  m_chartDisplay->updateLayout();
222  m_chartDisplay->replot();
223 }
224 
226 {
227  m_ui->m_bandComboBox->setCurrentIndex(index);
228 }
229 
231 {
232  if(m_mapDisplay->getSRID() != m_rasterLayer->getSRID())
233  env.transform(m_mapDisplay->getSRID(), m_rasterLayer->getSRID());
234 
235  if (!env.isValid() || !env.intersects(m_rasterLayer->getExtent()))
236  return;
237 
239 
240  if(!m_geom->isValid())
241  return;
242 
244 
245  drawGeom();
246 }
247 
249  unsigned int& inputRasterBand, bool& eqHistogram)
250 {
251  inputRasterBand = (unsigned int)m_ui->m_bandComboBox->currentText().toUInt();
252  eqHistogram = m_ui->m_equalizeHistogramCheckBox->isChecked();
253 }
254 
256 {
257  if (m_colorMapWidget->getForm()->m_tableWidget->rowCount() > 0)
258  return true;
259  else
260  return false;
261 }
262 
264 {
265  if(!m_geom.get() || !m_geom->isValid())
266  return;
267 
268  const te::gm::Envelope& env = m_mapDisplay->getExtent();
269 
270  te::qt::widgets::Canvas* canvasInstance = new Canvas(m_mapDisplay->width(), m_mapDisplay->height());
271  canvasInstance->setDevice(m_mapDisplay->getDraftPixmap(), false);
272  canvasInstance->setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
273 
274  canvasInstance->setPolygonContourWidth(2);
275  canvasInstance->setPolygonContourColor(te::color::RGBAColor(100, 177, 216, TE_OPAQUE));
276 
277  canvasInstance->draw(m_geom.get());
278 
279  m_mapDisplay->repaint();
280 
281  delete canvasInstance;
282 }
283 
285 {
287 
288  canvasInstance.clear();
289 
290  m_mapDisplay->repaint();
291 }
292 
294 {
295  if (m_geom->getGeomTypeId() == te::gm::PolygonType)
296  {
297  QApplication::setOverrideCursor(Qt::WaitCursor);
298 
299  te::gm::Envelope* env = new te::gm::Envelope(*m_geom->getMBR());
300 
301  // verify if is necessary convert the raster to the given srid
302  bool needRemap = false;
303  if ((m_mapDisplay->getSRID() != TE_UNKNOWN_SRS) && (m_rasterLayer->getSRID() != TE_UNKNOWN_SRS) && (m_mapDisplay->getSRID() != m_rasterLayer->getSRID()))
304  needRemap = true;
305 
306  // create a SRS converter
307  std::unique_ptr<te::srs::Converter> converter(new te::srs::Converter());
308 
309  if (needRemap)
310  {
311  converter->setSourceSRID(m_mapDisplay->getSRID());
312  converter->setTargetSRID(m_rasterLayer->getSRID());
313 
314  converter->convert(env->getLowerLeftX(), env->getLowerLeftY(), env->m_llx, env->m_lly);
315  converter->convert(env->getUpperRightX(), env->getUpperRightY(), env->m_urx, env->m_ury);
316  }
317 
318  std::unique_ptr<te::rst::Raster> raster(te::map::GetRaster(m_rasterLayer.get()));
319 
320  std::map<std::string, std::string> info;
321  info["FORCE_MEM_DRIVER"] = "TRUE";
322  //execute clipping
323  m_rasterPreview.reset(raster->trim(env, info));
324 
325  applyPreview();
326 
327  QApplication::restoreOverrideCursor();
328  }
329 }
330 
332 {
333  QApplication::setOverrideCursor(Qt::WaitCursor);
334 
335  const te::gm::Envelope& envRaster = *raster->getExtent();
336  const te::gm::Envelope& env = m_mapDisplay->getExtent();
337 
338  std::unique_ptr<te::se::CoverageStyle> cs(dynamic_cast<te::se::CoverageStyle*>(
340  assert(cs);
341 
342  te::qt::widgets::Canvas* canvasInstance = new Canvas(m_mapDisplay->width(), m_mapDisplay->height());
343  canvasInstance->setDevice(m_mapDisplay->getDraftPixmap(), false);
344  canvasInstance->setWindow(env.m_llx, env.m_lly, env.m_urx, env.m_ury);
345 
346  canvasInstance->setPolygonContourWidth(2);
347  canvasInstance->setPolygonContourColor(te::color::RGBAColor(100, 177, 216, TE_OPAQUE));
348 
349  // Draw raster
350  bool cancel = false;
351  te::map::DrawRaster(raster, canvasInstance, envRaster, raster->getSRID(), env, m_mapDisplay->getSRID(), cs.get(), nullptr, m_mapDisplay->getScale(), &cancel);
352 
353  if (!m_geom->isValid())
354  return;
355 
356  canvasInstance->draw(m_geom.get());
357 
358  m_mapDisplay->repaint();
359 
360  delete canvasInstance;
361 
362  QApplication::restoreOverrideCursor();
363 }
364 
366 {
367  if (!m_rasterPreview)
368  return;
369 
370  unsigned int inputRasterBand = 0;
371  bool eqHistogram = false;
372  getParameters( inputRasterBand, eqHistogram );
373 
374  //progress
376 
377  // Execute slicing
378  QApplication::setOverrideCursor(Qt::WaitCursor);
379  std::unique_ptr< te::rst::Raster > outRasterPtr;
380 
381  std::map<std::string, std::string> rinfo;
382  rinfo["MEM_RASTER_NROWS"] = boost::lexical_cast<std::string>(m_rasterPreview->getNumberOfRows());
383  rinfo["MEM_RASTER_NCOLS"] = boost::lexical_cast<std::string>(m_rasterPreview->getNumberOfColumns());
384  rinfo["MEM_RASTER_DATATYPE"] = boost::lexical_cast<std::string>(m_rasterPreview->getBandDataType(0));
385  rinfo["MEM_RASTER_NBANDS"] = boost::lexical_cast<std::string>(m_rasterPreview->getNumberOfBands());
386 
387  //Creating the color palette
388  std::unique_ptr<te::se::ColorMap> colorMap(getColorMap());
389 
390  std::vector<te::se::ParameterValue*> values = colorMap->getCategorize()->getThresholds();
391  std::map<double, double> slice_limits;
392  for (size_t i = 0; i < values.size(); ++i)
393  {
394  std::string limits = te::se::GetString(values[i]);
395  size_t pos = limits.find(" ");
396  double min = std::stod(limits.substr(0, pos));
397  double max = std::stod(limits.substr(pos));
398  slice_limits.insert(std::pair< double, double >(min,max));
399  }
400 
401  std::vector<te::se::ParameterValue*> colors = colorMap->getCategorize()->getThresholdValues();
402 
403  std::vector< te::rst::BandProperty::ColorEntry > palette;
404 
405  for(size_t i = 0; i < colors.size(); ++i)
406  {
407  //Acquiring each color and inserting it in the palette
408  std::string colorName = te::se::GetString(colors[i]);
409  te::color::RGBAColor rgbac(colorName);
410 
412  color.c1 = rgbac.getRed();
413  color.c2 = rgbac.getGreen();
414  color.c3 = rgbac.getBlue();
415  color.c4 = rgbac.getAlpha();
416  palette.push_back(color);
417  }
418 
419  if( te::rp::RasterSlicing( *m_rasterPreview, inputRasterBand, true,
420  static_cast<int>(palette.size()), eqHistogram, rinfo,
421  "MEM", true, &palette, outRasterPtr, slice_limits ) )
422  {
423  drawPreview(outRasterPtr.get());
424 
425  QApplication::restoreOverrideCursor();
426 
427  }
428  else
429  {
430  QMessageBox::critical(this, tr("Raster slicing"), tr("Raster slicing execution error.") +
431  ( " " + te::rp::Module::getLastLogStr() ).c_str());
432 
433  te::common::FreeContents(colors);
434 
435  QApplication::restoreOverrideCursor();
436 
437  return;
438  }
439 }
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
te::map::AbstractLayerPtr m_rasterLayer
Testing a better way to show the chart.
void clear()
It clears the canvas content and fills with the background color.
unsigned int band
void makeInvalid()
It will invalidated the envelope.
void adjustDisplay()
Updates the general display settings according to the ChartStyle. The adjusted properties are: Title;...
te::qt::widgets::HistogramChart * m_histogramChart
The preview histogram chart.
void setDevice(QPaintDevice *device, bool takeOwnerShip)
It sets new device as QPrinter.
void setSRID(int srid=0)
This function sets the srid of the display when the wizard was initiated.
bool intersects(const Envelope &rhs) const
It returns true if the envelopes "spatially intersects".
void getParameters(unsigned int &inputRasterBand, bool &eqHistogram)
This function returns the parameters as configured by the user.
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:307
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
void attach(QwtPlot *plot)
It atttaches a QwtPlot to this Cahrt.
std::unique_ptr< Ui::RasterSlicingWizardPageForm > m_ui
The wizard page form.
TEQTWIDGETSEXPORT Histogram * createHistogram(te::map::AbstractLayerPtr layer, int propId, int slices, int stat, bool readall=true)
Histogram Creator.
bool RasterSlicing(const te::rst::Raster &inputRaster, const unsigned int inputRasterBand, const bool createPaletteRaster, const unsigned int slicesNumber, const bool eqHistogram, const std::map< std::string, std::string > &rasterInfo, const std::string &rasterType, const bool enableProgress, std::vector< te::rst::BandProperty::ColorEntry > const *const palettePtr, std::unique_ptr< te::rst::Raster > &outRasterPtr, std::map< double, double > limits)
Generate all wavelet planes from the given input raster.
void setGridChecked(bool newGridChecked)
It sets the boolean used to decided weather to display the grid or not.
Definition: ChartStyle.cpp:155
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
TESEEXPORT Style * CreateCoverageStyle(const std::vector< te::rst::BandProperty * > &properties)
Try creates an appropriate coverage style based on given band properties.
te::qt::widgets::MapDisplay * m_mapDisplay
The map display.
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:317
double m_urx
Upper right corner x-coordinate.
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:312
virtual double getScale() const
Calculates and return the current scale.
short c4
alpha or blackband.
Definition: BandProperty.h:75
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
void setPolygonContourColor(const te::color::RGBAColor &color)
It sets the pen color used to draw the boundary of polygon geometries.
A widget to control the display of a set of layers.
static te::dt::Date ds(2010, 01, 01)
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
std::unique_ptr< te::gm::Geometry > m_geom
The geometry.
A class to represent a Histogram.
Definition: Histogram.h:56
void draw(const te::gm::Geometry *geom)
It draws the geometry on canvas.
void applyPreview()
This function applies preview of raster slicing result.
void setTitle(QString newTitle)
It sets the style&#39;s title.
Definition: ChartStyle.cpp:71
static const std::string getLastLogStr()
Returns the last log string generated by this module.
te::gm::Envelope m_extent
The envelope that can be used to trim the output raster.
void setHistogram(te::qt::widgets::Histogram *newHistogram)
It sets the chart&#39;s histogram.
bool isComplete() const
This function evaluates the page to confirm if the user has completed the minimum configuration requi...
void clearCanvas()
This function cleans the canvas.
TEMAPEXPORT void DrawRaster(te::da::DataSetType *type, te::da::DataSourcePtr ds, Canvas *canvas, const te::gm::Envelope &bbox, int bboxSRID, const te::gm::Envelope &visibleArea, int srid, te::se::CoverageStyle *style, te::map::RasterContrast *rc, const double &scale, bool *cancel)
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).
double m_llx
Lower left corner x-coordinate.
std::unique_ptr< te::rst::Raster > m_rasterPreview
The raster used for preview.
An Envelope defines a 2D rectangular region.
void setExtent(const te::gm::Envelope &extent)
This function sets the envelope that will be used to trim tha raster if the user requests it...
An abstract class for raster data strucutures.
RasterSlicingWizardPage(QWidget *parent=0)
Constructor.
void setAxisY(QString newAxisY)
It sets the style&#39;s y axis label.
Definition: ChartStyle.cpp:101
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
std::string getCurrentBand()
This function returns the band from the raster taht will be used.
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
std::unique_ptr< te::rst::Raster > m_raster
The raster that will be sliced.
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:322
void setPolygonContourWidth(int w)
It sets the polygon contour width.
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
A class to represent a chart display.
Definition: ChartDisplay.h:65
void setLayer(te::map::AbstractLayerPtr layer)
This function sets the layer that contains the raster that will be sliced.
void setAxisX(QString newAxisX)
It sets the style&#39;s x axis label.
Definition: ChartStyle.cpp:91
double m_lly
Lower left corner y-coordinate.
std::unique_ptr< te::qt::widgets::RpToolsWidget > m_tool
Toolbar.
A Converter is responsible for the conversion of coordinates between different Coordinate Systems (CS...
Definition: Converter.h:53
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
bool trimRaster()
This function will check if the conditions required to trim the raster have been met.
int getSRID() const
Returns the raster spatial reference system identifier.
short c1
gray, red, cyan or hue.
Definition: BandProperty.h:72
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
double m_ury
Upper right corner y-coordinate.
te::se::ColorMap * getColorMap()
This function returns the Color Map generated by the user.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
void setMapDisplay(te::qt::widgets::MapDisplay *mapDisplay)
This function sets the map display.
This class is used to navigate over a DataSetLayer (having a raster representation) and given a set o...
Definition: RpToolsWidget.h:70
short c3
blue, yellow, or saturation.
Definition: BandProperty.h:74
te::qt::widgets::SlicingColorMapWidget * m_colorMapWidget
The widget used to edit the legend of the output layer.
te::qt::widgets::ChartDisplay * m_chartDisplay
The display used to draw the preview histogram.
An structure to represent a color tuple.
Definition: BandProperty.h:70
const te::gm::Envelope & getExtent()
This function returns the envelope that can be used to trim the raster if the user requests it...
int m_srid
The current srid from the display.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void transform(int oldsrid, int newsrid)
It will transform the coordinates of the Envelope from the old SRS to the new one.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
TESEEXPORT std::string GetString(const te::se::ParameterValue *param)
It gets the parameter value as a string.
void setActionGroup(QActionGroup *actionGroup)
This function sets the QActionGroup.
A ColorMap defines either the colors of a pallette-type raster source or the mapping of numeric pixel...
Definition: ColorMap.h:61
short c2
green, magenta, or lightness.
Definition: BandProperty.h:73
bool isValid() const
It tells if the rectangle is valid or not.
int getSRID()
This function returns the srid of the display when the wizard was initiated.
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
An object that when created shows a cursor during its scope.
Definition: ScopedCursor.h:48
void drawGeom()
This function draws geometry after the envelope was acquired.