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/LiteralInt32.h"
40 #include "../dataaccess/query/PropertyName.h"
41 #include "../dataaccess/query/Select.h"
42 #include "../dataaccess/query/ST_Dump.h"
43 #include "../dataaccess/query/ST_Collect.h"
44 #include "../dataaccess/query/ST_MakePolygon.h"
45 #include "../dataaccess/query/ST_SetSRID.h"
46 #include "../dataaccess/query/SubSelect.h"
47 #include "../dataaccess/query/Where.h"
48 #include "../dataaccess/utils/Utils.h"
49 
50 #include "../geometry/GeometryProperty.h"
51 
52 #include "LineToPolygonQuery.h"
53 #include "Utils.h"
54 
55 // STL
56 #include <memory>
57 #include <vector>
58 
60 {}
61 
63 {}
64 
65 bool te::vp::LineToPolygonQuery::run() throw (te::common::Exception)
66 {
67  std::auto_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::auto_ptr<te::da::DataSetType> sourceDSetType(m_inDsrc->getDataSetType(m_inDsetName));
72  te::gm::GeometryProperty* geomPropSource = te::da::GetFirstGeomProperty(sourceDSetType.get());
73 
74  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(m_converter->getResult());
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::Expression* e_makePolygon = new te::da::ST_MakePolygon(e_dump);
102  te::da::Field* f_makePolygon = new te::da::Field(*e_makePolygon, " polygon");
103  pol_fields->push_back(f_makePolygon);
104  }
105  }
106 
107  te::da::FromItem* fromItemPol = new te::da::DataSetName(m_inDsetName);
108  te::da::From* fromPol = new te::da::From;
109  fromPol->push_back(fromItemPol);
110 
111  te::da::Where* w_oid = 0;
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* gName = new te::da::PropertyName("polygon");
132  te::da::Expression* e_collect = new te::da::ST_Collect(gName);
133  te::da::Field* f_collect = new te::da::Field(*e_collect, props[i]->getName());
134  line_fields->push_back(f_collect);
135  }
136  }
137 
138  te::da::FromItem* fromItem = new te::da::SubSelect(subSelect_Pol);
139  te::da::From* from = new te::da::From;
140  from->push_back(fromItem);
141 
142  te::da::Select select(line_fields, from);
143 
144 // Group by
145  te::da::GroupBy* groupBy = new te::da::GroupBy();
146  for(std::size_t i = 0; i < props.size(); ++i)
147  {
148  if(props[i]->getType() != te::dt::GEOMETRY_TYPE)
149  {
150  te::da::GroupByItem* e_groupBy = new te::da::GroupByItem(props[i]->getName());
151  groupBy->push_back(e_groupBy);
152  }
153  }
154  select.setGroupBy(groupBy);
155 
156  std::auto_ptr<te::da::DataSet> dsQuery(m_inDsrc->query(select));
157 
158  te::vp::Save(m_outDsrc.get(), dsQuery.get(), outDsType.get());
159  return true;
160 
161 }
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
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
This is an abstract class that models a query expression.
Definition: Expression.h:47
ST_MakePolygon statistical function.
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
Line To Polygon Vector Processing functions.
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