VectorToVectorOp.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 VectorToVectorOp.cpp
22  */
23 
24 #include "../core/logger/Logger.h"
25 #include "../dataaccess/dataset/DataSet.h"
26 #include "../dataaccess/dataset/DataSetAdapter.h"
27 #include "../dataaccess/dataset/DataSetType.h"
28 #include "../dataaccess/dataset/DataSetTypeConverter.h"
29 #include "../dataaccess/dataset/ObjectIdSet.h"
30 #include "../dataaccess/datasource/DataSource.h"
31 #include "../dataaccess/datasource/DataSourceCapabilities.h"
32 #include "../dataaccess/datasource/DataSourceTransactor.h"
33 #include "../dataaccess/utils/Utils.h"
34 
35 #include "../datatype/Property.h"
36 #include "../datatype/StringProperty.h"
37 
38 #include "../geometry/GeometryProperty.h"
39 
40 #include "../statistics/core/Utils.h"
41 
42 #include "VectorToVectorOp.h"
43 
45  m_hasErrors(false),
46  m_outDset("")
47 {
48 }
49 
51 
54 {
55  m_fromLayer = fromLayer;
56  m_toLayer = toLayer;
57 }
58 
59 void te::attributefill::VectorToVectorOp::setParams(const std::map<std::string, std::vector<te::attributefill::OperationType> >& options, std::vector<std::string> toLayerProps)
60 {
61  m_options = options;
62  m_toLayerProps = toLayerProps;
63 }
64 
66 {
67  m_outDsrc = outDsrc;
68  m_outDset = dsname;
69 }
70 
72 {/*
73  if (!m_inDsetType.get())
74  return false;
75 
76  if (!m_inDsetType->hasGeom())
77  return false;
78 
79  if (m_groupProps.empty())
80  return false;
81 
82  if (m_outDset.empty() || !m_outDsrc.get())
83  return false;
84  */
85  return true;
86 }
87 
88 bool te::attributefill::VectorToVectorOp::save(std::unique_ptr<te::mem::DataSet> result, std::unique_ptr<te::da::DataSetType> outDsType)
89 {
90  std::unique_ptr<te::da::DataSourceTransactor> t;
91  try
92  {
93  // do any adaptation necessary to persist the output dataset
94  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(outDsType.get(), m_outDsrc->getCapabilities(), m_outDsrc->getEncoding());
95  te::da::DataSetType* dsTypeResult = converter->getResult();
96  std::unique_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(result.get(), converter));
97 
98  // create the primary key if it is possible
99  if (m_outDsrc->getCapabilities().getDataSetTypeCapabilities().supportsPrimaryKey())
100  {
101  std::string pk_name = dsTypeResult->getName() + "_pkey";
102  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pk_name, dsTypeResult);
103  pk->add(dsTypeResult->getProperty(0));
104  outDsType->setPrimaryKey(pk);
105  }
106 
107  t = m_outDsrc->getTransactor();
108 
109  t->begin();
110 
111  std::map<std::string, std::string> options;
112  // create the dataset
113  m_outDsrc->createDataSet(dsTypeResult, options);
114 
115  // copy from memory to output datasource
116  result->moveBeforeFirst();
117  m_outDsrc->add(dsTypeResult->getName(),result.get(), options);
118 
119  t->commit();
120  }
121  catch(te::common::Exception& e)
122  {
123 #ifdef TERRALIB_LOGGER_ENABLED
124  std::string ex = e.what();
125  ex += " | Ref: Error while persisting in the DataSource";
126  TE_CORE_LOG_DEBUG("attributefill", ex);
127 #endif //TERRALIB_LOGGER_ENABLED
128  t->rollBack();
129  m_hasErrors = true;
130  }
131  catch(std::exception& e)
132  {
133 #ifdef TERRALIB_LOGGER_ENABLED
134  std::string ex = e.what();
135  ex += " | Ref: Error while persisting in the DataSource";
136  TE_CORE_LOG_DEBUG("attributefill", ex);
137 #endif //TERRALIB_LOGGER_ENABLED
138 
139  t->rollBack();
140  m_hasErrors = true;
141  }
142 
143  return true;
144 }
145 
147 {
148  return m_hasErrors;
149 }
bool save(std::unique_ptr< te::mem::DataSet > result, std::unique_ptr< te::da::DataSetType > outDsType)
std::vector< std::string > m_toLayerProps
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
void setInput(te::map::AbstractLayerPtr fromLayer, te::map::AbstractLayerPtr toLayer)
te::map::AbstractLayerPtr m_toLayer
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsName)
boost::shared_ptr< DataSource > DataSourcePtr
A class that models the description of a dataset.
Definition: DataSetType.h:72
virtual const char * what() const
It outputs the exception message.
#define TE_CORE_LOG_DEBUG(channel, message)
Use this tag in order to log a message to a specified logger with the DEBUG level.
Definition: Logger.h:225
std::map< std::string, std::vector< te::attributefill::OperationType > > m_options
void setParams(const std::map< std::string, std::vector< te::attributefill::OperationType > > &options, std::vector< std::string > toLayerProps)
An converter for DataSetType.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
Vector To Vector operation.
te::map::AbstractLayerPtr m_fromLayer
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr