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 #include "../dataaccess/dataset/DataSet.h"
28 #include "../dataaccess/dataset/DataSetAdapter.h"
29 #include "../dataaccess/query/DataSetName.h"
30 #include "../dataaccess/query/Expression.h"
31 #include "../dataaccess/query/Field.h"
32 #include "../dataaccess/query/Fields.h"
33 #include "../dataaccess/query/From.h"
34 #include "../dataaccess/query/FromItem.h"
35 #include "../dataaccess/query/GroupBy.h"
36 #include "../dataaccess/query/GroupByItem.h"
37 #include "../dataaccess/query/LiteralInt32.h"
38 #include "../dataaccess/query/PropertyName.h"
39 #include "../dataaccess/query/Select.h"
40 #include "../dataaccess/query/ST_Boundary.h"
41 #include "../dataaccess/query/ST_Collect.h"
42 #include "../dataaccess/query/ST_Dump.h"
43 #include "../dataaccess/query/ST_DumpRings.h"
44 #include "../dataaccess/query/ST_SetSRID.h"
45 #include "../dataaccess/query/SubSelect.h"
46 #include "../dataaccess/query/Where.h"
47 #include "../dataaccess/utils/Utils.h"
48 
49 #include "../datatype/Property.h"
50 
51 #include "../geometry/GeometryProperty.h"
52 
53 #include "PolygonToLineQuery.h"
54 #include "Utils.h"
55 
56 // STL
57 #include <memory>
58 #include <string>
59 #include <vector>
60 
62 {}
63 
65 {}
66 
67 bool te::vp::PolygonToLineQuery::run() throw(te::common::Exception)
68 {
69 // Set output DataSetType
70  std::auto_ptr<te::da::DataSetType> outDsType = buildOutDataSetType();
71  std::vector<te::dt::Property*> props = outDsType->getProperties();
72 
73 // Get DataSetType from DataSource to compare geometry SRID with DataSetType of Layer.
74  std::auto_ptr<te::da::DataSetType> sourceDSetType(m_inDsrc->getDataSetType(m_inDsetName));
75  te::gm::GeometryProperty* geomPropSource = te::da::GetFirstGeomProperty(sourceDSetType.get());
76 
77  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(m_converter->getResult());
78 
79 // Subselect that apply the ST_Dump function in geometric column to separate multi polygons.
80  te::da::Fields* pol_fields = new te::da::Fields;
81  for(std::size_t i = 0; i < props.size(); ++i)
82  {
83  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
84  {
85  te::da::PropertyName* pName = new te::da::PropertyName(props[i]->getName());
86  te::da::Field* field = new te::da::Field(pName);
87  pol_fields->push_back(field);
88  }
89  else
90  {
91  te::da::Expression* e_geometry;
92 
93  if (geomPropSource->getSRID() != geomProp->getSRID())
94  {
95  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomProp->getSRID());
96  e_geometry = new te::da::ST_SetSRID(new te::da::PropertyName(props[i]->getName()), srid);
97  }
98  else
99  {
100  e_geometry = new te::da::PropertyName(props[i]->getName());
101  }
102 
103  te::da::Expression* e_dump = new te::da::ST_Dump(e_geometry);
104  te::da::Field* f_dump = new te::da::Field(*e_dump, " polygon");
105  pol_fields->push_back(f_dump);
106 
107  }
108  }
109 
110  te::da::FromItem* fromItemPol = new te::da::DataSetName(m_inDsetName);
111  te::da::From* fromPol = new te::da::From;
112  fromPol->push_back(fromItemPol);
113 
114  te::da::Where* w_oid = 0;
115 
116  if(m_oidSet)
117  w_oid = new te::da::Where(m_oidSet->getExpression());
118 
119  te::da::Select select_Pol(pol_fields, fromPol, w_oid);
120  te::da::SubSelect subSelect_Pol(select_Pol, "pol");
121 
122 // Subselect that apply the ST_DumpRings function in geometric column to get polygon as linestring.
123  te::da::Fields* line_fields = new te::da::Fields;
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::PropertyName* pName = new te::da::PropertyName(props[i]->getName());
129  te::da::Field* field = new te::da::Field(pName);
130  line_fields->push_back(field);
131  }
132  else
133  {
134  te::da::PropertyName* polName = new te::da::PropertyName("polygon");
135  te::da::Expression* e_dumpRings = new te::da::ST_DumpRings(polName);
136  te::da::Expression* e_boundary = new te::da::ST_Boundary(e_dumpRings);
137  te::da::Field* f_boundary = new te::da::Field(*e_boundary, " linestring");
138  line_fields->push_back(f_boundary);
139  }
140  }
141 
142  te::da::FromItem* fromItemLine = new te::da::SubSelect(subSelect_Pol);
143  te::da::From* fromLine = new te::da::From;
144  fromLine->push_back(fromItemLine);
145 
146  te::da::Select select_Line(line_fields, fromLine);
147  te::da::SubSelect subSelect_Line(select_Line, "line");
148 
149 // Collect the lines by register
150  te::da::Fields* union_fields = new te::da::Fields;
151  for(std::size_t i = 0; i < props.size(); ++i)
152  {
153  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
154  {
155  te::da::PropertyName* pName = new te::da::PropertyName(props[i]->getName());
156  te::da::Field* field = new te::da::Field(pName);
157  union_fields->push_back(field);
158  }
159  else
160  {
161  te::da::PropertyName* lineName = new te::da::PropertyName("linestring");
162  te::da::Expression* e_union = new te::da::ST_Collect(lineName);
163  te::da::Field* f_union = new te::da::Field(*e_union, props[i]->getName());
164  union_fields->push_back(f_union);
165  }
166  }
167 
168  te::da::FromItem* fromItem = new te::da::SubSelect(subSelect_Line);
169  te::da::From* from = new te::da::From;
170  from->push_back(fromItem);
171 
172  te::da::Select select(union_fields, from);
173 
174 // Group by
175  te::da::GroupBy* groupBy = new te::da::GroupBy();
176  for(std::size_t i = 0; i < props.size(); ++i)
177  {
178  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
179  {
180  te::da::GroupByItem* e_groupBy = new te::da::GroupByItem(props[i]->getName());
181  groupBy->push_back(e_groupBy);
182  }
183  }
184  select.setGroupBy(groupBy);
185 
186  std::auto_ptr<te::da::DataSet> dsQuery(m_inDsrc->query(select));
187 
188  te::vp::Save(m_outDsrc.get(), dsQuery.get(), outDsType.get());
189  return true;
190 
191 }
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
Geometric 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
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:172
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
URI C++ Library.
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
Spatial Set SRID operator.
Definition: ST_SetSRID.h:46
A Select can be used as a source of information in another query.
Definition: SubSelect.h:49
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557