All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LineToPolygonQuery.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 LineToPolygonQuery.h
22 
23  \brief Line To Polygon Vector Processing functions.
24 */
25 
26 //Terralib
27 #include "../dataaccess/dataset/DataSet.h"
28 
29 #include "../datatype/Property.h"
30 
31 #include "../dataaccess/query/DataSetName.h"
32 #include "../dataaccess/query/Expression.h"
33 #include "../dataaccess/query/Field.h"
34 #include "../dataaccess/query/Fields.h"
35 #include "../dataaccess/query/From.h"
36 #include "../dataaccess/query/FromItem.h"
37 #include "../dataaccess/query/GroupBy.h"
38 #include "../dataaccess/query/GroupByItem.h"
39 #include "../dataaccess/query/PropertyName.h"
40 #include "../dataaccess/query/Select.h"
41 #include "../dataaccess/query/ST_Dump.h"
42 #include "../dataaccess/query/ST_Collect.h"
43 #include "../dataaccess/query/ST_MakePolygon.h"
44 #include "../dataaccess/query/SubSelect.h"
45 #include "../dataaccess/query/Where.h"
46 
47 #include "LineToPolygonQuery.h"
48 #include "Utils.h"
49 
50 // STL
51 #include <memory>
52 #include <vector>
53 
55 {}
56 
58 {}
59 
60 bool te::vp::LineToPolygonQuery::run() throw (te::common::Exception)
61 {
62  std::auto_ptr<te::da::DataSetType> outDsType = buildOutDataSetType();
63  std::vector<te::dt::Property*> props = outDsType->getProperties();
64 
65 // Subselect that apply the ST_Dump function in geometric column to separate multi polygons.
66  te::da::Fields* pol_fields = new te::da::Fields;
67  for(std::size_t i = 0; i < props.size(); ++i)
68  {
69  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
70  {
71  te::da::PropertyName* pName = new te::da::PropertyName(props[i]->getName());
72  te::da::Field* field = new te::da::Field(pName);
73  pol_fields->push_back(field);
74  }
75  else
76  {
77  te::da::PropertyName* polName = new te::da::PropertyName(props[i]->getName());
78  te::da::Expression* e_dump = new te::da::ST_Dump(polName);
79  te::da::Expression* e_makePolygon = new te::da::ST_MakePolygon(e_dump);
80  te::da::Field* f_makePolygon = new te::da::Field(*e_makePolygon, " polygon");
81  pol_fields->push_back(f_makePolygon);
82  }
83  }
84 
85  te::da::FromItem* fromItemPol = new te::da::DataSetName(m_inDsetName);
86  te::da::From* fromPol = new te::da::From;
87  fromPol->push_back(fromItemPol);
88 
89  te::da::Where* w_oid = 0;
90 
91  if(m_oidSet)
92  w_oid = new te::da::Where(m_oidSet->getExpression());
93 
94  te::da::Select select_Pol(pol_fields, fromPol, w_oid);
95  te::da::SubSelect subSelect_Pol(select_Pol, "pol");
96 
97 // Subselect that apply the ST_DumpRings function in geometric column to get polygon as linestring.
98  te::da::Fields* line_fields = new te::da::Fields;
99  for(std::size_t i = 0; i < props.size(); ++i)
100  {
101  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
102  {
103  te::da::PropertyName* pName = new te::da::PropertyName(props[i]->getName());
104  te::da::Field* field = new te::da::Field(pName);
105  line_fields->push_back(field);
106  }
107  else
108  {
109  te::da::PropertyName* gName = new te::da::PropertyName("polygon");
110  te::da::Expression* e_collect = new te::da::ST_Collect(gName);
111  te::da::Field* f_collect = new te::da::Field(*e_collect, props[i]->getName());
112  line_fields->push_back(f_collect);
113  }
114  }
115 
116  te::da::FromItem* fromItem = new te::da::SubSelect(subSelect_Pol);
117  te::da::From* from = new te::da::From;
118  from->push_back(fromItem);
119 
120  te::da::Select select(line_fields, from);
121 
122 // Group by
123  te::da::GroupBy* groupBy = new te::da::GroupBy();
124  for(std::size_t i = 0; i < props.size(); ++i)
125  {
126  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
127  {
128  te::da::GroupByItem* e_groupBy = new te::da::GroupByItem(props[i]->getName());
129  groupBy->push_back(e_groupBy);
130  }
131  }
132  select.setGroupBy(groupBy);
133 
134  std::auto_ptr<te::da::DataSet> dsQuery(m_inDsrc->query(select));
135 
136  te::vp::Save(m_outDsrc.get(), dsQuery.get(), outDsType.get());
137  return true;
138 
139 }
boost::ptr_vector< GroupByItem > GroupBy
A class that can be used to model a GROUP BY clause.
Definition: GroupBy.h:37
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
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
A class that models the name of any property of an object.
Definition: PropertyName.h:50
void Save(te::da::DataSource *source, te::da::DataSet *result, te::da::DataSetType *outDsType)
Definition: Utils.cpp:213
ST_Collect statistical function.
Definition: ST_Collect.h:46
This is an abstract class that models a query expression.
Definition: Expression.h:47
ST_MakePolygon statistical function.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
void setGroupBy(GroupBy *g)
It sets the list of expressions used to condense the result set.
Definition: Select.cpp:957
ST_Dump statistical function.
Definition: ST_Dump.h:46
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
Line To Polygon Vector Processing functions.
A Select can be used as a source of information in another query.
Definition: SubSelect.h:49