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 "../common/StringUtils.h"
25 
26 #include "../dataaccess/dataset/DataSet.h"
27 #include "../dataaccess/dataset/DataSetAdapter.h"
28 #include "../dataaccess/dataset/DataSetType.h"
29 #include "../dataaccess/datasource/DataSource.h"
30 #include "../dataaccess/datasource/DataSourceCapabilities.h"
31 #include "../dataaccess/utils/Utils.h"
32 
33 #include "../datatype/Property.h"
34 #include "../datatype/StringProperty.h"
35 
36 #include "../geometry/GeometryProperty.h"
37 
38 #include "../statistics/core/Utils.h"
39 
40 #include "GeometricOp.h"
41 
42 #include <sstream>
43 
45  : m_outputLayer(false)
46 {
47 }
48 
50  std::string inDsetName,
51  std::auto_ptr<te::da::DataSetTypeConverter> converter)
52 {
53  m_inDsrc = inDsrc;
54  m_inDsetName = inDsetName;
55  m_converter = converter;
56 }
57 
58 void te::vp::GeometricOp::setParams(std::vector<std::string> selectedProps,
59  std::vector<te::vp::GeometricOperation> operations,
61  std::string attribute,
62  bool outputLayer)
63 {
64  m_selectedProps = selectedProps;
65  m_operations = operations;
66  m_objStrategy = objStrategy;
67  m_attribute = attribute;
68  m_outputLayer = outputLayer;
69 }
70 
71 void te::vp::GeometricOp::setOutput(std::auto_ptr<te::da::DataSource> outDsrc, std::string dsname)
72 {
73  m_outDsrc = outDsrc;
74  m_outDsetName = dsname;
75 }
76 
78 {
79  if (!m_converter.get())
80  return false;
81 
82  if (!m_converter->getResult()->hasGeom())
83  return false;
84 
85  if (m_outDsetName.empty() || !m_outDsrc.get())
86  return false;
87 
88  return true;
89 }
90 
91 std::vector<std::string> te::vp::GeometricOp::GetOutputDSetNames()
92 {
93  return m_outDsetNameVec;
94 }
95 
97  bool multiGeomColumns,
98  int geomOp)
99 {
100  te::da::DataSetType* dsType = 0;
101 
102  if(geomOp != -1)
103  {
104  switch(geomOp)
105  {
106  case 0:
107  {
108  dsType = new te::da::DataSetType(m_outDsetName + "_convex_hull");
109  dsType->setTitle(m_outDsetName + "_convex_hull");
110 
111  // Primary key
112  te::dt::SimpleProperty* pkProperty = new te::dt::SimpleProperty(m_outDsetName + "_id", te::dt::INT32_TYPE);
113  pkProperty->setAutoNumber(true);
114  dsType->add(pkProperty);
115 
116  te::da::PrimaryKey* pk = new te::da::PrimaryKey(m_outDsetName + "_convex_hull_pk", dsType);
117  pk->add(pkProperty);
118  dsType->setPrimaryKey(pk);
119 
120  break;
121  }
122  case 1:
123  {
124  dsType = new te::da::DataSetType(m_outDsetName + "_centroid");
125  dsType->setTitle(m_outDsetName + "_centroid");
126 
127  // Primary key
128  te::dt::SimpleProperty* pkProperty = new te::dt::SimpleProperty(m_outDsetName + "_id", te::dt::INT32_TYPE);
129  pkProperty->setAutoNumber(true);
130  dsType->add(pkProperty);
131 
132  te::da::PrimaryKey* pk = new te::da::PrimaryKey(m_outDsetName + "_centroid_pk", dsType);
133  pk->add(pkProperty);
134  dsType->setPrimaryKey(pk);
135 
136  break;
137  }
138  case 2:
139  {
140  dsType = new te::da::DataSetType(m_outDsetName + "_mbr");
141  dsType->setTitle(m_outDsetName + "_mbr");
142 
143  // Primary key
144  te::dt::SimpleProperty* pkProperty = new te::dt::SimpleProperty(m_outDsetName + "_id", te::dt::INT32_TYPE);
145  pkProperty->setAutoNumber(true);
146  dsType->add(pkProperty);
147 
148  te::da::PrimaryKey* pk = new te::da::PrimaryKey(m_outDsetName + "_mbr_pk", dsType);
149  pk->add(pkProperty);
150  dsType->setPrimaryKey(pk);
151 
152  break;
153  }
154  }
155  }
156  else
157  {
158  dsType = new te::da::DataSetType(m_outDsetName);
159  dsType->setTitle(m_outDsetName);
160 
161  // Primary key
162  te::dt::SimpleProperty* pkProperty = new te::dt::SimpleProperty(m_outDsetName + "_id", te::dt::INT32_TYPE);
163  pkProperty->setAutoNumber(true);
164  dsType->add(pkProperty);
165 
166  te::da::PrimaryKey* pk = new te::da::PrimaryKey(m_outDsetName + "_res_pk", dsType);
167  pk->add(pkProperty);
168  dsType->setPrimaryKey(pk);
169  }
170 
171 
172  if(geomOpStrategy == te::vp::ALL_OBJ)
173  {
174  for(std::size_t i = 0; i < m_selectedProps.size(); ++i)
175  {
176  te::dt::Property* prop = m_converter->getResult()->getProperty(m_selectedProps[i])->clone();
177  prop->setParent(0);
178  dsType->add(prop);
179  }
180  }
181 
182  if(geomOpStrategy == te::vp::AGGREG_BY_ATTRIBUTE)
183  {
184  te::dt::Property* prop = m_converter->getResult()->getProperty(m_attribute)->clone();
185  prop->setParent(0);
186  dsType->add(prop);
187  }
188 
189  for(std::size_t i = 0; i < m_operations.size(); ++i)
190  {
191  int op = m_operations[i];
192  switch(op)
193  {
194  case te::vp::AREA:
195  {
197  dsType->add(area);
198  }
199  break;
200  case te::vp::LINE:
201  {
203  dsType->add(line);
204  }
205  break;
206  case te::vp::PERIMETER:
207  {
209  dsType->add(perimeter);
210  }
211  break;
212  default:
213  break;
214  }
215  }
216 
217 // Geometry property.
218  te::gm::GeometryProperty* gp = te::da::GetFirstGeomProperty(m_converter->getResult());
219 
220  if(multiGeomColumns)
221  {
222  bool flagGeom = false;
223  for(std::size_t i = 0; i< m_operations.size(); ++i)
224  {
225  switch(m_operations[i])
226  {
227  case te::vp::CONVEX_HULL:
228  {
229  te::gm::GeometryProperty* convGeom = new te::gm::GeometryProperty("convex_hull");
231  convGeom->setSRID(gp->getSRID());
232  dsType->add(convGeom);
233  flagGeom = true;
234  }
235  break;
236  case te::vp::CENTROID:
237  {
238  te::gm::GeometryProperty* centroidGeom = new te::gm::GeometryProperty("centroid");
239  centroidGeom->setGeometryType(te::gm::PointType);
240  centroidGeom->setSRID(gp->getSRID());
241  dsType->add(centroidGeom);
242  flagGeom = true;
243  }
244  break;
245  case te::vp::MBR:
246  {
249  mbrGeom->setSRID(gp->getSRID());
250  dsType->add(mbrGeom);
251  flagGeom = true;
252  }
253  break;
254  default:
255  break;
256  }
257  }
258 
259  if(!flagGeom)
260  {
261  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("geom");
262  geometry->setSRID(gp->getSRID());
263 
264  switch(gp->getGeometryType())
265  {
266  case te::gm::PointType:
268  break;
271  break;
272  case te::gm::PolygonType:
274  default:
275  geometry->setGeometryType(gp->getGeometryType());
276  }
277 
278  dsType->add(geometry);
279  }
280 
281  }
282  else
283  {
284  bool flagGeom = false;
285  switch(geomOp)
286  {
287  case te::vp::CONVEX_HULL:
288  {
289  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("convex_hull");
291  geometry->setSRID(gp->getSRID());
292  dsType->add(geometry);
293  flagGeom = true;
294  }
295  break;
296  case te::vp::CENTROID:
297  {
298  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("centroid");
300  geometry->setSRID(gp->getSRID());
301  dsType->add(geometry);
302  flagGeom = true;
303  }
304  break;
305  case te::vp::MBR:
306  {
307  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("mbr");
309  geometry->setSRID(gp->getSRID());
310  dsType->add(geometry);
311  flagGeom = true;
312  }
313  break;
314  default:
315  break;
316 
317  }
318 
319  if(!flagGeom)
320  {
321  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("geom");
322  geometry->setSRID(gp->getSRID());
323 
324  switch(gp->getGeometryType())
325  {
326  case te::gm::PointType:
328  break;
331  break;
332  case te::gm::PolygonType:
334  default:
335  geometry->setGeometryType(gp->getGeometryType());
336  }
337 
338  dsType->add(geometry);
339 
340 
341  }
342  }
343 
344  return dsType;
345 }
346 
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:96
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:91
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
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::auto_ptr< te::da::DataSetTypeConverter > converter)
Definition: GeometricOp.cpp:49
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:71
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:77
Geometric operation.
The geographic operation Convex Hull.
Definition: Enums.h:102
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
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:58
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