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 GeometricOp.cpp
22  */
23 
24 #include "../dataaccess/utils/Utils.h"
25 #include "../geometry/GeometryProperty.h"
26 
27 #include "GeometricOp.h"
28 
30 
32  te::da::DataSourcePtr inDsrc, std::string inDsetName,
33  std::unique_ptr<te::da::DataSetTypeConverter> converter)
34 {
35  m_inDsrc = inDsrc;
36  m_inDsetName = inDsetName;
37  m_converter = std::move(converter);
38 }
39 
41  std::vector<std::string> selectedProps,
42  std::vector<te::vp::GeometricOperation> operations,
43  te::vp::GeometricOpObjStrategy objStrategy, std::string attribute,
44  int newSRID)
45 {
46  m_selectedProps = selectedProps;
47  m_operations = operations;
48  m_objStrategy = objStrategy;
50  m_newSRID = newSRID;
51 }
52 
53 void te::vp::GeometricOp::setOutput(std::unique_ptr<da::DataSource> outDsrc,
54  std::string dsname)
55 {
56  m_outDsrc = std::move(outDsrc);
57  m_outDsetName = dsname;
58 }
59 
61 {
62  if (!m_converter.get())
63  return false;
64 
65  if (!m_converter->getResult()->hasGeom())
66  return false;
67 
68  if (m_outDsetName.empty() || !m_outDsrc.get())
69  return false;
70 
71  return true;
72 }
73 
74 std::vector<std::string> te::vp::GeometricOp::GetOutputDSetNames()
75 {
76  return m_outDsetNameVec;
77 }
78 
80  te::vp::GeometricOpObjStrategy geomOpStrategy, bool multiGeomColumns,
81  int geomOp)
82 {
83  te::da::DataSetType* dsType = nullptr;
84 
85  if(geomOp != -1)
86  {
87  switch(geomOp)
88  {
89  case 0:
90  {
91  dsType = new te::da::DataSetType(m_outDsetName + "_convex_hull");
92  dsType->setTitle(m_outDsetName + "_convex_hull");
93 
94  // Primary key
97  pkProperty->setAutoNumber(true);
98  dsType->add(pkProperty);
99 
100  te::da::PrimaryKey* pk =
101  new te::da::PrimaryKey(m_outDsetName + "_convex_hull_pk", dsType);
102  pk->add(pkProperty);
103  dsType->setPrimaryKey(pk);
104 
105  break;
106  }
107  case 1:
108  {
109  dsType = new te::da::DataSetType(m_outDsetName + "_centroid");
110  dsType->setTitle(m_outDsetName + "_centroid");
111 
112  // Primary key
115  pkProperty->setAutoNumber(true);
116  dsType->add(pkProperty);
117 
118  te::da::PrimaryKey* pk =
119  new te::da::PrimaryKey(m_outDsetName + "_centroid_pk", dsType);
120  pk->add(pkProperty);
121  dsType->setPrimaryKey(pk);
122 
123  break;
124  }
125  case 2:
126  {
127  dsType = new te::da::DataSetType(m_outDsetName + "_mbr");
128  dsType->setTitle(m_outDsetName + "_mbr");
129 
130  // Primary key
133  pkProperty->setAutoNumber(true);
134  dsType->add(pkProperty);
135 
136  te::da::PrimaryKey* pk =
137  new te::da::PrimaryKey(m_outDsetName + "_mbr_pk", dsType);
138  pk->add(pkProperty);
139  dsType->setPrimaryKey(pk);
140 
141  break;
142  }
143  }
144  }
145  else
146  {
147  dsType = new te::da::DataSetType(m_outDsetName);
148  dsType->setTitle(m_outDsetName);
149 
150  // Primary key
151  te::dt::SimpleProperty* pkProperty =
153  pkProperty->setAutoNumber(true);
154  dsType->add(pkProperty);
155 
156  te::da::PrimaryKey* pk =
157  new te::da::PrimaryKey(m_outDsetName + "_res_pk", dsType);
158  pk->add(pkProperty);
159  dsType->setPrimaryKey(pk);
160  }
161 
162 
163  if(geomOpStrategy == te::vp::ALL_OBJ)
164  {
165  for(std::size_t i = 0; i < m_selectedProps.size(); ++i)
166  {
167  te::dt::Property* prop =
168  m_converter->getResult()->getProperty(m_selectedProps[i])->clone();
169  prop->setParent(nullptr);
170  dsType->add(prop);
171  }
172  }
173 
174  if(geomOpStrategy == te::vp::AGGREG_BY_ATTRIBUTE)
175  {
176  te::dt::Property* prop =
177  m_converter->getResult()->getProperty(m_attribute)->clone();
178  prop->setParent(nullptr);
179  dsType->add(prop);
180  }
181 
182  for(std::size_t i = 0; i < m_operations.size(); ++i)
183  {
184  int op = m_operations[i];
185  switch(op)
186  {
187  case te::vp::AREA:
188  {
189  te::dt::SimpleProperty* area =
191  dsType->add(area);
192  }
193  break;
194  case te::vp::LINE:
195  {
197  new te::dt::SimpleProperty("line_length", te::dt::DOUBLE_TYPE);
198  dsType->add(line);
199  }
200  break;
201  case te::vp::PERIMETER:
202  {
203  te::dt::SimpleProperty* perimeter =
205  dsType->add(perimeter);
206  }
207  break;
208  default:
209  break;
210  }
211  }
212 
213 // Geometry property.
216 
217  if(multiGeomColumns)
218  {
219  bool flagGeom = false;
220  for(std::size_t i = 0; i< m_operations.size(); ++i)
221  {
222  switch(m_operations[i])
223  {
224  case te::vp::CONVEX_HULL:
225  {
226  te::gm::GeometryProperty* convGeom =
227  new te::gm::GeometryProperty("convex_hull");
229  convGeom->setSRID(gp->getSRID());
230  dsType->add(convGeom);
231  flagGeom = true;
232  }
233  break;
234  case te::vp::CENTROID:
235  {
236  te::gm::GeometryProperty* centroidGeom =
237  new te::gm::GeometryProperty("centroid");
238  centroidGeom->setGeometryType(te::gm::PointType);
239  centroidGeom->setSRID(gp->getSRID());
240  dsType->add(centroidGeom);
241  flagGeom = true;
242  }
243  break;
244  case te::vp::MBR:
245  {
246  te::gm::GeometryProperty* mbrGeom =
247  new te::gm::GeometryProperty("mbr");
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;
269  case te::gm::PointMType:
271  break;
272  case te::gm::PointZType:
274  break;
275  case te::gm::PointZMType:
277  break;
280  break;
283  break;
286  break;
289  break;
290  case te::gm::PolygonType:
292  break;
295  break;
298  break;
301  break;
302  default:
303  geometry->setGeometryType(gp->getGeometryType());
304  }
305 
306  dsType->add(geometry);
307  }
308 
309  }
310  else
311  {
312  bool flagGeom = false;
313  switch(geomOp)
314  {
315  case te::vp::CONVEX_HULL:
316  {
317  te::gm::GeometryProperty* geometry =
318  new te::gm::GeometryProperty("convex_hull");
320  geometry->setSRID(gp->getSRID());
321  dsType->add(geometry);
322  flagGeom = true;
323  }
324  break;
325  case te::vp::CENTROID:
326  {
327  te::gm::GeometryProperty* geometry =
328  new te::gm::GeometryProperty("centroid");
330  geometry->setSRID(gp->getSRID());
331  dsType->add(geometry);
332  flagGeom = true;
333  }
334  break;
335  case te::vp::MBR:
336  {
337  te::gm::GeometryProperty* geometry =
338  new te::gm::GeometryProperty("mbr");
340  geometry->setSRID(gp->getSRID());
341  dsType->add(geometry);
342  flagGeom = true;
343  }
344  break;
345  default:
346  break;
347 
348  }
349 
350  if(!flagGeom)
351  {
352  te::gm::GeometryProperty* geometry = new te::gm::GeometryProperty("geom");
353  geometry->setSRID(gp->getSRID());
354 
355  switch(gp->getGeometryType())
356  {
357  case te::gm::PointType:
359  break;
360  case te::gm::PointMType:
362  break;
363  case te::gm::PointZType:
365  break;
366  case te::gm::PointZMType:
368  break;
371  break;
374  break;
377  break;
380  break;
381  case te::gm::PolygonType:
383  break;
386  break;
389  break;
392  break;
393  default:
394  geometry->setGeometryType(gp->getGeometryType());
395  }
396 
397  dsType->add(geometry);
398 
399 
400  }
401  }
402 
403  return dsType;
404 }
405 
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:79
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:74
std::vector< std::string > m_outDsetNameVec
Definition: GeometricOp.h:93
An atomic property like an integer or double.
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::unique_ptr< te::da::DataSetTypeConverter > converter)
Definition: GeometricOp.cpp:31
The geographic operation Line.
boost::shared_ptr< DataSource > DataSourcePtr
The geographic operation Minimum Bounding Rectangle.
void setParams(std::vector< std::string > selectedProps, std::vector< te::vp::GeometricOperation > operations, te::vp::GeometricOpObjStrategy objStrategy, std::string attribute, int newSRID)
Definition: GeometricOp.cpp:40
A class that models the description of a dataset.
Definition: DataSetType.h:72
void setOutput(std::unique_ptr< da::DataSource > outDsrc, std::string dsname)
Definition: GeometricOp.cpp:53
It models a property definition.
Definition: Property.h:59
GeometricOpObjStrategy
Defines the strategy used for the processing of the input geometries.
unsigned int line
The geographic operation Area.
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.
std::unique_ptr< te::da::DataSource > m_outDsrc
Definition: GeometricOp.h:91
std::string m_inDsetName
Definition: GeometricOp.h:82
Aggregate objects by attribute.
The geographic operation Perimeter.
std::string m_attribute
Definition: GeometricOp.h:88
The geographic operation Centroid.
void add(Constraint *c)
It adds a new constraint.
te::da::DataSourcePtr m_inDsrc
Definition: GeometricOp.h:81
std::vector< std::string > m_selectedProps
Definition: GeometricOp.h:85
std::vector< te::vp::GeometricOperation > m_operations
Definition: GeometricOp.h:86
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
virtual bool paramsAreValid()
Definition: GeometricOp.cpp:60
Geometric operation.
The geographic operation Convex Hull.
te::vp::GeometricOpObjStrategy m_objStrategy
Definition: GeometricOp.h:87
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
All objects individually.
std::string m_outDsetName
Definition: GeometricOp.h:92
void setPrimaryKey(PrimaryKey *pk)
It sets the primary key constraint.
std::unique_ptr< te::da::DataSetTypeConverter > m_converter
Definition: GeometricOp.h:83
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177