All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/utils/Utils.h"
33 
34 #include "../datatype/Property.h"
35 #include "../datatype/StringProperty.h"
36 
37 #include "../geometry/GeometryProperty.h"
38 
39 #include "../statistics/core/Utils.h"
40 
41 #include "VectorToVectorOp.h"
42 
44  m_hasErrors(false),
45  m_outDset("")
46 {
47 }
48 
50 {
51 }
52 
55 {
56  m_fromLayer = fromLayer;
57  m_toLayer = toLayer;
58 }
59 
60 void te::attributefill::VectorToVectorOp::setParams(const std::map<std::string, std::vector<te::attributefill::OperationType> >& options, std::vector<std::string> toLayerProps)
61 {
62  m_options = options;
63  m_toLayerProps = toLayerProps;
64 }
65 
67 {
68  m_outDsrc = outDsrc;
69  m_outDset = dsname;
70 }
71 
73 {/*
74  if (!m_inDsetType.get())
75  return false;
76 
77  if (!m_inDsetType->hasGeom())
78  return false;
79 
80  if (m_groupProps.empty())
81  return false;
82 
83  if (m_outDset.empty() || !m_outDsrc.get())
84  return false;
85  */
86  return true;
87 }
88 
89 bool te::attributefill::VectorToVectorOp::save(std::auto_ptr<te::mem::DataSet> result, std::auto_ptr<te::da::DataSetType> outDsType)
90 {
91 
92  try
93  {
94  // do any adaptation necessary to persist the output dataset
95  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(outDsType.get(), m_outDsrc->getCapabilities(), m_outDsrc->getEncoding());
96  te::da::DataSetType* dsTypeResult = converter->getResult();
97  std::auto_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(result.get(), converter));
98 
99  // create the primary key if it is possible
100  if (m_outDsrc->getCapabilities().getDataSetTypeCapabilities().supportsPrimaryKey())
101  {
102  std::string pk_name = dsTypeResult->getName() + "_pkey";
103  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pk_name, dsTypeResult);
104  pk->add(dsTypeResult->getProperty(0));
105  outDsType->setPrimaryKey(pk);
106  }
107 
108  std::map<std::string, std::string> options;
109  // create the dataset
110  m_outDsrc->createDataSet(dsTypeResult, options);
111 
112  // copy from memory to output datasource
113  result->moveBeforeFirst();
114  m_outDsrc->add(dsTypeResult->getName(),result.get(), options);
115  }
116  catch(te::common::Exception& e)
117  {
118 #ifdef TERRALIB_LOGGER_ENABLED
119  std::string ex = e.what();
120  ex += " | Ref: SAVE";
121  te::common::Logger::logDebug("attributefill", ex.c_str());
122 #endif //TERRALIB_LOGGER_ENABLED
123 
124  m_hasErrors = true;
125  }
126  catch(std::exception& e)
127  {
128 #ifdef TERRALIB_LOGGER_ENABLED
129  std::string ex = e.what();
130  ex += " | Ref: SAVE";
131  te::common::Logger::logDebug("attributefill", ex.c_str());
132 #endif //TERRALIB_LOGGER_ENABLED
133 
134  m_hasErrors = true;
135  }
136 
137  return true;
138 }
139 
141 {
142  return m_hasErrors;
143 }
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