src/terralib/edit/qt/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/edit/qt/Utils.cpp
22 
23  \brief Utility Qt functions for TerraLib Edit module.
24 */
25 
26 // TerraLib
27 #include "../../core/translator/Translator.h"
28 #include "../../dataaccess/datasource/DataSource.h"
29 #include "../../dataaccess/datasource/DataSourceFactory.h"
30 #include "../../dataaccess/datasource/DataSourceManager.h"
31 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
32 #include "../../dataaccess/utils/Utils.h"
33 #include "../../datatype/SimpleProperty.h"
34 #include "../../geometry/GeometryProperty.h"
35 #include "../../qt/widgets/canvas/Canvas.h"
36 #include "../../qt/widgets/Utils.h"
37 #include "../../qt/widgets/layer/utils/DataSet2Layer.h"
38 #include "../../srs/Config.h"
39 #include "../Utils.h"
40 #include "Utils.h"
41 
42 // Qt
43 #include <QColor>
44 #include <QFileDialog>
45 #include <QMessageBox>
46 
47 // STL
48 #include <cassert>
49 
50 // Boost
51 #include <boost/filesystem.hpp>
52 #include <boost/uuid/random_generator.hpp>
53 #include <boost/uuid/uuid_io.hpp>
54 
55 QPointF te::edit::GetPosition(QMouseEvent* e)
56 {
57 #if QT_VERSION >= 0x050000
58  return e->localPos();
59 #else
60  return e->posF();
61 #endif
62 }
63 
65 {
66 
68 
69  //get file path
70  QString fileName = QFileDialog::getSaveFileName(nullptr, TE_TR("Create Draft Layer..."), QString(), TE_TR("Shapefile (*.shp *.SHP);;"));
71 
72  if (fileName.isEmpty())
73  return layer;
74 
75  //create draft layer
76  boost::filesystem::path outfile(fileName.toUtf8().data());
77  std::string layerName = outfile.stem().string();
78 
79  //create data source info
80  std::string outputdataset = layerName;
81  std::size_t idx = outputdataset.find(".");
82  if (idx != std::string::npos)
83  outputdataset = outputdataset.substr(0, idx);
84 
85  std::string connInfo("file://");
86  connInfo += fileName.toUtf8().data();
87 
88  // let's include the new datasource in the managers
89  boost::uuids::basic_random_generator<boost::mt19937> gen;
90  boost::uuids::uuid u = gen();
91  std::string id = boost::uuids::to_string(u);
92 
94  dsInfo->setConnInfo(connInfo);
95  dsInfo->setTitle(layerName);
96  dsInfo->setAccessDriver("OGR");
97  dsInfo->setType("OGR");
98  dsInfo->setDescription(layerName);
99  dsInfo->setId(id);
100 
102  {
103  //create dataSetType
104  std::unique_ptr<te::da::DataSetType> dsType(new te::da::DataSetType(layerName));
105 
107  dsType->add(nameProperty);
108 
109  te::dt::SimpleProperty* descriptionProperty = new te::dt::SimpleProperty("descr", te::dt::STRING_TYPE);
110  dsType->add(descriptionProperty);
111 
112  te::gm::GeometryProperty* geomProperty = new te::gm::GeometryProperty("geom", srid, type);
113  dsType->add(geomProperty);
114 
115  {
116  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("OGR", connInfo);
117  ds->open();
118 
119  std::map<std::string, std::string> nopt;
120 
121  try
122  {
123  ds->createDataSet(dsType.get(), nopt);
124  }
125  catch (...)
126  {
127  return layer;
128  }
129  }
130 
132 
133  //create layer
134  te::qt::widgets::DataSet2Layer converter(id);
135  te::da::DataSetTypePtr dt(dsType.release());
136  layer = converter(dt);
137  layer->setVisibility(te::map::VISIBLE);
138 
139  return layer;
140  }
141  else
142  {
143  dsInfo = te::da::DataSourceInfoManager::getInstance().getByConnInfo(dsInfo->getConnInfoAsString());
144  te::da::DataSourcePtr dsPtr = te::da::GetDataSource(dsInfo->getId());
145 
146  //Overwrite the selected draft layer
147  dsPtr->dropDataSet(layerName);
148 
149  //create dataSetType
150  std::unique_ptr<te::da::DataSetType> dsType(new te::da::DataSetType(layerName));
151 
153  dsType->add(nameProperty);
154 
155  te::dt::SimpleProperty* descriptionProperty = new te::dt::SimpleProperty("descr", te::dt::STRING_TYPE);
156  dsType->add(descriptionProperty);
157 
158  te::gm::GeometryProperty* geomProperty = new te::gm::GeometryProperty("geom", srid, type);
159  dsType->add(geomProperty);
160 
161  {
162  std::map<std::string, std::string> nopt;
163  if (!dsPtr->isOpened())
164  dsPtr->open();
165 
166  try
167  {
168  dsPtr->createDataSet(dsType.get(), nopt);
169  }
170  catch (...)
171  {
172  return layer;
173  }
174  }
175 
176  //create layer
177  te::qt::widgets::DataSet2Layer converter(dsInfo->getId());
178  te::da::DataSetTypePtr dt(std::move(dsType));
179  layer = converter(dt);
180  layer->setVisibility(te::map::VISIBLE);
181 
182  return layer;
183  }
184 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Geometric property.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
An atomic property like an integer or double.
boost::shared_ptr< DataSource > DataSourcePtr
A class that models the description of a dataset.
Definition: DataSetType.h:72
static te::dt::Date ds(2010, 01, 01)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
TEEDITQTEXPORT te::map::AbstractLayerPtr CreateShapeFileLayer(const te::gm::GeomType &type, const int &srid)
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
static te::dt::TimeDuration dt(20, 30, 50, 11)
TEEDITQTEXPORT QPointF GetPosition(QMouseEvent *e)
Utility functions for the data access module.
A class that represents a data source component.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr