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 "../../../srs/SpatialReferenceSystemManager.h"
44 #include "../../widgets/datasource/selector/DataSourceSelectorDialog.h"
45 #include "ui_CreateCellularSpaceDialogForm.h"
49 #include <QFileDialog>
50 #include <QMessageBox>
51 #include <QToolButton>
54 #include <boost/filesystem.hpp>
55 #include <boost/uuid/random_generator.hpp>
56 #include <boost/uuid/uuid_io.hpp>
65 m_outputDataSetName(
""),
66 m_ui(new Ui::CreateCellularSpaceDialogForm)
72 m_ui->m_maskToolButton->setIcon(QIcon::fromTheme(
"cellspace-mask-hint"));
73 m_ui->m_noMaskToolButton->setIcon(QIcon::fromTheme(
"cellspace-no-mask-hint"));
74 m_ui->m_polygonsToolButton->setIcon(QIcon::fromTheme(
"cellspace-polygons-hint"));
75 m_ui->m_pointsToolButton->setIcon(QIcon::fromTheme(
"cellspace-points-hint"));
76 m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme(
"datasource"));
77 m_ui->m_srsToolButton->setIcon(QIcon::fromTheme(
"srs"));
80 m_ui->m_llxLineEdit->setValidator(
new QDoubleValidator(
this));
81 m_ui->m_llyLineEdit->setValidator(
new QDoubleValidator(
this));
82 m_ui->m_urxLineEdit->setValidator(
new QDoubleValidator(
this));
83 m_ui->m_uryLineEdit->setValidator(
new QDoubleValidator(
this));
84 m_ui->m_resXLineEdit->setValidator(
new QDoubleValidator(0, 99999999, 8,
this));
85 m_ui->m_resYLineEdit->setValidator(
new QDoubleValidator(0, 99999999, 8,
this));
86 m_ui->m_rowsLineEdit->setValidator(
new QIntValidator(
this));
87 m_ui->m_colsLineEdit->setValidator(
new QIntValidator(
this));
98 connect(
m_ui->m_llxLineEdit, SIGNAL(textChanged(
const QString &)),
this, SLOT(
onEnvelopeChanged(
const QString &)));
99 connect(
m_ui->m_llyLineEdit, SIGNAL(textChanged(
const QString &)),
this, SLOT(
onEnvelopeChanged(
const QString &)));
100 connect(
m_ui->m_urxLineEdit, SIGNAL(textChanged(
const QString &)),
this, SLOT(
onEnvelopeChanged(
const QString &)));
101 connect(
m_ui->m_uryLineEdit, SIGNAL(textChanged(
const QString &)),
this, SLOT(
onEnvelopeChanged(
const QString &)));
109 m_ui->m_helpPushButton->setNameSpace(
"dpi.inpe.br.plugins");
110 m_ui->m_helpPushButton->setPageReference(
"plugins/cellspace/cellspace.html");
121 m_ui->m_srsToolButton->setEnabled(
true);
122 m_ui->m_referenceGroupBox->setChecked(
false);
123 m_ui->m_referenceGroupBox->setEnabled(
false);
128 std::vector<std::string> nonSridLayers;
129 bool hasNonSridLayer =
false;
131 std::list<te::map::AbstractLayerPtr>::iterator it = layers.begin();
133 while(it != layers.end())
135 if(it->get()->getSRID() > 0)
137 std::auto_ptr<te::da::DataSetType> dsType = it->get()->getSchema();
138 if(dsType->hasGeom())
139 m_ui->m_layersComboBox->addItem(QString(it->get()->getTitle().c_str()), QVariant::fromValue(*it));
143 nonSridLayers.push_back(it->get()->getTitle());
144 hasNonSridLayer =
true;
151 QString w(tr(
"Some Layers were not added because they have no SRID:"));
153 for(std::size_t i = 0; i < nonSridLayers.size(); ++i)
154 w.append(nonSridLayers[i].c_str()).append(
"\n");
155 QMessageBox::warning(
this, tr(
"Cellular Spaces"), w);
158 if(nonSridLayers.size() == layers.size())
160 m_ui->m_srsToolButton->setEnabled(
true);
161 m_ui->m_referenceGroupBox->setChecked(
false);
162 m_ui->m_referenceGroupBox->setEnabled(
false);
168 m_currentSRID = layer->getSRID();
182 m_currentSRID = layer->getSRID();
193 m_ui->m_unitComboBox->setCurrentIndex(1);
197 setCurrentUnit(unit);
203 double resX = getResX();
209 QMessageBox::warning(
this, tr(
"Cellular Spaces"), tr(
"Invalid envelope!"));
213 int maxCols = (int)ceil((env.
m_urx - env.
m_llx)/resX);
215 m_ui->m_colsLineEdit->setText(QString::number(maxCols));
220 double resY = getResY();
226 QMessageBox::warning(
this, tr(
"Cellular Spaces"), tr(
"Invalid envelope!"));
230 int maxRows = (int)ceil((env.
m_ury - env.
m_lly)/resY);
232 m_ui->m_rowsLineEdit->setText(QString::number(maxRows));
237 int cols = m_ui->m_colsLineEdit->text().toInt();
243 QMessageBox::warning(
this, tr(
"Cellular Spaces"), tr(
"Invalid envelope!"));
263 if(layerUnit != currentUnit)
276 m_ui->m_resXLineEdit->setText(QString::number(resX));
281 int rows = m_ui->m_rowsLineEdit->text().toInt();
287 QMessageBox::warning(
this, tr(
"Cellular Spaces"), tr(
"Invalid envelope!"));
307 if(layerUnit != currentUnit)
320 m_ui->m_resYLineEdit->setText(QString::number(resY));
331 if(!checkList(errors))
333 QMessageBox::warning(
this, tr(
"Cellular Spaces"), errors.c_str());
337 m_outputDataSetName = m_ui->m_newLayerNameLineEdit->text().toStdString();
342 referenceLayer = getReferenceLayer();
344 double resX = getResX();
345 double resY = getResY();
346 bool isPolygonsAsMask = m_ui->m_maskToolButton->isChecked();
352 if(m_ui->m_polygonsToolButton->isChecked())
356 else if(m_ui->m_pointsToolButton->isChecked())
368 cellSpaceOp->createCellSpace(m_outDataSourceInfo, m_outputDataSetName, resX, resY, getEnvelope(), m_currentSRID, type);
370 cellSpaceOp->createCellSpace(m_outDataSourceInfo, m_outputDataSetName, referenceLayer, resX, resY, isPolygonsAsMask, type);
375 this->setCursor(Qt::ArrowCursor);
376 QMessageBox::warning(
this, tr(
"Cellular Spaces"), e.
what());
382 this->setCursor(Qt::ArrowCursor);
389 m_ui->m_newLayerNameLineEdit->clear();
390 m_ui->m_repositoryLineEdit->clear();
394 std::string accessDriver;
396 extension = tr(
"Shapefile (*.shp *.SHP);;");
397 accessDriver =
"OGR";
399 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."),
400 QString(), extension, 0, QFileDialog::DontConfirmOverwrite);
402 if (fileName.isEmpty())
405 boost::filesystem::path outfile(fileName.toStdString());
406 std::string aux = outfile.leaf().string();
407 m_ui->m_newLayerNameLineEdit->setText(aux.c_str());
408 aux = outfile.string();
409 m_ui->m_repositoryLineEdit->setText(aux.c_str());
413 m_ui->m_newLayerNameLineEdit->setEnabled(
false);
415 std::map<std::string, std::string> connInfo;
416 connInfo[
"URI"] = m_ui->m_repositoryLineEdit->text().toStdString();
420 m_outDataSourceInfo->setAccessDriver(accessDriver);
421 m_outDataSourceInfo->setConnInfo(connInfo);
426 m_ui->m_newLayerNameLineEdit->clear();
427 m_ui->m_repositoryLineEdit->clear();
432 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
434 if(dsPtrList.size() <= 0)
437 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
439 m_outDataSourceInfo = *it;
441 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
445 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
464 if(layerUnit != currentUnit)
468 QMessageBox::warning(
this, tr(
"Cellular Spaces"), tr(
"Unable to convert between the measuring unit of the layer projection and the selected!"));
483 m_ui->m_unitComboBox->addItem(uptr->getName().c_str(), QVariant::fromValue(uptr));
487 if(m_currentSRID > 0)
490 setCurrentUnit(unit);
494 m_ui->m_unitComboBox->setCurrentIndex(1);
502 env.
m_llx = m_ui->m_llxLineEdit->text().toDouble();
503 env.
m_lly = m_ui->m_llyLineEdit->text().toDouble();
504 env.
m_urx = m_ui->m_urxLineEdit->text().toDouble();
505 env.
m_ury = m_ui->m_uryLineEdit->text().toDouble();
513 sprintf(txt,
"%.8g",env.
m_llx);
514 m_ui->m_llxLineEdit->setText(QString(txt));
515 sprintf(txt,
"%.8g",env.
m_lly);
516 m_ui->m_llyLineEdit->setText(QString(txt));
517 sprintf(txt,
"%.8g",env.
m_urx);
518 m_ui->m_urxLineEdit->setText(QString(txt));
519 sprintf(txt,
"%.8g",env.
m_ury);
520 m_ui->m_uryLineEdit->setText(QString(txt));
527 env.
m_llx = m_ui->m_llxLineEdit->text().toDouble();
528 env.
m_lly = m_ui->m_llyLineEdit->text().toDouble();
529 env.
m_urx = m_ui->m_urxLineEdit->text().toDouble();
530 env.
m_ury = m_ui->m_uryLineEdit->text().toDouble();
537 QVariant varLayer = m_ui->m_layersComboBox->itemData(m_ui->m_layersComboBox->currentIndex(), Qt::UserRole);
543 QVariant varUnit = m_ui->m_unitComboBox->itemData(m_ui->m_unitComboBox->currentIndex(), Qt::UserRole);
549 for(
int i = 0; i < m_ui->m_unitComboBox->count(); ++i)
551 std::string name = m_ui->m_unitComboBox->itemText(i).toStdString();
553 if(unit->getName() == name)
555 m_ui->m_unitComboBox->setCurrentIndex(i);
563 bool noErrors =
true;
565 std::string name = m_ui->m_newLayerNameLineEdit->text().toStdString();
567 double resX = m_ui->m_resXLineEdit->text().toDouble();
568 double resY = m_ui->m_resYLineEdit->text().toDouble();
570 errors = tr(
"Some information is missing:").toStdString();
574 errors +=
"\n - " + tr(
"Output name").toStdString();
578 if(m_currentSRID <= 0)
580 errors +=
"\n - " + tr(
"Projection").toStdString();
588 errors +=
"\n - " + tr(
"Invalid envelope").toStdString();
595 if (resX < 0. || resX >= lWidth)
598 sprintf(txt,
"%.8g",lWidth);
599 errors +=
"\n - " + tr(
"Resolution in X must be > 0 and < ").toStdString() + txt;
603 if (resY < 0. || resY >= lHeight)
606 sprintf(txt,
"%.8g",lHeight);
607 errors +=
"\n - " + tr(
"Resolution in X must be > 0 and < ").toStdString() + txt;
619 boost::uuids::basic_random_generator<boost::mt19937> gen;
620 boost::uuids::uuid u = gen();
621 std::string id_ds = boost::uuids::to_string(u);
623 boost::filesystem::path uri(m_outDataSourceInfo->getConnInfo()[
"URI"]);
626 ds->setConnInfo(m_outDataSourceInfo->getConnInfo());
627 ds->setTitle(uri.stem().string());
628 ds->setAccessDriver(
"OGR");
630 ds->setDescription(uri.string());
636 m_outDataSourceInfo = ds;
638 m_outputDataSetName = uri.stem().string();
644 outDataSource->open();
653 if(isNoReference() || layer->getSRID() <= 0)
654 layer->setSRID(m_currentSRID);
662 srsDialog.setWindowTitle(tr(
"Choose the SRS"));
664 if(srsDialog.exec() == QDialog::Rejected)
673 setCurrentUnit(unit);
681 m_ui->m_llxLineEdit->clear();
682 m_ui->m_llyLineEdit->clear();
683 m_ui->m_urxLineEdit->clear();
684 m_ui->m_uryLineEdit->clear();
689 m_ui->m_resXLineEdit->clear();
690 m_ui->m_resYLineEdit->clear();
691 m_ui->m_colsLineEdit->clear();
692 m_ui->m_rowsLineEdit->clear();
699 m_ui->m_sridLabel->setText(name.c_str());
704 double resX = m_ui->m_resXLineEdit->text().toDouble();
720 if(layerUnit != currentUnit)
738 double resY = m_ui->m_resYLineEdit->text().toDouble();
754 if(layerUnit != currentUnit)
772 m_ui->m_srsToolButton->setEnabled(!isToggled);
781 onLayersComboBoxChanged(0);
787 if(m_ui->m_referenceGroupBox->isChecked())
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()
void onReferenceGroupBoxToggled(bool isToggled)
This class provide cellular spaces operations.
boost::shared_ptr< DataSetType > DataSetTypePtr
te::common::UnitOfMeasurePtr getCurrentUnit()
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.
double m_urx
Upper right corner x-coordinate.
double getWidth() const
It returns the envelope width.
void onRowsLineEditEditingFinished()
void onCreatePushButtonClicked()
const_iterator end() const
It returns an iterator to the end of the conteiner.
void setCurrentUnit(te::common::UnitOfMeasurePtr unit)
void onEnvelopeChanged(const QString &text)
double m_llx
Lower left corner x-coordinate.
void removeViewer(int viewerId)
Dettach a progress viewer.
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
void onLayersComboBoxChanged(int index)
std::map< unsigned int, UnitOfMeasurePtr >::const_iterator const_iterator
void onColsLineEditEditingFinished()
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)
te::map::AbstractLayerPtr getLayer()
te::map::AbstractLayerPtr getReferenceLayer()
A class that represents a data source component.
void onResYLineEditEditingFinished()
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
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.
std::auto_ptr< Ui::CreateCellularSpaceDialogForm > m_ui
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
void showEnvelope(const te::gm::Envelope env)
te::gm::Envelope getOutputEnvelope()
CreateCellularSpaceDialog(QWidget *parent=0, Qt::WindowFlags f=0)