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