All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
BufferOp.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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 BufferOp.cpp
22  */
23 
24 #include "../dataaccess/dataset/DataSet.h"
25 #include "../dataaccess/dataset/DataSetAdapter.h"
26 #include "../dataaccess/dataset/DataSetType.h"
27 #include "../dataaccess/dataset/DataSetTypeConverter.h"
28 #include "../dataaccess/datasource/DataSource.h"
29 #include "../dataaccess/datasource/DataSourceCapabilities.h"
30 #include "../dataaccess/utils/Utils.h"
31 
32 #include "../datatype/Property.h"
33 #include "../datatype/SimpleProperty.h"
34 #include "../datatype/StringProperty.h"
35 
36 #include "../geometry/GeometryProperty.h"
37 
38 #include "../statistics/core/Utils.h"
39 
40 #include "BufferOp.h"
41 
43  m_outDsetName("")
44 {
45 }
46 
48  std::string inDsetName,
49  std::auto_ptr<te::da::DataSetType> inDsetType,
50  const te::da::ObjectIdSet* oidSet)
51 {
52  m_inDsrc = inDsrc;
53  m_inDsetName = inDsetName;
54  m_inDsetType = inDsetType;
55  m_oidSet = oidSet;
56 }
57 
58 void te::vp::BufferOp::setParams(const double& distance,
59  const int& bufferPolygonRule,
60  const int& bufferBoundariesRule,
61  const bool& copyInputColumns,
62  const int& levels)
63 {
64  m_distance = distance;
65  m_bufferPolygonRule = bufferPolygonRule;
66  m_bufferBoundariesRule = bufferBoundariesRule;
67  m_copyInputColumns = copyInputColumns;
68  m_levels = levels;
69 }
70 
71 void te::vp::BufferOp::setOutput(te::da::DataSourcePtr outDsrc, std::string dsname)
72 {
73  m_outDsrc = outDsrc;
74  m_outDsetName = dsname;
75 }
76 
78 {
79  if (!m_inDsetType->hasGeom())
80  return false;
81 
82  if (m_outDsetName.empty() || !m_outDsrc.get())
83  return false;
84 
85  return true;
86 }
87 
88 bool te::vp::BufferOp::save(std::auto_ptr<te::mem::DataSet> result, std::auto_ptr<te::da::DataSetType> outDsType)
89 {
90  // do any adaptation necessary to persist the output dataset
91  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(outDsType.get(), m_outDsrc->getCapabilities());
92  te::da::DataSetType* dsTypeResult = converter->getResult();
93  std::auto_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(result.get(), converter));
94 
95  std::map<std::string, std::string> options;
96  // create the dataset
97  m_outDsrc->createDataSet(dsTypeResult, options);
98 
99  // copy from memory to output datasource
100  result->moveBeforeFirst();
101  m_outDsrc->add(dsTypeResult->getName(),result.get(), options);
102 
103  // create the primary key if it is possible
104  if (m_outDsrc->getCapabilities().getDataSetTypeCapabilities().supportsPrimaryKey())
105  {
106  std::string pk_name = dsTypeResult->getName() + "_pkey";
107  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pk_name, dsTypeResult);
108  pk->add(dsTypeResult->getProperty(0));
109  m_outDsrc->addPrimaryKey(m_outDsetName,pk);
110  }
111 
112  return true;
113 }
114 
116 {
117  te::da::DataSetType* dsType = new te::da::DataSetType(m_outDsetName);
118 
119  //Primary key
120  te::dt::SimpleProperty* pkProperty = new te::dt::SimpleProperty(m_outDsetName + "_id", te::dt::INT32_TYPE);
121  pkProperty->setAutoNumber(true);
122  dsType->add(pkProperty);
123 
124  te::da::PrimaryKey* pk = new te::da::PrimaryKey(m_outDsetName + "_pk", dsType);
125  pk->add(pkProperty);
126  dsType->setPrimaryKey(pk);
127 
129  dsType->add(levelProperty);
130 
131  te::dt::SimpleProperty* distanceProperty = new te::dt::SimpleProperty("distance", te::dt::DOUBLE_TYPE);
132  dsType->add(distanceProperty);
133 
134 
135  if(m_copyInputColumns)
136  {
137  std::vector<te::dt::Property*> props = m_inDsetType->getProperties();
138 
139  for(std::size_t i = 0; i < props.size(); ++i)
140  {
141  const std::string name = props[i]->getName();
142  int type = props[i]->getType();
143 
144  if(type != te::dt::GEOMETRY_TYPE && name != "FID")
145  {
146  te::dt::SimpleProperty* s_prop = new te::dt::SimpleProperty(name, type);
147  dsType->add(s_prop);
148  }
149  }
150  }
151 
152  te::gm::GeometryProperty* gp = te::da::GetFirstGeomProperty(m_inDsetType.get());
153  std::auto_ptr<te::gm::GeometryProperty> p (static_cast<te::gm::GeometryProperty*>(gp->clone()));
154  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("geom");
156  geometry->setSRID(p->getSRID());
157  dsType->add(geometry);
158 
159  return dsType;
160 }
void setAutoNumber(bool a)
It tells if the property is an autonumber or not.
Geometric property.
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
void setGeometryType(GeomType t)
It sets the geometry subtype.
An atomic property like an integer or double.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
Buffer operation.
A class that models the description of a dataset.
Definition: DataSetType.h:72
te::dt::Property * clone() const
It returns a clone of the object.
An converter for DataSetType.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
te::da::DataSetType * GetDataSetType()
Definition: BufferOp.cpp:115
void setParams(const double &distance, const int &bufferPolygonRule, const int &bufferBoundariesRule, const bool &copyInputColumns, const int &levels)
Definition: BufferOp.cpp:58
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::auto_ptr< te::da::DataSetType > inDsetType, const te::da::ObjectIdSet *oidSet=0)
Definition: BufferOp.cpp:47
virtual bool paramsAreValid()
Definition: BufferOp.cpp:77
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsname)
Definition: BufferOp.cpp:71
void add(Constraint *c)
It adds a new constraint.
bool save(std::auto_ptr< te::mem::DataSet > result, std::auto_ptr< te::da::DataSetType > outDsType)
Definition: BufferOp.cpp:88
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:508
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:595
void setPrimaryKey(PrimaryKey *pk)
It sets the primary key constraint.