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 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 = 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 
96  bool multiGeomColumns,
97  int geomOp)
98 {
99  te::da::DataSetType* dsType = 0;
100 
101  if(geomOp != -1)
102  {
103  switch(geomOp)
104  {
105  case 0:
106  {
107  dsType = new te::da::DataSetType(m_outDsetName + "_convex_hull");
108  dsType->setTitle(m_outDsetName + "_convex_hull");
109 
110  // Primary key
111  te::dt::SimpleProperty* pkProperty = new te::dt::SimpleProperty(m_outDsetName + "_id", te::dt::INT32_TYPE);
112  pkProperty->setAutoNumber(true);
113  dsType->add(pkProperty);
114 
115  te::da::PrimaryKey* pk = new te::da::PrimaryKey(m_outDsetName + "_convex_hull_pk", dsType);
116  pk->add(pkProperty);
117  dsType->setPrimaryKey(pk);
118 
119  break;
120  }
121  case 1:
122  {
123  dsType = new te::da::DataSetType(m_outDsetName + "_centroid");
124  dsType->setTitle(m_outDsetName + "_centroid");
125 
126  // Primary key
127  te::dt::SimpleProperty* pkProperty = new te::dt::SimpleProperty(m_outDsetName + "_id", te::dt::INT32_TYPE);
128  pkProperty->setAutoNumber(true);
129  dsType->add(pkProperty);
130 
131  te::da::PrimaryKey* pk = new te::da::PrimaryKey(m_outDsetName + "_centroid_pk", dsType);
132  pk->add(pkProperty);
133  dsType->setPrimaryKey(pk);
134 
135  break;
136  }
137  case 2:
138  {
139  dsType = new te::da::DataSetType(m_outDsetName + "_mbr");
140  dsType->setTitle(m_outDsetName + "_mbr");
141 
142  // Primary key
143  te::dt::SimpleProperty* pkProperty = new te::dt::SimpleProperty(m_outDsetName + "_id", te::dt::INT32_TYPE);
144  pkProperty->setAutoNumber(true);
145  dsType->add(pkProperty);
146 
147  te::da::PrimaryKey* pk = new te::da::PrimaryKey(m_outDsetName + "_mbr_pk", dsType);
148  pk->add(pkProperty);
149  dsType->setPrimaryKey(pk);
150 
151  break;
152  }
153  }
154  }
155  else
156  {
157  dsType = new te::da::DataSetType(m_outDsetName);
158  dsType->setTitle(m_outDsetName);
159  }
160 
161 
162  if(geomOpStrategy == te::vp::ALL_OBJ)
163  {
164  for(std::size_t i = 0; i < m_selectedProps.size(); ++i)
165  {
166  te::dt::Property* prop = m_inDsetType->getProperty(m_selectedProps[i])->clone();
167  prop->setParent(0);
168  dsType->add(prop);
169  }
170  }
171 
172  if(geomOpStrategy == te::vp::AGGREG_BY_ATTRIBUTE)
173  {
174  te::dt::Property* prop = m_inDsetType->getProperty(m_attribute)->clone();
175  prop->setParent(0);
176  dsType->add(prop);
177  }
178 
179  for(std::size_t i = 0; i < m_operations.size(); ++i)
180  {
181  int op = m_operations[i];
182  switch(op)
183  {
184  case te::vp::AREA:
185  {
187  dsType->add(area);
188  }
189  break;
190  case te::vp::LINE:
191  {
193  dsType->add(line);
194  }
195  break;
196  case te::vp::PERIMETER:
197  {
199  dsType->add(perimeter);
200  }
201  break;
202  default:
203  break;
204  }
205  }
206 
207 // Geometry property.
208  te::gm::GeometryProperty* gp = te::da::GetFirstGeomProperty(m_inDsetType.get());
209 
210  if(multiGeomColumns)
211  {
212  bool flagGeom = false;
213  for(std::size_t i = 0; i< m_operations.size(); ++i)
214  {
215  switch(m_operations[i])
216  {
217  case te::vp::CONVEX_HULL:
218  {
219  te::gm::GeometryProperty* convGeom = new te::gm::GeometryProperty("convex_hull");
221  convGeom->setSRID(gp->getSRID());
222  dsType->add(convGeom);
223  flagGeom = true;
224  }
225  break;
226  case te::vp::CENTROID:
227  {
228  te::gm::GeometryProperty* centroidGeom = new te::gm::GeometryProperty("centroid");
229  centroidGeom->setGeometryType(te::gm::PointType);
230  centroidGeom->setSRID(gp->getSRID());
231  dsType->add(centroidGeom);
232  flagGeom = true;
233  }
234  break;
235  case te::vp::MBR:
236  {
239  mbrGeom->setSRID(gp->getSRID());
240  dsType->add(mbrGeom);
241  flagGeom = true;
242  }
243  break;
244  default:
245  break;
246  }
247  }
248 
249  if(!flagGeom)
250  {
251  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("geom");
252  geometry->setSRID(gp->getSRID());
253 
254  switch(gp->getGeometryType())
255  {
256  case te::gm::PointType:
258  break;
261  break;
262  case te::gm::PolygonType:
264  default:
265  geometry->setGeometryType(gp->getGeometryType());
266  }
267 
268  dsType->add(geometry);
269  }
270 
271  }
272  else
273  {
274  bool flagGeom = false;
275  switch(geomOp)
276  {
277  case te::vp::CONVEX_HULL:
278  {
279  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("convex_hull");
281  geometry->setSRID(gp->getSRID());
282  dsType->add(geometry);
283  flagGeom = true;
284  }
285  break;
286  case te::vp::CENTROID:
287  {
288  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("centroid");
290  geometry->setSRID(gp->getSRID());
291  dsType->add(geometry);
292  flagGeom = true;
293  }
294  break;
295  case te::vp::MBR:
296  {
297  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("mbr");
299  geometry->setSRID(gp->getSRID());
300  dsType->add(geometry);
301  flagGeom = true;
302  }
303  break;
304  default:
305  break;
306 
307  }
308 
309  if(!flagGeom)
310  {
311  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("geom");
312  geometry->setSRID(gp->getSRID());
313 
314  switch(gp->getGeometryType())
315  {
316  case te::gm::PointType:
318  break;
321  break;
322  case te::gm::PolygonType:
324  default:
325  geometry->setGeometryType(gp->getGeometryType());
326  }
327 
328  dsType->add(geometry);
329 
330 
331  }
332  }
333 
334  return dsType;
335 }
336 
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)
Definition: GeometricOp.cpp:95
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
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.
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:557
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:177