All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ClippingWizardPage.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/qt/widgets/rp/ClippingWizardPage.cpp
22 
23  \brief This file defines a class for a Clipping Wizard page.
24 */
25 
26 // TerraLib
27 #include "../../../common/StringUtils.h"
28 #include "../../../dataaccess/dataset/DataSet.h"
29 #include "../../../dataaccess/dataset/ObjectIdSet.h"
30 #include "../../../dataaccess/utils/Utils.h"
31 #include "../../../geometry/Geometry.h"
32 #include "../../../geometry/GeometryProperty.h"
33 #include "../../../geometry/GeometryCollection.h"
34 #include "../../../geometry/LinearRing.h"
35 #include "../../../geometry/Polygon.h"
36 #include "../../../geometry/Utils.h"
37 #include "../../../raster/Grid.h"
38 #include "../../../raster/Raster.h"
39 #include "../canvas/Canvas.h"
40 #include "../canvas/MapDisplay.h"
41 #include "RasterNavigatorWidget.h"
42 #include "ClippingWizardPage.h"
43 #include "ui_ClippingWizardPageForm.h"
44 
45 // Qt
46 #include <QGridLayout>
47 #include <QCheckBox>
48 #include <QDoubleValidator>
49 #include <QIntValidator>
50 #include <QMessageBox>
51 
52 // STL
53 #include <memory>
54 
56 
58  : QWizardPage(parent),
59  m_ui(new Ui::ClippingWizardPageForm),
60  m_layer(0)
61 {
62 // setup controls
63  m_ui->setupUi(this);
64 
66 
67 //build form
68  QGridLayout* displayLayout = new QGridLayout(m_ui->m_frame);
70  m_navigator->showAsPreview(false, true);
71  m_navigator->hideColorCompositionTool(true);
72  m_navigator->hideGeomTool(true);
73  m_navigator->hideInfoTool(true);
74  m_navigator->hidePickerTool(true);
75  displayLayout->addWidget(m_navigator.get());
76  displayLayout->setContentsMargins(0,0,0,0);
77 
78  connect(m_navigator.get(), SIGNAL(envelopeAcquired(te::gm::Envelope)), this, SLOT(onEnvelopeAcquired(te::gm::Envelope)));
79  connect(m_navigator.get(), SIGNAL(mapDisplayExtentChanged()), this, SLOT(drawGeom()));
80 
81 //configure page
82  this->setTitle(tr("Clipping"));
83  this->setSubTitle(tr("Select the type of clipping and set their specific parameters."));
84 
85  m_ui->m_llxLineEdit->setValidator(new QDoubleValidator(this));
86  m_ui->m_llyLineEdit->setValidator(new QDoubleValidator(this));
87  m_ui->m_urxLineEdit->setValidator(new QDoubleValidator(this));
88  m_ui->m_uryLineEdit->setValidator(new QDoubleValidator(this));
89 
90  m_ui->m_startRowLineEdit->setValidator(new QIntValidator(this));
91  m_ui->m_startColumnLineEdit->setValidator(new QIntValidator(this));
92  m_ui->m_widthLineEdit->setValidator(new QIntValidator(this));
93  m_ui->m_heightLineEdit->setValidator(new QIntValidator(this));
94 
95 //connects
96  connect(m_ui->m_strategyTypeComboBox, SIGNAL(activated(int)), this, SLOT(onStrategyTypeComboBoxActivated(int)));
97  connect(m_ui->m_strategyTypeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onStrategyTypeComboBoxChanged(int)));
98  connect(m_ui->m_layerComboBox, SIGNAL(activated(int)), this, SLOT(onLayerComboBoxActivated(int)));
99  connect(m_ui->m_layerComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onLayerComboBoxChanged(int)));
100 
101  connect(m_ui->m_llxLineEdit, SIGNAL(editingFinished()), this, SIGNAL(completeChanged()));
102  connect(m_ui->m_llyLineEdit, SIGNAL(editingFinished()), this, SIGNAL(completeChanged()));
103  connect(m_ui->m_urxLineEdit, SIGNAL(editingFinished()), this, SIGNAL(completeChanged()));
104  connect(m_ui->m_uryLineEdit, SIGNAL(editingFinished()), this, SIGNAL(completeChanged()));
105 
106  connect(m_ui->m_startRowLineEdit, SIGNAL(editingFinished()), this, SIGNAL(completeChanged()));
107  connect(m_ui->m_startColumnLineEdit, SIGNAL(editingFinished()), this, SIGNAL(completeChanged()));
108  connect(m_ui->m_widthLineEdit, SIGNAL(editingFinished()), this, SIGNAL(completeChanged()));
109  connect(m_ui->m_heightLineEdit, SIGNAL(editingFinished()), this, SIGNAL(completeChanged()));
110 }
111 
113 {
114 }
115 
117 {
118  int index = m_ui->m_strategyTypeComboBox->currentIndex();
119  int type = m_ui->m_strategyTypeComboBox->itemData(index).toInt();
120 
121  if(type == CLIPPING_EXTENT)
122  {
123  if(m_ui->m_llxLineEdit->text().isEmpty() ||
124  m_ui->m_llyLineEdit->text().isEmpty() ||
125  m_ui->m_urxLineEdit->text().isEmpty() ||
126  m_ui->m_uryLineEdit->text().isEmpty()
127  )
128  return false;
129 
130  return true;
131  }
132  else if(type == CLIPPING_DIMENSION)
133  {
134  if(m_ui->m_startRowLineEdit->text().isEmpty() ||
135  m_ui->m_startColumnLineEdit->text().isEmpty() ||
136  m_ui->m_widthLineEdit->text().isEmpty() ||
137  m_ui->m_heightLineEdit->text().isEmpty()
138  )
139  return false;
140 
141  return true;
142  }
143  else if(type == CLIPPING_LAYER)
144  {
145  if(m_ui->m_layerComboBox->currentText().isEmpty())
146  return false;
147 
148  return true;
149  }
150 
151  return false;
152 }
153 
155 {
156  m_layer = layer;
157 
158  std::list<te::map::AbstractLayerPtr> list;
159 
160  list.push_back(m_layer);
161 
162  m_navigator->set(m_layer);
163 }
164 
165 void te::qt::widgets::ClippingWizardPage::setList(std::list<te::map::AbstractLayerPtr>& layerList)
166 {
167  std::list<te::map::AbstractLayerPtr>::iterator it = layerList.begin();
168 
169  while(it != layerList.end())
170  {
172 
173  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
174 
175  if(dsType->hasGeom())
176  m_ui->m_layerComboBox->addItem(l->getTitle().c_str(), QVariant::fromValue(l));
177 
178  ++it;
179  }
180 
181  if(m_ui->m_layerComboBox->count() > 0)
182  onLayerComboBoxActivated(0);
183 }
184 
186 {
187  return m_layer;
188 }
189 
191 {
192  int index = m_ui->m_strategyTypeComboBox->currentIndex();
193  int type = m_ui->m_strategyTypeComboBox->itemData(index).toInt();
194 
195  return type == CLIPPING_EXTENT;
196 }
197 
199 {
200  int index = m_ui->m_strategyTypeComboBox->currentIndex();
201  int type = m_ui->m_strategyTypeComboBox->itemData(index).toInt();
202 
203  return type == CLIPPING_DIMENSION;
204 }
205 
207 {
208  int index = m_ui->m_strategyTypeComboBox->currentIndex();
209  int type = m_ui->m_strategyTypeComboBox->itemData(index).toInt();
210 
211  return type == CLIPPING_LAYER;
212 }
213 
215 {
216  if(m_ui->m_aggregAllRadioButton->isChecked())
217  return true;
218  if(m_ui->m_groupByRadioButton->isChecked())
219  return false;
220  if(m_ui->m_selectedGeomRadioButton->isChecked())
221  return true;
222 
223  return true;
224 }
225 
227 {
228  env.m_llx = m_ui->m_llxLineEdit->text().toDouble();
229  env.m_lly = m_ui->m_llyLineEdit->text().toDouble();
230  env.m_urx = m_ui->m_urxLineEdit->text().toDouble();
231  env.m_ury = m_ui->m_uryLineEdit->text().toDouble();
232 }
233 
234 void te::qt::widgets::ClippingWizardPage::getDimensionClipping(int& x, int& y, int& width, int& height)
235 {
236  x = m_ui->m_startColumnLineEdit->text().toInt();
237  y = m_ui->m_startRowLineEdit->text().toInt();
238  width = m_ui->m_widthLineEdit->text().toInt();
239  height = m_ui->m_heightLineEdit->text().toInt();
240 }
241 
243  std::auto_ptr< te::gm::GeometryCollection >& geomColl)
244 {
245  //get layer
246  int index = m_ui->m_layerComboBox->currentIndex();
247  QVariant varLayer = m_ui->m_layerComboBox->itemData(index, Qt::UserRole);
248  te::map::AbstractLayerPtr layer = varLayer.value<te::map::AbstractLayerPtr>();
249 
250  //get geometry property data
251  std::auto_ptr<te::da::DataSetType> dsType = layer->getSchema();
252  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(dsType.get());
253  std::string name = geomProp->getName();
254 
255  geomColl.reset( new te::gm::GeometryCollection(0, geomProp->getGeometryType(), geomProp->getSRID()) );
256 
257  if(m_ui->m_aggregAllRadioButton->isChecked())
258  {
259  std::auto_ptr<te::da::DataSet> ds = layer->getData();
260  ds->moveBeforeFirst();
261 
262  while(ds->moveNext())
263  {
264  geomColl->add(static_cast<te::gm::Geometry*>(ds->getGeometry(name)->clone()));
265  }
266  }
267  if(m_ui->m_selectedGeomRadioButton->isChecked())
268  {
269  const te::da::ObjectIdSet* oidSet = layer->getSelected();
270  if(!oidSet)
271  {
272  QMessageBox::information(this, "Clipping", "Select the layer objects to perform the clipping operation.");
273  return;
274  }
275 
276  std::auto_ptr<te::da::DataSet> ds = layer->getData(oidSet);
277  ds->moveBeforeFirst();
278  while(ds->moveNext())
279  {
280  geomColl->add(static_cast<te::gm::Geometry*>(ds->getGeometry(name)->clone()));
281  }
282  }
283  if(m_ui->m_groupByRadioButton->isChecked())
284  {
285  std::string propName = m_ui->m_layerAttrComboBox->currentText().toStdString();
286 
287  std::map<std::string, std::vector<te::gm::Geometry*> > groups;
288  std::map<std::string, std::vector<te::gm::Geometry*> >::iterator itg;
289 
290  std::auto_ptr<te::da::DataSet> ds = layer->getData();
291 
292  ds->moveBeforeFirst();
293  while(ds->moveNext())
294  {
295  std::string value = ds->getAsString(propName);
296 
297  itg = groups.find(value);
298  if (itg==groups.end())
299  {
300  std::vector<te::gm::Geometry*> geomVec;
301  geomVec.push_back(static_cast<te::gm::Geometry*>(ds->getGeometry(name)->clone()));
302  groups.insert(std::pair<std::string, std::vector<te::gm::Geometry*> >(value,geomVec));
303  }
304  else
305  itg->second.push_back(static_cast<te::gm::Geometry*>(ds->getGeometry(name)->clone()));
306  }
307 
308  itg = groups.begin();
309  while(itg != groups.end())
310  {
311  te::gm::Geometry* resultGeometry(0);
312  std::vector<te::gm::Geometry*> aggregVec = itg->second;
313  if(aggregVec.size() < 2)
314  geomColl->add(aggregVec[0]);
315  if(aggregVec.size() == 2)
316  {
317  resultGeometry = aggregVec[0]->Union(aggregVec[1]);
318  geomColl->add(resultGeometry);
319  }
320  if(aggregVec.size() > 2)
321  {
322  te::gm::GeometryCollection* geomCollTemp = new te::gm::GeometryCollection(0, te::gm::GeometryCollectionType, aggregVec[0]->getSRID());
323 
324  for(std::size_t i = 1; i < aggregVec.size(); ++i)
325  {
326  std::auto_ptr<te::gm::Geometry> currentGeom(aggregVec[i]);
327 
328  if(currentGeom->isValid())
329  geomCollTemp->add(currentGeom.release());
330  }
331 
332  resultGeometry = aggregVec[0]->Union(geomCollTemp);
333  geomColl->add(resultGeometry);
334 
335  }
336  ++itg;
337  }
338 
339  }
340 }
341 
343 {
344  emit completeChanged();
345 }
346 
348 {
349  switch(index)
350  {
351  case 0:
352  {
353  m_navigator->removeVectorial();
354  break;
355  }
356  case 1:
357  {
358  m_navigator->removeVectorial();
359  break;
360  }
361  case 2:
362  {
363  if(m_ui->m_layerComboBox->count() > 0)
364  {
365  QVariant varLayer = m_ui->m_layerComboBox->itemData(0, Qt::UserRole);
367 
368  if(l->isValid())
369  m_navigator->setVectorial(l);
370  }
371  break;
372  }
373  }
374 
375  drawGeom();
376 }
377 
379 {
380  m_ui->m_layerAttrComboBox->clear();
381 
382  QVariant varLayer = m_ui->m_layerComboBox->itemData(index, Qt::UserRole);
384 
385  std::auto_ptr<te::da::DataSetType> dsType = l->getSchema();
386 
387  for(std::size_t t = 0; t < dsType->getProperties().size(); ++t)
388  {
389  m_ui->m_layerAttrComboBox->addItem(dsType->getProperties()[t]->getName().c_str());
390  }
391 }
392 
394 {
395  if((m_ui->m_strategyTypeComboBox->currentText() == "Layer") && m_ui->m_layerComboBox->count() > 0)
396  {
397  QVariant varLayer = m_ui->m_layerComboBox->itemData(index, Qt::UserRole);
399 
400  if(l->isValid())
401  m_navigator->setVectorial(l);
402  }
403 }
404 
406 {
407  if(!env.isValid())
408  return;
409 
410  int index = m_ui->m_strategyTypeComboBox->currentIndex();
411  int type = m_ui->m_strategyTypeComboBox->itemData(index).toInt();
412 
413  if(type == CLIPPING_EXTENT)
414  {
415  m_envExt = env;
416 
417  m_ui->m_llxLineEdit->setText(QString::number(env.getLowerLeftX(), 'f', 5));
418  m_ui->m_llyLineEdit->setText(QString::number(env.getLowerLeftY(), 'f', 5));
419  m_ui->m_urxLineEdit->setText(QString::number(env.getUpperRightX(), 'f', 5));
420  m_ui->m_uryLineEdit->setText(QString::number(env.getUpperRightY(), 'f', 5));
421  }
422  else if(type == CLIPPING_DIMENSION)
423  {
424  m_envDim = env;
425 
426  std::auto_ptr<te::da::DataSet> ds = m_layer->getData();
427 
428  if(ds.get())
429  {
430  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
431  std::auto_ptr<te::rst::Raster> inputRst = ds->getRaster(rpos);
432 
433  if(inputRst.get())
434  {
435  // find the envelope coords in grid coordenates
436  te::gm::Coord2D ul = inputRst->getGrid()->geoToGrid((double) env.getLowerLeftX(), (double) env.getUpperRightY());
437  te::gm::Coord2D lr = inputRst->getGrid()->geoToGrid((double) env.getUpperRightX(), (double) env.getLowerLeftY());
438 
439  m_ui->m_startRowLineEdit->setText(QString::number((int)ul.y));
440  m_ui->m_startColumnLineEdit->setText(QString::number((int)ul.x));
441  m_ui->m_heightLineEdit->setText(QString::number((int)(lr.y - ul.y)));
442  m_ui->m_widthLineEdit->setText(QString::number((int)(lr.x - ul.x)));
443  }
444  }
445  }
446 
447  drawGeom();
448 
449  emit completeChanged();
450 }
451 
453 {
454  m_ui->m_strategyTypeComboBox->clear();
455 
456  m_ui->m_strategyTypeComboBox->addItem(tr("Extent"), CLIPPING_EXTENT);
457  m_ui->m_strategyTypeComboBox->addItem(tr("Dimension"), CLIPPING_DIMENSION);
458  m_ui->m_strategyTypeComboBox->addItem(tr("Layer"), CLIPPING_LAYER);
459 }
460 
462 {
463  te::qt::widgets::MapDisplay* mapDisplay = m_navigator->getDisplay();
464 
465  mapDisplay->getDraftPixmap()->fill(QColor(0, 0, 0, 0));
466 
467  const te::gm::Envelope& mapExt = mapDisplay->getExtent();
468 
469  te::qt::widgets::Canvas canvasInstance(mapDisplay->getDraftPixmap());
470  canvasInstance.setWindow(mapExt.m_llx, mapExt.m_lly, mapExt.m_urx, mapExt.m_ury);
471 
472  canvasInstance.setPolygonContourWidth(2);
473  canvasInstance.setPolygonContourColor(te::color::RGBAColor(100, 177, 216, TE_OPAQUE));
474  canvasInstance.setPolygonFillColor(te::color::RGBAColor(100, 177, 216, 80));
475 
476  te::gm::Geometry* geom = 0;
477 
478  int index = m_ui->m_strategyTypeComboBox->currentIndex();
479  int type = m_ui->m_strategyTypeComboBox->itemData(index).toInt();
480 
481  if(type == CLIPPING_EXTENT)
482  {
483  if(m_envExt.isValid())
484  geom = te::gm::GetGeomFromEnvelope(&m_envExt, m_layer->getSRID());
485  }
486  else if(type == CLIPPING_DIMENSION)
487  {
488  if(m_envDim.isValid())
489  geom = te::gm::GetGeomFromEnvelope(&m_envDim, m_layer->getSRID());
490  }
491 
492  if(geom)
493  {
494  canvasInstance.draw(geom);
495 
496  delete geom;
497  }
498 
499  mapDisplay->repaint();
500 }
void onEnvelopeAcquired(te::gm::Envelope env)
std::auto_ptr< Ui::ClippingWizardPageForm > m_ui
Geometric property.
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
void getLayerClipping(std::auto_ptr< te::gm::GeometryCollection > &geomColl)
void getExtentClipping(te::gm::Envelope &env)
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:410
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
Definition: Envelope.h:400
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
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:420
void setList(std::list< te::map::AbstractLayerPtr > &layerList)
This file defines a class for a Clipping Wizard page.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
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).
Definition: Canvas.cpp:147
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
int getSRID() const
It returns the spatial reference system identifier associated to this property.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
Definition: MapDisplay.cpp:63
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
A canvas built on top of Qt.
Definition: Canvas.h:54
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
Definition: MapDisplay.cpp:222
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
This class is used to navigate over a DataSetLayer (having a raster representation) and given a set o...
void add(Geometry *g)
It adds the geometry into the collection.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Definition: Envelope.h:390
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
void set(te::map::AbstractLayerPtr layer)
This method is used to set the selected layer for clipping operation.
This file has the RasterNavigatorWidget class.
std::auto_ptr< te::qt::widgets::RasterNavigatorWidget > m_navigator
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:481
virtual Geometry * Union(const Geometry *const rhs) const
It returns a geometric object that represents the point set union with another geometry.
Definition: Geometry.cpp:544
It is a collection of other geometric objects.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void getDimensionClipping(int &x, int &y, int &width, int &height)
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
Definition: Utils.cpp:38
const std::string & getName() const
It returns the property name.
Definition: Property.h:127