ClippingWizard.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/ClippingWizard.cpp
22 
23  \brief A Qt dialog that allows users to run a clipping operation defined by RP module.
24 */
25 
26 // TerraLib
27 #include "../../../common/progress/ProgressManager.h"
28 #include "../../../geometry/Envelope.h"
29 #include "../../../dataaccess/dataset/DataSet.h"
30 #include "../../../dataaccess/utils/Utils.h"
31 #include "../../../geometry/Geometry.h"
32 #include "../../../geometry/GeometryCollection.h"
33 #include "../../../raster/Grid.h"
34 #include "../../../raster/Interpolator.h"
35 #include "../../../raster/PositionIterator.h"
36 #include "../../../raster/Raster.h"
37 #include "../../../raster/RasterFactory.h"
38 #include "../../../raster/Utils.h"
39 #include "../../../maptools/Utils.h"
40 #include "../../../rp/Module.h"
41 #include "../help/HelpPushButton.h"
42 #include "../layer/search/LayerSearchWidget.h"
43 #include "../layer/search/LayerSearchWizardPage.h"
44 #include "../progress/ProgressViewerDialog.h"
45 #include "ClippingWizard.h"
46 #include "ClippingWizardPage.h"
47 #include "../raster/RasterInfoWidget.h"
48 #include "RasterInfoWizardPage.h"
49 #include "Utils.h"
50 
51 // STL
52 #include <cassert>
53 
54 // Qt
55 #include <QApplication>
56 #include <QMessageBox>
57 #include <QActionGroup>
58 
60  : QWizard(parent)
61 {
62  //configure the wizard
63  this->setWizardStyle(QWizard::ModernStyle);
64  this->setWindowTitle(tr("Clipping"));
65  //this->setFixedSize(640, 580);
66 
67  this->setOption(QWizard::HaveHelpButton, true);
68  this->setOption(QWizard::HelpButtonOnRight, false);
69 
71 
72  this->setButton(QWizard::HelpButton, helpButton);
73 
74  helpButton->setPageReference("plugins/rp/rp_clipping.html");
75 
76  connect(this, SIGNAL(currentIdChanged(int)), SLOT(onPageChanged(int)));
77 
78  addPages();
79 }
80 
82 {
83  m_clippingPage->clearCanvas();
84 }
85 
87 {
88  if(currentPage() == m_layerSearchPage.get())
89  {
90  std::list<te::map::AbstractLayerPtr> list = m_layerSearchPage->getSearchWidget()->getSelecteds();
91 
92  if(list.empty() == false)
93  {
94  te::map::AbstractLayerPtr l = *list.begin();
95 
96  m_clippingPage->set(l);
97  }
98 
99  return m_layerSearchPage->isComplete();
100  }
101  else if(currentPage() == m_clippingPage.get())
102  {
103  bool res = m_clippingPage->isComplete();
104 
105  if(!res)
106  QMessageBox::warning(this, tr("Warning"), tr("Select at least one band."));
107 
108  return res;
109  }
110  else if(currentPage() == m_rasterInfoPage.get())
111  {
112  return execute();
113  }
114 
115  return true;
116 }
117 
118 void te::qt::widgets::ClippingWizard::setList(std::list<te::map::AbstractLayerPtr>& layerList)
119 {
120  m_layerSearchPage->getSearchWidget()->setList(layerList);
121  m_layerSearchPage->getSearchWidget()->filterOnlyByRaster();
122 
123  m_clippingPage->setList(layerList);
124 }
125 
127 {
128  m_clippingPage->set(layer);
129 }
130 
132 {
133  m_clippingPage->setMapDisplay(mapDisplay);
134 }
135 
136 void te::qt::widgets::ClippingWizard::setActionGroup(QActionGroup* actionGroup)
137 {
138  m_clippingPage->setActionGroup(actionGroup);
139 }
140 
141 std::vector<te::map::AbstractLayerPtr> te::qt::widgets::ClippingWizard::getOutputLayers()
142 {
143  return m_outputLayer;
144 }
145 
147 {
148  m_clippingPage->drawGeom();
149 }
150 
152 {
156 
157  addPage(m_layerSearchPage.get());
158  addPage(m_clippingPage.get());
159  addPage(m_rasterInfoPage.get());
160 
161  //for contrast only one layer can be selected
162  m_layerSearchPage->getSearchWidget()->enableMultiSelection(false);
163 }
164 
166 {
167  if(m_rasterInfoPage->getWidget()->fileExists())
168  {
169  QMessageBox::warning(this, tr("Clipping"), tr("File already exists."));
170  return false;
171  }
172 
173  //progress
175 
176  QApplication::setOverrideCursor(Qt::WaitCursor);
177 
178  bool res = false;
179 
180  try
181  {
182  if(m_clippingPage->isExtentClipping())
183  res = executeLayerClipping();
184  else if(m_clippingPage->isLayerExtentClipping())
186  else if(m_clippingPage->isDimensionClipping())
187  res = executeDimensionClipping();
188  else if(m_clippingPage->isLayerClipping())
189  {
190  if(m_clippingPage->isGroupByAttribute())
192  else
193  res = executeLayerClipping();
194  }
195  }
196  catch(const std::exception& e)
197  {
198  QMessageBox::warning(this, tr("Clipping"), e.what());
199 
200  QApplication::restoreOverrideCursor();
201 
202  return false;
203  }
204  catch(...)
205  {
206  QMessageBox::warning(this, tr("Clipping"), tr("An exception has occurred!"));
207 
208  QApplication::restoreOverrideCursor();
209 
210  return false;
211  }
212 
213  QApplication::restoreOverrideCursor();
214 
215  return res;
216 }
217 
219 {
221 
222  //get raster
223 
224  std::unique_ptr<te::rst::Raster> inputRst(te::map::GetRaster(layer.get()));
225 
226  //get parameters
227  te::gm::Envelope env;
228 
229  m_clippingPage->getLayerExtentClipping(env);
230 
231  if (!env.intersects(*inputRst->getExtent()))
232  {
233  QMessageBox::warning(this, tr("Clipping"), tr("Selected area do not intersects the raster extent."));
234  return false;
235  }
236 
237  std::map<std::string, std::string> info = m_rasterInfoPage->getWidget()->getInfo();
238 
239  //execute clipping
240  te::rst::Raster* outputRst = inputRst->trim(&env, info);
241 
242  if (outputRst)
243  {
244  delete outputRst;
245 
246  //set output layer
247  te::map::AbstractLayerPtr outputLayer = te::qt::widgets::createLayer(m_rasterInfoPage->getWidget()->getType(),
248  m_rasterInfoPage->getWidget()->getInfo());
249 
250  m_outputLayer.push_back(outputLayer);
251 
252  te::qt::widgets::applyRasterMultiResolution(tr("Clipping"), te::map::GetRaster(outputLayer.get()));
253 
254  emit addLayer(outputLayer);
255 
256  QMessageBox::information(this, tr("Clipping"), tr("Clipping ended sucessfully."));
257  }
258  else
259  {
260  QMessageBox::critical(this, tr("Clipping"), tr("Clipping execution error.") +
261  (" " + te::rp::Module::getLastLogStr() ).c_str());
262 
263  return false;
264  }
265  return true;
266 }
267 
269 {
271 
272  //get raster
273 
274  std::unique_ptr<te::rst::Raster> inputRst(te::map::GetRaster(layer.get()));
275 
276  //get parameters
277  int x, y, width, height;
278 
279  m_clippingPage->getDimensionClipping(x, y, width, height);
280 
281  std::map<std::string, std::string> info = m_rasterInfoPage->getWidget()->getInfo();
282 
283  if (y + height > (int)inputRst->getNumberOfRows() ||
284  x + width > (int)inputRst->getNumberOfColumns())
285  {
286  QMessageBox::warning(this, tr("Clipping"), tr("Selected area beyond the raster boundaries."));
287  return false;
288  }
289 
290  //execute clipping
291  te::rst::Raster* outputRst = inputRst->resample(te::rst::NearestNeighbor, y, x, height, width, height, width, info);
292 
293  if (outputRst)
294  {
295  delete outputRst;
296 
297  //set output layer
298  te::map::AbstractLayerPtr outputLayer = te::qt::widgets::createLayer(m_rasterInfoPage->getWidget()->getType(),
299  m_rasterInfoPage->getWidget()->getInfo());
300 
301  m_outputLayer.push_back(outputLayer);
302 
303  te::qt::widgets::applyRasterMultiResolution(tr("Clipping"), te::map::GetRaster(outputLayer.get()));
304 
305  emit addLayer(outputLayer);
306 
307  QMessageBox::information(this, tr("Clipping"), tr("Clipping ended sucessfully."));
308  }
309  else
310  {
311  QMessageBox::critical(this, tr("Clipping"), tr("Clipping execution error.") +
312  (" " + te::rp::Module::getLastLogStr() ).c_str());
313 
314  return false;
315  }
316  return true;
317 }
318 
320 {
322 
323  //get raster
324 
325  std::unique_ptr<te::rst::Raster> inputRst(te::map::GetRaster(layer.get()));
326 
327  //get parameters
328  std::vector<te::gm::Geometry*> geomVec;
329 
330  if(m_clippingPage->isExtentClipping())
331  m_clippingPage->getExtentClipping(geomVec);
332  else
333  m_clippingPage->getLayerClipping(geomVec);
334 
335  if (geomVec.empty())
336  return false;
337 
338  for(std::size_t i = 0; i < geomVec.size(); ++i)
339  {
340  if (geomVec[i]->getSRID() != inputRst->getSRID())
341  {
342  try
343  {
344  geomVec[i]->transform(inputRst->getSRID());
345  }
346  catch(...)
347  {
348  QMessageBox::critical(this, tr("Clipping"), tr("Layer reprojection failed."));
349 
350  const std::size_t geomVecSize = geomVec.size();
351  for( std::size_t geomVecIdx = 0 ; geomVecIdx < geomVecSize ; ++geomVecIdx )
352  {
353  delete geomVec[ geomVecIdx ];
354  }
355 
356  return false;
357  }
358  }
359  }
360 
361  // Creating the output raster (single raster case)
362 
363  std::unique_ptr<te::rst::Raster> outputRst;
364 
365  if (m_clippingPage->isSingleRasterResult())
366  {
367  std::vector<te::rst::BandProperty*> bands;
368 
369  for (std::size_t t = 0; t < inputRst->getNumberOfBands(); ++t)
370  {
371  te::rst::BandProperty* b = new te::rst::BandProperty(*inputRst->getBand(t)->getProperty());
372  b->m_type = inputRst->getBand(t)->getProperty()->getType();
373  b->m_noDataValue = inputRst->getBand(t)->getProperty()->m_noDataValue;
374  bands.push_back(b);
375  }
376 
377  // raster geographic limits
378 
379  std::unique_ptr< te::gm::Envelope > envPtr;
380  {
381  // Guessing limits
382 
383  double llx = std::numeric_limits< double >::max();
384  double lly = std::numeric_limits< double >::max();
385  double urx = -1.0 * std::numeric_limits< double >::max();
386  double ury = -1.0 * std::numeric_limits< double >::max();
387 
388  const std::size_t geomVecSize = geomVec.size();
389  for( std::size_t geomVecIdx = 0 ; geomVecIdx < geomVecSize ; ++geomVecIdx )
390  {
391  llx = std::min( llx, geomVec[ geomVecIdx ]->getMBR()->getLowerLeftX() );
392  lly = std::min( lly, geomVec[ geomVecIdx ]->getMBR()->getLowerLeftY() );
393  urx = std::max( urx, geomVec[ geomVecIdx ]->getMBR()->getUpperRightX() );
394  ury = std::max( ury, geomVec[ geomVecIdx ]->getMBR()->getUpperRightY() );
395  }
396 
397  // guessing lines/cols
398 
399  double llCol = 0;
400  double llRow = 0;
401  inputRst->getGrid()->geoToGrid( llx, lly, llCol, llRow );
402 
403  double urCol = 0;
404  double urRow = 0;
405  inputRst->getGrid()->geoToGrid( urx, ury, urCol, urRow );
406 
407  envPtr.reset( new te::gm::Envelope() );
408  inputRst->getGrid()->gridToGeo( (double)std::floor( llCol ) - 0.5,
409  (double)std::floor( llRow ) - 0.5, envPtr->m_llx, envPtr->m_lly );
410  inputRst->getGrid()->gridToGeo( (double)std::ceil( urCol ) + 0.5,
411  (double)std::ceil( urRow ) + 0.5, envPtr->m_urx, envPtr->m_ury );
412  }
413 
414  te::rst::Grid* grid = new te::rst::Grid(inputRst->getGrid()->getResolutionX(),
415  inputRst->getGrid()->getResolutionY(), envPtr.release(), inputRst->getSRID());
416 
417  std::string type = m_rasterInfoPage->getWidget()->getType();
418 
419  std::map<std::string, std::string> rinfo =
420  m_rasterInfoPage->getWidget()->getInfo();
421 
422  try
423  {
424  outputRst.reset(te::rst::RasterFactory::make(type, grid, bands, rinfo));
425  }
426  catch(...)
427  {
428  outputRst.reset();
429  }
430  if( outputRst.get() == nullptr )
431  {
432  QMessageBox::critical(this, tr("Clipping"), tr("Output raster creation error."));
433 
434  const std::size_t geomVecSize = geomVec.size();
435  for( std::size_t geomVecIdx = 0 ; geomVecIdx < geomVecSize ; ++geomVecIdx )
436  {
437  delete geomVec[ geomVecIdx ];
438  }
439 
440  return false;
441  }
442 
443  te::rst::FillRaster(outputRst.get(), outputRst->getBand(0)->getProperty()->m_noDataValue);
444  }
445 
446  // Iterating over each geometry
447 
448  te::gm::Polygon* polygon = nullptr;
449  std::vector< std::complex< double > > doubleVec;
450  unsigned int outRow = 0;
451  unsigned int outCol = 0;
452  te::gm::Coord2D inputCoord;
453  te::gm::Coord2D outputCoord;
454 
455  for(std::size_t i = 0; i < geomVec.size(); ++i)
456  {
457  std::vector< te::gm::Geometry* > singleGeometriesPtrs;
458  te::gm::Multi2Single(geomVec[i], singleGeometriesPtrs);
459 
460  for (std::size_t singleGeometriesPtrsIdx = 0; singleGeometriesPtrsIdx <
461  singleGeometriesPtrs.size(); ++singleGeometriesPtrsIdx)
462  {
463  te::gm::Geometry* currGeomPtr = singleGeometriesPtrs[singleGeometriesPtrsIdx];
464 
465  if (currGeomPtr->getGeomTypeId() == te::gm::PolygonType)
466  {
467  // creating the output raster : for the case where multiple rasters
468  // should be generated (one for each geometry )
469 
470  if (!m_clippingPage->isSingleRasterResult())
471  {
472  std::vector<te::rst::BandProperty*> bands;
473 
474  for (std::size_t t = 0; t < inputRst->getNumberOfBands(); ++t)
475  {
476  te::rst::BandProperty* b = new te::rst::BandProperty(*inputRst->getBand(t)->getProperty());
477  b->m_type = inputRst->getBand(t)->getProperty()->getType();
478  b->m_noDataValue = inputRst->getBand(t)->getProperty()->m_noDataValue;
479  bands.push_back(b);
480  }
481 
482  std::unique_ptr< te::gm::Envelope > envPtr;
483  {
484  te::gm::Envelope const* unionEnvelopePtr = currGeomPtr->getMBR();
485 
486  double llCol = 0;
487  double llRow = 0;
488  inputRst->getGrid()->geoToGrid( unionEnvelopePtr->m_llx, unionEnvelopePtr->m_lly, llCol, llRow );
489 
490  double urCol = 0;
491  double urRow = 0;
492  inputRst->getGrid()->geoToGrid( unionEnvelopePtr->m_urx, unionEnvelopePtr->m_ury, urCol, urRow );
493 
494  envPtr.reset( new te::gm::Envelope() );
495  inputRst->getGrid()->gridToGeo( (double)std::floor( llCol ) - 0.5,
496  (double)std::floor( llRow ) - 0.5, envPtr->m_llx, envPtr->m_lly );
497  inputRst->getGrid()->gridToGeo( (double)std::ceil( urCol ) + 0.5,
498  (double)std::ceil( urRow ) + 0.5, envPtr->m_urx, envPtr->m_ury );
499  }
500 
501  te::rst::Grid* grid = new te::rst::Grid(inputRst->getGrid()->getResolutionX(),
502  inputRst->getGrid()->getResolutionY(), envPtr.release(), inputRst->getSRID());
503 
504  std::string type = m_rasterInfoPage->getWidget()->getType();
505 
506  std::map<std::string, std::string> rinfo =
507  m_rasterInfoPage->getWidget()->getInfo(static_cast<int>(i));
508 
509  try
510  {
511  outputRst.reset(te::rst::RasterFactory::make(type, grid, bands, rinfo));
512  }
513  catch(...)
514  {
515  outputRst.reset();
516  }
517  if( outputRst.get() == nullptr )
518  {
519  QMessageBox::critical(this, tr("Clipping"), tr("Output raster creation error."));
520 
521  const std::size_t geomVecSize = geomVec.size();
522  for( std::size_t geomVecIdx = 0 ; geomVecIdx < geomVecSize ; ++geomVecIdx )
523  {
524  delete geomVec[ geomVecIdx ];
525  }
526 
527  return false;
528  }
529 
530  te::rst::FillRaster(outputRst.get(), outputRst->getBand(0)->getProperty()->m_noDataValue);
531  }
532 
533  // Iterating over the current geometry
534 
535  polygon = (te::gm::Polygon*)currGeomPtr;
537  te::rst::PolygonIterator<double>::begin(outputRst.get(), polygon);
539  te::rst::PolygonIterator<double>::end(outputRst.get(), polygon);
540  te::rst::Interpolator interp(inputRst.get(), te::rst::NearestNeighbor);
541  const te::rst::Grid& inputGrid = *inputRst->getGrid();
542  const te::rst::Grid& outputGrid = *outputRst->getGrid();
543 
544  while (it != itend)
545  {
546  outRow = it.getRow();
547  outCol = it.getColumn();
548 
549  outputGrid.gridToGeo((double)outCol, (double)outRow, outputCoord.x,
550  outputCoord.y);
551  inputGrid.geoToGrid(outputCoord.x, outputCoord.y, inputCoord.x,
552  inputCoord.y);
553 
554  interp.getValues(inputCoord.x, inputCoord.y, doubleVec);
555 
556  outputRst->setValues(outCol, outRow, doubleVec);
557 
558  ++it;
559  }
560 
561  if (!m_clippingPage->isSingleRasterResult())
562  {
563  if(outputRst)
564  {
565  outputRst.reset();
566 
567  te::map::AbstractLayerPtr outputLayer = te::qt::widgets::createLayer(m_rasterInfoPage->getWidget()->getType(),
568  m_rasterInfoPage->getWidget()->getInfo(static_cast<int>(i)));
569 
570  //set output layer
571  m_outputLayer.push_back(outputLayer);
572  }
573  else
574  {
575  QMessageBox::information(this, tr("Clipping"), tr("Clipping execution error.") +
576  (" " + te::rp::Module::getLastLogStr() ).c_str());
577  }
578 
579  }
580  }
581  }
582  }
583 
584  if(!m_clippingPage->isSingleRasterResult())
585  {
586  int addOutPutLayer = QMessageBox::question(this, tr("Clipping"),
587  tr("Would you like to add ") + QString::number(m_outputLayer.size()) + (" layer(s) on active project?"),
588  QMessageBox::Yes | QMessageBox::No);
589  if(addOutPutLayer == QMessageBox::Yes)
590  {
591  for(std::size_t i=0; i < m_outputLayer.size(); ++i)
592  {
594 
595  emit addLayer(m_outputLayer[i]);
596  }
597  }
598 
599  QMessageBox::information(this, tr("Clipping"), tr("Clipping ended sucessfully."));
600  }
601 
602  //set output layer ( for single output raster mode )
603 
604  if (m_clippingPage->isSingleRasterResult())
605  {
606  if(outputRst)
607  {
608  outputRst.reset();
609 
610  te::map::AbstractLayerPtr outputLayer = te::qt::widgets::createLayer(m_rasterInfoPage->getWidget()->getType(),
611  m_rasterInfoPage->getWidget()->getInfo());
612 
613  m_outputLayer.push_back(outputLayer);
614 
615  te::qt::widgets::applyRasterMultiResolution(tr("Clipping"), te::map::GetRaster(outputLayer.get()));
616 
617  emit addLayer(outputLayer);
618 
619  QMessageBox::information(this, tr("Clipping"), tr("Clipping ended sucessfully."));
620  }
621  else
622  {
623  QMessageBox::critical(this, tr("Clipping"), tr("Clipping execution error.") +
624  (" " + te::rp::Module::getLastLogStr() ).c_str());
625 
626  const std::size_t geomVecSize = geomVec.size();
627  for( std::size_t geomVecIdx = 0 ; geomVecIdx < geomVecSize ; ++geomVecIdx )
628  {
629  delete geomVec[ geomVecIdx ];
630  }
631 
632  return false;
633  }
634 
635  }
636 
637  const std::size_t geomVecSize = geomVec.size();
638  for( std::size_t geomVecIdx = 0 ; geomVecIdx < geomVecSize ; ++geomVecIdx )
639  {
640  delete geomVec[ geomVecIdx ];
641  }
642 
643  return true;
644 }
645 
647 {
649 
650  //get raster
651 
652  std::unique_ptr<te::rst::Raster> inputRst(te::map::GetRaster(layer.get()));
653 
654  //get parameters
655  std::map<std::string, te::gm::Geometry*> geomMap;
656 
657  m_clippingPage->getLayerClippingAttribute(geomMap);
658 
659  if (geomMap.empty())
660  return false;
661 
662  std::map<std::string, te::gm::Geometry*>::iterator itg;
663 
664  itg = geomMap.begin();
665 
666  while(itg != geomMap.end())
667  {
668  if (itg->second->getSRID() != inputRst->getSRID())
669  {
670  try
671  {
672  itg->second->transform(inputRst->getSRID());
673  }
674  catch(...)
675  {
676  QMessageBox::critical(this, tr("Clipping"), tr("Layer reprojection failed."));
677  return false;
678  }
679  }
680  ++itg;
681  }
682 
683  // Creating the output raster (single raster case)
684 
685  std::unique_ptr<te::rst::Raster> outputRst;
686 
687  // Iterating over each geometry
688 
689  te::gm::Polygon* polygon = nullptr;
690  std::vector< std::complex< double > > doubleVec;
691  unsigned int outRow = 0;
692  unsigned int outCol = 0;
693  te::gm::Coord2D inputCoord;
694  te::gm::Coord2D outputCoord;
695 
696  itg = geomMap.begin();
697 
698  int count = 0;
699 
700  while(itg != geomMap.end())
701  {
702  std::vector< te::gm::Geometry* > singleGeometriesPtrs;
703  te::gm::Multi2Single(itg->second, singleGeometriesPtrs);
704 
705  std::vector<te::rst::BandProperty*> bands;
706 
707  for (std::size_t t = 0; t < inputRst->getNumberOfBands(); ++t)
708  {
709  te::rst::BandProperty* b = new te::rst::BandProperty(*inputRst->getBand(t)->getProperty());
710  b->m_type = inputRst->getBand(t)->getProperty()->getType();
711  b->m_noDataValue = inputRst->getBand(t)->getProperty()->m_noDataValue;
712  bands.push_back(b);
713  }
714 
715  std::unique_ptr<te::gm::Geometry> unionMbr = te::gm::GetGeometryUnion(singleGeometriesPtrs);
716 
717  std::unique_ptr< te::gm::Envelope > envPtr;
718  {
719  te::gm::Envelope const* unionEnvelopePtr = unionMbr->getMBR();
720 
721  double llCol = 0;
722  double llRow = 0;
723  inputRst->getGrid()->geoToGrid( unionEnvelopePtr->m_llx, unionEnvelopePtr->m_lly, llCol, llRow );
724 
725  double urCol = 0;
726  double urRow = 0;
727  inputRst->getGrid()->geoToGrid( unionEnvelopePtr->m_urx, unionEnvelopePtr->m_ury, urCol, urRow );
728 
729  envPtr.reset( new te::gm::Envelope() );
730  inputRst->getGrid()->gridToGeo( (double)std::floor( llCol ) - 0.5,
731  (double)std::floor( llRow ) - 0.5, envPtr->m_llx, envPtr->m_lly );
732  inputRst->getGrid()->gridToGeo( (double)std::ceil( urCol ) + 0.5,
733  (double)std::ceil( urRow ) + 0.5, envPtr->m_urx, envPtr->m_ury );
734  }
735 
736  te::rst::Grid* grid = new te::rst::Grid(inputRst->getGrid()->getResolutionX(),
737  inputRst->getGrid()->getResolutionY(), envPtr.release(), inputRst->getSRID());
738 
739  std::string type = m_rasterInfoPage->getWidget()->getType();
740 
741  std::map<std::string, std::string> rinfo =
742  m_rasterInfoPage->getWidget()->getInfo(count, itg->first);
743 
744  try
745  {
746  outputRst.reset(te::rst::RasterFactory::make(type, grid, bands, rinfo));
747  }
748  catch(...)
749  {
750  outputRst.reset();
751  }
752  if( outputRst.get() == nullptr )
753  {
754  QMessageBox::critical(this, tr("Clipping"), tr("Output raster creation error."));
755  return false;
756  }
757 
758  te::rst::FillRaster(outputRst.get(), outputRst->getBand(0)->getProperty()->m_noDataValue);
759 
760 
761  for (std::size_t singleGeometriesPtrsIdx = 0; singleGeometriesPtrsIdx <
762  singleGeometriesPtrs.size(); ++singleGeometriesPtrsIdx)
763  {
764  te::gm::Geometry* currGeomPtr = singleGeometriesPtrs[singleGeometriesPtrsIdx];
765 
766  if (currGeomPtr->getGeomTypeId() == te::gm::PolygonType)
767  {
768  // creating the output raster : for the case where multiple rasters
769  // should be generated (one for each geometry )
770 
771  // Iterating over the current geometry
772 
773  polygon = (te::gm::Polygon*)currGeomPtr;
775  te::rst::PolygonIterator<double>::begin(outputRst.get(), polygon);
777  te::rst::PolygonIterator<double>::end(outputRst.get(), polygon);
778  te::rst::Interpolator interp(inputRst.get(), te::rst::NearestNeighbor);
779  const te::rst::Grid& inputGrid = *inputRst->getGrid();
780  const te::rst::Grid& outputGrid = *outputRst->getGrid();
781 
782  while (it != itend)
783  {
784  outRow = it.getRow();
785  outCol = it.getColumn();
786 
787  outputGrid.gridToGeo((double)outCol, (double)outRow, outputCoord.x,
788  outputCoord.y);
789  inputGrid.geoToGrid(outputCoord.x, outputCoord.y, inputCoord.x,
790  inputCoord.y);
791 
792  interp.getValues(inputCoord.x, inputCoord.y, doubleVec);
793 
794  outputRst->setValues(outCol, outRow, doubleVec);
795 
796  ++it;
797  }
798  }
799  }
800 
801  if(outputRst)
802  {
803  outputRst.reset();
804 
805  te::map::AbstractLayerPtr outputLayer = te::qt::widgets::createLayer(m_rasterInfoPage->getWidget()->getType(),
806  m_rasterInfoPage->getWidget()->getInfo(count, itg->first));
807 
808  //set output layer
809  m_outputLayer.push_back(outputLayer);
810  }
811  else
812  {
813  QMessageBox::information(this, tr("Clipping"), tr("Clipping execution error.") +
814  (" " + te::rp::Module::getLastLogStr() ).c_str());
815  }
816 
817  ++count;
818 
819  ++itg;
820  }
821 
822  int addOutPutLayer = QMessageBox::question(this, tr("Clipping"),
823  tr("Would you like to add ") + QString::number(m_outputLayer.size()) + (" layer(s) on active project?"),
824  QMessageBox::Yes | QMessageBox::No);
825  if(addOutPutLayer == QMessageBox::Yes)
826  {
827  for(std::size_t i=0; i < m_outputLayer.size(); ++i)
828  {
830 
831  emit addLayer(m_outputLayer[i]);
832  }
833  }
834 
835  QMessageBox::information(this, tr("Clipping"), tr("Clipping ended sucessfully."));
836 
837  return true;
838 }
839 
841 {
842  this->adjustSize();
843 }
Near neighborhood interpolation method.
double y
y-coordinate.
Definition: Coord2D.h:114
bool intersects(const Envelope &rhs) const
It returns true if the envelopes "spatially intersects".
void setMapDisplay(te::qt::widgets::MapDisplay *mapDisplay)
A raster band description.
Definition: BandProperty.h:61
double x
x-coordinate.
Definition: Coord2D.h:113
std::unique_ptr< te::qt::widgets::RasterInfoWizardPage > m_rasterInfoPage
void setPageReference(const QString &ref)
Sets the documentation page reference.
It interpolates one pixel based on a selected algorithm. Methods currently available are Nearest Neig...
Definition: Interpolator.h:55
GeomType getGeomTypeId() const _NOEXCEPT_OP(true)
It returns the geometry subclass type identifier.
virtual Raster * trim(const te::gm::Envelope *env, const std::map< std::string, std::string > &rinfo) const
Subsetting operation for trimming (cropping) the raster.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
double m_urx
Upper right corner x-coordinate.
virtual Raster * resample(int method, unsigned int drow, unsigned int dcolumn, unsigned int height, unsigned int width, unsigned int newheight, unsigned int newwidth, const std::map< std::string, std::string > &rinfo) const
Resample a subset of the raster, given a box.
TEGEOMEXPORT std::unique_ptr< te::gm::Geometry > GetGeometryUnion(const std::vector< te::gm::Geometry * > &geomVec)
It returns the union of a geometry vector.
TERASTEREXPORT void FillRaster(te::rst::Raster *rin, const std::complex< double > &value)
Fill a Raster with provided value (all bands).
A widget to control the display of a set of layers.
unsigned int getRow() const
Returns the current row in iterator.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
int m_type
The data type of the elements in the band ( See te::dt namespace basic data types for reference )...
Definition: BandProperty.h:133
std::unique_ptr< te::qt::widgets::LayerSearchWizardPage > m_layerSearchPage
void setActionGroup(QActionGroup *actionGroup)
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
Definition: BandProperty.h:136
A Qt dialog that allows users to run a clipping operation defined by RP module.
TEGEOMEXPORT void Multi2Single(const te::gm::Geometry *g, std::vector< te::gm::Geometry * > &geoms)
It will get a GeometryCollection and distribute in a vector.
static const std::string getLastLogStr()
Returns the last log string generated by this module.
This file defines a class for a Raster Info Wizard page.
This file defines a class for a Clipping Wizard page.
std::vector< te::map::AbstractLayerPtr > getOutputLayers()
int b
Definition: TsRtree.cpp:32
double m_llx
Lower left corner x-coordinate.
std::unique_ptr< te::qt::widgets::ClippingWizardPage > m_clippingPage
static PolygonIterator end(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to after the end of the iterator.
const Envelope * getMBR() const _NOEXCEPT_OP(true)
It returns the minimum bounding rectangle for the geometry in an internal representation.
An Envelope defines a 2D rectangular region.
An abstract class for raster data strucutures.
This class is GUI used to define the raster info parameters for raster factory.
list bands
Definition: compose.py:2
Utility functions for the data access module.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
double m_lly
Lower left corner y-coordinate.
static PolygonIterator begin(const te::rst::Raster *r, const te::gm::Polygon *p, const IterationType iterationType)
Returns an iterator referring to the first value of the band.
void setLayer(te::map::AbstractLayerPtr layer)
void addLayer(te::map::AbstractLayerPtr layer)
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
TEQTWIDGETSEXPORT te::map::AbstractLayerPtr createLayer(const std::string &driverName, const te::core::URI &connInfo)
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
double m_ury
Upper right corner y-coordinate.
static Raster * make()
It creates and returns an empty raster with default raster driver.
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
void setList(std::list< te::map::AbstractLayerPtr > &layerList)
unsigned int getColumn() const
Returns the current column in iterator.
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
This class is GUI used to define the clipping parameters for the RP constast operation.
std::vector< te::map::AbstractLayerPtr > m_outputLayer
TEQTWIDGETSEXPORT void applyRasterMultiResolution(const QString &toolName, te::rst::Raster *raster)