All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PolygonToLineQuery.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 PolygonToLineQuery.h
22 
23  \brief Polygon To Line Vector Processing functions.
24 */
25 
26 //Terralib
27 
28 #include "../dataaccess/dataset/DataSet.h"
29 
30 #include "../datatype/Property.h"
31 
32 #include "../dataaccess/query/DataSetName.h"
33 #include "../dataaccess/query/Expression.h"
34 #include "../dataaccess/query/Field.h"
35 #include "../dataaccess/query/Fields.h"
36 #include "../dataaccess/query/From.h"
37 #include "../dataaccess/query/FromItem.h"
38 #include "../dataaccess/query/GroupBy.h"
39 #include "../dataaccess/query/GroupByItem.h"
40 #include "../dataaccess/query/PropertyName.h"
41 #include "../dataaccess/query/Select.h"
42 #include "../dataaccess/query/ST_Boundary.h"
43 #include "../dataaccess/query/ST_Collect.h"
44 #include "../dataaccess/query/ST_Dump.h"
45 #include "../dataaccess/query/ST_DumpRings.h"
46 #include "../dataaccess/query/SubSelect.h"
47 #include "../dataaccess/query/Where.h"
48 
49 #include "PolygonToLineQuery.h"
50 #include "Utils.h"
51 
52 // STL
53 #include <memory>
54 #include <string>
55 #include <vector>
56 
58 {}
59 
61 {}
62 
63 bool te::vp::PolygonToLineQuery::run() throw(te::common::Exception)
64 {
65  std::auto_ptr<te::da::DataSetType> outDsType = buildOutDataSetType();
66  std::vector<te::dt::Property*> props = outDsType->getProperties();
67 
68 // Subselect that apply the ST_Dump function in geometric column to separate multi polygons.
69  te::da::Fields* pol_fields = new te::da::Fields;
70  for(std::size_t i = 0; i < props.size(); ++i)
71  {
72  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
73  {
74  te::da::PropertyName* pName = new te::da::PropertyName(props[i]->getName());
75  te::da::Field* field = new te::da::Field(pName);
76  pol_fields->push_back(field);
77  }
78  else
79  {
80  te::da::PropertyName* gName = new te::da::PropertyName(props[i]->getName());
81  te::da::Expression* e_dump = new te::da::ST_Dump(gName);
82  te::da::Field* f_dump = new te::da::Field(*e_dump, " polygon");
83  pol_fields->push_back(f_dump);
84  }
85  }
86 
87  te::da::FromItem* fromItemPol = new te::da::DataSetName(m_inDsetName);
88  te::da::From* fromPol = new te::da::From;
89  fromPol->push_back(fromItemPol);
90 
91  te::da::Where* w_oid = 0;
92 
93  if(m_oidSet)
94  w_oid = new te::da::Where(m_oidSet->getExpression());
95 
96  te::da::Select select_Pol(pol_fields, fromPol, w_oid);
97  te::da::SubSelect subSelect_Pol(select_Pol, "pol");
98 
99 // Subselect that apply the ST_DumpRings function in geometric column to get polygon as linestring.
100  te::da::Fields* line_fields = new te::da::Fields;
101  for(std::size_t i = 0; i < props.size(); ++i)
102  {
103  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
104  {
105  te::da::PropertyName* pName = new te::da::PropertyName(props[i]->getName());
106  te::da::Field* field = new te::da::Field(pName);
107  line_fields->push_back(field);
108  }
109  else
110  {
111  te::da::PropertyName* polName = new te::da::PropertyName("polygon");
112  te::da::Expression* e_dumpRings = new te::da::ST_DumpRings(polName);
113  te::da::Expression* e_boundary = new te::da::ST_Boundary(e_dumpRings);
114  te::da::Field* f_boundary = new te::da::Field(*e_boundary, " linestring");
115  line_fields->push_back(f_boundary);
116  }
117  }
118 
119  te::da::FromItem* fromItemLine = new te::da::SubSelect(subSelect_Pol);
120  te::da::From* fromLine = new te::da::From;
121  fromLine->push_back(fromItemLine);
122 
123  te::da::Select select_Line(line_fields, fromLine);
124  te::da::SubSelect subSelect_Line(select_Line, "line");
125 
126 // Collect the lines by register
127  te::da::Fields* union_fields = new te::da::Fields;
128  for(std::size_t i = 0; i < props.size(); ++i)
129  {
130  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
131  {
132  te::da::PropertyName* pName = new te::da::PropertyName(props[i]->getName());
133  te::da::Field* field = new te::da::Field(pName);
134  union_fields->push_back(field);
135  }
136  else
137  {
138  te::da::PropertyName* lineName = new te::da::PropertyName("linestring");
139  te::da::Expression* e_union = new te::da::ST_Collect(lineName);
140  te::da::Field* f_union = new te::da::Field(*e_union, props[i]->getName());
141  union_fields->push_back(f_union);
142  }
143  }
144 
145  te::da::FromItem* fromItem = new te::da::SubSelect(subSelect_Line);
146  te::da::From* from = new te::da::From;
147  from->push_back(fromItem);
148 
149  te::da::Select select(union_fields, from);
150 
151 // Group by
152  te::da::GroupBy* groupBy = new te::da::GroupBy();
153  for(std::size_t i = 0; i < props.size(); ++i)
154  {
155  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
156  {
157  te::da::GroupByItem* e_groupBy = new te::da::GroupByItem(props[i]->getName());
158  groupBy->push_back(e_groupBy);
159  }
160  }
161  select.setGroupBy(groupBy);
162 
163  std::auto_ptr<te::da::DataSet> dsQuery(m_inDsrc->query(select));
164 
165  te::vp::Save(m_outDsrc.get(), dsQuery.get(), outDsType.get());
166  return true;
167 
168 }
ST_DumpRings statistical function.
Definition: ST_DumpRings.h:46
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
ST_Boundary statistical function.
Definition: ST_Boundary.h:46
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
Polygon To Line Vector Processing functions.
This is an abstract class that models a query expression.
Definition: Expression.h:47
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
A Select can be used as a source of information in another query.
Definition: SubSelect.h:49