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 "../common/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 {
52 }
53 
56 {
57  m_fromLayer = fromLayer;
58  m_toLayer = toLayer;
59 }
60 
61 void te::attributefill::VectorToVectorOp::setParams(const std::map<std::string, std::vector<te::attributefill::OperationType> >& options, std::vector<std::string> toLayerProps)
62 {
63  m_options = options;
64  m_toLayerProps = toLayerProps;
65 }
66 
68 {
69  m_outDsrc = outDsrc;
70  m_outDset = dsname;
71 }
72 
74 {/*
75  if (!m_inDsetType.get())
76  return false;
77 
78  if (!m_inDsetType->hasGeom())
79  return false;
80 
81  if (m_groupProps.empty())
82  return false;
83 
84  if (m_outDset.empty() || !m_outDsrc.get())
85  return false;
86  */
87  return true;
88 }
89 
90 bool te::attributefill::VectorToVectorOp::save(std::auto_ptr<te::mem::DataSet> result, std::auto_ptr<te::da::DataSetType> outDsType)
91 {
92  std::auto_ptr<te::da::DataSourceTransactor> t;
93  try
94  {
95  // do any adaptation necessary to persist the output dataset
96  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(outDsType.get(), m_outDsrc->getCapabilities(), m_outDsrc->getEncoding());
97  te::da::DataSetType* dsTypeResult = converter->getResult();
98  std::auto_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(result.get(), converter));
99 
100  // create the primary key if it is possible
101  if (m_outDsrc->getCapabilities().getDataSetTypeCapabilities().supportsPrimaryKey())
102  {
103  std::string pk_name = dsTypeResult->getName() + "_pkey";
104  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pk_name, dsTypeResult);
105  pk->add(dsTypeResult->getProperty(0));
106  outDsType->setPrimaryKey(pk);
107  }
108 
109  t = m_outDsrc->getTransactor();
110 
111  t->begin();
112 
113  std::map<std::string, std::string> options;
114  // create the dataset
115  m_outDsrc->createDataSet(dsTypeResult, options);
116 
117  // copy from memory to output datasource
118  result->moveBeforeFirst();
119  m_outDsrc->add(dsTypeResult->getName(),result.get(), options);
120 
121  t->commit();
122  }
123  catch(te::common::Exception& e)
124  {
125 #ifdef TERRALIB_LOGGER_ENABLED
126  std::string ex = e.what();
127  ex += " | Ref: Error while persisting in the DataSource";
128  te::common::Logger::logDebug("attributefill", ex.c_str());
129 #endif //TERRALIB_LOGGER_ENABLED
130  t->rollBack();
131  m_hasErrors = true;
132  }
133  catch(std::exception& e)
134  {
135 #ifdef TERRALIB_LOGGER_ENABLED
136  std::string ex = e.what();
137  ex += " | Ref: Error while persisting in the DataSource";
138  te::common::Logger::logDebug("attributefill", ex.c_str());
139 #endif //TERRALIB_LOGGER_ENABLED
140 
141  t->rollBack();
142  m_hasErrors = true;
143  }
144 
145  return true;
146 }
147 
149 {
150  return m_hasErrors;
151 }
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)
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsName)
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
A class that models the description of a dataset.
Definition: DataSetType.h:72
virtual const char * what() const
It outputs the exception message.
Definition: Exception.cpp:58
void setParams(const std::map< std::string, std::vector< te::attributefill::OperationType > > &options, std::vector< std::string > toLayerProps)
An converter for DataSetType.
bool save(std::auto_ptr< te::mem::DataSet > result, std::auto_ptr< te::da::DataSetType > outDsType)
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
Vector To Vector operation.
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:644
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr