All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GeometricOpMemory.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 GeographicOpMemory.h
22 
23  \brief Geographic Vector Processing functions.
24 */
25 
26 //Terralib
27 #include "terralib_config.h"
28 
29 #include "../common/progress/TaskProgress.h"
30 #include "../common/Logger.h"
31 #include "../common/Translator.h"
32 
33 #include "../dataaccess/dataset/DataSet.h"
34 #include "../dataaccess/utils/Utils.h"
35 #include "../datatype/Property.h"
36 #include "../datatype/SimpleProperty.h"
37 #include "../datatype/StringProperty.h"
38 
39 #include "../geometry/Geometry.h"
40 #include "../geometry/GeometryProperty.h"
41 #include "../geometry/GeometryCollection.h"
42 #include "../geometry/LineString.h"
43 #include "../geometry/MultiLineString.h"
44 #include "../geometry/MultiPolygon.h"
45 #include "../geometry/Point.h"
46 #include "../geometry/Polygon.h"
47 #include "../geometry/Utils.h"
48 
49 #include "../memory/DataSet.h"
50 #include "../memory/DataSetItem.h"
51 
52 #include "GeometricOpMemory.h"
53 #include "Config.h"
54 #include "Exception.h"
55 #include "Utils.h"
56 
57 // STL
58 #include <map>
59 #include <math.h>
60 #include <string>
61 #include <vector>
62 
63 // BOOST
64 #include <boost/lexical_cast.hpp>
65 #include <boost/algorithm/string.hpp>
66 
68 {}
69 
71 {}
72 
73 
74 bool te::vp::GeometricOpMemory::run() throw(te::common::Exception)
75 {
76 // get the geometric operation and/or tabular operation.
77  std::vector<int> opGeom;
78  std::vector<int> opTab;
79  std::vector<te::da::DataSetType*> dsTypeVec;
80 
81  for(std::size_t i = 0; i < m_operations.size(); ++i)
82  {
83  switch (m_operations[i])
84  {
85  case te::vp::CENTROID:
86  opGeom.push_back(te::vp::CENTROID);
87  break;
89  opGeom.push_back(te::vp::CONVEX_HULL);
90  break;
91  case te::vp::MBR:
92  opGeom.push_back(te::vp::MBR);
93  break;
94  case te::vp::AREA:
95  opTab.push_back(te::vp::AREA);
96  break;
97  case te::vp::LINE:
98  opTab.push_back(te::vp::LINE);
99  break;
100  case te::vp::PERIMETER:
101  opTab.push_back(te::vp::PERIMETER);
102  break;
103  default:
104  {
105 #ifdef TERRALIB_LOGGER_ENABLED
106  te::common::Logger::logDebug("vp", "Geometric Operation - The operation is not valid.");
107 #endif //TERRALIB_LOGGER_ENABLED
108  }
109  }
110  }
111 
112  if(m_outputLayer)
113  {
114  bool hasMultiGeomColumns = false;
115  bool result = false;
116 
117  switch(m_objStrategy)
118  {
119  case te::vp::ALL_OBJ:
120  {
121  if(hasMultiGeomColumns) //Condição se o DataSource suporta mais de uma coluna geometrica...
122  {
123  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::ALL_OBJ, true));
124  }
125  else
126  {
127  if(opGeom.size() > 0)
128  {
129  for(std::size_t i = 0; i < opGeom.size(); ++i)
130  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::ALL_OBJ, false, opGeom[i]));
131  }
132  else
133  {
134  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::ALL_OBJ, false));
135  }
136  }
137 
138  for(std::size_t dsTypePos = 0; dsTypePos < dsTypeVec.size(); ++dsTypePos)
139  {
140  m_outDsetNameVec.push_back(dsTypeVec[dsTypePos]->getName());
141 
142  std::auto_ptr<te::da::DataSetType> outDataSetType(dsTypeVec[dsTypePos]);
143  std::auto_ptr<te::mem::DataSet> outDataSet(SetAllObjects(dsTypeVec[dsTypePos], opTab, opGeom));
144 
145  try
146  {
147  te::vp::Save(m_outDsrc.get(), outDataSet.get(), outDataSetType.get());
148  result = true;
149  }
150  catch(...)
151  {
152  result = false;
153  }
154 
155  if(!result)
156  return result;
157  }
158 
159  }
160  break;
161  case te::vp::AGGREG_OBJ:
162  {
163  if(hasMultiGeomColumns) //Condição se o DataSource suporta mais de uma coluna geometrica...
164  {
165  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::AGGREG_OBJ, true));
166  }
167  else
168  {
169  if(opGeom.size() > 0)
170  {
171  for(std::size_t i = 0; i < opGeom.size(); ++i)
172  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::AGGREG_OBJ, false, opGeom[i]));
173  }
174  else
175  {
176  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::AGGREG_OBJ, false));
177  }
178  }
179 
180  for(std::size_t dsTypePos = 0; dsTypePos < dsTypeVec.size(); ++dsTypePos)
181  {
182  m_outDsetNameVec.push_back(dsTypeVec[dsTypePos]->getName());
183 
184  std::auto_ptr<te::da::DataSetType> outDataSetType(dsTypeVec[dsTypePos]);
185  std::auto_ptr<te::mem::DataSet> outDataSet(SetAggregObj(dsTypeVec[dsTypePos], opTab, opGeom));
186 
187  te::vp::Save(m_outDsrc.get(), outDataSet.get(), outDataSetType.get());
188  result = true;
189 
190  if(!result)
191  return result;
192  }
193 
194  }
195  break;
197  {
198  if(hasMultiGeomColumns) //Condição se o DataSource suporta mais de uma coluna geometrica...
199  {
201  }
202  else
203  {
204  if(opGeom.size() > 0)
205  {
206  for(std::size_t i = 0; i < opGeom.size(); ++i)
207  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::AGGREG_BY_ATTRIBUTE, false, opGeom[i]));
208  }
209  else
210  {
212  }
213  }
214 
215  for(std::size_t dsTypePos = 0; dsTypePos < dsTypeVec.size(); ++dsTypePos)
216  {
217  m_outDsetNameVec.push_back(dsTypeVec[dsTypePos]->getName());
218 
219  std::auto_ptr<te::da::DataSetType> outDataSetType(dsTypeVec[dsTypePos]);
220  std::auto_ptr<te::mem::DataSet> outDataSet(SetAggregByAttribute(dsTypeVec[dsTypePos], opTab, opGeom));
221  try
222  {
223  te::vp::Save(m_outDsrc.get(), outDataSet.get(), outDataSetType.get());
224  result = true;
225  }
226  catch(...)
227  {
228  result = false;
229  }
230 
231  if(!result)
232  return result;
233  }
234  }
235  break;
236  default:
237  {
238 #ifdef TERRALIB_LOGGER_ENABLED
239  te::common::Logger::logDebug("vp", "Geometric Operation - Strategy Not found!");
240 #endif //TERRALIB_LOGGER_ENABLED
241  }
242  return false;
243  }
244 
245  return result;
246  }
247  else
248  {
249  //Descobrir se o DataSource suporta adição de mais de uma coluna geometrica.
250  return false;
251  }
252 }
253 
254 
256  std::vector<int> tabVec,
257  std::vector<int> geoVec)
258 {
259  std::auto_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(dsType));
260 
261  int pk = 0;
262  std::auto_ptr<te::da::DataSet> inDset = m_inDsrc->getDataSet(m_inDsetName);
263  inDset->moveBeforeFirst();
264 
265  std::size_t geom_pos = te::da::GetFirstSpatialPropertyPos(inDset.get());
266 
267  while(inDset->moveNext())
268  {
269  te::mem::DataSetItem* item = new te::mem::DataSetItem(outDSet.get());
270 
271  item->setInt32(0, pk);
272  bool geom = true;
273 
274  if(m_selectedProps.size() > 0)
275  {
276  for(std::size_t prop_pos = 0; prop_pos < m_selectedProps.size(); ++prop_pos)
277  {
278  item->setValue(m_selectedProps[prop_pos], inDset->getValue(m_selectedProps[prop_pos]).release());
279  }
280  }
281 
282  std::auto_ptr<te::gm::Geometry> in_geom = inDset->getGeometry(geom_pos);
283 
284  if(tabVec.size() > 0)
285  {
286  for(std::size_t tabPos = 0; tabPos < tabVec.size(); ++tabPos)
287  {
288  switch(tabVec[tabPos])
289  {
290  case te::vp::AREA:
291  {
292  double area = 0;
293  area = CalculateTabularOp(tabVec[tabPos], in_geom.get());
294  item->setDouble("area", area);
295  }
296  break;
297  case te::vp::LINE:
298  {
299  double line = 0;
300  line = CalculateTabularOp(tabVec[tabPos], in_geom.get());
301  item->setDouble("line_length", line);
302  }
303  break;
304  case te::vp::PERIMETER:
305  {
306  double perimeter = 0;
307  perimeter = CalculateTabularOp(tabVec[tabPos], in_geom.get());
308  item->setDouble("perimeter", perimeter);
309  }
310  break;
311  default:
312  {
313 #ifdef TERRALIB_LOGGER_ENABLED
314  te::common::Logger::logDebug("vp", "Geometric Operation - Could not insert the tabular value.");
315 #endif //TERRALIB_LOGGER_ENABLED
316  }
317  }
318  }
319  }
320 
321  if(geoVec.size() > 0)
322  {
323  for(std::size_t geoPos = 0; geoPos < geoVec.size(); ++geoPos)
324  {
325  switch(geoVec[geoPos])
326  {
327  case te::vp::CONVEX_HULL:
328  {
329  std::size_t pos = te::da::GetPropertyPos(dsType, "convex_hull");
330  if(pos < dsType->size())
331  {
332  std::auto_ptr<te::gm::Geometry> convexHull(in_geom->convexHull());
333  convexHull->setSRID(in_geom->getSRID());
334 
335  if(convexHull->getGeomTypeId() == te::gm::PolygonType)
336  {
337  item->setGeometry("convex_hull", convexHull.release());
338  geom = true;
339  }
340  else
341  geom = false;
342  }
343  }
344  break;
345  case te::vp::CENTROID:
346  {
347  std::size_t pos = te::da::GetPropertyPos(dsType, "centroid");
348  if(pos < dsType->size())
349  {
350  const te::gm::Envelope* env = in_geom->getMBR();
351  te::gm::Coord2D center = env->getCenter();
352  te::gm::Point* point = new te::gm::Point(center.x, center.y, in_geom->getSRID());
353  item->setGeometry("centroid", point);
354  }
355  }
356  break;
357  case te::vp::MBR:
358  {
359  std::size_t pos = te::da::GetPropertyPos(dsType, "mbr");
360  if(pos < dsType->size())
361  {
362  std::auto_ptr<te::gm::Geometry> mbr(in_geom->getEnvelope());
363  mbr->setSRID(in_geom->getSRID());
364  if(mbr->getGeomTypeId() == te::gm::PolygonType)
365  item->setGeometry("mbr", mbr.release());
366  else
367  geom = false;
368  }
369  }
370  break;
371  default:
372  {
373  #ifdef TERRALIB_LOGGER_ENABLED
374  te::common::Logger::logDebug("vp", "Geometric Operation - Could not insert the geometric value.");
375  #endif //TERRALIB_LOGGER_ENABLED
376  }
377  }
378  }
379  }
380  else
381  {
382  std::auto_ptr<te::gm::Geometry> g(in_geom.release());
384 
385  switch(g->getGeomTypeId())
386  {
387  case te::gm::PointType:
388  {
389  if(g->isValid())
390  teGeomColl->add(g.release());
391  }
392  break;
394  {
395  if(g->isValid())
396  teGeomColl->add(g.release());
397  }
398  break;
399  case te::gm::PolygonType:
400  {
401  if(g->isValid())
402  teGeomColl->add(g.release());
403  }
404  break;
405  default:
406  {
407 #ifdef TERRALIB_LOGGER_ENABLED
408  te::common::Logger::logDebug("vp", "Geometric Operation - Could not insert the geometry in collection.");
409 #endif //TERRALIB_LOGGER_ENABLED
410  }
411  }
412  if(teGeomColl->getNumGeometries() != 0)
413  item->setGeometry("geom", teGeomColl);
414  else
415  item->setGeometry("geom", g.release());
416  }
417 
418  if(geom)
419  {
420  outDSet->add(item);
421  ++pk;
422  }
423  }
424 
425  return outDSet.release();
426 }
427 
428 
430  std::vector<int> tabVec,
431  std::vector<int> geoVec)
432 {
433  std::auto_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(dsType));
434 
435  int pk = 0;
436  std::auto_ptr<te::da::DataSet> inDset = m_inDsrc->getDataSet(m_inDsetName);
437  std::size_t geom_pos = te::da::GetFirstSpatialPropertyPos(inDset.get());
438  // move first to take a seed geom.
439  inDset->moveFirst();
440  te::mem::DataSetItem* item = new te::mem::DataSetItem(outDSet.get());
441 
442  item->setInt32(0, pk);
443 
444  std::auto_ptr<te::gm::Geometry> seedGeom = inDset->getGeometry(geom_pos);
445  int srid = seedGeom->getSRID();
447 
448  if(inDset->size() > 1)
449  {
450  while(inDset->moveNext())
451  {
452  std::auto_ptr<te::gm::Geometry> c_geom = inDset->getGeometry(geom_pos);
453  if(c_geom->isValid())
454  teGeomColl->add(c_geom.release());
455  }
456  }
457 
458  if(teGeomColl->getNumGeometries() > 1)
459  {
460  seedGeom.reset(seedGeom->Union(teGeomColl));
461  seedGeom->setSRID(srid);
462  }
463 
464  if(tabVec.size() > 0)
465  {
466  for(std::size_t tabPos = 0; tabPos < tabVec.size(); ++tabPos)
467  {
468  switch(tabVec[tabPos])
469  {
470  case te::vp::AREA:
471  {
472  double area = 0;
473  area = CalculateTabularOp(tabVec[tabPos], seedGeom.get());
474  item->setDouble("area", area);
475  }
476  break;
477  case te::vp::LINE:
478  {
479  double line = 0;
480  line = CalculateTabularOp(tabVec[tabPos], seedGeom.get());
481  item->setDouble("line_length", line);
482  }
483  break;
484  case te::vp::PERIMETER:
485  {
486  double perimeter = 0;
487  perimeter = CalculateTabularOp(tabVec[tabPos], seedGeom.get());
488  item->setDouble("perimeter", perimeter);
489  }
490  break;
491  default:
492  {
493 #ifdef TERRALIB_LOGGER_ENABLED
494  te::common::Logger::logDebug("vp", "Geometric Operation - Could not insert the tabular value.");
495 #endif //TERRALIB_LOGGER_ENABLED
496  }
497  }
498  }
499  }
500 
501  if(geoVec.size() > 0)
502  {
503  for(std::size_t geoPos = 0; geoPos < geoVec.size(); ++geoPos)
504  {
505  switch(geoVec[geoPos])
506  {
507  case te::vp::CONVEX_HULL:
508  {
509  std::size_t pos = te::da::GetPropertyPos(dsType, "convex_hull");
510  if(pos < dsType->size())
511  {
512  std::auto_ptr<te::gm::Geometry> convexHull(seedGeom->convexHull());
513  convexHull->setSRID(seedGeom->getSRID());
514  item->setGeometry("convex_hull", convexHull.release());
515  }
516  }
517  break;
518  case te::vp::CENTROID:
519  {
520  std::size_t pos = te::da::GetPropertyPos(dsType, "centroid");
521  if(pos < dsType->size())
522  {
523  const te::gm::Envelope* env = seedGeom->getMBR();
524  te::gm::Coord2D center = env->getCenter();
525  te::gm::Point* point = new te::gm::Point(center.x, center.y, seedGeom->getSRID());
526  item->setGeometry("centroid", point);
527  }
528  }
529  break;
530  case te::vp::MBR:
531  {
532  std::size_t pos = te::da::GetPropertyPos(dsType, "mbr");
533  if(pos < dsType->size())
534  {
535  std::auto_ptr<te::gm::Geometry> mbr(seedGeom->getEnvelope());
536  mbr->setSRID(seedGeom->getSRID());
537  item->setGeometry("mbr", mbr.release());
538  }
539  }
540  break;
541  default:
542  {
543 #ifdef TERRALIB_LOGGER_ENABLED
544  te::common::Logger::logDebug("vp", "Geometric Operation - Could not insert the geometry in collection.");
545 #endif //TERRALIB_LOGGER_ENABLED
546  }
547  }
548  }
549  }
550  else
551  {
552  std::auto_ptr<te::gm::Geometry> g(seedGeom.release());
554 
555  switch(g->getGeomTypeId())
556  {
557  case te::gm::PointType:
558  {
559  if(g->isValid())
560  teGeomColl->add(g.release());
561  }
562  break;
564  {
565  if(g->isValid())
566  teGeomColl->add(g.release());
567  }
568  break;
569  case te::gm::PolygonType:
570  {
571  if(g->isValid())
572  teGeomColl->add(g.release());
573  }
574  break;
575  default:
576  {
577 #ifdef TERRALIB_LOGGER_ENABLED
578  te::common::Logger::logDebug("vp", "Geometric Operation - Could not insert the geometry in collection.");
579 #endif //TERRALIB_LOGGER_ENABLED
580  }
581  }
582  if(teGeomColl->getNumGeometries() != 0)
583  item->setGeometry("geom", teGeomColl);
584  else
585  item->setGeometry("geom", g.release());
586  }
587 
588  outDSet->add(item);
589 
590  return outDSet.release();
591 }
592 
594  std::vector<int> tabVec,
595  std::vector<int> geoVec)
596 {
597  std::map<std::string, te::gm::Geometry*> geometries;
598 
599  std::auto_ptr<te::da::DataSet> inDset = m_inDsrc->getDataSet(m_inDsetName);
600 
601  te::gm::GeometryProperty* propGeom = static_cast<te::gm::GeometryProperty*>(m_inDsetType->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE));
602  std::size_t geom_pos = te::da::GetFirstSpatialPropertyPos(inDset.get());
603 
604  // move first to take a seed geom.
605  inDset->moveFirst();
606  std::auto_ptr<te::gm::Geometry> seedGeom = inDset->getGeometry(geom_pos);
607 
608  if(inDset->size() > 1)
609  {
610  std::map<std::string, std::vector<te::mem::DataSetItem*> > groups;
611  std::map<std::string, std::vector<te::mem::DataSetItem*> >::iterator itg;
612  size_t nprops = inDset->getNumProperties();
613 
614  // move first to take a seed geom.
615  inDset->moveBeforeFirst();
616  while(inDset->moveNext())
617  {
618  std::string key = inDset->getAsString(m_attribute);
619  te::mem::DataSetItem* item = new te::mem::DataSetItem(inDset.get());
620  for(std::size_t j=0; j<nprops; ++j)
621  {
622  if (!inDset->isNull(j))
623  {
624  std::auto_ptr<te::dt::AbstractData> val = inDset->getValue(j);
625  item->setValue(j,val.release());
626  }
627  }
628 
629  itg = groups.find(key);
630  if (itg==groups.end())
631  {
632  std::vector<te::mem::DataSetItem*> dataSetItemVector;
633  dataSetItemVector.push_back(item);
634  groups.insert(std::pair<std::string, std::vector<te::mem::DataSetItem*> >(key,dataSetItemVector));
635  }
636  else
637  itg->second.push_back(item);
638  }
639 
640  itg = groups.begin();
641  while(itg != groups.end())
642  {
643  // calculate the spatial aggregation
644  te::gm::Geometry* geometry = te::vp::GetGeometryUnion(itg->second, geom_pos, propGeom->getGeometryType());
645 
646  if(geometry->isValid())
647  geometries.insert(std::pair<std::string, te::gm::Geometry*>(itg->first,geometry));
648 
649  ++itg;
650  }
651 
652  }
653  else
654  {
655  geometries.insert(std::pair<std::string, te::gm::Geometry*>(inDset->getAsString(m_attribute), seedGeom.release()));
656  }
657 
658 // insert result in dataset.
659  std::auto_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(dsType));
660  std::map<std::string, te::gm::Geometry*>::iterator itGeom;
661  int i = 0;
662 
663  itGeom = geometries.begin();
664  while(itGeom != geometries.end())
665  {
666  te::mem::DataSetItem* outItem = new te::mem::DataSetItem(outDSet.get());
667 
668  // inserting the id in dataSet.
669  outItem->setInt32(0, i);
670  // inserting aggregated attribute.
671  te::dt::Property* prop = m_inDsetType->getProperty(m_attribute);
672  switch(prop->getType())
673  {
674  case te::dt::STRING_TYPE:
675  outItem->setString(1, itGeom->first);
676  break;
677  case te::dt::INT16_TYPE:
678  outItem->setInt16(1, boost::lexical_cast<int>(itGeom->first));
679  break;
680  case te::dt::INT32_TYPE:
681  outItem->setInt32(1, boost::lexical_cast<int>(itGeom->first));
682  break;
683  case te::dt::INT64_TYPE:
684  outItem->setInt64(1, boost::lexical_cast<int>(itGeom->first));
685  break;
686  case te::dt::DOUBLE_TYPE:
687  outItem->setDouble(1, boost::lexical_cast<double>(itGeom->first));
688  break;
689  default:
690 #ifdef TERRALIB_LOGGER_ENABLED
691  te::common::Logger::logDebug("vp", "Geometric Operation - Could not insert the aggregated value.");
692 #endif //TERRALIB_LOGGER_ENABLED
693  break;
694  }
695 
696  // inserting geometries.
697  if(tabVec.size() > 0)
698  {
699  for(std::size_t tabPos = 0; tabPos < tabVec.size(); ++tabPos)
700  {
701  switch(tabVec[tabPos])
702  {
703  case te::vp::AREA:
704  {
705  double area = 0;
706  area = CalculateTabularOp(tabVec[tabPos], itGeom->second);
707  outItem->setDouble("area", area);
708  }
709  break;
710  case te::vp::LINE:
711  {
712  double line = 0;
713  line = CalculateTabularOp(tabVec[tabPos], itGeom->second);
714  outItem->setDouble("line_length", line);
715  }
716  break;
717  case te::vp::PERIMETER:
718  {
719  double perimeter = 0;
720  perimeter = CalculateTabularOp(tabVec[tabPos], itGeom->second);
721  outItem->setDouble("perimeter", perimeter);
722  }
723  break;
724  default:
725  {
726 #ifdef TERRALIB_LOGGER_ENABLED
727  te::common::Logger::logDebug("vp", "Geometric Operation - Could not insert the tabular value.");
728 #endif //TERRALIB_LOGGER_ENABLED
729  }
730  }
731  }
732  }
733 
734  if(geoVec.size() > 0)
735  {
736  for(std::size_t geoPos = 0; geoPos < geoVec.size(); ++geoPos)
737  {
738  switch(geoVec[geoPos])
739  {
740  case te::vp::CONVEX_HULL:
741  {
742  std::size_t pos = te::da::GetPropertyPos(dsType, "convex_hull");
743  if(pos < dsType->size())
744  {
745  std::auto_ptr<te::gm::Geometry> convexHull(itGeom->second->convexHull());
746  convexHull->setSRID(seedGeom->getSRID());
747  outItem->setGeometry("convex_hull", convexHull.release());
748  }
749  }
750  break;
751  case te::vp::CENTROID:
752  {
753  std::size_t pos = te::da::GetPropertyPos(dsType, "centroid");
754  if(pos < dsType->size())
755  {
756  const te::gm::Envelope* env = itGeom->second->getMBR();
757  te::gm::Coord2D center = env->getCenter();
758  te::gm::Point* point = new te::gm::Point(center.x, center.y, itGeom->second->getSRID());
759  outItem->setGeometry("centroid", point);
760  }
761  }
762  break;
763  case te::vp::MBR:
764  {
765  std::size_t pos = te::da::GetPropertyPos(dsType, "mbr");
766  if(pos < dsType->size())
767  {
768  std::auto_ptr<te::gm::Geometry> mbr(itGeom->second->getEnvelope());
769  mbr->setSRID(seedGeom->getSRID());
770  outItem->setGeometry("mbr", mbr.release());
771  }
772  }
773  break;
774  default:
775  {
776 #ifdef TERRALIB_LOGGER_ENABLED
777  te::common::Logger::logDebug("vp", "Geometric Operation - Could not insert the geometric value.");
778 #endif //TERRALIB_LOGGER_ENABLED
779  }
780  }
781  }
782  }
783  else
784  {
785  te::gm::GeometryCollection* teGeomColl = new te::gm::GeometryCollection(0, te::gm::GeometryCollectionType, itGeom->second->getSRID());
786 
787  switch(itGeom->second->getGeomTypeId())
788  {
789  case te::gm::PointType:
790  {
791  if(itGeom->second->isValid())
792  teGeomColl->add(itGeom->second);
793  }
794  break;
796  {
797  if(itGeom->second->isValid())
798  teGeomColl->add(itGeom->second);
799  }
800  break;
801  case te::gm::PolygonType:
802  {
803  if(itGeom->second->isValid())
804  teGeomColl->add(itGeom->second);
805  }
806  break;
807  default:
808  {
809 #ifdef TERRALIB_LOGGER_ENABLED
810  te::common::Logger::logDebug("vp", "Geometric Operation - Could not insert the geometry in collection.");
811 #endif //TERRALIB_LOGGER_ENABLED
812  }
813  }
814  if(teGeomColl->getNumGeometries() != 0)
815  outItem->setGeometry("geom", teGeomColl);
816  else
817  outItem->setGeometry("geom", itGeom->second);
818  }
819 
820  outDSet->add(outItem);
821  ++itGeom;
822  ++i;
823  }
824 
825  return outDSet.release();
826 }
827 
828 
830  te::gm::Geometry* geom)
831 {
832  double value = 0;
833  te::gm::GeomType geomType = geom->getGeomTypeId();
834 
835  switch(tabOperation)
836  {
837  case te::vp::AREA:
838  {
839  if(geomType == te::gm::PolygonType)
840  {
841  te::gm::Polygon* pol = dynamic_cast<te::gm::Polygon*>(geom);
842  if(pol)
843  value = pol->getArea();
844  }
845  if(geomType == te::gm::MultiPolygonType)
846  {
847  std::string type = geom->getGeometryType();
848 
849  if(type == "GeometryCollection")
850  {
851  te::gm::GeometryCollection* m_geoColl = dynamic_cast<te::gm::GeometryCollection*>(geom);
852 
853  if(m_geoColl)
854  {
855  std::vector<te::gm::Geometry*> geomVec = m_geoColl->getGeometries();
856  for(std::size_t i = 0; i < geomVec.size(); ++i)
857  {
858  te::gm::GeomType inType = geomVec[i]->getGeomTypeId();
859  if(inType == te::gm::PolygonType)
860  {
861  te::gm::Polygon* m_pol = dynamic_cast<te::gm::Polygon*>(geomVec[i]);
862  if(m_pol)
863  value += m_pol->getArea();
864  }
865  if(inType == te::gm::MultiPolygonType)
866  {
867  te::gm::MultiPolygon* m_pol = dynamic_cast<te::gm::MultiPolygon*>(geomVec[i]);
868  if(m_pol)
869  value += m_pol->getArea();
870  }
871  }
872  }
873  }
874  else
875  {
876  te::gm::MultiPolygon* m_pol = dynamic_cast<te::gm::MultiPolygon*>(geom);
877  if(m_pol)
878  value = m_pol->getArea();
879  }
880  }
881  return value;
882  }
883  break;
884  case te::vp::LINE:
885  {
886  if(geomType == te::gm::LineStringType)
887  {
888  te::gm::LineString* lineString = dynamic_cast<te::gm::LineString*>(geom);
889  if(lineString)
890  value = lineString->getLength();
891  }
892  if(geomType == te::gm::MultiLineStringType)
893  {
894  te::gm::MultiLineString* m_lineString = dynamic_cast<te::gm::MultiLineString*>(geom);
895  if(m_lineString)
896  value = m_lineString->getLength();
897  }
898  return value;
899  }
900  break;
901  case te::vp::PERIMETER:
902  {
903  if(geomType == te::gm::PolygonType)
904  {
905  te::gm::Polygon* pol = dynamic_cast<te::gm::Polygon*>(geom);
906  if(pol)
907  value = pol->getPerimeter();
908  }
909  if(geomType == te::gm::MultiPolygonType)
910  {
911  std::string type = geom->getGeometryType();
912 
913  if(type == "GeometryCollection")
914  {
915  te::gm::GeometryCollection* m_geoColl = dynamic_cast<te::gm::GeometryCollection*>(geom);
916 
917  if(m_geoColl)
918  {
919  std::vector<te::gm::Geometry*> geomVec = m_geoColl->getGeometries();
920  for(std::size_t i = 0; i < geomVec.size(); ++i)
921  {
922  te::gm::GeomType inType = geomVec[i]->getGeomTypeId();
923  if(inType == te::gm::PolygonType)
924  {
925  te::gm::Polygon* m_pol = dynamic_cast<te::gm::Polygon*>(geomVec[i]);
926  if(m_pol)
927  value += m_pol->getPerimeter();
928  }
929  if(inType == te::gm::MultiPolygonType)
930  {
931  te::gm::MultiPolygon* m_pol = dynamic_cast<te::gm::MultiPolygon*>(geomVec[i]);
932  if(m_pol)
933  value += m_pol->getPerimeter();
934  }
935  }
936  }
937  }
938  else
939  {
940  te::gm::MultiPolygon* m_pol = dynamic_cast<te::gm::MultiPolygon*>(geom);
941  if(m_pol)
942  value = m_pol->getPerimeter();
943  }
944  }
945  return value;
946  }
947  break;
948  default:
949  {
950 #ifdef TERRALIB_LOGGER_ENABLED
951  te::common::Logger::logDebug("vp", "Geometric Operation - Could not calculate the operation.");
952 #endif //TERRALIB_LOGGER_ENABLED
953  }
954  }
955 
956  return value;
957 }
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
te::da::DataSetType * GetDataSetType(te::vp::GeometricOpObjStrategy, bool MultiGeomColumns, int geomOp=-1)
Definition: GeometricOp.cpp:95
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
Geometric property.
An exception class for the Vector processing module.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
Utility functions for the data access module.
double y
y-coordinate.
Definition: Coord2D.h:114
void setDouble(std::size_t i, double value)
It sets the value of the i-th property.
The geographic operation Line.
Definition: Enums.h:106
double CalculateTabularOp(int tabOperation, te::gm::Geometry *geom)
The geographic operation Minimum Bounding Rectangle.
Definition: Enums.h:104
double x
x-coordinate.
Definition: Coord2D.h:113
A class that models the description of a dataset.
Definition: DataSetType.h:72
virtual double getLength() const
The length of this curve in the unit associated to its spatial reference system.
Definition: Curve.cpp:50
std::auto_ptr< te::dt::AbstractData > getValue(std::size_t i) const
It returns the value of the i-th property.
double getPerimeter() const
It returns the length of the boundary for the surface.
te::gm::Geometry * GetGeometryUnion(const std::vector< te::mem::DataSetItem * > &items, size_t geomIdx, te::gm::GeomType outGeoType)
It returns the union of a geometry vector.
Definition: Utils.cpp:52
void Save(te::da::DataSource *source, te::da::DataSet *result, te::da::DataSetType *outDsType)
Definition: Utils.cpp:213
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
Definition: Utils.cpp:500
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
te::mem::DataSet * SetAggregObj(te::da::DataSetType *dsType, std::vector< int > tabVec, std::vector< int > geoVec)
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
void setInt16(std::size_t i, boost::int16_t value)
It sets the value of the i-th property.
double getPerimeter() const
It returns the length of the boundary for the surface.
It models a property definition.
Definition: Property.h:59
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
Coord2D getCenter() const
It returns the rectangle's center coordinate.
Definition: Envelope.cpp:51
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
Definition: Utils.cpp:462
The geographic operation Area.
Definition: Enums.h:105
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
Aggregate all objects.
Definition: Enums.h:118
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
te::mem::DataSet * SetAggregByAttribute(te::da::DataSetType *dsType, std::vector< int > tabVec, std::vector< int > geoVec)
te::mem::DataSet * SetAllObjects(te::da::DataSetType *dsType, std::vector< int > tabVec, std::vector< int > geoVec)
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
virtual const std::string & getGeometryType() const =0
It returns the name of the geometry subclass.
Aggregate objects by attribute.
Definition: Enums.h:119
The geographic operation Perimeter.
Definition: Enums.h:107
virtual bool isValid() const
It tells if the geometry is well formed.
Definition: Geometry.cpp:168
GeomType getGeomTypeId() const
It returns the geometry subclass type identifier.
Definition: Geometry.h:178
The geographic operation Centroid.
Definition: Enums.h:103
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
double getArea() const
It returns the area of this surface, as measured in the spatial reference system of this surface...
int getType() const
It returns the property data type.
Definition: Property.h:161
MultiLineString is a MultiCurve whose elements are LineStrings.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
double getArea() const
It returns the area of this MultiSurface, as measured in the spatial reference system of this multisu...
void add(Geometry *g)
It adds the geometry into the collection.
const std::vector< Geometry * > & getGeometries() const
It returns a reference to the internal list of geometries.
The geographic operation Convex Hull.
Definition: Enums.h:102
double getLength() const
It returns the Length of this MultiCurve which is equal to the sum of the lengths of the element Curv...
Definition: MultiCurve.cpp:71
Configuration flags for the Terrralib Vector Processing module.
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
void setInt64(std::size_t i, boost::int64_t value)
It sets the value of the i-th property.
It is a collection of other geometric objects.
All objects individually.
Definition: Enums.h:117