GeometricOpQuery.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 AggregationQuery.h
22 
23  \brief Aggregation Vector Processing functions.
24 */
25 
26 //Terralib
27 
28 #include "../BuildConfig.h"
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/dataset/DataSetAdapter.h"
35 
36 #include "../datatype/Property.h"
37 #include "../datatype/SimpleProperty.h"
38 #include "../datatype/StringProperty.h"
39 
40 #include "../dataaccess/query/Avg.h"
41 #include "../dataaccess/query/Count.h"
42 #include "../dataaccess/query/DataSetName.h"
43 #include "../dataaccess/query/Expression.h"
44 #include "../dataaccess/query/Field.h"
45 #include "../dataaccess/query/Fields.h"
46 #include "../dataaccess/query/From.h"
47 #include "../dataaccess/query/FromItem.h"
48 #include "../dataaccess/query/GroupBy.h"
49 #include "../dataaccess/query/GroupByItem.h"
50 #include "../dataaccess/query/Literal.h"
51 #include "../dataaccess/query/LiteralBool.h"
52 #include "../dataaccess/query/LiteralInt32.h"
53 #include "../dataaccess/query/PropertyName.h"
54 #include "../dataaccess/query/Select.h"
55 #include "../dataaccess/query/ST_Area.h"
56 #include "../dataaccess/query/ST_Centroid.h"
57 #include "../dataaccess/query/ST_ConvexHull.h"
58 #include "../dataaccess/query/ST_Envelope.h"
59 #include "../dataaccess/query/ST_Length.h"
60 #include "../dataaccess/query/ST_Perimeter.h"
61 #include "../dataaccess/query/ST_SetSRID.h"
62 #include "../dataaccess/query/ST_Union.h"
63 #include "../dataaccess/query/Variance.h"
64 #include "../dataaccess/utils/Utils.h"
65 
66 #include "../geometry/Geometry.h"
67 #include "../geometry/GeometryCollection.h"
68 #include "../geometry/GeometryProperty.h"
69 #include "../geometry/Utils.h"
70 
71 #include "../memory/DataSet.h"
72 #include "../memory/DataSetItem.h"
73 
74 #include "../statistics/core/SummaryFunctions.h"
75 #include "../statistics/core/StringStatisticalSummary.h"
76 #include "../statistics/core/NumericStatisticalSummary.h"
77 
78 #include "GeometricOpQuery.h"
79 #include "Config.h"
80 #include "Exception.h"
81 #include "Utils.h"
82 
83 // STL
84 #include <map>
85 #include <math.h>
86 #include <string>
87 #include <vector>
88 
89 // BOOST
90 #include <boost/lexical_cast.hpp>
91 #include <boost/algorithm/string.hpp>
92 
94 {}
95 
97 {}
98 
99 bool te::vp::GeometricOpQuery::run() throw(te::common::Exception)
100 {
101 // get the geometric operation and/or tabular operation.
102  std::vector<int> opGeom;
103  std::vector<int> opTab;
104  std::vector<te::da::DataSetType*> dsTypeVec;
105 
106  for(std::size_t i = 0; i < m_operations.size(); ++i)
107  {
108  switch(m_operations[i])
109  {
110  case te::vp::CENTROID:
111  opGeom.push_back(te::vp::CENTROID);
112  break;
113  case te::vp::CONVEX_HULL:
114  opGeom.push_back(te::vp::CONVEX_HULL);
115  break;
116  case te::vp::MBR:
117  opGeom.push_back(te::vp::MBR);
118  break;
119  case te::vp::AREA:
120  opTab.push_back(te::vp::AREA);
121  break;
122  case te::vp::LINE:
123  opTab.push_back(te::vp::LINE);
124  break;
125  case te::vp::PERIMETER:
126  opTab.push_back(te::vp::PERIMETER);
127  break;
128  default:
129  {
130 #ifdef TERRALIB_LOGGER_ENABLED
131  te::common::Logger::logDebug("vp", "Geometric Operation - The operation is not valid.");
132 #endif //TERRALIB_LOGGER_ENABLED
133  }
134  }
135  }
136 
137  if(m_outputLayer)
138  {
139  bool hasMultiGeomColumns = false;
140  bool result = false;
141 
142  switch(m_objStrategy)
143  {
144  case te::vp::ALL_OBJ:
145  {
146  if(hasMultiGeomColumns) //Condição se o DataSource suporta mais de uma soluna geometrica...
147  {
148  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::ALL_OBJ, true));
149  }
150  else
151  {
152  if(opGeom.size() > 0)
153  {
154  for(std::size_t i = 0; i < opGeom.size(); ++i)
155  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::ALL_OBJ, false, opGeom[i]));
156  }
157  else
158  {
159  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::ALL_OBJ, false));
160  }
161  }
162 
163  for(std::size_t dsTypePos = 0; dsTypePos < dsTypeVec.size(); ++dsTypePos)
164  {
165  m_outDsetNameVec.push_back(dsTypeVec[dsTypePos]->getName());
166 
167  std::auto_ptr<te::da::DataSetType> outDataSetType(dsTypeVec[dsTypePos]);
168  std::auto_ptr<te::mem::DataSet> outDataSet(SetAllObjects(dsTypeVec[dsTypePos], opTab, opGeom));
169 
170  te::vp::Save(m_outDsrc.get(), outDataSet.get(), outDataSetType.get());
171  result = true;
172 
173  if(!result)
174  return result;
175  }
176  }
177  break;
178  case te::vp::AGGREG_OBJ:
179  {
180  if(hasMultiGeomColumns) //Condição se o DataSource suporta mais de uma coluna geometrica...
181  {
182  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::AGGREG_OBJ, true));
183  }
184  else
185  {
186  if(opGeom.size() > 0)
187  {
188  for(std::size_t i = 0; i < opGeom.size(); ++i)
189  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::AGGREG_OBJ, false, opGeom[i]));
190  }
191  else
192  {
193  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::AGGREG_OBJ, false));
194  }
195  }
196 
197  for(std::size_t dsTypePos = 0; dsTypePos < dsTypeVec.size(); ++dsTypePos)
198  {
199  m_outDsetNameVec.push_back(dsTypeVec[dsTypePos]->getName());
200 
201  std::auto_ptr<te::da::DataSetType> outDataSetType(dsTypeVec[dsTypePos]);
202  std::auto_ptr<te::mem::DataSet> outDataSet(SetAggregObj(dsTypeVec[dsTypePos], opTab, opGeom));
203  try
204  {
205  te::vp::Save(m_outDsrc.get(), outDataSet.get(), outDataSetType.get());
206  result = true;
207  }
208  catch(...)
209  {
210  result = false;
211  }
212  if(!result)
213  return result;
214  }
215  }
216  break;
218  {
219  if(hasMultiGeomColumns) //Condição se o DataSource suporta mais de uma coluna geometrica...
220  {
222  }
223  else
224  {
225  if(opGeom.size() > 0)
226  {
227  for(std::size_t i = 0; i < opGeom.size(); ++i)
228  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(te::vp::AGGREG_BY_ATTRIBUTE, false, opGeom[i]));
229  }
230  else
231  {
233  }
234  }
235 
236  for(std::size_t dsTypePos = 0; dsTypePos < dsTypeVec.size(); ++dsTypePos)
237  {
238  m_outDsetNameVec.push_back(dsTypeVec[dsTypePos]->getName());
239 
240  std::auto_ptr<te::da::DataSetType> outDataSetType(dsTypeVec[dsTypePos]);
241  std::auto_ptr<te::mem::DataSet> outDataSet(SetAggregByAttribute(dsTypeVec[dsTypePos], opTab, opGeom));
242  try
243  {
244  te::vp::Save(m_outDsrc.get(), outDataSet.get(), outDataSetType.get());
245  result = true;
246  }
247  catch(...)
248  {
249  result = false;
250  }
251  if(!result)
252  return result;
253  }
254  }
255  break;
256  default:
257  {
258 #ifdef TERRALIB_LOGGER_ENABLED
259  te::common::Logger::logDebug("vp", "Geometric Operation - Strategy Not found!");
260 #endif //TERRALIB_LOGGER_ENABLED
261  }
262  return false;
263  }
264 
265  return result;
266 
267  }
268  else
269  {
270  //Descobrir se o DataSource suporta adição de mais de uma coluna geometrica.
271  return false;
272  }
273 
274  return false;
275 }
276 
278  std::vector<int> tabVec,
279  std::vector<int> geoVec)
280 {
281  std::auto_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(dsType));
282  std::auto_ptr<te::da::DataSet> inDset = m_inDsrc->getDataSet(m_inDsetName);
283  std::size_t geom_pos = te::da::GetFirstSpatialPropertyPos(inDset.get());
284 
285  te::da::Fields* fields = new te::da::Fields;
286 
287  if(m_selectedProps.size() > 0)
288  {
289  for(std::size_t prop_pos = 0; prop_pos < m_selectedProps.size(); ++prop_pos)
290  {
291  te::da::Field* f_prop = new te::da::Field(m_selectedProps[prop_pos]);
292  fields->push_back(f_prop);
293  }
294  }
295 
296  std::string name = inDset->getPropertyName(geom_pos);
297 
298  if(tabVec.size() > 0)
299  {
300  for(std::size_t tabPos = 0; tabPos < tabVec.size(); ++tabPos)
301  {
302  te::da::Expression* ex;
303  te::da::Field* f;
304 
305  switch(tabVec[tabPos])
306  {
307  case te::vp::AREA:
308  {
309  ex = new te::da::ST_Area(te::da::PropertyName(name));
310  f = new te::da::Field(*ex, "area");
311  fields->push_back(f);
312  }
313  break;
314  case te::vp::LINE:
315  {
316  ex = new te::da::ST_Length(te::da::PropertyName(name));
317  f = new te::da::Field(*ex, "line_length");
318  fields->push_back(f);
319  }
320  break;
321  case te::vp::PERIMETER:
322  {
324  f = new te::da::Field(*ex, "perimeter");
325  fields->push_back(f);
326  }
327  break;
328  default:
329  {
330 #ifdef TERRALIB_LOGGER_ENABLED
331  te::common::Logger::logDebug("vp", "Geometric Operation - Invalid field to add in query.");
332 #endif //TERRALIB_LOGGER_ENABLED
333  }
334  }
335  }
336  }
337 
338 
339  std::auto_ptr<te::da::DataSetType>dsTypeSource(m_inDsrc->getDataSetType(m_inDsetName));
340 
341  te::gm::GeometryProperty* geomPropertySource = te::da::GetFirstGeomProperty(dsTypeSource.get());
342  te::gm::GeometryProperty* geomPropertyLayer = te::da::GetFirstGeomProperty(dsType);
343 
344  if(geoVec.size() > 0)
345  {
346  for(std::size_t geoPos = 0; geoPos < geoVec.size(); ++geoPos)
347  {
348  te::da::Expression* ex_operation;
349  te::da::Expression* ex_geom;
350  te::da::Field* f_geom;
351 
352  switch(geoVec[geoPos])
353  {
354  case te::vp::CONVEX_HULL:
355  {
356  std::size_t pos = dsType->getPropertyPosition("convex_hull");
357  if( pos < outDSet->getNumProperties() && pos > 0)
358  {
359  if (geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
360  {
361  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomPropertyLayer->getSRID());
362  ex_geom = new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
363  }
364  else
365  {
366  ex_geom = new te::da::PropertyName(name);
367  }
368 
369  ex_operation = new te::da::ST_ConvexHull(*ex_geom);
370  f_geom = new te::da::Field(*ex_operation, "convex_hull");
371  fields->push_back(f_geom);
372  }
373  }
374  break;
375  case te::vp::CENTROID:
376  {
377  std::size_t pos = dsType->getPropertyPosition("centroid");
378  if( pos < outDSet->getNumProperties() && pos > 0)
379  {
380  if (geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
381  {
382  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomPropertyLayer->getSRID());
383  ex_geom = new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
384  }
385  else
386  {
387  ex_geom = new te::da::PropertyName(name);
388  }
389 
390  ex_operation = new te::da::ST_Centroid(*ex_geom);
391  f_geom = new te::da::Field(*ex_operation, "centroid");
392  fields->push_back(f_geom);
393  }
394  }
395  break;
396  case te::vp::MBR:
397  {
398  std::size_t pos = dsType->getPropertyPosition("mbr");
399  if( pos < outDSet->getNumProperties() && pos > 0)
400  {
401  if (geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
402  {
403  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomPropertyLayer->getSRID());
404  ex_geom = new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
405  }
406  else
407  {
408  ex_geom = new te::da::PropertyName(name);
409  }
410 
411  ex_operation = new te::da::ST_Envelope(*ex_geom);
412  f_geom = new te::da::Field(*ex_operation, "mbr");
413  fields->push_back(f_geom);
414  }
415  }
416  break;
417  default:
418  {
419 #ifdef TERRALIB_LOGGER_ENABLED
420  te::common::Logger::logDebug("vp", "Geometric Operation - Invalid field to add in query.");
421 #endif //TERRALIB_LOGGER_ENABLED
422  }
423  }
424  }
425  }
426  else
427  {
428  te::da::Expression* ex_geom;
429 
430  if (geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
431  {
432  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomPropertyLayer->getSRID());
433  ex_geom = new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
434  }
435  else
436  {
437  ex_geom = new te::da::PropertyName(name);
438  }
439 
440  te::da::Field* f_prop = new te::da::Field(*ex_geom);
441  fields->push_back(f_prop);
442  }
443 
444  te::da::FromItem* fromItem = new te::da::DataSetName(m_converter->getResult()->getName());
445  te::da::From* from = new te::da::From;
446  from->push_back(fromItem);
447 
448  te::da::Select select(fields, from);
449 
450  std::auto_ptr<te::da::DataSet> dsQuery = m_inDsrc->query(select);
451 
452  if (dsQuery->isEmpty())
453  return false;
454 
455  SetOutputDSet(dsQuery.get(), outDSet.get());
456 
457  return outDSet.release();
458 }
459 
461  std::vector<int> tabVec,
462  std::vector<int> geoVec)
463 {
464  std::auto_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(dsType));
465 
466  std::auto_ptr<te::da::DataSet> inDset = m_inDsrc->getDataSet(m_inDsetName);
467  std::size_t geom_pos = te::da::GetFirstSpatialPropertyPos(inDset.get());
468  std::string name = inDset->getPropertyName(geom_pos);
469 
470  te::da::Fields* fields = new te::da::Fields;
471 
472  if(tabVec.size() > 0)
473  {
474  for(std::size_t tabPos = 0; tabPos < tabVec.size(); ++tabPos)
475  {
476  te::da::Expression* ex;
477  te::da::Field* f;
478 
479  switch(tabVec[tabPos])
480  {
481  case te::vp::AREA:
482  {
484  ex = new te::da::ST_Area(*e_union);
485  f = new te::da::Field(*ex, "area");
486  fields->push_back(f);
487  }
488  break;
489  case te::vp::LINE:
490  {
492  ex = new te::da::ST_Length(*e_union);
493  f = new te::da::Field(*ex, "line_length");
494  fields->push_back(f);
495  }
496  break;
497  case te::vp::PERIMETER:
498  {
500  ex = new te::da::ST_Perimeter(*e_union);
501  f = new te::da::Field(*ex, "perimeter");
502  fields->push_back(f);
503  }
504  break;
505  default:
506  {
507 #ifdef TERRALIB_LOGGER_ENABLED
508  te::common::Logger::logDebug("vp", "Geometric Operation - Invalid field to add in query.");
509 #endif //TERRALIB_LOGGER_ENABLED
510  }
511  }
512  }
513  }
514 
515 
516  std::auto_ptr<te::da::DataSetType>dsTypeSource(m_inDsrc->getDataSetType(m_inDsetName));
517 
518  te::gm::GeometryProperty* geomPropertySource = te::da::GetFirstGeomProperty(dsTypeSource.get());
519  te::gm::GeometryProperty* geomPropertyLayer = te::da::GetFirstGeomProperty(dsType);
520 
521  if(geoVec.size() > 0)
522  {
523  for(std::size_t geoPos = 0; geoPos < geoVec.size(); ++geoPos)
524  {
525  te::da::Expression* ex_operation;
526  te::da::Expression* ex_geom;
527  te::da::Field* f_geom;
528 
529  switch(geoVec[geoPos])
530  {
531  case te::vp::CONVEX_HULL:
532  {
533  std::size_t pos = dsType->getPropertyPosition("convex_hull");
534  if( pos < outDSet->getNumProperties() && pos > 0)
535  {
536  if (geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
537  {
538  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomPropertyLayer->getSRID());
539  ex_geom = new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
540  }
541  else
542  {
543  ex_geom = new te::da::PropertyName(name);
544  }
545 
546  te::da::Expression* e_union = new te::da::ST_Union(*ex_geom);
547  ex_operation = new te::da::ST_ConvexHull(*e_union);
548  f_geom = new te::da::Field(*ex_operation, "convex_hull");
549  fields->push_back(f_geom);
550  }
551  }
552  break;
553  case te::vp::CENTROID:
554  {
555  std::size_t pos = dsType->getPropertyPosition("centroid");
556  if( pos < outDSet->getNumProperties() && pos > 0)
557  {
558  if (geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
559  {
560  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomPropertyLayer->getSRID());
561  ex_geom = new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
562  }
563  else
564  {
565  ex_geom = new te::da::PropertyName(name);
566  }
567 
568  te::da::Expression* e_union = new te::da::ST_Union(*ex_geom);
569  ex_operation = new te::da::ST_Centroid(*e_union);
570  f_geom = new te::da::Field(*ex_operation, "centroid");
571  fields->push_back(f_geom);
572  }
573  }
574  break;
575  case te::vp::MBR:
576  {
577  std::size_t pos = dsType->getPropertyPosition("mbr");
578  if( pos < outDSet->getNumProperties() && pos > 0)
579  {
580  if (geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
581  {
582  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomPropertyLayer->getSRID());
583  ex_geom = new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
584  }
585  else
586  {
587  ex_geom = new te::da::PropertyName(name);
588  }
589 
590  te::da::Expression* e_union = new te::da::ST_Union(*ex_geom);
591  ex_operation = new te::da::ST_Envelope(*e_union);
592  f_geom = new te::da::Field(*ex_operation, "mbr");
593  fields->push_back(f_geom);
594  }
595  }
596  break;
597  default:
598  {
599 #ifdef TERRALIB_LOGGER_ENABLED
600  te::common::Logger::logDebug("vp", "Geometric Operation - Invalid field to add in query.");
601 #endif //TERRALIB_LOGGER_ENABLED
602  }
603  }
604  }
605  }
606  else
607  {
608  te::da::Expression* e_union;
609 
610  if (geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
611  {
612  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomPropertyLayer->getSRID());
613  e_union = new te::da::ST_Union(new te::da::ST_SetSRID(new te::da::PropertyName(name), srid));
614  }
615  else
616  {
617  e_union = new te::da::ST_Union(te::da::PropertyName(name));
618  }
619 
620  te::da::Field* f_prop = new te::da::Field(*e_union, name);
621  fields->push_back(f_prop);
622  }
623 
624  te::da::FromItem* fromItem = new te::da::DataSetName(m_converter->getResult()->getName());
625  te::da::From* from = new te::da::From;
626  from->push_back(fromItem);
627 
628  te::da::Select select(fields, from);
629 
630  std::auto_ptr<te::da::DataSet> dsQuery = m_inDsrc->query(select);
631 
632  if (dsQuery->isEmpty())
633  return false;
634 
635  SetOutputDSet(dsQuery.get(), outDSet.get());
636 
637  return outDSet.release();
638 }
639 
641  std::vector<int> tabVec,
642  std::vector<int> geoVec)
643 {
644  std::auto_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(dsType));
645  std::auto_ptr<te::da::DataSet> inDset = m_inDsrc->getDataSet(m_inDsetName);
646  std::size_t geom_pos = te::da::GetFirstSpatialPropertyPos(inDset.get());
647  std::string name = inDset->getPropertyName(geom_pos);
648 
649  te::da::Fields* fields = new te::da::Fields;
650 
651  te::da::Field* f_aggreg = new te::da::Field(m_attribute);
652  fields->push_back(f_aggreg);
653 
654  if(tabVec.size() > 0)
655  {
656  for(std::size_t tabPos = 0; tabPos < tabVec.size(); ++tabPos)
657  {
658  te::da::Expression* ex;
659  te::da::Field* f;
660 
661  switch(tabVec[tabPos])
662  {
663  case te::vp::AREA:
664  {
666  ex = new te::da::ST_Area(*e_union);
667  f = new te::da::Field(*ex, "area");
668  fields->push_back(f);
669  }
670  break;
671  case te::vp::LINE:
672  {
674  ex = new te::da::ST_Length(*e_union);
675  f = new te::da::Field(*ex, "line_length");
676  fields->push_back(f);
677  }
678  break;
679  case te::vp::PERIMETER:
680  {
682  ex = new te::da::ST_Perimeter(*e_union);
683  f = new te::da::Field(*ex, "perimeter");
684  fields->push_back(f);
685  }
686  break;
687  default:
688  {
689 #ifdef TERRALIB_LOGGER_ENABLED
690  te::common::Logger::logDebug("vp", "Geometric Operation - Invalid field to add in query.");
691 #endif //TERRALIB_LOGGER_ENABLED
692  }
693  }
694  }
695  }
696 
697 
698  std::auto_ptr<te::da::DataSetType>dsTypeSource(m_inDsrc->getDataSetType(m_inDsetName));
699 
700  te::gm::GeometryProperty* geomPropertySource = te::da::GetFirstGeomProperty(dsTypeSource.get());
701  te::gm::GeometryProperty* geomPropertyLayer = te::da::GetFirstGeomProperty(dsType);
702 
703  if(geoVec.size() > 0)
704  {
705  for(std::size_t geoPos = 0; geoPos < geoVec.size(); ++geoPos)
706  {
707  te::da::Expression* ex_operation;
708  te::da::Expression* ex_geom;
709  te::da::Field* f_geom;
710 
711  switch(geoVec[geoPos])
712  {
713  case te::vp::CONVEX_HULL:
714  {
715  std::size_t pos = dsType->getPropertyPosition("convex_hull");
716  if( pos < outDSet->getNumProperties() && pos > 0)
717  {
718  if (geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
719  {
720  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomPropertyLayer->getSRID());
721  ex_geom = new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
722  }
723  else
724  {
725  ex_geom = new te::da::PropertyName(name);
726  }
727 
728  te::da::Expression* e_union = new te::da::ST_Union(*ex_geom);
729  ex_operation = new te::da::ST_ConvexHull(*e_union);
730  f_geom = new te::da::Field(*ex_operation, "convex_hull");
731  fields->push_back(f_geom);
732  }
733  }
734  break;
735  case te::vp::CENTROID:
736  {
737  std::size_t pos = dsType->getPropertyPosition("centroid");
738  if( pos < outDSet->getNumProperties() && pos > 0)
739  {
740  if (geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
741  {
742  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomPropertyLayer->getSRID());
743  ex_geom = new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
744  }
745  else
746  {
747  ex_geom = new te::da::PropertyName(name);
748  }
749 
750  te::da::Expression* e_union = new te::da::ST_Union(*ex_geom);
751  ex_operation = new te::da::ST_Centroid(*e_union);
752  f_geom = new te::da::Field(*ex_operation, "centroid");
753  fields->push_back(f_geom);
754  }
755  }
756  break;
757  case te::vp::MBR:
758  {
759  std::size_t pos = dsType->getPropertyPosition("mbr");
760  if( pos < outDSet->getNumProperties() && pos > 0)
761  {
762  if (geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
763  {
764  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomPropertyLayer->getSRID());
765  ex_geom = new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
766  }
767  else
768  {
769  ex_geom = new te::da::PropertyName(name);
770  }
771 
772  te::da::Expression* e_union = new te::da::ST_Union(*ex_geom);
773  ex_operation = new te::da::ST_Envelope(*e_union);
774  f_geom = new te::da::Field(*ex_operation, "mbr");
775  fields->push_back(f_geom);
776  }
777  }
778  break;
779  default:
780  {
781 #ifdef TERRALIB_LOGGER_ENABLED
782  te::common::Logger::logDebug("vp", "Geometric Operation - Invalid field to add in query.");
783 #endif //TERRALIB_LOGGER_ENABLED
784  }
785  }
786  }
787  }
788  else
789  {
790  te::da::Expression* e_union;
791 
792  if (geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
793  {
794  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomPropertyLayer->getSRID());
795  e_union = new te::da::ST_Union(new te::da::ST_SetSRID(new te::da::PropertyName(name), srid));
796  }
797  else
798  {
799  e_union = new te::da::ST_Union(te::da::PropertyName(name));
800  }
801 
802  te::da::Field* f_prop = new te::da::Field(*e_union, name);
803  fields->push_back(f_prop);
804  }
805 
806  te::da::FromItem* fromItem = new te::da::DataSetName(m_converter->getResult()->getName());
807  te::da::From* from = new te::da::From;
808  from->push_back(fromItem);
809 
810  te::da::Select select(fields, from);
811 
812  te::da::GroupBy* groupBy = new te::da::GroupBy();
813  te::da::GroupByItem* e_groupBy = new te::da::GroupByItem(m_attribute);
814  groupBy->push_back(e_groupBy);
815 
816  select.setGroupBy(groupBy);
817 
818  std::auto_ptr<te::da::DataSet> dsQuery = m_inDsrc->query(select);
819 
820  if (dsQuery->isEmpty())
821  return false;
822 
823  SetOutputDSet(dsQuery.get(), outDSet.get());
824 
825  return outDSet.release();
826 }
827 
829  te::mem::DataSet* outDataSet)
830 {
831  std::size_t numProps = inDataSet->getNumProperties();
832  int pk = 0;
833 
834  bool geomFlag = true;
835 
836  inDataSet->moveBeforeFirst();
837  while(inDataSet->moveNext())
838  {
839  geomFlag = true;
840 
841  te::mem::DataSetItem* dItem = new te::mem::DataSetItem(outDataSet);
842 
843  dItem->setInt32(0, pk);
844 
845  for(std::size_t i = 0; i < numProps; ++i)
846  {
847  int type = inDataSet->getPropertyDataType(i);
848  switch(type)
849  {
850  case te::dt::INT16_TYPE:
851  dItem->setInt16(i+1, inDataSet->getInt16(i));
852  break;
853  case te::dt::INT32_TYPE:
854  dItem->setInt32(i+1, inDataSet->getInt32(i));
855  break;
856  case te::dt::INT64_TYPE:
857  dItem->setInt64(i+1, inDataSet->getInt64(i));
858  break;
859  case te::dt::DOUBLE_TYPE:
860  dItem->setDouble(i+1, inDataSet->getDouble(i));
861  break;
862  case te::dt::STRING_TYPE:
863  dItem->setString(i+1, inDataSet->getString(i));
864  break;
866  {
867  std::string inPropName = inDataSet->getPropertyName(i);
868 
869  if(inPropName == "convex_hull")
870  {
871  te::gm::Geometry* geom = inDataSet->getGeometry(i).release();
872  if(geom->isValid())
873  {
874  if(geom->getGeomTypeId() == te::gm::PolygonType)
875  dItem->setGeometry("convex_hull", geom);
876  else
877  geomFlag = false;
878  }
879  }
880  else if(inPropName == "centroid")
881  {
882  te::gm::Geometry* geom = inDataSet->getGeometry(i).release();
883  if(geom->isValid())
884  dItem->setGeometry("centroid", geom);
885  }
886  else if(inPropName == "mbr")
887  {
888  te::gm::Geometry* geom = inDataSet->getGeometry(i).release();
889  if(geom->isValid())
890  {
891  if(geom->getGeomTypeId() == te::gm::PolygonType)
892  dItem->setGeometry("mbr", geom);
893  else
894  geomFlag = false;
895  }
896  }
897  else
898  {
899  te::gm::Geometry* geom = inDataSet->getGeometry(i).release();
900 
901  switch(geom->getGeomTypeId())
902  {
903  case te::gm::PointType:
904  {
906  if(geom->isValid())
907  {
908  teGeomColl->setGeometryN(0, geom);
909  dItem->setGeometry(i+1, teGeomColl);
910  }
911  }
912  break;
914  {
916  if(geom->isValid())
917  {
918  teGeomColl->setGeometryN(0, geom);
919  dItem->setGeometry(i+1, teGeomColl);
920  }
921  }
922  break;
923  case te::gm::PolygonType:
924  {
926  if(geom->isValid())
927  {
928  teGeomColl->setGeometryN(0, geom);
929  dItem->setGeometry(i+1, teGeomColl);
930  }
931  }
932  break;
933  default:
934  {
935  dItem->setGeometry(i+1, geom);
936  }
937  break;
938  }
939  }
940  }
941  break;
942  default:
943  {
944 #ifdef TERRALIB_LOGGER_ENABLED
945  te::common::Logger::logDebug("vp", "Geometric Operation - Property type not found.");
946 #endif //TERRALIB_LOGGER_ENABLED
947  }
948  }
949  }
950  if(geomFlag)
951  {
952  ++pk;
953  outDataSet->add(dItem);
954  }
955  }
956 }
te::da::DataSetType * GetDataSetType(te::vp::GeometricOpObjStrategy, bool MultiGeomColumns, int geomOp=-1)
Definition: GeometricOp.cpp:96
int getSRID() const
It returns the Spatial Reference System ID associated to this geometric object.
Definition: Geometry.h:189
virtual boost::int16_t getInt16(std::size_t i) const =0
Method for retrieving a 16-bit integer attribute value (2 bytes long).
boost::ptr_vector< GroupByItem > GroupBy
A class that can be used to model a GROUP BY clause.
Definition: GroupBy.h:37
Geometric property.
An exception class for the Vector processing module.
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.
A class that can be used in a GROUP BY clause.
Definition: GroupByItem.h:50
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
The Field class can be used to model an expression that takes part of the output items of a SELECT...
Definition: Field.h:50
virtual boost::int32_t getInt32(std::size_t i) const =0
Method for retrieving a 32-bit integer attribute value (4 bytes long).
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
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
The geographic operation Minimum Bounding Rectangle.
Definition: Enums.h:104
A class that models the name of any property of an object.
Definition: PropertyName.h:50
A class that models the description of a dataset.
Definition: DataSetType.h:72
virtual boost::int64_t getInt64(std::size_t i) const =0
Method for retrieving a 64-bit integer attribute value (8 bytes long).
void Save(te::da::DataSource *source, te::da::DataSet *result, te::da::DataSetType *outDsType)
Definition: Utils.cpp:172
void setInt16(std::size_t i, boost::int16_t value)
It sets the value of the i-th property.
Spatial length operator.
Definition: ST_Length.h:50
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
Spatial envelope operator.
Definition: ST_Envelope.h:50
This is an abstract class that models a query expression.
Definition: Expression.h:47
void add(DataSetItem *item)
It adds a new item to the dataset and takes its ownership.
Definition: DataSet.cpp:172
Spatial perimeter operator.
Definition: ST_Perimeter.h:50
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
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
Spatial area operator.
Definition: ST_Area.h:50
Aggregate all objects.
Definition: Enums.h:118
virtual std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
URI C++ Library.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
Aggregate objects by attribute.
Definition: Enums.h:119
void SetOutputDSet(te::da::DataSet *inDataSet, te::mem::DataSet *outDataSet)
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
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
void setGroupBy(GroupBy *g)
It sets the list of expressions used to condense the result set.
Definition: Select.cpp:957
The geographic operation Centroid.
Definition: Enums.h:103
te::mem::DataSet * SetAggregByAttribute(te::da::DataSetType *dsType, std::vector< int > tabVec, std::vector< int > geoVec)
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
Spatial Set SRID operator.
Definition: ST_SetSRID.h:46
Spatial centroid operator.
Definition: ST_Centroid.h:50
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
void setGeometryN(std::size_t i, Geometry *g)
It sets the n-th geometry in this geometry collection.
The geographic operation Convex Hull.
Definition: Enums.h:102
std::size_t getPropertyPosition(const std::string &name) const
It returns the property position based on its name.
ST_Union statistical function.
Definition: ST_Union.h:46
te::mem::DataSet * SetAllObjects(te::da::DataSetType *dsType, std::vector< int > tabVec, std::vector< int > geoVec)
Configuration flags for the Terrralib Vector Processing module.
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
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.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
All objects individually.
Definition: Enums.h:117
te::mem::DataSet * SetAggregObj(te::da::DataSetType *dsType, std::vector< int > tabVec, std::vector< int > geoVec)