src/terralib/qt/widgets/tools/ReadPixelTool.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 ReadPixel.cpp
22 
23  \brief An example of MapDisplay Tool. The only purpose of this tool is to show how you can implement a new tool. Do not consider it as a final application.
24 */
25 
26 // TerraLib
27 #include "../../../common/STLUtils.h"
28 #include "../../../dataaccess/dataset/DataSet.h"
29 #include "../../../dataaccess/utils/Utils.h"
30 #include "../../../geometry.h"
31 #include "../../../maptools/DataSetLayer.h"
32 #include "../../../maptools/RasterTransform.h"
33 #include "../../../maptools/RasterTransformConfigurer.h"
34 #include "../../../qt/widgets/canvas/Canvas.h"
35 #include "../../../qt/widgets/canvas/MapDisplay.h"
36 #include "../../../raster/Raster.h"
37 #include "../../../raster/RasterSummary.h"
38 #include "../../../raster/RasterSummaryManager.h"
39 #include "../../../raster/Utils.h"
40 #include "../../../se/Utils.h"
41 #include "../../../se.h"
42 #include "ReadPixelTool.h"
43 
44 // Qt
45 #include <QMouseEvent>
46 #include <QToolTip>
47 
48 // STL
49 #include <cassert>
50 
52  : te::qt::widgets::AbstractTool(display, parent)
53 {
54  assert(layer);
55  m_layer = layer;
56 }
57 
59 
61 {
62  if(e->button() != Qt::LeftButton)
63  return false;
64 
65  // Converts clicked point to world coordinates
66 #if QT_VERSION >= 0x050000
67  QPointF qpoint = m_display->transform(e->localPos());
68 #else
69  QPointF qpoint = m_display->transform(e->posF());
70 #endif
71 
72  te::map::DataSetLayer* dsL = dynamic_cast<te::map::DataSetLayer*>(m_layer.get());
73 
74  if(dsL == nullptr)
75  return false;
76 
77  // * Under revision *
78  te::se::CoverageStyle* cs = dynamic_cast<te::se::CoverageStyle*>(dsL->getStyle());
79  assert(cs);
80 
81 // for while, consider one rule
82  const te::se::Rule* rule = cs->getRule(0);
83 
84  const std::vector<te::se::Symbolizer*>& symbolizers = rule->getSymbolizers();
85  assert(!symbolizers.empty());
86 
87 // for while, consider one raster symbolizer
88  te::se::RasterSymbolizer* rs = dynamic_cast<te::se::RasterSymbolizer*>(symbolizers[0]);
89  assert(rs);
90 
91  //get input raster
92  std::unique_ptr<te::da::DataSet> ds = m_layer->getData();
93 
94  if(ds.get())
95  {
96  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
97  te::rst::Raster* inputRst = ds->getRaster(rpos).get();
98 
99  if(inputRst)
100  {
101 
102  //create the raster transform
103  te::map::RasterTransform rt(inputRst, nullptr);
104 
105  double rMin, rMax;
106 
109  const std::complex<double>* cmin = rsMin->at(0).m_minVal;
110  const std::complex<double>* cmax = rsMax->at(0).m_maxVal;
111  rMin = cmin->real();
112  rMax = cmax->real();
113 
114  rt.setLinearTransfParameters(0, 255, rMin, rMax);
115 
116  //configure the raster transformation using the raster symbolizer
118 
119  rtc.configure();
120 
121  te::gm::Coord2D coord = inputRst->getGrid()->geoToGrid(qpoint.x(), qpoint.y());
122 
123  int x = te::rst::Round(coord.x);
124  int y = te::rst::Round(coord.y);
125 
126  if((x >=0 && x < (int)inputRst->getNumberOfColumns()) && (y >=0 && y < (int)inputRst->getNumberOfRows()))
127  {
128  QString information("<h2>Read Pixel</h2><ul>");
129 
130  information += "<h3>Location</h3>";
131 
132  QString column, line, xCoord, yCoord;
133  column.setNum(x);
134  line.setNum(y);
135  xCoord.setNum(qpoint.x());
136  yCoord.setNum(qpoint.y());
137 
138  information += "<li><b>" + QString("Line ") + ":</b> " + line + "</li>";
139  information += "<li><b>" + QString("Column ") + ":</b> " + column + "</li>";
140  information += "<li><b>" + QString("Coord X ") + ":</b> " + xCoord + "</li>";
141  information += "<li><b>" + QString("Coord Y ") + ":</b> " + yCoord + "</li>";
142 
143  information += "<h3>Original Values</h3>";
144 
145  for(size_t t = 0; t < inputRst->getNumberOfBands(); ++t )
146  {
147  double val;
148  inputRst->getValue(x, y, val, t);
149 
150  QString band, value;
151  band.setNum(t);
152  value.setNum(val);
153 
154  information += "<li><b>" + QString("Value for band ") + band + ":</b> " + value + "</li>";
155 
156  }
157 
158  te::color::RGBAColor color = rt.apply(x, y);
159 
160  information += "<h3>Transformed Values</h3>";
161 
162  QString red, green, blue, alpha;
163  red.setNum(color.getRed());
164  green.setNum(color.getGreen());
165  blue.setNum(color.getBlue());
166  alpha.setNum(color.getAlpha());
167 
168  QString rBand, gBand, bBand;
172 
173  information += "<li><b>" + QString("Red Channel: </b>") + rBand + "<b> - Value:</b> " + red + "</li>";
174  information += "<li><b>" + QString("Green Channel: </b>") + gBand + "<b> - Value:</b> " + green + "</li>";
175  information += "<li><b>" + QString("Blue Channel: </b>") + bBand + "<b> - Value:</b> " + blue + "</li>";
176  information += "<li><b>" + QString("Alpha Channel value") + ":</b> " + alpha + "</li>";
177 
178 
179  information += "<h3>Symbolizer</h3>";
180 
181  QString type;
182 
183  if(rs->getChannelSelection()->getColorCompositionType() == te::se::RGB_COMPOSITION)
184  {
185  type = "RGB Composition";
186  }
187  else if(rs->getChannelSelection()->getColorCompositionType() == te::se::GRAY_COMPOSITION)
188  {
189  type = "Gray Composition";
190  }
191  else
192  {
193  type = "Unknown Composition";
194  }
195 
196  information += "<li><b>" + QString("Color Composition ") + ":</b> " + type + "</li>";
197 
198  if(rs->getOpacity())
199  {
200  QString opacity = te::se::GetString(rs->getOpacity()).c_str();
201  information += "<li><b>" + QString("Opacity Value ") + ":</b> " + opacity + "</li>";
202  }
203 
204  if(rs->getGain())
205  {
206  QString gain = te::se::GetString(rs->getGain()).c_str();
207  information += "<li><b>" + QString("Gain Value ") + ":</b> " + gain + "</li>";
208  }
209 
210  if(rs->getOffset())
211  {
212  QString offset = te::se::GetString(rs->getOffset()).c_str();
213  information += "<li><b>" + QString("Offset Value ") + ":</b> " + offset + "</li>";
214  }
215 
216  if(rs->getChannelSelection()->getRedChannel() &&
217  rs->getChannelSelection()->getRedChannel()->getContrastEnhancement())
218  {
219  QString c;
220  c.setNum(rs->getChannelSelection()->getRedChannel()->getContrastEnhancement()->getGammaValue());
221 
222  information += "<li><b>" + QString("Red Contrast") + ":</b> " + c + "</li>";
223  }
224 
225  if(rs->getChannelSelection()->getGreenChannel() &&
226  rs->getChannelSelection()->getGreenChannel()->getContrastEnhancement())
227  {
228  QString c;
229  c.setNum(rs->getChannelSelection()->getGreenChannel()->getContrastEnhancement()->getGammaValue());
230 
231  information += "<li><b>" + QString("Green Contraste") + ":</b> " + c + "</li>";
232  }
233 
234  if(rs->getChannelSelection()->getBlueChannel() &&
235  rs->getChannelSelection()->getBlueChannel()->getContrastEnhancement())
236  {
237  QString c;
238  c.setNum(rs->getChannelSelection()->getBlueChannel()->getContrastEnhancement()->getGammaValue());
239 
240  information += "<li><b>" + QString("Blue Contraste") + ":</b> " + c + "</li>";
241  }
242 
243  if(rs->getChannelSelection()->getGrayChannel() &&
244  rs->getChannelSelection()->getGrayChannel()->getContrastEnhancement())
245  {
246  QString c;
247  c.setNum(rs->getChannelSelection()->getGrayChannel()->getContrastEnhancement()->getGammaValue());
248 
249  information += "<li><b>" + QString("Gray Contraste") + ":</b> " + c + "</li>";
250  }
251 
252  information += "</ul>";
253 
254  // Show attributes
255  QToolTip::showText(QCursor::pos(), information, m_display, m_display->rect());
256  }
257  }
258  }
259 
260  return true;
261 }
unsigned int band
double y
y-coordinate.
Definition: Coord2D.h:114
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:307
double x
x-coordinate.
Definition: Coord2D.h:113
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
Definition: CoverageStyle.h:45
void configure()
Configure Transformation.
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:317
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:312
A widget to control the display of a set of layers.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
static te::dt::Date ds(2010, 01, 01)
This class defines an interface for objects that can receive application events and respond to them...
Definition: AbstractTool.h:63
void setLinearTransfParameters(double vmin, double vmax, double rmin, double rmax)
Set parameters of linear transformation.
unsigned int line
Rule * getRule(std::size_t i) const
Definition: Style.cpp:105
virtual QPointF transform(const QPointF &p)
Transforms the given point, in screen coordinates, to a point in world coordinates.
static RasterSummaryManager & getInstance()
It returns a reference to the singleton instance.
An abstract class for raster data strucutures.
virtual te::se::Style * getStyle() const
It returns the Style associated to the layer.
URI C++ Library.
Definition: Attributes.h:37
TERASTEREXPORT int Round(double val)
Round a double value to a integer value.
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:322
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
Definition: RasterSummary.h:44
The RasterSymbolizer describes how to render raster/matrix-coverage data (e.g., satellite photos...
const std::vector< Symbolizer * > & getSymbolizers() const
Definition: Rule.cpp:158
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
void apply(double icol, double ilin, double ocol, double olin)
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:76
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
A Raster Transform configurer generates a Raster Transform given a RasterSymbolzier.
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
A Raster Transform is a class that defines functions to transform a styled raster.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Calculate the min value.
Calculate the max value.
std::map< RGBChannels, short > & getRGBMap()
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
TESEEXPORT std::string GetString(const te::se::ParameterValue *param)
It gets the parameter value as a string.
ReadPixelTool(te::qt::widgets::MapDisplay *display, te::map::AbstractLayerPtr layer, QObject *parent=0)