All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GeometricOp.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 GeneralOp.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/StringProperty.h"
34 
35 #include "../geometry/GeometryProperty.h"
36 
37 #include "../statistics/core/Utils.h"
38 
39 #include "GeometricOp.h"
40 
41 #include <sstream>
42 
44  m_outDsetName("")
45 {
46 }
47 
49  std::string inDsetName,
50  std::auto_ptr<te::da::DataSetType> inDsetType)
51 {
52  m_inDsrc = inDsrc;
53  m_inDsetName = inDsetName;
54  m_inDsetType = inDsetType;
55 }
56 
57 void te::vp::GeometricOp::setParams(std::vector<std::string> selectedProps,
58  std::vector<te::vp::GeometricOperation> operations,
60  std::string attribute,
61  bool outputLayer)
62 {
63  m_selectedProps = selectedProps;
64  m_operations = operations;
65  m_objStrategy = objStrategy;
66  m_attribute = attribute;
67  m_outputLayer = m_outputLayer;
68 }
69 
70 void te::vp::GeometricOp::setOutput(std::auto_ptr<te::da::DataSource> outDsrc, std::string dsname)
71 {
72  m_outDsrc = outDsrc;
73  m_outDsetName = dsname;
74 }
75 
77 {
78  if (!m_inDsetType.get())
79  return false;
80 
81  if (!m_inDsetType->hasGeom())
82  return false;
83 
84  if (m_outDsetName.empty() || !m_outDsrc.get())
85  return false;
86 
87  return true;
88 }
89 
90 std::vector<std::string> te::vp::GeometricOp::GetOutputDSetNames()
91 {
92  return m_outDsetNameVec;
93 }
94 
95 bool te::vp::GeometricOp::save(std::auto_ptr<te::mem::DataSet> result, std::auto_ptr<te::da::DataSetType> outDsType)
96 {
97  // do any adaptation necessary to persist the output dataset
98  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(outDsType.get(), m_outDsrc->getCapabilities());
99  te::da::DataSetType* dsTypeResult = converter->getResult();
100  std::auto_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(result.get(), converter));
101 
102  std::map<std::string, std::string> options;
103  // create the dataset
104  m_outDsrc->createDataSet(dsTypeResult, options);
105 
106  // copy from memory to output datasource
107  result->moveBeforeFirst();
108  std::string name = dsTypeResult->getName();
109  m_outDsrc->add(dsTypeResult->getName(),result.get(), options);
110 
111  // create the primary key if it is possible
112  if (m_outDsrc->getCapabilities().getDataSetTypeCapabilities().supportsPrimaryKey())
113  {
114  std::string pk_name = dsTypeResult->getName() + "_pkey";
115  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pk_name, dsTypeResult);
116  pk->add(dsTypeResult->getProperty(0));
117  m_outDsrc->addPrimaryKey(outDsType->getName(), pk);
118  }
119 
120  return true;
121 }
122 
124  bool multiGeomColumns,
125  int geomOp)
126 {
127  te::da::DataSetType* dsType = 0;
128 
129  if(geomOp != -1)
130  {
131  switch(geomOp)
132  {
133  case 0:
134  {
135  dsType = new te::da::DataSetType(m_outDsetName + "_convex_hull");
136  dsType->setTitle(m_outDsetName + "_convex_hull");
137  break;
138  }
139  case 1:
140  {
141  dsType = new te::da::DataSetType(m_outDsetName + "_centroid");
142  dsType->setTitle(m_outDsetName + "_centroid");
143  break;
144  }
145  case 2:
146  {
147  dsType = new te::da::DataSetType(m_outDsetName + "_mbr");
148  dsType->setTitle(m_outDsetName + "_mbr");
149  break;
150  }
151  }
152  }
153  else
154  {
155  dsType = new te::da::DataSetType(m_outDsetName);
156  dsType->setTitle(m_outDsetName);
157  }
158  // Primary key
159  te::dt::SimpleProperty* pkProperty = new te::dt::SimpleProperty(m_outDsetName + "_id", te::dt::INT32_TYPE);
160  pkProperty->setAutoNumber(true);
161  dsType->add(pkProperty);
162 
163  te::da::PrimaryKey* pk = new te::da::PrimaryKey(m_outDsetName + "_pk", dsType);
164  pk->add(pkProperty);
165  dsType->setPrimaryKey(pk);
166 
167  if(geomOpStrategy == te::vp::ALL_OBJ)
168  {
169  for(std::size_t i = 0; i < m_selectedProps.size(); ++i)
170  {
171  te::dt::Property* prop = m_inDsetType->getProperty(m_selectedProps[i])->clone();
172  prop->setParent(0);
173  dsType->add(prop);
174  }
175  }
176 
177  if(geomOpStrategy == te::vp::AGGREG_BY_ATTRIBUTE)
178  {
179  te::dt::Property* prop = m_inDsetType->getProperty(m_attribute)->clone();
180  prop->setParent(0);
181  dsType->add(prop);
182  }
183 
184  for(std::size_t i = 0; i < m_operations.size(); ++i)
185  {
186  int op = m_operations[i];
187  switch(op)
188  {
189  case te::vp::AREA:
190  {
192  dsType->add(area);
193  }
194  break;
195  case te::vp::LINE:
196  {
198  dsType->add(line);
199  }
200  break;
201  case te::vp::PERIMETER:
202  {
204  dsType->add(perimeter);
205  }
206  break;
207  }
208  }
209 
210 // Geometry property.
211  te::gm::GeometryProperty* gp = te::da::GetFirstGeomProperty(m_inDsetType.get());
212 
213  if(multiGeomColumns)
214  {
215  bool flagGeom = false;
216  for(std::size_t i = 0; i< m_operations.size(); ++i)
217  {
218  switch(m_operations[i])
219  {
220  case te::vp::CONVEX_HULL:
221  {
222  te::gm::GeometryProperty* convGeom = new te::gm::GeometryProperty("convex_hull");
224  convGeom->setSRID(gp->getSRID());
225  dsType->add(convGeom);
226  flagGeom = true;
227  }
228  break;
229  case te::vp::CENTROID:
230  {
231  te::gm::GeometryProperty* centroidGeom = new te::gm::GeometryProperty("centroid");
232  centroidGeom->setGeometryType(te::gm::PointType);
233  centroidGeom->setSRID(gp->getSRID());
234  dsType->add(centroidGeom);
235  flagGeom = true;
236  }
237  break;
238  case te::vp::MBR:
239  {
242  mbrGeom->setSRID(gp->getSRID());
243  dsType->add(mbrGeom);
244  flagGeom = true;
245  }
246  break;
247  }
248  }
249 
250  if(!flagGeom)
251  {
252  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("geom");
253  geometry->setSRID(gp->getSRID());
254 
255  switch(gp->getGeometryType())
256  {
257  case te::gm::PointType:
259  break;
262  break;
263  case te::gm::PolygonType:
265  default:
266  geometry->setGeometryType(gp->getGeometryType());
267  }
268 
269  dsType->add(geometry);
270  }
271 
272  }
273  else
274  {
275  bool flagGeom = false;
276  switch(geomOp)
277  {
278  case te::vp::CONVEX_HULL:
279  {
280  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("convex_hull");
282  geometry->setSRID(gp->getSRID());
283  dsType->add(geometry);
284  flagGeom = true;
285  }
286  break;
287  case te::vp::CENTROID:
288  {
289  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("centroid");
291  geometry->setSRID(gp->getSRID());
292  dsType->add(geometry);
293  flagGeom = true;
294  }
295  break;
296  case te::vp::MBR:
297  {
298  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("mbr");
300  geometry->setSRID(gp->getSRID());
301  dsType->add(geometry);
302  flagGeom = true;
303  }
304  break;
305  }
306 
307  if(!flagGeom)
308  {
309  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("geom");
310  geometry->setSRID(gp->getSRID());
311 
312  switch(gp->getGeometryType())
313  {
314  case te::gm::PointType:
316  break;
319  break;
320  case te::gm::PolygonType:
322  default:
323  geometry->setGeometryType(gp->getGeometryType());
324  }
325 
326  dsType->add(geometry);
327 
328 
329  }
330  }
331 
332  return dsType;
333 }
334 
void setAutoNumber(bool a)
It tells if the property is an autonumber or not.
void setTitle(const std::string &title)
It sets a human descriptive title for the DataSetType.
Definition: DataSetType.h:137
te::da::DataSetType * GetDataSetType(te::vp::GeometricOpObjStrategy, bool MultiGeomColumns, int geomOp=-1)
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.
std::vector< std::string > GetOutputDSetNames()
Definition: GeometricOp.cpp:90
An atomic property like an integer or double.
The geographic operation Line.
Definition: Enums.h:106
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
The geographic operation Minimum Bounding Rectangle.
Definition: Enums.h:104
A class that models the description of a dataset.
Definition: DataSetType.h:72
virtual Property * clone() const =0
It returns a clone of the object.
It models a property definition.
Definition: Property.h:59
GeometricOpObjStrategy
Defines the strategy used for the processing of the input geometries.
Definition: Enums.h:115
An converter for DataSetType.
The geographic operation Area.
Definition: Enums.h:105
int getSRID() const
It returns the spatial reference system identifier associated to this property.
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
Aggregate objects by attribute.
Definition: Enums.h:119
The geographic operation Perimeter.
Definition: Enums.h:107
void setOutput(std::auto_ptr< te::da::DataSource > outDsrc, std::string dsname)
Definition: GeometricOp.cpp:70
The geographic operation Centroid.
Definition: Enums.h:103
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: GeometricOp.cpp:95
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
virtual bool paramsAreValid()
Definition: GeometricOp.cpp:76
Geometric operation.
The geographic operation Convex Hull.
Definition: Enums.h:102
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 setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::auto_ptr< te::da::DataSetType > inDsetType)
Definition: GeometricOp.cpp:48
All objects individually.
Definition: Enums.h:117
void setParams(std::vector< std::string > selectedProps, std::vector< te::vp::GeometricOperation > operations, te::vp::GeometricOpObjStrategy objStrategy, std::string attribute, bool outputLayer)
Definition: GeometricOp.cpp:57
void setPrimaryKey(PrimaryKey *pk)
It sets the primary key constraint.
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:159