27 #include "../../../cellspace/CellSpaceOperations.h"
28 #include "../../../common/Exception.h"
29 #include "../../../common/progress/ProgressManager.h"
30 #include "../../../common/StringUtils.h"
31 #include "../../../common/Translator.h"
32 #include "../../../common/UnitsOfMeasureManager.h"
33 #include "../../../dataaccess/datasource/DataSource.h"
34 #include "../../../dataaccess/datasource/DataSourceFactory.h"
35 #include "../../../dataaccess/datasource/DataSourceInfoManager.h"
36 #include "../../../dataaccess/datasource/DataSourceTransactor.h"
37 #include "../../../dataaccess/datasource/DataSourceManager.h"
38 #include "../../../dataaccess/utils/Utils.h"
39 #include "../../../qt/widgets/layer/utils/DataSet2Layer.h"
40 #include "../../../qt/widgets/help/HelpPushButton.h"
41 #include "../../../qt/widgets/progress/ProgressViewerDialog.h"
42 #include "../../../qt/widgets/srs/SRSManagerDialog.h"
43 #include "../../../qt/widgets/utils/ScopedCursor.h"
44 #include "../../../srs/SpatialReferenceSystemManager.h"
45 #include "../../../srs/Converter.h"
46 #include "../../widgets/datasource/selector/DataSourceSelectorDialog.h"
47 #include "ui_CreateCellularSpaceDialogForm.h"
51 #include <QFileDialog>
52 #include <QMessageBox>
53 #include <QToolButton>
56 #include <boost/filesystem.hpp>
57 #include <boost/uuid/random_generator.hpp>
58 #include <boost/uuid/uuid_io.hpp>
67 m_outputDataSetName(
""),
68 m_ui(new
Ui::CreateCellularSpaceDialogForm)
74 QSize iconSize(32, 32);
76 m_ui->m_imgLabel->setPixmap(QIcon::fromTheme(
"cellspace-no-mask-hint").pixmap(48, 48));
77 m_ui->m_maskRadioButton->setIconSize(iconSize);
78 m_ui->m_maskRadioButton->setIcon(QIcon::fromTheme(
"cellspace-mask-hint"));
79 m_ui->m_noMaskRadioButton->setIconSize(iconSize);
80 m_ui->m_noMaskRadioButton->setIcon(QIcon::fromTheme(
"cellspace-no-mask-hint"));
81 m_ui->m_polygonsRadioButton->setIconSize(iconSize);
82 m_ui->m_polygonsRadioButton->setIcon(QIcon::fromTheme(
"cellspace-polygons-hint"));
83 m_ui->m_pointsRadioButton->setIconSize(iconSize);
84 m_ui->m_pointsRadioButton->setIcon(QIcon::fromTheme(
"cellspace-points-hint"));
85 m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme(
"datasource"));
86 m_ui->m_srsToolButton->setIcon(QIcon::fromTheme(
"srs"));
89 m_ui->m_llxLineEdit->setValidator(
new QDoubleValidator(
this));
90 m_ui->m_llyLineEdit->setValidator(
new QDoubleValidator(
this));
91 m_ui->m_urxLineEdit->setValidator(
new QDoubleValidator(
this));
92 m_ui->m_uryLineEdit->setValidator(
new QDoubleValidator(
this));
93 m_ui->m_resXLineEdit->setValidator(
new QDoubleValidator(0, 99999999, 8,
this));
94 m_ui->m_resYLineEdit->setValidator(
new QDoubleValidator(0, 99999999, 8,
this));
103 connect(
m_ui->m_llxLineEdit, SIGNAL(textChanged(
const QString &)),
this, SLOT(
onEnvelopeChanged(
const QString &)));
104 connect(
m_ui->m_llyLineEdit, SIGNAL(textChanged(
const QString &)),
this, SLOT(
onEnvelopeChanged(
const QString &)));
105 connect(
m_ui->m_urxLineEdit, SIGNAL(textChanged(
const QString &)),
this, SLOT(
onEnvelopeChanged(
const QString &)));
106 connect(
m_ui->m_uryLineEdit, SIGNAL(textChanged(
const QString &)),
this, SLOT(
onEnvelopeChanged(
const QString &)));
113 m_ui->m_helpPushButton->setNameSpace(
"dpi.inpe.br.plugins");
114 m_ui->m_helpPushButton->setPageReference(
"plugins/cellspace/cellspace.html");
125 m_ui->m_noMaskRadioButton->setChecked(
true);
126 m_ui->m_maskRadioButton->setEnabled(
false);
127 m_ui->m_resSRIDLabel->setText(
"No SRS defined");
128 m_ui->m_layerSRIDLabel->setText(
"No SRS defined");
129 m_ui->m_unitComboBox->setCurrentIndex(0);
134 std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
135 m_bbSRID = it->get()->getSRID();
136 while(it != layers.end())
138 m_ui->m_layersComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant::fromValue(*it));
141 m_ui->m_layersComboBox->setCurrentIndex(0);
149 boost::uuids::basic_random_generator<boost::mt19937> gen;
150 boost::uuids::uuid u = gen();
151 std::string id_ds = boost::uuids::to_string(u);
153 boost::filesystem::path uri(m_outDataSourceInfo->getConnInfo()[
"URI"]);
156 ds->setConnInfo(m_outDataSourceInfo->getConnInfo());
157 ds->setTitle(uri.stem().string());
158 ds->setAccessDriver(
"OGR");
160 ds->setDescription(uri.string());
166 m_outDataSourceInfo = ds;
168 m_outputDataSetName = uri.stem().string();
172 outDataSource->open();
180 layer->setSRID(m_bbSRID);
192 m_bbSRID = layer->getSRID();
202 setResolutionUnit(unit);
206 m_ui->m_resSRIDLabel->setText(
"No SRS defined");
207 m_ui->m_layerSRIDLabel->setText(
"No SRS defined");
208 m_ui->m_unitComboBox->setCurrentIndex(0);
211 showEnvelope(layer->getExtent(),precision);
212 m_ui->m_maskRadioButton->setChecked(
true);
224 srsDialog.setWindowTitle(tr(
"Choose the SRS"));
226 if(srsDialog.exec() == QDialog::Rejected)
231 if (m_ui->m_maskRadioButton->isChecked())
235 ((newSRID <= 0 && refLayer->getSRID() > 0) ||
236 (newSRID > 0 && refLayer->getSRID() <= 0) ))
237 m_ui->m_noMaskRadioButton->setChecked(
true);
242 m_ui->m_unitComboBox->setCurrentIndex(0);
243 m_ui->m_resSRIDLabel->setText(
"No SRS defined");
248 m_ui->m_resSRIDLabel->setText(name.c_str());
262 showEnvelope(env, precision);
268 setResolutionUnit(unit);
275 if (m_bbSRID <= 0 && index != 0)
277 QMessageBox::warning(
this, tr(
"Cellular Spaces"), tr(
"There is no no spatial reference defined for the new cellular space. It is not possible to select a unit for the resolution."));
278 m_ui->m_unitComboBox->setCurrentIndex(0);
290 if (!resUnit || !bbUnit)
293 if (resUnit == bbUnit)
308 QMessageBox::warning(
this, tr(
"Cellular Spaces"), tr(
"Unable to convert between the selected unit for the resolution and the unit of new cell layer SRS"));
309 m_ui->m_unitComboBox->setCurrentText(bbUnit->getName().c_str());
317 QMessageBox::warning(
this, tr(
"Cellular Spaces"), tr(
"Invalid bounding box."));
321 double resX = m_ui->m_resXLineEdit->text().toDouble();
324 int maxCols = (int)ceil((env.
m_urx - env.
m_llx)/resX);
325 m_ui->m_colsLineEdit->setText(QString::number(maxCols));
333 QMessageBox::warning(
this, tr(
"Cellular Spaces"), tr(
"Invalid bounding box."));
337 double resY = m_ui->m_resXLineEdit->text().toDouble();
340 int maxRows = (int)ceil((env.
m_ury - env.
m_lly)/resY);
341 m_ui->m_rowsLineEdit->setText(QString::number(maxRows));
346 m_ui->m_newLayerNameLineEdit->clear();
347 m_ui->m_repositoryLineEdit->clear();
352 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
354 if(dsPtrList.empty())
357 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
359 m_outDataSourceInfo = *it;
361 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
365 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
370 m_ui->m_newLayerNameLineEdit->clear();
371 m_ui->m_repositoryLineEdit->clear();
373 if (m_ui->m_colsLineEdit->text().isEmpty())
374 onResXLineEditEditingFinished();
376 if (m_ui->m_rowsLineEdit->text().isEmpty())
377 onResYLineEditEditingFinished();
381 std::string accessDriver;
383 extension = tr(
"Shapefile (*.shp *.SHP);;");
384 accessDriver =
"OGR";
386 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."),
387 QString(), extension, 0, QFileDialog::DontConfirmOverwrite);
389 if (fileName.isEmpty())
392 boost::filesystem::path outfile(fileName.toStdString());
393 std::string aux = outfile.leaf().string();
394 m_ui->m_newLayerNameLineEdit->setText(aux.c_str());
395 aux = outfile.string();
396 m_ui->m_repositoryLineEdit->setText(aux.c_str());
400 m_ui->m_newLayerNameLineEdit->setEnabled(
false);
402 std::map<std::string, std::string> connInfo;
403 connInfo[
"URI"] = m_ui->m_repositoryLineEdit->text().toStdString();
407 m_outDataSourceInfo->setAccessDriver(accessDriver);
408 m_outDataSourceInfo->setConnInfo(connInfo);
414 if(!checkList(errors))
416 QMessageBox::warning(
this, tr(
"Cellular Spaces"), errors.c_str());
424 if(m_ui->m_polygonsRadioButton->isChecked())
434 if (m_ui->m_maskRadioButton->isChecked())
435 referenceLayer = getReferenceLayer();
437 m_outputDataSetName = m_ui->m_newLayerNameLineEdit->text().toStdString();
447 cellSpaceOp->createCellSpace(m_outDataSourceInfo, m_outputDataSetName,
448 getResX(), getResY(), getEnvelope(), m_bbSRID,
455 this->setCursor(Qt::ArrowCursor);
456 QMessageBox::warning(
this, tr(
"Cellular Spaces"), e.
what());
462 this->setCursor(Qt::ArrowCursor);
469 m_ui->m_unitComboBox->addItem(
"UNIT");
478 m_ui->m_unitComboBox->addItem(uptr->getName().c_str(), QVariant::fromValue(uptr));
486 setResolutionUnit(unit);
489 m_ui->m_unitComboBox->setCurrentIndex(0);
494 m_ui->m_llxLineEdit->setText(QString::number(env.
m_llx,
'f',precision));
495 m_ui->m_llyLineEdit->setText(QString::number(env.
m_lly,
'f',precision));
496 m_ui->m_urxLineEdit->setText(QString::number(env.
m_urx,
'f',precision));
497 m_ui->m_uryLineEdit->setText(QString::number(env.
m_ury,
'f',precision));
504 m_ui->m_unitComboBox->setCurrentIndex(0);
508 for(
int i = 0; i < m_ui->m_unitComboBox->count(); ++i)
510 std::string name = m_ui->m_unitComboBox->itemText(i).toStdString();
512 if(unit->getName() == name)
514 m_ui->m_unitComboBox->setCurrentIndex(i);
522 QVariant varUnit = m_ui->m_unitComboBox->itemData(m_ui->m_unitComboBox->currentIndex(), Qt::UserRole);
528 QVariant varLayer = m_ui->m_layersComboBox->itemData(m_ui->m_layersComboBox->currentIndex(), Qt::UserRole);
535 env.
m_llx = m_ui->m_llxLineEdit->text().toDouble();
536 env.
m_lly = m_ui->m_llyLineEdit->text().toDouble();
537 env.
m_urx = m_ui->m_urxLineEdit->text().toDouble();
538 env.
m_ury = m_ui->m_uryLineEdit->text().toDouble();
545 m_ui->m_llxLineEdit->clear();
546 m_ui->m_llyLineEdit->clear();
547 m_ui->m_urxLineEdit->clear();
548 m_ui->m_uryLineEdit->clear();
554 double resX = m_ui->m_resXLineEdit->text().toDouble();
565 double resY = m_ui->m_resYLineEdit->text().toDouble();
603 return (convertAngleToPlanar(res, to));
606 return (convertPlanarToAngle(res, from));
613 switch (planar->getId())
632 switch (planar->getId())
651 m_ui->m_resXLineEdit->clear();
652 m_ui->m_resYLineEdit->clear();
653 m_ui->m_colsLineEdit->clear();
654 m_ui->m_rowsLineEdit->clear();
662 m_ui->m_resSRIDLabel->setText(name.c_str());
666 m_ui->m_resSRIDLabel->setText(
"No SRS defined");
672 bool noErrors =
true;
673 errors = tr(
"Consistency errors:").toStdString();
678 errors +=
"\n - " + tr(
"Invalid bounding box").toStdString();
682 double resX = getResX();
683 if (resX < 0. || resX >= env.
getWidth())
687 errors +=
"\n - " + tr(
"X resolution must be greater than 0 and smaller than the bounding box Width ").toStdString() + txt;
691 double resY = getResY();
692 if (resY < 0. || resY >= env.
getHeight())
696 errors +=
"\n - " + tr(
"Y resolution must be greater than 0 and smaller than the bounding box Height ").toStdString() + txt;
700 std::string name = m_ui->m_newLayerNameLineEdit->text().toStdString();
703 errors +=
"\n - " + tr(
"Output layer name is empty").toStdString();
707 if (m_ui->m_maskRadioButton->isChecked())
710 if (!((referenceLayer->getSRID() > 0 && m_bbSRID > 0) ||
711 (referenceLayer->getSRID() <= 0 && m_bbSRID <= 0)))
713 errors +=
"\n - " + tr(
"Reference layer and output layer have incompatible SRSs. It is not possible to create cells using polygons as masks.").toStdString();
const_iterator begin() const
It returns an iterator to the beginning of the conteiner.
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
void onTargetDatasourceToolButtonClicked()
This class provide cellular spaces operations.
boost::shared_ptr< DataSetType > DataSetTypePtr
te::gm::Envelope getEnvelope()
boost::shared_ptr< DataSource > DataSourcePtr
void onTargetFileToolButtonClicked()
void onSrsToolButtonClicked()
bool checkList(std::string &errors)
virtual const char * what() const
It outputs the exception message.
void setResolutionUnit(te::common::UnitOfMeasurePtr unit)
double m_urx
Upper right corner x-coordinate.
double getWidth() const
It returns the envelope width.
void onCreatePushButtonClicked()
const_iterator end() const
It returns an iterator to the end of the conteiner.
bool convertPlanarToAngle(double &val, te::common::UnitOfMeasurePtr planar)
void onEnvelopeChanged(const QString &text)
double m_llx
Lower left corner x-coordinate.
void removeViewer(int viewerId)
Dettach a progress viewer.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
bool unitConvertion(double &res, te::common::UnitOfMeasurePtr from, te::common::UnitOfMeasurePtr to)
An Envelope defines a 2D rectangular region.
void onLayersComboBoxChanged(int index)
std::map< unsigned int, UnitOfMeasurePtr >::const_iterator const_iterator
void onUnitComboBoxChanged(int index)
double getConversion(const std::string &unitFromName, const std::string &unitToName) const
Calculates a multiplicative factor to convert from a given unit to its base unit and vice-versa...
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
double m_lly
Lower left corner y-coordinate.
void initUnitsOfMeasure()
~CreateCellularSpaceDialog()
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
double m_ury
Upper right corner y-coordinate.
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
void showEnvelope(const te::gm::Envelope &env, int precision)
te::map::AbstractLayerPtr getLayer()
te::map::AbstractLayerPtr getReferenceLayer()
A class that represents a data source component.
void onResYLineEditEditingFinished()
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.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr)
double getHeight() const
It returns the envelope height.
void onResXLineEditEditingFinished()
bool isValid() const
It tells if the rectangle is valid or not.
te::common::UnitOfMeasurePtr getResolutionUnit()
std::auto_ptr< Ui::CreateCellularSpaceDialogForm > m_ui
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
bool convertAngleToPlanar(double &val, te::common::UnitOfMeasurePtr planar)
CreateCellularSpaceDialog(QWidget *parent=0, Qt::WindowFlags f=0)