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 #include "../geometry/GeometryProperty.h"
51 
52 #include "PolygonToLineQuery.h"
53 #include "Utils.h"
54 
55 // STL
56 #include <memory>
57 #include <string>
58 #include <vector>
59 
61 
63 
65 {
66 // Set output DataSetType
67  std::unique_ptr<te::da::DataSetType> outDsType = buildOutDataSetType();
68  std::vector<te::dt::Property*> props = outDsType->getProperties();
69 
70 // Get DataSetType from DataSource to compare geometry SRID with DataSetType of Layer.
71  std::unique_ptr<te::da::DataSetType> sourceDSetType(m_inDsrc->getDataSetType(m_inDsetName));
72  te::gm::GeometryProperty* geomPropSource = te::da::GetFirstGeomProperty(sourceDSetType.get());
73 
75 
76 // Subselect that apply the ST_Dump function in geometric column to separate multi polygons.
77  te::da::Fields* pol_fields = new te::da::Fields;
78  for(std::size_t i = 0; i < props.size(); ++i)
79  {
80  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
81  {
82  te::da::PropertyName* pName = new te::da::PropertyName(props[i]->getName());
83  te::da::Field* field = new te::da::Field(pName);
84  pol_fields->push_back(field);
85  }
86  else
87  {
88  te::da::Expression* e_geometry;
89 
90  if (geomPropSource->getSRID() != geomProp->getSRID())
91  {
92  te::da::LiteralInt32* srid = new te::da::LiteralInt32(geomProp->getSRID());
93  e_geometry = new te::da::ST_SetSRID(new te::da::PropertyName(props[i]->getName()), srid);
94  }
95  else
96  {
97  e_geometry = new te::da::PropertyName(props[i]->getName());
98  }
99 
100  te::da::Expression* e_dump = new te::da::ST_Dump(e_geometry);
101  te::da::Field* f_dump = new te::da::Field(*e_dump, " polygon");
102  pol_fields->push_back(f_dump);
103 
104  }
105  }
106 
108  te::da::From* fromPol = new te::da::From;
109  fromPol->push_back(fromItemPol);
110 
111  te::da::Where* w_oid = nullptr;
112 
113  if(m_oidSet)
114  w_oid = new te::da::Where(m_oidSet->getExpression());
115 
116  te::da::Select select_Pol(pol_fields, fromPol, w_oid);
117  te::da::SubSelect subSelect_Pol(select_Pol, "pol");
118 
119 // Subselect that apply the ST_DumpRings function in geometric column to get polygon as linestring.
120  te::da::Fields* line_fields = new te::da::Fields;
121  for(std::size_t i = 0; i < props.size(); ++i)
122  {
123  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
124  {
125  te::da::PropertyName* pName = new te::da::PropertyName(props[i]->getName());
126  te::da::Field* field = new te::da::Field(pName);
127  line_fields->push_back(field);
128  }
129  else
130  {
131  te::da::PropertyName* polName = new te::da::PropertyName("polygon");
132  te::da::Expression* e_dumpRings = new te::da::ST_DumpRings(polName);
133  te::da::Expression* e_boundary = new te::da::ST_Boundary(e_dumpRings);
134  te::da::Field* f_boundary = new te::da::Field(*e_boundary, " linestring");
135  line_fields->push_back(f_boundary);
136  }
137  }
138 
139  te::da::FromItem* fromItemLine = new te::da::SubSelect(subSelect_Pol);
140  te::da::From* fromLine = new te::da::From;
141  fromLine->push_back(fromItemLine);
142 
143  te::da::Select select_Line(line_fields, fromLine);
144  te::da::SubSelect subSelect_Line(select_Line, "line");
145 
146 // Collect the lines by register
147  te::da::Fields* union_fields = new te::da::Fields;
148  for(std::size_t i = 0; i < props.size(); ++i)
149  {
150  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
151  {
152  te::da::PropertyName* pName = new te::da::PropertyName(props[i]->getName());
153  te::da::Field* field = new te::da::Field(pName);
154  union_fields->push_back(field);
155  }
156  else
157  {
158  te::da::PropertyName* lineName = new te::da::PropertyName("linestring");
159  te::da::Expression* e_union = new te::da::ST_Collect(lineName);
160  te::da::Field* f_union = new te::da::Field(*e_union, props[i]->getName());
161  union_fields->push_back(f_union);
162  }
163  }
164 
165  te::da::FromItem* fromItem = new te::da::SubSelect(subSelect_Line);
166  te::da::From* from = new te::da::From;
167  from->push_back(fromItem);
168 
169  te::da::Select select(union_fields, from);
170 
171 // Group by
172  te::da::GroupBy* groupBy = new te::da::GroupBy();
173  for(std::size_t i = 0; i < props.size(); ++i)
174  {
175  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
176  {
177  te::da::GroupByItem* e_groupBy = new te::da::GroupByItem(props[i]->getName());
178  groupBy->push_back(e_groupBy);
179  }
180  }
181  select.setGroupBy(groupBy);
182 
183  std::unique_ptr<te::da::DataSet> dsQuery(m_inDsrc->query(select));
184 
185  te::vp::Save(m_outDsrc.get(), dsQuery.get(), outDsType.get());
186  return true;
187 
188 }
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.
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...
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.
Base exception class for plugin module.
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.
TEVPEXPORT void Save(te::da::DataSource *source, te::da::DataSet *result, te::da::DataSetType *outDsType, const bool &enableProgress=true)
URI C++ Library.
Definition: Attributes.h:37
std::unique_ptr< te::da::DataSetTypeConverter > m_converter
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
Utility functions for the data access module.
te::da::DataSourcePtr m_inDsrc
void setGroupBy(GroupBy *g)
It sets the list of expressions used to condense the result set.
Definition: Select.cpp:809
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
te::da::DataSourcePtr m_outDsrc
const te::da::ObjectIdSet * m_oidSet
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)
Expression * getExpression() const
It returns the expression that can be used to retrieve the data set that contains the all indentified...
std::unique_ptr< te::da::DataSetType > buildOutDataSetType()