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
4  applications.
5 
6  TerraLib is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License,
9  or (at your option) any later version.
10 
11  TerraLib is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with TerraLib. See COPYING. If not, write to
18  TerraLib Team at <terralib-team@terralib.org>.
19  */
20 
21 /*!
22  \file GeometricOpQuery.h
23 
24  \brief Geometric Operation Vector Processing functions.
25 */
26 
27 // Terralib
28 
29 #include "../common/progress/TaskProgress.h"
30 #include "../core/logger/Logger.h"
31 #include "../core/translator/Translator.h"
32 
33 #include "../dataaccess/dataset/DataSet.h"
34 #include "../dataaccess/dataset/DataSetAdapter.h"
35 
36 #include "../dataaccess/query/DataSetName.h"
37 #include "../dataaccess/query/Expression.h"
38 #include "../dataaccess/query/Field.h"
39 #include "../dataaccess/query/Fields.h"
40 #include "../dataaccess/query/From.h"
41 #include "../dataaccess/query/FromItem.h"
42 #include "../dataaccess/query/GroupBy.h"
43 #include "../dataaccess/query/GroupByItem.h"
44 #include "../dataaccess/query/LiteralInt32.h"
45 #include "../dataaccess/query/PropertyName.h"
46 #include "../dataaccess/query/Select.h"
47 #include "../dataaccess/query/ST_Area.h"
48 #include "../dataaccess/query/ST_Centroid.h"
49 #include "../dataaccess/query/ST_ConvexHull.h"
50 #include "../dataaccess/query/ST_Envelope.h"
51 #include "../dataaccess/query/ST_Length.h"
52 #include "../dataaccess/query/ST_Perimeter.h"
53 #include "../dataaccess/query/ST_SetSRID.h"
54 #include "../dataaccess/query/ST_Transform.h"
55 #include "../dataaccess/query/ST_Union.h"
56 #include "../dataaccess/utils/Utils.h"
57 
58 #include "../geometry/Geometry.h"
59 #include "../geometry/GeometryCollection.h"
60 #include "../geometry/GeometryProperty.h"
61 
62 #include "../memory/DataSet.h"
63 #include "../memory/DataSetItem.h"
64 
65 #include "GeometricOpQuery.h"
66 #include "Utils.h"
67 
69 
71 
73 {
74  // get the geometric operation and/or tabular operation.
75  std::vector<int> opGeom;
76  std::vector<int> opTab;
77  std::vector<te::da::DataSetType*> dsTypeVec;
78 
79  for(std::size_t i = 0; i < m_operations.size(); ++i)
80  {
81  switch(m_operations[i])
82  {
83  case te::vp::CENTROID:
84  opGeom.push_back(te::vp::CENTROID);
85  break;
87  opGeom.push_back(te::vp::CONVEX_HULL);
88  break;
89  case te::vp::MBR:
90  opGeom.push_back(te::vp::MBR);
91  break;
92  case te::vp::AREA:
93  opTab.push_back(te::vp::AREA);
94  break;
95  case te::vp::LINE:
96  opTab.push_back(te::vp::LINE);
97  break;
98  case te::vp::PERIMETER:
99  opTab.push_back(te::vp::PERIMETER);
100  break;
101  default:
102  {
103  TE_LOG_INFO(
104  "Vector Processing - Geometric Operation - The operation is not "
105  "valid.");
106  }
107  }
108  }
109 
110  bool hasMultiGeomColumns = false;
111  bool result = false;
112 
113  switch(m_objStrategy)
114  {
115  case te::vp::ALL_OBJ:
116  {
117  if(hasMultiGeomColumns) // Condição se o DataSource suporta mais de uma
118  // soluna geometrica...
119  {
120  dsTypeVec.push_back(
122  }
123  else
124  {
125  if(opGeom.size() > 0)
126  {
127  for(std::size_t i = 0; i < opGeom.size(); ++i)
128  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(
129  te::vp::ALL_OBJ, false, opGeom[i]));
130  }
131  else
132  {
133  dsTypeVec.push_back(
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::unique_ptr<te::da::DataSetType> outDataSetType(
143  dsTypeVec[dsTypePos]);
144  std::unique_ptr<te::mem::DataSet> outDataSet(
145  SetAllObjects(dsTypeVec[dsTypePos], opTab, opGeom));
146 
147  te::vp::Save(m_outDsrc.get(), outDataSet.get(), outDataSetType.get());
148  result = true;
149 
150  if(!result)
151  return result;
152  }
153  }
154  break;
155  case te::vp::AGGREG_OBJ:
156  {
157  if(hasMultiGeomColumns) // Condição se o DataSource suporta mais de uma
158  // coluna geometrica...
159  {
160  dsTypeVec.push_back(
162  }
163  else
164  {
165  if(opGeom.size() > 0)
166  {
167  for(std::size_t i = 0; i < opGeom.size(); ++i)
168  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(
169  te::vp::AGGREG_OBJ, false, opGeom[i]));
170  }
171  else
172  {
173  dsTypeVec.push_back(
175  }
176  }
177 
178  for(std::size_t dsTypePos = 0; dsTypePos < dsTypeVec.size(); ++dsTypePos)
179  {
180  m_outDsetNameVec.push_back(dsTypeVec[dsTypePos]->getName());
181 
182  std::unique_ptr<te::da::DataSetType> outDataSetType(
183  dsTypeVec[dsTypePos]);
184  std::unique_ptr<te::mem::DataSet> outDataSet(
185  SetAggregObj(dsTypeVec[dsTypePos], opTab, opGeom));
186  try
187  {
188  te::vp::Save(m_outDsrc.get(), outDataSet.get(), outDataSetType.get());
189  result = true;
190  }
191  catch(...)
192  {
193  result = false;
194  }
195  if(!result)
196  return result;
197  }
198  }
199  break;
201  {
202  if(hasMultiGeomColumns) // Condição se o DataSource suporta mais de uma
203  // coluna geometrica...
204  {
205  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(
207  }
208  else
209  {
210  if(opGeom.size() > 0)
211  {
212  for(std::size_t i = 0; i < opGeom.size(); ++i)
213  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(
214  te::vp::AGGREG_BY_ATTRIBUTE, false, opGeom[i]));
215  }
216  else
217  {
218  dsTypeVec.push_back(te::vp::GeometricOp::GetDataSetType(
220  }
221  }
222 
223  for(std::size_t dsTypePos = 0; dsTypePos < dsTypeVec.size(); ++dsTypePos)
224  {
225  m_outDsetNameVec.push_back(dsTypeVec[dsTypePos]->getName());
226 
227  std::unique_ptr<te::da::DataSetType> outDataSetType(
228  dsTypeVec[dsTypePos]);
229  std::unique_ptr<te::mem::DataSet> outDataSet(
230  SetAggregByAttribute(dsTypeVec[dsTypePos], opTab, opGeom));
231  try
232  {
233  te::vp::Save(m_outDsrc.get(), outDataSet.get(), outDataSetType.get());
234  result = true;
235  }
236  catch(...)
237  {
238  result = false;
239  }
240  if(!result)
241  return result;
242  }
243  }
244  break;
245  default:
246  {
247  TE_LOG_INFO("Vector Processing - Geometric Operation - Strategy Not found!");
248  }
249  return false;
250  }
251 
252  return result;
253 }
254 
256  te::da::DataSetType* dsType, std::vector<int> tabVec,
257  std::vector<int> geoVec)
258 {
259  std::unique_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(dsType));
260 
261  std::unique_ptr<te::da::DataSetType> inDsetType = m_inDsrc->getDataSetType(m_inDsetName);
262  te::gm::GeometryProperty* geomProperty = te::da::GetFirstGeomProperty(inDsetType.get());
263  std::string name = geomProperty->getName();
264 
265  te::da::Fields* fields = new te::da::Fields;
266 
267  if(m_selectedProps.size() > 0)
268  {
269  for(std::size_t prop_pos = 0; prop_pos < m_selectedProps.size(); ++prop_pos)
270  {
271  te::da::Field* f_prop = new te::da::Field(m_selectedProps[prop_pos]);
272  fields->push_back(f_prop);
273  }
274  }
275 
276  if(tabVec.size() > 0)
277  {
278  for(std::size_t tabPos = 0; tabPos < tabVec.size(); ++tabPos)
279  {
280  te::da::Expression* ex;
281  te::da::Field* f;
282 
283  switch(tabVec[tabPos])
284  {
285  case te::vp::AREA:
286  {
287  if(geomProperty->getSRID() != m_newSRID)
288  {
290  ex = new te::da::ST_Area(*transform_ex);
291  }
292  else
293  {
294  ex = new te::da::ST_Area(te::da::PropertyName(name));
295  }
296 
297  f = new te::da::Field(*ex, "area");
298  fields->push_back(f);
299  }
300  break;
301  case te::vp::LINE:
302  {
303  if(geomProperty->getSRID() != m_newSRID)
304  {
306  ex = new te::da::ST_Length(*transform_ex);
307  }
308  else
309  {
310  ex = new te::da::ST_Length(te::da::PropertyName(name));
311  }
312 
313  f = new te::da::Field(*ex, "line_length");
314  fields->push_back(f);
315  }
316  break;
317  case te::vp::PERIMETER:
318  {
319  if(geomProperty->getSRID() != m_newSRID)
320  {
322  ex = new te::da::ST_Perimeter(*transform_ex);
323  }
324  else
325  {
327  }
328  f = new te::da::Field(*ex, "perimeter");
329  fields->push_back(f);
330  }
331  break;
332  default:
333  {
334  TE_LOG_INFO(
335  "Vector Processing - Geometric Operation - Invalid field to add "
336  "in query.");
337  }
338  }
339  }
340  }
341 
342  std::unique_ptr<te::da::DataSetType> dsTypeSource(
343  m_inDsrc->getDataSetType(m_inDsetName));
344 
345  te::gm::GeometryProperty* geomPropertySource =
346  te::da::GetFirstGeomProperty(dsTypeSource.get());
347  te::gm::GeometryProperty* geomPropertyLayer =
349 
350  if(geoVec.size() > 0)
351  {
352  for(std::size_t geoPos = 0; geoPos < geoVec.size(); ++geoPos)
353  {
354  te::da::Expression* ex_operation;
355  te::da::Expression* ex_geom;
356  te::da::Field* f_geom;
357 
358  switch(geoVec[geoPos])
359  {
360  case te::vp::CONVEX_HULL:
361  {
362  std::size_t pos = dsType->getPropertyPosition("convex_hull");
363  if(pos < outDSet->getNumProperties() && pos > 0)
364  {
365  if(geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
366  {
367  te::da::LiteralInt32* srid =
368  new te::da::LiteralInt32(geomPropertyLayer->getSRID());
369  ex_geom =
370  new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
371  }
372  else
373  {
374  ex_geom = new te::da::PropertyName(name);
375  }
376 
377  ex_operation = new te::da::ST_ConvexHull(*ex_geom);
378  f_geom = new te::da::Field(*ex_operation, "convex_hull");
379  fields->push_back(f_geom);
380  }
381  }
382  break;
383  case te::vp::CENTROID:
384  {
385  std::size_t pos = dsType->getPropertyPosition("centroid");
386  if(pos < outDSet->getNumProperties() && pos > 0)
387  {
388  if(geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
389  {
390  te::da::LiteralInt32* srid =
391  new te::da::LiteralInt32(geomPropertyLayer->getSRID());
392  ex_geom =
393  new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
394  }
395  else
396  {
397  ex_geom = new te::da::PropertyName(name);
398  }
399 
400  ex_operation = new te::da::ST_Centroid(*ex_geom);
401  f_geom = new te::da::Field(*ex_operation, "centroid");
402  fields->push_back(f_geom);
403  }
404  }
405  break;
406  case te::vp::MBR:
407  {
408  std::size_t pos = dsType->getPropertyPosition("mbr");
409  if(pos < outDSet->getNumProperties() && pos > 0)
410  {
411  if(geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
412  {
413  te::da::LiteralInt32* srid =
414  new te::da::LiteralInt32(geomPropertyLayer->getSRID());
415  ex_geom =
416  new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
417  }
418  else
419  {
420  ex_geom = new te::da::PropertyName(name);
421  }
422 
423  ex_operation = new te::da::ST_Envelope(*ex_geom);
424  f_geom = new te::da::Field(*ex_operation, "mbr");
425  fields->push_back(f_geom);
426  }
427  }
428  break;
429  default:
430  {
431  TE_LOG_INFO(
432  "Vector Processing - Geometric Operation - Invalid field to add "
433  "in query.");
434  }
435  }
436  }
437  }
438  else
439  {
440  te::da::Expression* ex_geom;
441 
442  if(geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
443  {
444  te::da::LiteralInt32* srid =
445  new te::da::LiteralInt32(geomPropertyLayer->getSRID());
446  ex_geom = new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
447  }
448  else
449  {
450  ex_geom = new te::da::PropertyName(name);
451  }
452 
453  te::da::Field* f_prop = new te::da::Field(*ex_geom);
454  fields->push_back(f_prop);
455  }
456 
457  te::da::FromItem* fromItem =
458  new te::da::DataSetName(m_converter->getResult()->getName());
459  te::da::From* from = new te::da::From;
460  from->push_back(fromItem);
461 
462  te::da::Select select(fields, from);
463 
464  std::unique_ptr<te::da::DataSet> dsQuery = m_inDsrc->query(select);
465 
466  if(dsQuery->isEmpty())
467  return nullptr;
468 
469  SetOutputDSet(dsQuery.get(), outDSet.get());
470 
471  return outDSet.release();
472 }
473 
475  te::da::DataSetType* dsType, std::vector<int> tabVec,
476  std::vector<int> geoVec)
477 {
478  std::unique_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(dsType));
479 
480  std::unique_ptr<te::da::DataSetType> inDsetType = m_inDsrc->getDataSetType(m_inDsetName);
481  te::gm::GeometryProperty* geomProperty = te::da::GetFirstGeomProperty(inDsetType.get());
482  std::string name = geomProperty->getName();
483 
484  te::da::Fields* fields = new te::da::Fields;
485 
486  if(tabVec.size() > 0)
487  {
488  for(std::size_t tabPos = 0; tabPos < tabVec.size(); ++tabPos)
489  {
490  te::da::Expression* ex;
491  te::da::Field* f;
492 
493  switch(tabVec[tabPos])
494  {
495  case te::vp::AREA:
496  {
498  if(geomProperty->getSRID() != m_newSRID)
499  {
500  te::da::Expression* transform_ex = new te::da::ST_Transform(*e_union, m_newSRID);
501  ex = new te::da::ST_Area(*transform_ex);
502  }
503  else
504  {
505  ex = new te::da::ST_Area(*e_union);
506  }
507 
508  f = new te::da::Field(*ex, "area");
509  fields->push_back(f);
510  }
511  break;
512  case te::vp::LINE:
513  {
515  if(geomProperty->getSRID() != m_newSRID)
516  {
517  te::da::Expression* transform_ex = new te::da::ST_Transform(*e_union, m_newSRID);
518  ex = new te::da::ST_Length(*transform_ex);
519  }
520  else
521  {
522  ex = new te::da::ST_Length(*e_union);
523  }
524 
525  f = new te::da::Field(*ex, "line_length");
526  fields->push_back(f);
527  }
528  break;
529  case te::vp::PERIMETER:
530  {
532  if(geomProperty->getSRID() != m_newSRID)
533  {
534  te::da::Expression* transform_ex = new te::da::ST_Transform(*e_union, m_newSRID);
535  ex = new te::da::ST_Perimeter(*transform_ex);
536  }
537  else
538  {
539  ex = new te::da::ST_Perimeter(*e_union);
540  }
541 
542  f = new te::da::Field(*ex, "perimeter");
543  fields->push_back(f);
544  }
545  break;
546  default:
547  {
548  TE_LOG_INFO(
549  "Vector Processing - Geometric Operation - Invalid field to add "
550  "in query.");
551  }
552  }
553  }
554  }
555 
556  std::unique_ptr<te::da::DataSetType> dsTypeSource(
557  m_inDsrc->getDataSetType(m_inDsetName));
558 
559  te::gm::GeometryProperty* geomPropertySource =
560  te::da::GetFirstGeomProperty(dsTypeSource.get());
561  te::gm::GeometryProperty* geomPropertyLayer =
563 
564  if(geoVec.size() > 0)
565  {
566  for(std::size_t geoPos = 0; geoPos < geoVec.size(); ++geoPos)
567  {
568  te::da::Expression* ex_operation;
569  te::da::Expression* ex_geom;
570  te::da::Field* f_geom;
571 
572  switch(geoVec[geoPos])
573  {
574  case te::vp::CONVEX_HULL:
575  {
576  std::size_t pos = dsType->getPropertyPosition("convex_hull");
577  if(pos < outDSet->getNumProperties() && pos > 0)
578  {
579  if(geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
580  {
581  te::da::LiteralInt32* srid =
582  new te::da::LiteralInt32(geomPropertyLayer->getSRID());
583  ex_geom =
584  new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
585  }
586  else
587  {
588  ex_geom = new te::da::PropertyName(name);
589  }
590 
591  te::da::Expression* e_union = new te::da::ST_Union(*ex_geom);
592  ex_operation = new te::da::ST_ConvexHull(*e_union);
593  f_geom = new te::da::Field(*ex_operation, "convex_hull");
594  fields->push_back(f_geom);
595  }
596  }
597  break;
598  case te::vp::CENTROID:
599  {
600  std::size_t pos = dsType->getPropertyPosition("centroid");
601  if(pos < outDSet->getNumProperties() && pos > 0)
602  {
603  if(geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
604  {
605  te::da::LiteralInt32* srid =
606  new te::da::LiteralInt32(geomPropertyLayer->getSRID());
607  ex_geom =
608  new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
609  }
610  else
611  {
612  ex_geom = new te::da::PropertyName(name);
613  }
614 
615  te::da::Expression* e_union = new te::da::ST_Union(*ex_geom);
616  ex_operation = new te::da::ST_Centroid(*e_union);
617  f_geom = new te::da::Field(*ex_operation, "centroid");
618  fields->push_back(f_geom);
619  }
620  }
621  break;
622  case te::vp::MBR:
623  {
624  std::size_t pos = dsType->getPropertyPosition("mbr");
625  if(pos < outDSet->getNumProperties() && pos > 0)
626  {
627  if(geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
628  {
629  te::da::LiteralInt32* srid =
630  new te::da::LiteralInt32(geomPropertyLayer->getSRID());
631  ex_geom =
632  new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
633  }
634  else
635  {
636  ex_geom = new te::da::PropertyName(name);
637  }
638 
639  te::da::Expression* e_union = new te::da::ST_Union(*ex_geom);
640  ex_operation = new te::da::ST_Envelope(*e_union);
641  f_geom = new te::da::Field(*ex_operation, "mbr");
642  fields->push_back(f_geom);
643  }
644  }
645  break;
646  default:
647  {
648  TE_LOG_INFO(
649  "Vector Processing - Geometric Operation - Invalid field to add "
650  "in query.");
651  }
652  }
653  }
654  }
655  else
656  {
657  te::da::Expression* e_union;
658 
659  if(geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
660  {
661  te::da::LiteralInt32* srid =
662  new te::da::LiteralInt32(geomPropertyLayer->getSRID());
663  e_union = new te::da::ST_Union(
664  new te::da::ST_SetSRID(new te::da::PropertyName(name), srid));
665  }
666  else
667  {
668  e_union = new te::da::ST_Union(te::da::PropertyName(name));
669  }
670 
671  te::da::Field* f_prop = new te::da::Field(*e_union, name);
672  fields->push_back(f_prop);
673  }
674 
675  te::da::FromItem* fromItem =
676  new te::da::DataSetName(m_converter->getResult()->getName());
677  te::da::From* from = new te::da::From;
678  from->push_back(fromItem);
679 
680  te::da::Select select(fields, from);
681 
682  std::unique_ptr<te::da::DataSet> dsQuery = m_inDsrc->query(select);
683 
684  if(dsQuery->isEmpty())
685  return nullptr;
686 
687  SetOutputDSet(dsQuery.get(), outDSet.get());
688 
689  return outDSet.release();
690 }
691 
693  te::da::DataSetType* dsType, std::vector<int> tabVec,
694  std::vector<int> geoVec)
695 {
696  std::unique_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(dsType));
697 
698  std::unique_ptr<te::da::DataSetType> inDsetType = m_inDsrc->getDataSetType(m_inDsetName);
699  te::gm::GeometryProperty* geomProperty = te::da::GetFirstGeomProperty(inDsetType.get());
700  std::string name = geomProperty->getName();
701 
702  te::da::Fields* fields = new te::da::Fields;
703 
704  te::da::Field* f_aggreg = new te::da::Field(m_attribute);
705  fields->push_back(f_aggreg);
706 
707  if(tabVec.size() > 0)
708  {
709  for(std::size_t tabPos = 0; tabPos < tabVec.size(); ++tabPos)
710  {
711  te::da::Expression* ex;
712  te::da::Field* f;
713 
714  switch(tabVec[tabPos])
715  {
716  case te::vp::AREA:
717  {
719 
720  if(geomProperty->getSRID() != m_newSRID)
721  {
722  te::da::Expression* transform_ex = new te::da::ST_Transform(*e_union, m_newSRID);
723  ex = new te::da::ST_Area(*transform_ex);
724  }
725  else
726  {
727  ex = new te::da::ST_Area(*e_union);
728  }
729 
730  f = new te::da::Field(*ex, "area");
731  fields->push_back(f);
732  }
733  break;
734  case te::vp::LINE:
735  {
737 
738  if(geomProperty->getSRID() != m_newSRID)
739  {
740  te::da::Expression* transform_ex = new te::da::ST_Transform(*e_union, m_newSRID);
741  ex = new te::da::ST_Length(*transform_ex);
742  }
743  else
744  {
745  ex = new te::da::ST_Length(*e_union);
746  }
747 
748  f = new te::da::Field(*ex, "line_length");
749  fields->push_back(f);
750  }
751  break;
752  case te::vp::PERIMETER:
753  {
755 
756  if(geomProperty->getSRID() != m_newSRID)
757  {
758  te::da::Expression* transform_ex = new te::da::ST_Transform(*e_union, m_newSRID);
759  ex = new te::da::ST_Perimeter(*transform_ex);
760  }
761  else
762  {
763  ex = new te::da::ST_Perimeter(*e_union);
764  }
765 
766  f = new te::da::Field(*ex, "perimeter");
767  fields->push_back(f);
768  }
769  break;
770  default:
771  {
772  TE_LOG_INFO(
773  "Vector Processing - Geometric Operation - Invalid field to add "
774  "in query.");
775  }
776  }
777  }
778  }
779 
780  std::unique_ptr<te::da::DataSetType> dsTypeSource(
781  m_inDsrc->getDataSetType(m_inDsetName));
782 
783  te::gm::GeometryProperty* geomPropertySource =
784  te::da::GetFirstGeomProperty(dsTypeSource.get());
785  te::gm::GeometryProperty* geomPropertyLayer =
787 
788  if(geoVec.size() > 0)
789  {
790  for(std::size_t geoPos = 0; geoPos < geoVec.size(); ++geoPos)
791  {
792  te::da::Expression* ex_operation;
793  te::da::Expression* ex_geom;
794  te::da::Field* f_geom;
795 
796  switch(geoVec[geoPos])
797  {
798  case te::vp::CONVEX_HULL:
799  {
800  std::size_t pos = dsType->getPropertyPosition("convex_hull");
801  if(pos < outDSet->getNumProperties() && pos > 0)
802  {
803  if(geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
804  {
805  te::da::LiteralInt32* srid =
806  new te::da::LiteralInt32(geomPropertyLayer->getSRID());
807  ex_geom =
808  new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
809  }
810  else
811  {
812  ex_geom = new te::da::PropertyName(name);
813  }
814 
815  te::da::Expression* e_union = new te::da::ST_Union(*ex_geom);
816  ex_operation = new te::da::ST_ConvexHull(*e_union);
817  f_geom = new te::da::Field(*ex_operation, "convex_hull");
818  fields->push_back(f_geom);
819  }
820  }
821  break;
822  case te::vp::CENTROID:
823  {
824  std::size_t pos = dsType->getPropertyPosition("centroid");
825  if(pos < outDSet->getNumProperties() && pos > 0)
826  {
827  if(geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
828  {
829  te::da::LiteralInt32* srid =
830  new te::da::LiteralInt32(geomPropertyLayer->getSRID());
831  ex_geom =
832  new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
833  }
834  else
835  {
836  ex_geom = new te::da::PropertyName(name);
837  }
838 
839  te::da::Expression* e_union = new te::da::ST_Union(*ex_geom);
840  ex_operation = new te::da::ST_Centroid(*e_union);
841  f_geom = new te::da::Field(*ex_operation, "centroid");
842  fields->push_back(f_geom);
843  }
844  }
845  break;
846  case te::vp::MBR:
847  {
848  std::size_t pos = dsType->getPropertyPosition("mbr");
849  if(pos < outDSet->getNumProperties() && pos > 0)
850  {
851  if(geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
852  {
853  te::da::LiteralInt32* srid =
854  new te::da::LiteralInt32(geomPropertyLayer->getSRID());
855  ex_geom =
856  new te::da::ST_SetSRID(new te::da::PropertyName(name), srid);
857  }
858  else
859  {
860  ex_geom = new te::da::PropertyName(name);
861  }
862 
863  te::da::Expression* e_union = new te::da::ST_Union(*ex_geom);
864  ex_operation = new te::da::ST_Envelope(*e_union);
865  f_geom = new te::da::Field(*ex_operation, "mbr");
866  fields->push_back(f_geom);
867  }
868  }
869  break;
870  default:
871  {
872  TE_LOG_INFO(
873  "Vector Processing - Geometric Operation - Invalid field to add "
874  "in query.");
875  }
876  }
877  }
878  }
879  else
880  {
881  te::da::Expression* e_union;
882 
883  if(geomPropertySource->getSRID() != geomPropertyLayer->getSRID())
884  {
885  te::da::LiteralInt32* srid =
886  new te::da::LiteralInt32(geomPropertyLayer->getSRID());
887  e_union = new te::da::ST_Union(
888  new te::da::ST_SetSRID(new te::da::PropertyName(name), srid));
889  }
890  else
891  {
892  e_union = new te::da::ST_Union(te::da::PropertyName(name));
893  }
894 
895  te::da::Field* f_prop = new te::da::Field(*e_union, name);
896  fields->push_back(f_prop);
897  }
898 
899  te::da::FromItem* fromItem =
900  new te::da::DataSetName(m_converter->getResult()->getName());
901  te::da::From* from = new te::da::From;
902  from->push_back(fromItem);
903 
904  te::da::Select select(fields, from);
905 
906  te::da::GroupBy* groupBy = new te::da::GroupBy();
908  groupBy->push_back(e_groupBy);
909 
910  select.setGroupBy(groupBy);
911 
912  std::unique_ptr<te::da::DataSet> dsQuery = m_inDsrc->query(select);
913 
914  if(dsQuery->isEmpty())
915  return nullptr;
916 
917  SetOutputDSet(dsQuery.get(), outDSet.get());
918 
919  return outDSet.release();
920 }
921 
923  te::mem::DataSet* outDataSet)
924 {
925  std::size_t numProps = inDataSet->getNumProperties();
926  int pk = 0;
927 
928  inDataSet->moveBeforeFirst();
929  while(inDataSet->moveNext())
930  {
931  bool geomFlag = true;
932 
933  te::mem::DataSetItem* dItem = new te::mem::DataSetItem(outDataSet);
934 
935  dItem->setInt32(0, pk);
936 
937  for(std::size_t i = 0; i < numProps; ++i)
938  {
939  if (inDataSet->isNull(i))
940  continue;
941 
942  int type = inDataSet->getPropertyDataType(i);
943  switch(type)
944  {
945  case te::dt::INT16_TYPE:
946  dItem->setInt16(i + 1, inDataSet->getInt16(i));
947  break;
948  case te::dt::INT32_TYPE:
949  dItem->setInt32(i + 1, inDataSet->getInt32(i));
950  break;
951  case te::dt::INT64_TYPE:
952  dItem->setInt64(i + 1, inDataSet->getInt64(i));
953  break;
954  case te::dt::DOUBLE_TYPE:
955  dItem->setDouble(i + 1, inDataSet->getDouble(i));
956  break;
957  case te::dt::STRING_TYPE:
958  dItem->setString(i + 1, inDataSet->getString(i));
959  break;
961  {
962  std::string inPropName = inDataSet->getPropertyName(i);
963  std::unique_ptr<te::gm::Geometry> geom(
964  inDataSet->getGeometry(i).release());
965 
966  if(inPropName == "convex_hull")
967  {
968  if(geom->getGeomTypeId() == te::gm::PolygonType)
969  dItem->setGeometry("convex_hull", geom.release());
970  else
971  geomFlag = false;
972  }
973  else if(inPropName == "centroid")
974  {
975  dItem->setGeometry("centroid", geom.release());
976  }
977  else if(inPropName == "mbr")
978  {
979  if(geom->getGeomTypeId() == te::gm::PolygonType)
980  dItem->setGeometry("mbr", geom.release());
981  else
982  geomFlag = false;
983  }
984  else
985  {
986  te::gm::GeometryCollection* teGeomColl =
988  0, te::gm::GeometryCollectionType, geom->getSRID());
989 
990  switch(geom->getGeomTypeId())
991  {
992  case te::gm::PointType:
993  case te::gm::PointMType:
994  case te::gm::PointZType:
995  case te::gm::PointZMType:
1000  case te::gm::PolygonType:
1001  case te::gm::PolygonMType:
1002  case te::gm::PolygonZType:
1003  case te::gm::PolygonZMType:
1004  {
1005  if(geom->isValid())
1006  teGeomColl->add(geom.release());
1007  }
1008  break;
1009  default:
1010  {
1011  TE_LOG_INFO(
1012  "Vector Processing - Geometric Operation - Could not "
1013  "insert the geometry in collection.");
1014  }
1015  }
1016 
1017  if(teGeomColl->getNumGeometries() != 0)
1018  dItem->setGeometry(i + 1, teGeomColl);
1019  else
1020  dItem->setGeometry(i + 1, geom.release());
1021  }
1022  }
1023  break;
1024  default:
1025  {
1026  TE_LOG_INFO(
1027  "Vector Processing - Geometric Operation - Property type not "
1028  "found.");
1029  }
1030  }
1031  }
1032  if(geomFlag)
1033  {
1034  ++pk;
1035  outDataSet->add(dItem);
1036  }
1037  }
1038 }
virtual std::unique_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
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:79
boost::ptr_vector< GroupByItem > GroupBy
A class that can be used to model a GROUP BY clause.
Definition: GroupBy.h:37
Geometric property.
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
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...
std::vector< std::string > m_outDsetNameVec
Definition: GeometricOp.h:93
void setDouble(std::size_t i, double value)
It sets the value of the i-th property.
The geographic operation Line.
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
The geographic operation Minimum Bounding Rectangle.
A class that models the name of any property of an object.
Base exception class for plugin module.
A class that models the description of a dataset.
Definition: DataSetType.h:72
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
#define TE_LOG_INFO(message)
Use this tag in order to log a message to the TerraLib default logger with the INFO level...
Definition: Logger.h:315
Spatial envelope operator.
Definition: ST_Envelope.h:50
This is an abstract class that models a query expression.
void add(DataSetItem *item)
It adds a new item to the dataset and takes its ownership.
Spatial perimeter operator.
Definition: ST_Perimeter.h:50
Geometric Operation Vector Processing functions.
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.
TEVPEXPORT void Save(te::da::DataSource *source, te::da::DataSet *result, te::da::DataSetType *outDsType, const bool &enableProgress=true)
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
The geographic operation Area.
Spatial area operator.
Definition: ST_Area.h:50
Aggregate all objects.
virtual boost::int16_t getInt16(std::size_t i) const =0
Method for retrieving a 16-bit integer attribute value (2 bytes long).
int getSRID() const
It returns the spatial reference system identifier associated to this property.
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
URI C++ Library.
Definition: Attributes.h:37
virtual boost::int32_t getInt32(std::size_t i) const =0
Method for retrieving a 32-bit integer attribute value (4 bytes long).
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
std::unique_ptr< te::da::DataSource > m_outDsrc
Definition: GeometricOp.h:91
std::string m_inDsetName
Definition: GeometricOp.h:82
Aggregate objects by attribute.
void SetOutputDSet(te::da::DataSet *inDataSet, te::mem::DataSet *outDataSet)
The geographic operation Perimeter.
std::string m_attribute
Definition: GeometricOp.h:88
Utility functions for the data access module.
void setGroupBy(GroupBy *g)
It sets the list of expressions used to condense the result set.
Definition: Select.cpp:809
The geographic operation Centroid.
te::mem::DataSet * SetAggregByAttribute(te::da::DataSetType *dsType, std::vector< int > tabVec, std::vector< int > geoVec)
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
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...
A dataset is the unit of information manipulated by the data access module of TerraLib.
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
void add(Geometry *g)
It adds the geometry into the collection.
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
The geographic operation Convex Hull.
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
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
te::mem::DataSet * SetAllObjects(te::da::DataSetType *dsType, std::vector< int > tabVec, std::vector< int > geoVec)
virtual boost::int64_t getInt64(std::size_t i) const =0
Method for retrieving a 64-bit integer attribute value (8 bytes long).
te::vp::GeometricOpObjStrategy m_objStrategy
Definition: GeometricOp.h:87
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
virtual std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
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)
All objects individually.
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
te::mem::DataSet * SetAggregObj(te::da::DataSetType *dsType, std::vector< int > tabVec, std::vector< int > geoVec)
std::unique_ptr< te::da::DataSetTypeConverter > m_converter
Definition: GeometricOp.h:83
const std::string & getName() const
It returns the property name.
Definition: Property.h:127