All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 {
60 }
61 
63 {
64  if(e->button() != Qt::LeftButton)
65  return false;
66 
67  // Converts clicked point to world coordinates
68 #if QT_VERSION >= 0x050000
69  QPointF qpoint = m_display->transform(e->localPos());
70 #else
71  QPointF qpoint = m_display->transform(e->posF());
72 #endif
73 
74  te::map::DataSetLayer* dsL = dynamic_cast<te::map::DataSetLayer*>(m_layer.get());
75 
76  if(dsL == 0)
77  return false;
78 
79  // * Under revision *
80  te::se::CoverageStyle* cs = dynamic_cast<te::se::CoverageStyle*>(dsL->getStyle());
81  assert(cs);
82 
83 // get the raster symbolizer
84  std::size_t nRules = cs->getRules().size();
85  assert(nRules >= 1);
86 
87 // for while, consider one rule
88  const te::se::Rule* rule = cs->getRule(0);
89 
90  const std::vector<te::se::Symbolizer*>& symbolizers = rule->getSymbolizers();
91  assert(!symbolizers.empty());
92 
93 // for while, consider one raster symbolizer
94  te::se::RasterSymbolizer* rs = dynamic_cast<te::se::RasterSymbolizer*>(symbolizers[0]);
95  assert(rs);
96 
97  //get input raster
98  std::auto_ptr<te::da::DataSet> ds = m_layer->getData();
99 
100  if(ds.get())
101  {
102  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
103  te::rst::Raster* inputRst = ds->getRaster(rpos).get();
104 
105  if(inputRst)
106  {
107 
108  //create the raster transform
109  te::map::RasterTransform rt(inputRst, 0);
110 
111  double rMin, rMax;
112 
115  const std::complex<double>* cmin = rsMin->at(0).m_minVal;
116  const std::complex<double>* cmax = rsMax->at(0).m_maxVal;
117  rMin = cmin->real();
118  rMax = cmax->real();
119 
120  rt.setLinearTransfParameters(0, 255, rMin, rMax);
121 
122  //configure the raster transformation using the raster symbolizer
124 
125  rtc.configure();
126 
127  te::gm::Coord2D coord = inputRst->getGrid()->geoToGrid(qpoint.x(), qpoint.y());
128 
129  int x = te::rst::Round(coord.x);
130  int y = te::rst::Round(coord.y);
131 
132  if((x >=0 && x < (int)inputRst->getNumberOfColumns()) && (y >=0 && y < (int)inputRst->getNumberOfRows()))
133  {
134  QString information("<h2>Read Pixel</h2><ul>");
135 
136  information += "<h3>Location</h3>";
137 
138  QString column, line, xCoord, yCoord;
139  column.setNum(x);
140  line.setNum(y);
141  xCoord.setNum(qpoint.x());
142  yCoord.setNum(qpoint.y());
143 
144  information += "<li><b>" + QString("Line ") + ":</b> " + line + "</li>";
145  information += "<li><b>" + QString("Column ") + ":</b> " + column + "</li>";
146  information += "<li><b>" + QString("Coord X ") + ":</b> " + xCoord + "</li>";
147  information += "<li><b>" + QString("Coord Y ") + ":</b> " + yCoord + "</li>";
148 
149  information += "<h3>Original Values</h3>";
150 
151  for(size_t t = 0; t < inputRst->getNumberOfBands(); ++t )
152  {
153  double val;
154  inputRst->getValue(x, y, val, t);
155 
156  QString band, value;
157  band.setNum(t);
158  value.setNum(val);
159 
160  information += "<li><b>" + QString("Value for band ") + band + ":</b> " + value + "</li>";
161 
162  }
163 
164  te::color::RGBAColor color = rt.apply(x, y);
165 
166  information += "<h3>Transformed Values</h3>";
167 
168  QString red, green, blue, alpha;
169  red.setNum(color.getRed());
170  green.setNum(color.getGreen());
171  blue.setNum(color.getBlue());
172  alpha.setNum(color.getAlpha());
173 
174  QString rBand, gBand, bBand;
178 
179  information += "<li><b>" + QString("Red Channel: </b>") + rBand + "<b> - Value:</b> " + red + "</li>";
180  information += "<li><b>" + QString("Green Channel: </b>") + gBand + "<b> - Value:</b> " + green + "</li>";
181  information += "<li><b>" + QString("Blue Channel: </b>") + bBand + "<b> - Value:</b> " + blue + "</li>";
182  information += "<li><b>" + QString("Alpha Channel value") + ":</b> " + alpha + "</li>";
183 
184 
185  information += "<h3>Symbolizer</h3>";
186 
187  QString type;
188 
189  if(rs->getChannelSelection()->getColorCompositionType() == te::se::RGB_COMPOSITION)
190  {
191  type = "RGB Composition";
192  }
193  else if(rs->getChannelSelection()->getColorCompositionType() == te::se::GRAY_COMPOSITION)
194  {
195  type = "Gray Composition";
196  }
197  else
198  {
199  type = "Unknown Composition";
200  }
201 
202  information += "<li><b>" + QString("Color Composition ") + ":</b> " + type + "</li>";
203 
204  if(rs->getOpacity())
205  {
206  QString opacity = te::se::GetString(rs->getOpacity()).c_str();
207  information += "<li><b>" + QString("Opacity Value ") + ":</b> " + opacity + "</li>";
208  }
209 
210  if(rs->getGain())
211  {
212  QString gain = te::se::GetString(rs->getGain()).c_str();
213  information += "<li><b>" + QString("Gain Value ") + ":</b> " + gain + "</li>";
214  }
215 
216  if(rs->getOffset())
217  {
218  QString offset = te::se::GetString(rs->getOffset()).c_str();
219  information += "<li><b>" + QString("Offset Value ") + ":</b> " + offset + "</li>";
220  }
221 
222  if(rs->getChannelSelection()->getRedChannel() &&
223  rs->getChannelSelection()->getRedChannel()->getContrastEnhancement())
224  {
225  QString c;
226  c.setNum(rs->getChannelSelection()->getRedChannel()->getContrastEnhancement()->getGammaValue());
227 
228  information += "<li><b>" + QString("Red Contrast") + ":</b> " + c + "</li>";
229  }
230 
231  if(rs->getChannelSelection()->getGreenChannel() &&
232  rs->getChannelSelection()->getGreenChannel()->getContrastEnhancement())
233  {
234  QString c;
235  c.setNum(rs->getChannelSelection()->getGreenChannel()->getContrastEnhancement()->getGammaValue());
236 
237  information += "<li><b>" + QString("Green Contraste") + ":</b> " + c + "</li>";
238  }
239 
240  if(rs->getChannelSelection()->getBlueChannel() &&
241  rs->getChannelSelection()->getBlueChannel()->getContrastEnhancement())
242  {
243  QString c;
244  c.setNum(rs->getChannelSelection()->getBlueChannel()->getContrastEnhancement()->getGammaValue());
245 
246  information += "<li><b>" + QString("Blue Contraste") + ":</b> " + c + "</li>";
247  }
248 
249  if(rs->getChannelSelection()->getGrayChannel() &&
250  rs->getChannelSelection()->getGrayChannel()->getContrastEnhancement())
251  {
252  QString c;
253  c.setNum(rs->getChannelSelection()->getGrayChannel()->getContrastEnhancement()->getGammaValue());
254 
255  information += "<li><b>" + QString("Gray Contraste") + ":</b> " + c + "</li>";
256  }
257 
258  information += "</ul>";
259 
260  // Show attributes
261  QToolTip::showText(QCursor::pos(), information, m_display, m_display->rect());
262  }
263  }
264  }
265 
266  return true;
267 }
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:295
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:305
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:300
A widget to control the display of a set of layers.
Definition: MapDisplay.h:66
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
This class defines an interface for objects that can receive application events and respond to them...
Definition: AbstractTool.h:62
An example of MapDisplay Tool. The only purpose of this tool is to show how you can implement a new t...
void setLinearTransfParameters(double vmin, double vmax, double rmin, double rmax)
Set parameters of linear transformation.
Rule * getRule(std::size_t i) const
Definition: Style.cpp:105
const std::vector< Rule * > & getRules() const
Definition: Style.cpp:94
ReadPixelTool(te::qt::widgets::MapDisplay *display, te::map::AbstractLayerPtr layer, QObject *parent=0)
static RasterSummaryManager & getInstance()
It returns a reference to the singleton instance.
TERASTEREXPORT int Round(double val)
Round a double value to a integer value.
Definition: Utils.cpp:298
te::map::AbstractLayerPtr m_layer
Definition: ReadPixelTool.h:68
An abstract class for raster data strucutures.
Definition: Raster.h:71
virtual te::se::Style * getStyle() const
It returns the Style associated to the layer.
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
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
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:78
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.
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
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)
Definition: Utils.cpp:481
Calculate the min value.
Definition: Enums.h:40
Calculate the max value.
Definition: Enums.h:41
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.
Definition: Utils.cpp:453