DataSetDisplay.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 terralib/myqt/widgets/mapdisplay/DataSetDisplay.cpp
22 
23  \brief A map display for a dataset.
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/DataSet.h"
28 #include "../../../dataaccess/dataset/DataSetType.h"
29 #include "../../../dataaccess/datasource/DataSource.h"
30 #include "../../../dataaccess/datasource/DataSourceManager.h"
31 #include "../../../dataaccess/datasource/DataSourceTransactor.h"
32 #include "../../../dataaccess/utils/Utils.h"
33 #include "../../../geometry/Envelope.h"
34 #include "../../../geometry/Geometry.h"
35 #include "../../../geometry/GeometryProperty.h"
36 #include "../../../maptools/Utils.h"
37 #include "../../../se/CoverageStyle.h"
38 #include "../../../se/Utils.h"
39 #include "../canvas/Canvas.h"
40 #include "../Utils.h"
41 #include "DataSetDisplay.h"
42 
43 // STL
44 #include <cassert>
45 
46 // Qt
47 #include <QMessageBox>
48 #include <QResizeEvent>
49 
51  : QFrame(parent, f)
52 {
53 }
54 
56 
58 {
59  if(ds.get() == nullptr)
60  return;
61 
62  try
63  {
64  te::da::DataSourcePtr auxDs = te::da::DataSourceManager::getInstance().get(ds->getId(), ds->getType(), ds->getConnInfo());
65 
66  if(dataset->hasRaster())
67  {
68  std::unique_ptr<te::da::DataSourceTransactor> t = auxDs->getTransactor();
69 
70  draw(dataset, auxDs, t->getDataSet(dataset->getName()).get());
71  }
72  else
73  draw(dataset, auxDs);
74 
75  }
76  catch(const std::exception& e)
77  {
78  QMessageBox::warning(this,
79  tr("TerraLib Qt Components"),
80  tr(e.what()));
81  }
82  catch(...)
83  {
84  QMessageBox::warning(this,
85  tr("TerraLib Qt Components"),
86  tr("Unknown error when displaying dataset!"));
87  }
88 }
89 
91 {
92  if((dataset.get() == nullptr) || (ds.get() == nullptr))
93  return;
94 
95  if(m_canvas.get() == nullptr)
96  m_canvas.reset(new Canvas(width(), height()));
97 
98  if(dataset->size() == 0)
99  te::da::LoadProperties(dataset.get(), ds->getId());
100 
101  if(dataset->hasRaster())
102  {
103  std::size_t rpos = te::da::GetFirstPropertyPos(datasetData, te::dt::RASTER_TYPE);
104 
105  std::unique_ptr<te::rst::Raster> rst = datasetData->getRaster(rpos);
106 
107  te::gm::Envelope* mbr = rst->getExtent();
108 
109  if (mbr == nullptr)
110  return;
111 
112  m_canvas->calcAspectRatio(mbr);
113 
114  m_canvas->setWindow(mbr->getLowerLeftX(), mbr->getLowerLeftY(), mbr->getUpperRightX(), mbr->getUpperRightY());
115 
116  te::se::Style* style = te::se::CreateCoverageStyle(rst->getNumberOfBands());
117  te::se::CoverageStyle* cs = dynamic_cast<te::se::CoverageStyle*>(style);
118 
119  te::gm::Envelope* rstEnv = rst->getExtent();
120  int rstSrid = rst->getSRID();
121 
122  bool cancel = false;
123 
124  te::map::DrawRaster(rst.get(), m_canvas.get(), *rstEnv, rstSrid, *rstEnv, rstSrid, cs, nullptr, 0, &cancel);
125  }
126  else if(dataset->hasGeom())
127  {
128 
130 
131  std::unique_ptr<te::gm::Envelope> mbr(te::da::GetExtent(dataset->getName(), gp->getName(), ds->getId()));
132 
133  if(mbr.get() == nullptr)
134  return;
135 
136  m_canvas->calcAspectRatio(mbr.get());
137 
138  m_canvas->setWindow(mbr->getLowerLeftX(), mbr->getLowerLeftY(), mbr->getUpperRightX(), mbr->getUpperRightY());
139 
140  switch(gp->getGeometryType())
141  {
142  case te::gm::PolygonType:
150  {
151  Config2DrawPolygons(m_canvas.get(), Qt::red, Qt::black);
152  }
153  break;
154 
163  {
164  Config2DrawLines(m_canvas.get(), Qt::black);
165  }
166  break;
167 
168  case te::gm::PointType:
169  case te::gm::PointZType:
170  case te::gm::PointMType:
171  case te::gm::PointZMType:
176  {
177  Config2DrawPoints(m_canvas.get(), "circle", 1, Qt::black, Qt::transparent, 1);
178  }
179  break;
180 
181  default:
182  break;
183  }
184 
185  if(datasetData)
186  {
187  std::size_t gpos = te::da::GetFirstPropertyPos(datasetData, te::dt::GEOMETRY_TYPE);
188 
189  while(datasetData->moveNext())
190  {
191  if (datasetData->isNull(gpos))
192  continue;
193 
194  std::unique_ptr<te::gm::Geometry> g(datasetData->getGeometry(gpos));
195 
196  if (g.get())
197  m_canvas->draw(g.get());
198  }
199  }
200  else
201  {
202  std::unique_ptr<te::da::DataSet> feature(ds->getDataSet(dataset->getName()));
203 
204  std::size_t gpos = te::da::GetFirstPropertyPos(feature.get(), te::dt::GEOMETRY_TYPE);
205 
206  while(feature->moveNext())
207  {
208  if (feature->isNull(gpos))
209  continue;
210 
211  std::unique_ptr<te::gm::Geometry> g(feature->getGeometry(gpos));
212 
213  if (g.get())
214  m_canvas->draw(g.get());
215  }
216  }
217  }
218  else
219  {
220  m_canvas->setBackgroundColor(te::color::RGBAColor(0, 0, 0, 255)); // fill black
221  }
222 
223  repaint();
224 }
225 
227 {
228  if(m_canvas.get() == nullptr)
229  m_canvas.reset(new Canvas(width(), height()));
230 
231  m_canvas->setBackgroundColor(te::color::RGBAColor(255, 255, 255, 0));
232  m_canvas->clear();
233 
234  repaint();
235 }
236 
238 {
239  if(m_canvas.get() == nullptr)
240  return;
241 
242  QPainter painter(this);
243 
244  painter.drawPixmap(0, 0, *(m_canvas->getPixmap()));
245 
246  painter.end();
247 
248 }
249 
251 {
252  assert(e);
253 
254  if(m_canvas.get() == nullptr)
255  return;
256 
257  m_canvas->resize(e->size().width(), e->size().height());
258 
259  QFrame::resizeEvent(e);
260 }
261 
virtual std::unique_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
Geometric property.
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
boost::shared_ptr< DataSource > DataSourcePtr
virtual std::unique_ptr< te::rst::Raster > getRaster(std::size_t i) const =0
Method for retrieving a raster attribute value.
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
Definition: CoverageStyle.h:45
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.
TEQTWIDGETSEXPORT void Config2DrawLines(te::map::Canvas *canvas, const QColor &color, const std::size_t &width=1)
It configs (i.e. prepares) the given canvas to draw lines.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
void draw(const te::da::DataSetTypePtr &dataset, const te::da::DataSourcePtr &ds, te::da::DataSet *datasetData=0)
DataSetDisplay(QWidget *parent=0, Qt::WindowFlags f=0)
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.
void draw(const te::da::DataSetTypePtr &dataset, const te::da::DataSourceInfoPtr &ds)
TEQTWIDGETSEXPORT void Config2DrawPolygons(te::map::Canvas *canvas, const QColor &fillColor, const QColor &contourColor, const std::size_t &contourWidth=1)
It configs (i.e. prepares) the given canvas to draw polygons.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
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)
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
TEQTWIDGETSEXPORT void Config2DrawPoints(te::map::Canvas *canvas, const QColor &color, const std::size_t &width=1)
It configs (i.e. prepares) the given canvas to draw points.
A dataset is the unit of information manipulated by the data access module of TerraLib.
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 resizeEvent(QResizeEvent *e)
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
std::unique_ptr< Canvas > m_canvas
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
const std::string & getName() const
It returns the property name.
Definition: Property.h:127