All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  std::list<te::map::AbstractLayerPtr>::iterator it = m_layers.begin();
98 
99  while(it != m_layers.end())
100  {
101  std::auto_ptr<te::da::DataSetType> dsType = it->get()->getSchema();
102  if(dsType->hasGeom())
103  {
104  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(dsType.get());
105  int type = geomProp->getGeometryType();
106  }
107 
108  ++it;
109  }
110 }
111 
113 {
114  return m_selectedLayer;
115 }
116 
118 {
119  return m_dataSource;
120 }
121 
123 {
124  te::map::DataSetLayer* dsLayer = dynamic_cast<te::map::DataSetLayer*>(m_selectedLayer.get());
125  if(!dsLayer)
126  {
127  QMessageBox::information(this, "Address Geocoding", "Can not execute this operation on this type of layer.");
128  return;
129  }
130 
131  m_dataSource = te::da::GetDataSource(dsLayer->getDataSourceId(), true);
132  if(!m_dataSource.get())
133  {
134  QMessageBox::information(this, "Address Geocoding", "The selected input data source can not be accessed.");
135  }
136 
137  m_selectedProps = m_widget->getOutputValues();
138  if(m_selectedProps.empty())
139  {
140  QMessageBox::information(this, "Address Geocoding", "Select at least one attribute.");
141  return;
142  }
143 
144 //Checks whether the table already contains a column called tsvector.
145  std::auto_ptr<te::da::DataSetType> schema = m_selectedLayer->getSchema();
146  const std::vector<te::dt::Property*>& properties = schema->getProperties();
147 
148  bool addNewColumn = true;
149  for(std::size_t i = 0; i < properties.size(); ++i)
150  {
151  std::string name = properties[i]->getName();
152  if(name == "tsvector")
153  addNewColumn = false;
154  }
155 
156 // ALTER TABLE adding a new columns of tsvector type.
157  if(addNewColumn == true)
158  {
159  std::auto_ptr<te::da::DataSourceTransactor> trans = m_dataSource->getTransactor();
160  std::string alterTable = "ALTER TABLE "+ m_selectedLayer->getTitle() + " ADD tsvector tsvector";
161  trans->execute(alterTable);
162  }
163 
164 
165 
166 // UPDATE values in tsvector column.
167  std::string updateTable = "UPDATE " + m_selectedLayer->getTitle() + " SET tsvector = to_tsvector('english', ";
168 
169  for(std::size_t selProps = 0; selProps < m_selectedProps.size(); ++selProps)
170  {
171  if(selProps == 0)
172  updateTable += " "+ m_selectedProps[selProps];
173  else
174  updateTable += "||' '||"+ m_selectedProps[selProps];
175  }
176 
177  updateTable += ")";
178 
179  m_dataSource->execute(updateTable);
180 
181 
182  unsigned dot = m_selectedLayer->getTitle().find_last_of(".");
183  std::string table = m_selectedLayer->getTitle().substr(dot+1);
184 
185 //DROP INDEX if exists.
186  std::string dropIndex = "DROP INDEX IF EXISTS " + table +"_idx";
187  m_dataSource->execute(dropIndex);
188 
189 //CREATE INDEX to speed up the text search.
190  std::string createIndex = "CREATE INDEX "+ table +"_idx ON "+ m_selectedLayer->getTitle() + " USING GIN(tsvector)";
191 
192  m_dataSource->execute(createIndex);
193 
194  this->accept();
195 }
196 
198 {
199  reject();
200 }
201 
203 {
204  setVisible(context.isShow());
205  if(context.isShow() == true)
206  show();
207  else
208  hide();
209 }
210 
211 void te::layout::MapLayerChoiceOutside::setPosition( const double& x, const double& y )
212 {
213  move(x,y);
214  refresh();
215 }
216 
218 {
219  QPointF posF = pos();
220  qreal valuex = posF.x();
221  qreal valuey = posF.y();
222 
223  te::gm::Coord2D coordinate;
224  coordinate.x = valuex;
225  coordinate.y = valuey;
226 
227  return coordinate;
228 }
229 
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
Geometric property.
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
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
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...
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr