RasterizationWizard.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 //Terralib
21 #include "../../../common/progress/ProgressManager.h"
22 #include "../../../dataaccess/datasource/DataSource.h"
23 #include "../../../dataaccess/datasource/DataSourceInfoManager.h"
24 #include "../../../dataaccess/datasource/DataSourceInfoManager.h"
25 #include "../../../dataaccess/datasource/DataSourceManager.h"
26 #include "../../../dataaccess/utils/Utils.h"
27 #include "../../../core/filesystem/FileSystem.h"
28 #include "../../../datatype/SimpleProperty.h"
29 #include "../../../geometry/Envelope.h"
30 #include "../../../geometry/GeometryProperty.h"
31 #include "../../../memory/DataSet.h"
32 #include "../../../memory/DataSetItem.h"
33 #include "../../../raster/BandProperty.h"
34 #include "../../../qt/widgets/utils/ScopedCursor.h"
35 #include "../../../qt/widgets/rp/Utils.h"
36 #include "../../../raster/Grid.h"
37 #include "../../../raster/RasterFactory.h"
38 #include "../../../se/Categorize.h"
39 #include "../../../se/ColorMap.h"
40 #include "../../../se/MapItem.h"
41 #include "../../../se/ParameterValue.h"
42 #include "../../../se/RasterSymbolizer.h"
43 #include "../../../se/Recode.h"
44 #include "../../../se/Utils.h"
45 #include "../../../maptools/Utils.h"
46 #include "../help/HelpPushButton.h"
47 #include "../layer/search/LayerSearchWidget.h"
48 #include "../layer/search/LayerSearchWizardPage.h"
49 #include "../progress/ProgressViewerDialog.h"
50 #include "RasterizationWizard.h"
52 
53 // BOOST
54 #include <boost/algorithm/string.hpp>
55 #include <boost/filesystem.hpp>
56 #include <boost/lexical_cast.hpp>
57 #include <boost/uuid/random_generator.hpp>
58 #include <boost/uuid/uuid_io.hpp>
59 
60 // Qt
61 #include <QApplication>
62 #include <QColor>
63 #include <QMessageBox>
64 #include <QTableWidget>
65 
67 QWizard(parent)
68 {
69  this->setWizardStyle(QWizard::ModernStyle);
70  this->setWindowTitle(tr("Rasterization"));
71 
72  this->setOption(QWizard::HaveHelpButton, true);
73  this->setOption(QWizard::HelpButtonOnRight, false);
74 
76 
77  this->setButton(QWizard::HelpButton, helpButton);
78 
79  helpButton->setPageReference("plugins/rp/rp_rasterization.html");
80 
81  connect(this, SIGNAL(currentIdChanged(int)), SLOT(onPageChanged(int)));
82 
83  addPages();
84 }
85 
87 
89 {
90  if(currentPage() == m_layerSearchPage.get())
91  {
92  std::list<te::map::AbstractLayerPtr> list = m_layerSearchPage->getSearchWidget()->getSelecteds();
93 
94  if(list.empty() == false)
95  {
96  m_inputLayer = *list.begin();;
97 
98  m_vectorPage->setLayer(m_inputLayer);
99  }
100 
101  return m_layerSearchPage->isComplete();
102  }
103  else if(currentPage() == m_vectorPage.get())
104  {
105  return execute();
106  }
107 
108  return true;
109 }
110 
111 void te::qt::widgets::RasterizationWizard::setList(std::list<te::map::AbstractLayerPtr> &layerList)
112 {
113  m_layerSearchPage->getSearchWidget()->setList(layerList);
114 }
115 
117 {
118  return m_outputLayer;
119 }
120 
122 {
123  m_layerSearchPage.reset(new LayerSearchWizardPage(this));
124  addPage(m_layerSearchPage.get());
125 
126  m_vectorPage.reset(new RasterizationWizardPage(this));
127  addPage(m_vectorPage.get());
128 }
129 
131 {
132  int rows = rst->getNumberOfRows();
133  int cols = rst->getNumberOfColumns();
134 
135  for (int r = 0; r < rows; ++r)
136  {
137  for (int c = 0; c < cols; ++c)
138  {
139  rst->setValue(c, r, dummyValue);
140  }
141  }
142 }
143 
145 {
146  if(te::core::FileSystem::exists(m_vectorPage->getRepositoryName()))
147  {
148  QMessageBox::warning(this, tr("Rasterization"), tr("File already exists."));
149  return false;
150  }
151 
152  //progress
154 
155  try
156  {
157  m_vectorPage->validate();
158  }
159  catch (const te::common::Exception& e)
160  {
161  QMessageBox::warning(this, tr("Rasterization"), e.what());
162  return false;
163  }
164 
165  te::gm::Envelope envAux = m_vectorPage->getEnvelope();
166 
167  te::gm::Envelope* env = new te::gm::Envelope(envAux.m_llx, envAux.m_lly, envAux.m_urx, envAux.m_ury);
168 
169  double resX = m_vectorPage->getResX();
170  double resY = m_vectorPage->getResY();
171  int srid = m_vectorPage->getSrid();
172 
173  std::string uriStr = m_vectorPage->getRepositoryName();
174 
175  boost::filesystem::path uri(uriStr);
176 
177  std::string attrName = m_vectorPage->getAttributeName();
178 
179  std::map<std::string, std::vector<int> > infos = m_vectorPage->getInformations();
180 
181  if (infos.empty())
182  {
183  QMessageBox::warning(this, tr("Rasterization"), tr("None legend was created!"));
184  return false;
185  }
186 
187  te::rst::Grid* grid = new te::rst::Grid(resX, resY, env, srid);
188 
189  // create bands
190  std::vector<te::rst::BandProperty*> vecBandProp;
191 
192  te::rst::BandProperty* bProp = createBandProperty(infos.size());
193  bProp->m_noDataValue = 0;
194 
195  vecBandProp.push_back(bProp);
196 
197  std::map<std::string, std::string> dsinfo;
198  dsinfo["URI"] = uri.string();
199 
200  try
201  {
202  // create raster
203  std::unique_ptr<te::rst::Raster> rst(te::rst::RasterFactory::make("GDAL", grid, vecBandProp, dsinfo));
204 
205  setDummy(rst.get(), 0);
206 
207  std::unique_ptr<te::da::DataSetType> schema = m_inputLayer->getSchema();
208  std::unique_ptr<te::da::DataSet> data = m_inputLayer->getData();
209 
210  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(data.get());
211 
212  data->moveBeforeFirst();
213 
214  std::vector<te::gm::Geometry*> geoms;
215  std::vector<double> values;
216 
217  {
219  task.setMessage(TE_TR("Preparing..."));
220  task.setTotalSteps((int)data->size());
221 
222  while (data->moveNext())
223  {
224  std::string classItem = data->getValue(attrName)->toString();
225 
226  if (infos.find(classItem) == infos.end())
227  continue;
228 
229  std::unique_ptr<te::gm::Geometry> geom = data->getGeometry(geomPos);
230 
231  geom->transform(srid);
232 
233  if (geom->isValid())
234  {
235  geoms.push_back(geom.release());
236  values.push_back(infos[classItem][0]);
237  }
238 
239  if (!task.isActive())
240  {
241  QMessageBox::warning(this, tr("Rasterize"), tr("Rasterize operation canceled!"));
242  return false;
243  }
244 
245  task.pulse();
246  }
247  }
248 
249  rst->rasterize(geoms, values, 0);
250 
251  rst.reset(nullptr);
252 
254 
255  te::se::Recode* r = new te::se::Recode();
256  r->setFallbackValue("#000000");
257  r->setLookupValue(new te::se::ParameterValue("Rasterdata"));
258 
260 
261  std::map<std::string, std::vector<int> >::iterator it = infos.begin();
262  for (it = infos.begin(); it != infos.end(); ++it)
263  {
264  QColor color;
265  color.setRgb(it->second[1], it->second[2], it->second[3]);
266 
267  double dataDouble = it->second[0];
268  std::string colorStr = color.name().toUtf8().data();
269 
270  te::se::MapItem* m = new te::se::MapItem();
271  m->setValue(new te::se::ParameterValue(colorStr));
272  m->setData(dataDouble);
273  m->setTitle(it->first);
274 
275  r->add(m);
276  }
277 
278  if (cm)
279  {
280  cm->setCategorize(nullptr);
281  cm->setInterpolate(nullptr);
282  cm->setRecode(r);
283  }
284 
286  rs->setColorMap(cm);
287 
288  boost::filesystem::path p(uri);
289  p.replace_extension("leg");
290 
291  m_vectorPage->saveLegend(p.string());
292 
293  QMessageBox::information(this, tr("Rasterization"), tr("Rasterization ended sucessfully."));
294 
295  }
296  catch (const std::exception& e)
297  {
298  QMessageBox::warning(this, tr("Rasterization"), e.what());
299  return false;
300  }
301 
303 
304  return true;
305 }
306 
307 std::unique_ptr<te::da::DataSetType> te::qt::widgets::RasterizationWizard::createDataSetType(std::string dataSetName, int srid)
308 {
309  std::unique_ptr<te::da::DataSetType> dsType(new te::da::DataSetType(dataSetName));
310 
311  //create id property
313  dsType->add(idProperty);
314 
315  //create geometry property
316  te::gm::GeometryProperty* geomProperty = new te::gm::GeometryProperty("geom", srid, te::gm::PolygonType);
317  dsType->add(geomProperty);
318 
319  //create primary key
320  std::string pkName = "pk_id";
321  pkName+= "_" + dataSetName;
322  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pkName, dsType.get());
323  pk->add(idProperty);
324 
325  return dsType;
326 }
327 
328 std::unique_ptr<te::mem::DataSet> te::qt::widgets::RasterizationWizard::createDataSet(te::da::DataSetType* dsType, std::vector<te::gm::Geometry*>& geoms)
329 {
330  std::unique_ptr<te::mem::DataSet> ds(new te::mem::DataSet(dsType));
331 
332  for(std::size_t t = 0; t < geoms.size(); ++t)
333  {
334  //create dataset item
335  te::mem::DataSetItem* item = new te::mem::DataSetItem(ds.get());
336 
337  //set id
338  item->setInt32("id", (int)t);
339 
340  //set geometry
341  item->setGeometry("geom", geoms[t]);
342 
343  ds->add(item);
344  }
345 
346  return ds;
347 }
348 
350 {
351  //save dataset
352  dataSet->moveBeforeFirst();
353 
354  std::map<std::string, std::string> options;
355 
356  ds->createDataSet(dsType, options);
357 
358  ds->add(dataSetName, dataSet, options);
359 }
360 
362 {
363  int nbytes = std::ceil((log((float) size) / log(2.)) / 8);
364 
365  if (nbytes <= 1)
366  return new te::rst::BandProperty(0, te::dt::CHAR_TYPE, "");
367  else if (nbytes <= 2)
368  return new te::rst::BandProperty(0, te::dt::INT16_TYPE, "");
369  else if (nbytes <= 4)
370  return new te::rst::BandProperty(0, te::dt::INT32_TYPE, "");
371  else
372  return new te::rst::BandProperty(0, te::dt::DOUBLE_TYPE, "");
373 }
374 
376 {
377  this->adjustSize();
378 }
void setList(std::list< te::map::AbstractLayerPtr > &layerList)
te::map::AbstractLayerPtr m_outputLayer
virtual void setValue(unsigned int c, unsigned int r, const double value, std::size_t b=0)
Sets the attribute value in a band of a cell.
Geometric property.
void setMessage(const std::string &message)
Set the task message.
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
An atomic property like an integer or double.
boost::shared_ptr< DataSource > DataSourcePtr
A raster band description.
Definition: BandProperty.h:61
A class that models the description of a dataset.
Definition: DataSetType.h:72
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
void setPageReference(const QString &ref)
Sets the documentation page reference.
virtual const char * what() const
It outputs the exception message.
TESEEXPORT RasterSymbolizer * GetRasterSymbolizer(Style *s)
Try to get raster symbolizer from a style.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
double m_urx
Upper right corner x-coordinate.
void setCategorize(Categorize *c)
Definition: ColorMap.cpp:67
static te::dt::Date ds(2010, 01, 01)
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
Definition: BandProperty.h:136
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
The "ParameterValueType" uses WFS-Filter expressions to give values for SE graphic parameters...
bool isActive() const
Verify if the task is active.
void setTotalSteps(int value)
Set the task total stepes.
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
te::map::AbstractLayerPtr getOutputLayer()
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
double m_llx
Lower left corner x-coordinate.
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
void setData(const double &d)
Definition: MapItem.cpp:57
An Envelope defines a 2D rectangular region.
te::rst::BandProperty * createBandProperty(const std::size_t size)
An abstract class for raster data strucutures.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
void setTitle(const std::string &title)
Definition: MapItem.cpp:83
void setLookupValue(ParameterValue *v)
Definition: Recode.cpp:62
te::gm::Polygon * p
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
The RasterSymbolizer describes how to render raster/matrix-coverage data (e.g., satellite photos...
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Transformation of discrete values to other values.
Definition: Recode.h:75
void setRecode(Recode *i)
Definition: ColorMap.cpp:89
double m_lly
Lower left corner y-coordinate.
bool moveBeforeFirst()
It moves the internal pointer to a position before the first item in the collection.
std::unique_ptr< te::qt::widgets::LayerSearchWizardPage > m_layerSearchPage
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
void setDummy(te::rst::Raster *rst, const double dummyValue)
TEQTWIDGETSEXPORT te::map::AbstractLayerPtr createLayer(const std::string &driverName, const te::core::URI &connInfo)
void setValue(ParameterValue *v)
Definition: MapItem.cpp:62
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
double m_ury
Upper right corner y-coordinate.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
void add(MapItem *m)
Definition: Recode.cpp:68
static Raster * make()
It creates and returns an empty raster with default raster driver.
void setColorMap(ColorMap *c)
std::unique_ptr< te::da::DataSetType > createDataSetType(std::string dataSetName, int srid)
void saveDataSet(te::mem::DataSet *dataSet, te::da::DataSetType *dsType, te::da::DataSourcePtr ds, std::string dataSetName)
std::unique_ptr< te::qt::widgets::RasterizationWizardPage > m_vectorPage
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
std::unique_ptr< te::mem::DataSet > createDataSet(te::da::DataSetType *dsType, std::vector< te::gm::Geometry * > &geoms)
void setInterpolate(Interpolate *i)
Definition: ColorMap.cpp:78
A ColorMap defines either the colors of a pallette-type raster source or the mapping of numeric pixel...
Definition: ColorMap.h:61
TEQTWIDGETSEXPORT void applyRasterMultiResolution(const QString &toolName, te::rst::Raster *raster)
te::map::AbstractLayerPtr m_inputLayer
void setFallbackValue(const std::string &v)
Definition: se/Function.cpp:33