MapLayerChoiceOutside.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/layout/qt/outside/MapLayerChoiceOutside.cpp
22 
23  \brief A dialog configure the input layer to address geocoding operation
24 */
25 
26 // TerraLib
27 #include "../../../common/Logger.h"
28 #include "../../../common/progress/ProgressManager.h"
29 #include "../../../common/Translator.h"
30 #include "../../../common/STLUtils.h"
31 #include "../../../dataaccess/dataset/DataSetType.h"
32 #include "../../../dataaccess/dataset/ObjectIdSet.h"
33 #include "../../../dataaccess/datasource/DataSourceCapabilities.h"
34 #include "../../../dataaccess/datasource/DataSourceInfo.h"
35 #include "../../../dataaccess/datasource/DataSourceInfoManager.h"
36 #include "../../../dataaccess/datasource/DataSourceFactory.h"
37 #include "../../../dataaccess/datasource/DataSourceManager.h"
38 #include "../../../dataaccess/utils/Utils.h"
39 #include "../../../datatype/Enums.h"
40 #include "../../../datatype/Property.h"
41 #include "../../../geometry/GeometryProperty.h"
42 #include "../../../maptools/AbstractLayer.h"
43 #include "../../../postgis/Transactor.h"
44 #include "../../../qt/af/Utils.h"
45 #include "../../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h"
46 #include "../../../qt/widgets/layer/utils/DataSet2Layer.h"
47 #include "../../../qt/widgets/progress/ProgressViewerDialog.h"
48 #include "../../../qt/widgets/utils/DoubleListWidget.h"
49 #include "../../../statistics/core/Utils.h"
50 #include "MapLayerChoiceOutside.h"
51 #include "ui_MapLayerChoice.h"
52 
53 // Qt
54 #include <QFileDialog>
55 #include <QGridLayout>
56 #include <QMessageBox>
57 
58 // Boost
59 #include <boost/algorithm/string.hpp>
60 #include <boost/filesystem.hpp>
61 #include <boost/uuid/random_generator.hpp>
62 #include <boost/uuid/uuid_io.hpp>
63 
64 #include <iostream>
65 #include <string>
66 
68  : QDialog(0),
69  OutsideObserver(controller, o),
70  m_ui(new Ui::MapLayerChoice),
71  m_layers(std::list<te::map::AbstractLayerPtr>())
72 {
73 // add controls
74  m_ui->setupUi(this);
75 
76  m_widget.reset(new te::qt::widgets::DoubleListWidget(m_ui->m_widget));
77 
78  QGridLayout* displayLayout = new QGridLayout(m_ui->m_widget);
79  displayLayout->addWidget(m_widget.get());
80 
81  m_widget->setLeftLabel("Available Attributes");
82  m_widget->setRightLabel("Selected Attributes");
83 
84  connect(m_ui->m_okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButtonClicked()));
85  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
86 
87 }
88 
90 {
91 }
92 
93 void te::layout::MapLayerChoiceOutside::setLayers(std::list<te::map::AbstractLayerPtr> layers)
94 {
95  m_layers = layers;
96 }
97 
99 {
100  return m_selectedLayer;
101 }
102 
104 {
105  return m_dataSource;
106 }
107 
109 {
110  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(m_selectedLayer.get());
111  if(!dsLayer)
112  {
113  QMessageBox::information(this, "Address Geocoding", "Can not execute this operation on this type of layer.");
114  return;
115  }
116 
117  m_dataSource = te::da::GetDataSource(dsLayer->getDataSourceId(), true);
118  if(!m_dataSource.get())
119  {
120  QMessageBox::information(this, "Address Geocoding", "The selected input data source can not be accessed.");
121  }
122 
123  m_selectedProps = m_widget->getOutputValues();
124  if(m_selectedProps.empty())
125  {
126  QMessageBox::information(this, "Address Geocoding", "Select at least one attribute.");
127  return;
128  }
129 
130 //Checks whether the table already contains a column called tsvector.
131  std::auto_ptr<te::da::DataSetType> schema = m_selectedLayer->getSchema();
132  const std::vector<te::dt::Property*>& properties = schema->getProperties();
133 
134  bool addNewColumn = true;
135  for(std::size_t i = 0; i < properties.size(); ++i)
136  {
137  std::string name = properties[i]->getName();
138  if(name == "tsvector")
139  addNewColumn = false;
140  }
141 
142 // ALTER TABLE adding a new columns of tsvector type.
143  if(addNewColumn == true)
144  {
145  std::auto_ptr<te::da::DataSourceTransactor> trans = m_dataSource->getTransactor();
146  std::string alterTable = "ALTER TABLE "+ m_selectedLayer->getTitle() + " ADD tsvector tsvector";
147  trans->execute(alterTable);
148  }
149 
150 
151 
152 // UPDATE values in tsvector column.
153  std::string updateTable = "UPDATE " + m_selectedLayer->getTitle() + " SET tsvector = to_tsvector('english', ";
154 
155  for(std::size_t selProps = 0; selProps < m_selectedProps.size(); ++selProps)
156  {
157  if(selProps == 0)
158  updateTable += " "+ m_selectedProps[selProps];
159  else
160  updateTable += "||' '||"+ m_selectedProps[selProps];
161  }
162 
163  updateTable += ")";
164 
165  m_dataSource->execute(updateTable);
166 
167 
168  unsigned dot = m_selectedLayer->getTitle().find_last_of(".");
169  std::string table = m_selectedLayer->getTitle().substr(dot+1);
170 
171 //DROP INDEX if exists.
172  std::string dropIndex = "DROP INDEX IF EXISTS " + table +"_idx";
173  m_dataSource->execute(dropIndex);
174 
175 //CREATE INDEX to speed up the text search.
176  std::string createIndex = "CREATE INDEX "+ table +"_idx ON "+ m_selectedLayer->getTitle() + " USING GIN(tsvector)";
177 
178  m_dataSource->execute(createIndex);
179 
180  this->accept();
181 }
182 
184 {
185  reject();
186 }
187 
189 {
190  setVisible(context.isShow());
191  if(context.isShow() == true)
192  show();
193  else
194  hide();
195 }
196 
197 void te::layout::MapLayerChoiceOutside::setPosition( const double& x, const double& y )
198 {
199  move(x,y);
200  refresh();
201 }
202 
204 {
205  QPointF posF = pos();
206  qreal valuex = posF.x();
207  qreal valuey = posF.y();
208 
209  te::gm::Coord2D coordinate;
210  coordinate.x = valuex;
211  coordinate.y = valuey;
212 
213  return coordinate;
214 }
215 
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Definition: Utils.cpp:262
Abstract class to represent an observable. "Model" part of MVC component.
Definition: Observable.h:56
Class responsible for maintaining the drawing context of a MVC component. It is always used by the "M...
Definition: ContextItem.h:49
double y
y-coordinate.
Definition: Coord2D.h:114
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
double x
x-coordinate.
Definition: Coord2D.h:113
A dialog configure the input layer to address geocoding operation.
virtual te::gm::Coord2D getPosition()
Method that returns the position llx, lly Reimplement this function in a ItemObserver subclass to pro...
Abstract class to represent an observer. "View" part of MVC widget. All classes representing the grap...
void setLayers(std::list< te::map::AbstractLayerPtr > layers)
Set the layer that can be used.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
virtual void setPosition(const double &x, const double &y)
Change coordinate llx,lly of the MVC widget. Reimplement this function in a ItemController subclass t...
std::auto_ptr< te::qt::widgets::DoubleListWidget > m_widget
te::map::AbstractLayerPtr getLayer()
MapLayerChoiceOutside(OutsideController *controller, Observable *o)
const std::string & getDataSourceId() const
URI C++ Library.
virtual void updateObserver(ContextItem context)
Reimplemented from Observer.
std::auto_ptr< Ui::MapLayerChoice > m_ui
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
Abstract class to represent a controller. "Controller" part of MVC widget. All classes representing t...
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr