src/terralib/qt/widgets/rp/Utils.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/Utils.h
22 
23  \brief Utility functions for the qt widgets rp Module.
24 */
25 
26 // TerraLib
27 #include "../../../common/SystemApplicationSettings.h"
28 #include "../../../common/Exception.h"
29 #include "../../../core/translator/Translator.h"
30 #include "../../../dataaccess/dataset/DataSetType.h"
31 #include "../../../dataaccess/datasource/DataSource.h"
32 #include "../../../dataaccess/datasource/DataSourceFactory.h"
33 #include "../../../dataaccess/datasource/DataSourceInfoManager.h"
34 #include "../../../dataaccess/datasource/DataSourceManager.h"
35 #include "../../../dataaccess/utils/Utils.h"
36 #include "../../../maptools/Utils.h"
37 #include "../../../raster/Grid.h"
38 #include "../../../raster/Interpolator.h"
39 #include "../layer/utils/DataSet2Layer.h"
40 #include "Utils.h"
41 
42 // Boost
43 #include <boost/uuid/random_generator.hpp>
44 #include <boost/uuid/uuid_io.hpp>
45 
46 //QT
47 #include <QMessageBox>
48 #include <QApplication>
49 #include <QSettings>
50 
52  const te::core::URI& connInfo)
53 {
55 
56  static boost::uuids::basic_random_generator<boost::mt19937> gen;
57 
58  boost::uuids::uuid valU = gen();
59  std::string id = boost::uuids::to_string(valU);
60 
61  std::unique_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::make(driverName, connInfo));
62  ds->open();
63 
64  std::vector<std::string> dsNames = ds->getDataSetNames();
65  assert(!dsNames.empty());
66 
67  //add ds info
69  dsInfoPtr->setConnInfo(connInfo);
70  dsInfoPtr->setId(id);
71  dsInfoPtr->setTitle(dsNames[0]);
72  dsInfoPtr->setAccessDriver(driverName);
73  dsInfoPtr->setType(driverName);
74 
75  te::da::DataSourcePtr dsPtr(ds.release());
76 
77  if (!te::da::DataSourceInfoManager::getInstance().add(dsInfoPtr))
78  {
79  //Datasource already exist - use its id
80  dsInfoPtr = te::da::DataSourceInfoManager::getInstance().getByConnInfo(dsInfoPtr->getConnInfoAsString());
81  dsPtr->setId(dsInfoPtr->getId());
82  }
83  else
84  {
85  dsPtr->setId(id);
87  }
88 
89  //create layer
90  te::da::DataSetTypePtr dsType(dsPtr->getDataSetType(dsNames[0]).release());
91 
92  te::qt::widgets::DataSet2Layer ds2l(dsPtr->getId());
93 
94  layer = ds2l(dsType);
95 
96  return layer;
97 }
98 
100  const std::string& connInfo)
101 {
102  const te::core::URI uri(connInfo);
103  return createLayer(driverName, uri);
104 }
105 
107  const std::map<std::string, std::string>& connInfo)
108 {
110 
111  static boost::uuids::basic_random_generator<boost::mt19937> gen;
112 
113  boost::uuids::uuid valU = gen();
114  std::string id = boost::uuids::to_string(valU);
115 
116  //Adjusting the URI based on the connection info parameters
117  std::string connStr("file://");
118 
119  std::map<std::string, std::string>::const_iterator it = connInfo.begin();
120  std::map<std::string, std::string>::const_iterator itend = connInfo.end();
121 
122  int count = 0;
123 
124  while (it != itend)
125  {
126  if (it->first == "URI" || it->first == "SOURCE")
127  {
128  connStr += it->second;
129  ++count;
130  ++it;
131  continue;
132  }
133 
134  if (count == 1)
135  connStr += "?";
136 
137  connStr += it->first + "=" + it->second + "&";
138  ++count;
139  ++it;
140  }
141 
142  std::unique_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::make(driverName, connStr));
143  ds->open();
144 
145  std::vector<std::string> dsNames = ds->getDataSetNames();
146  assert(!dsNames.empty());
147 
148  //add ds info
150  dsInfoPtr->setConnInfo(connStr);
151  dsInfoPtr->setId(id);
152  dsInfoPtr->setTitle(dsNames[0]);
153  dsInfoPtr->setAccessDriver(driverName);
154  dsInfoPtr->setType(driverName);
155 
156  te::da::DataSourcePtr dsPtr(ds.release());
157 
158  if (!te::da::DataSourceInfoManager::getInstance().add(dsInfoPtr))
159  {
160  //Datasource already exist - use its id
161  dsInfoPtr = te::da::DataSourceInfoManager::getInstance().getByConnInfo(dsInfoPtr->getConnInfoAsString());
162  dsPtr->setId(dsInfoPtr->getId());
163  }
164  else
165  {
166  dsPtr->setId(id);
168  }
169 
170  //create layer
171  te::da::DataSetTypePtr dsType(dsPtr->getDataSetType(dsNames[0]).release());
172 
173  te::qt::widgets::DataSet2Layer ds2l(dsPtr->getId());
174 
175  layer = ds2l(dsType);
176 
177  return layer;
178 }
179 
181 {
182  te::rst::Raster* raster = nullptr;
183 
184  //get box info
185  te::gm::Envelope reprojectedBBOX(boxDisplay);
186  reprojectedBBOX.transform(displaySRID, layer->getSRID());
187  te::gm::Envelope ibbox = reprojectedBBOX.intersection(layer->getExtent());
188 
189  //get raster
190  std::unique_ptr<te::da::DataSet> ds(layer->getData());
191  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
192  std::unique_ptr<te::rst::Raster> inputRst = ds->getRaster(rpos);
193 
194  te::gm::Coord2D startGrid = inputRst->getGrid()->geoToGrid(ibbox.getLowerLeftX(), ibbox.getLowerLeftY());
195  te::gm::Coord2D endGrid = inputRst->getGrid()->geoToGrid(ibbox.getUpperRightX(), ibbox.getUpperRightY());
196 
197  int w = (int)endGrid.x - (int)startGrid.x;
198  int h = (int)startGrid.y - (int)endGrid.y;
199 
200  raster = te::map::GetExtentRaster(inputRst.get(), w, h, reprojectedBBOX, displaySRID, ibbox, layer->getSRID());
201 
202  return raster;
203 }
204 
205 void te::qt::widgets::applyRasterMultiResolution(const QString& toolName, te::rst::Raster* raster)
206 {
207  QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::instance()->organizationName(), QApplication::instance()->applicationName());
208 
209  int interpolatorValue = settings.value("raster_config/default_interpolator",
210  te::common::SystemApplicationSettings::getInstance().getValue("Application.rasterConfigDefaultInterpolator").c_str()).toInt();
211  int level = settings.value("raster_config/default_level",
212  te::common::SystemApplicationSettings::getInstance().getValue("Application.rasteConfigDefaultLevel").c_str()).toInt();
213 
214  bool question = settings.value("raster_config/default_question",
215  te::common::SystemApplicationSettings::getInstance().getValue("Application.rasterConfigDefaultQuestion").c_str()).toBool();
216 
217  bool option = settings.value("raster_config/default_option",
218  te::common::SystemApplicationSettings::getInstance().getValue("Application.rasterConfigDefaultOption").c_str()).toBool();
219 
220  te::rst::Interpolator::Method interpolator = (te::rst::Interpolator::Method)interpolatorValue;
221 
222 
223  if(question)
224  {
225  int addMultiResolution = QMessageBox::question(nullptr, toolName,
226  TE_TR("Would you like to add multi resolution on output layer?"),
227  QMessageBox::Yes | QMessageBox::No);
228 
229  if(addMultiResolution == QMessageBox::Yes)
230  {
231  try
232  {
233  raster->createMultiResolution(level, interpolator);
234  }
235  catch(...)
236  {
237  QMessageBox::warning(nullptr, toolName, TE_TR("Error creating multi resolution."));
238  }
239 
240  QMessageBox::information(nullptr, TE_TR("Information"), TE_TR("Multi resolution created."));
241  }
242  }
243 
244  if(option)
245  {
246  try
247  {
248  raster->createMultiResolution(level, interpolator);
249  }
250  catch(...)
251  {
252  QMessageBox::warning(nullptr, toolName, TE_TR("Error creating multi resolution."));
253  }
254  }
255 }
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
virtual bool createMultiResolution(const unsigned int levels, const InterpolationMethod interpMethod)=0
Create a sub-sampled multi-resolution pyramid.
double y
y-coordinate.
Definition: Coord2D.h:114
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
double x
x-coordinate.
Definition: Coord2D.h:113
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
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.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
InterpolationMethod
Allowed interpolation methods.
InterpolationMethod Method
Allowed interpolation methods.
Definition: Interpolator.h:62
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
An abstract class for raster data strucutures.
TEQTWIDGETSEXPORT te::rst::Raster * getRasterVisibleArea(te::map::AbstractLayerPtr layer, te::gm::Envelope boxDisplay, int displaySRID)
Utility functions for the data access module.
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
te::map::DataSetLayerPtr createLayer(te::da::DataSourcePtr source, te::da::DataSetTypePtr &dataset)
TEQTWIDGETSEXPORT te::map::AbstractLayerPtr createLayer(const std::string &driverName, const te::core::URI &connInfo)
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
TEMAPEXPORT te::rst::Raster * GetExtentRaster(te::rst::Raster *raster, int w, int h, const te::gm::Envelope &bbox, int bboxSRID, const te::gm::Envelope &visibleArea, int srid)
A class that represents a data source component.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Envelope intersection(const Envelope &rhs) const
It returns an envelope that represents the point set intersection with another envelope.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void transform(int oldsrid, int newsrid)
It will transform the coordinates of the Envelope from the old SRS to the new one.
TEQTWIDGETSEXPORT void applyRasterMultiResolution(const QString &toolName, te::rst::Raster *raster)
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr