All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ObjectIdSet.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 terralib/dataaccess/dataset/ObjectIdSet.cpp
22 
23  \brief This class represents a set of unique ids created in the same context. i.e. from the same data set.
24 */
25 
26 // TerraLib
27 #include "../../common/STLUtils.h"
28 #include "../../common/Translator.h"
29 #include "../../datatype/Enums.h"
30 #include "../../geometry/Envelope.h"
31 #include "../query/And.h"
32 #include "../query/Expression.h"
33 #include "../query/Field.h"
34 #include "../query/In.h"
35 #include "../query/Literal.h"
36 #include "../query/LiteralEnvelope.h"
37 #include "../query/LiteralString.h"
38 #include "../query/ST_Intersects.h"
39 #include "../Exception.h"
40 #include "ObjectId.h"
41 #include "ObjectIdSet.h"
42 
43 // STL
44 #include <cassert>
45 
46 te::da::ObjectIdSet::ObjectIdSet() : m_expression(0), m_expByClauseIn(false)
47 {
48 }
49 
51  : m_pnames(rhs.m_pnames),
52  m_ppos(rhs.m_ppos),
53  m_ptypes(rhs.m_ptypes),
54  m_expression(0),
55  m_expByClauseIn(rhs.m_expByClauseIn)
56 {
57  if(copyOids)
58  {
59  if(rhs.getExpression())
60  m_expression = rhs.getExpression()->clone();
61 
62  std::set<ObjectId*, te::common::LessCmp<ObjectId*> >::const_iterator it;
63  for(it = rhs.m_oids.begin(); it != rhs.m_oids.end(); ++it)
64  m_oids.insert((*it)->clone());
65  }
66 }
67 
69 {
71 
72  delete m_expression;
73 }
74 
75 void te::da::ObjectIdSet::addProperty(const std::string& name, std::size_t pos, int type)
76 {
77  assert(!name.empty());
78  m_pnames.push_back(name);
79  m_ppos.push_back(pos);
80  m_ptypes.push_back(type);
81 }
82 
84 {
85  assert(oid);
86  m_oids.insert(oid);
87 }
88 
89 void te::da::ObjectIdSet::setExpression(te::da::Expression* expression, bool isClauseIn)
90 {
91  delete m_expression;
92 
93  m_expression = expression;
94 
95  m_expByClauseIn = isClauseIn;
96 }
97 
98 void te::da::ObjectIdSet::setExpressionByIntersection(std::string geomAttrName, te::gm::Envelope env, int srid)
99 {
100  te::da::Field* field = new te::da::Field(geomAttrName);
101  te::da::LiteralEnvelope* lenv = new te::da::LiteralEnvelope(env, srid);
102  te::da::ST_Intersects* intersects = new te::da::ST_Intersects(field->getExpression(), lenv);
103 
104  setExpression(intersects, false);
105 }
106 
107 void te::da::ObjectIdSet::setExpressionByInClause(const std::string source)
108 {
109  assert(m_pnames.size() == m_ptypes.size());
110 
111  Expression* ins = 0;
112  Expression* tmp = 0;
113 
114  // for each property used to be part of the object identification builds a IN clause
115  for(std::size_t i = 0; i < m_pnames.size(); ++i)
116  {
117  In* in = 0;
118  if(source.empty())
119  in = new In(m_pnames[i]);
120  else
121  in = new In(source + "." + m_pnames[i]);
122 
123  // for each object in the set include its property value in the IN clause
124  std::set<ObjectId*, te::common::LessCmp<ObjectId*> >::const_iterator it;
125  for(it = m_oids.begin(); it != m_oids.end(); ++it)
126  {
127  const boost::ptr_vector<te::dt::AbstractData>& data = (*it)->getValue();
128 
129  if(i >= data.size())
130  continue;
131 
132  if(m_ptypes[i] == te::dt::STRING_TYPE)
133  in->add(new LiteralString(data[i].toString()));
134  else
135  in->add(new Literal(data[i]));
136  }
137 
138  if(in->getNumArgs() > 0)
139  {
140  if(i > 0)
141  {
142  tmp = *ins || *in;
143  delete ins;
144  delete in;
145  ins = tmp;
146  }
147  else
148  ins = in;
149  }
150  }
151 
152  setExpression(ins, true);
153 }
154 
156 {
157  assert(m_pnames.size() == m_ptypes.size());
158 
159  Expression* ins = 0;
160  Expression* tmp = 0;
161 
162  // for each property used to be part of the object identification builds a IN clause
163  for(std::size_t i = 0; i < m_pnames.size(); ++i)
164  {
165  In* in = 0;
166  if(source.empty())
167  in = new In(m_pnames[i]);
168  else
169  in = new In(source + "." + m_pnames[i]);
170 
171  // for each object in the set include its property value in the IN clause
172  std::set<ObjectId*, te::common::LessCmp<ObjectId*> >::const_iterator it;
173  for(it = m_oids.begin(); it != m_oids.end(); ++it)
174  {
175  const boost::ptr_vector<te::dt::AbstractData>& data = (*it)->getValue();
176 
177  if(i >= data.size())
178  continue;
179 
180  if(m_ptypes[i] == te::dt::STRING_TYPE)
181  in->add(new LiteralString(data[i].toString()));
182  else
183  in->add(new Literal(data[i]));
184  }
185 
186  if(in->getNumArgs() > 0)
187  {
188  if(i > 0)
189  {
190  tmp = *ins || *in;
191  delete ins;
192  delete in;
193  ins = tmp;
194  }
195  else
196  ins = in;
197  }
198  }
199 
200  return ins;
201 }
202 
204 {
205  if(m_expression)
206  return m_expression->clone();
207  else
208  return getExpressionByInClause();
209 }
210 
212 {
213  te::common::FreeContents(m_oids);
214  m_oids.clear();
215 }
216 
217 std::size_t te::da::ObjectIdSet::size() const
218 {
219  return m_oids.size();
220 }
221 
222 const std::vector<std::string>& te::da::ObjectIdSet::getPropertyNames() const
223 {
224  return m_pnames;
225 }
226 
227 const std::vector<std::size_t>& te::da::ObjectIdSet::getPropertyPos() const
228 {
229  return m_ppos;
230 }
231 
232 const std::vector<int>& te::da::ObjectIdSet::getPropertyTypes() const
233 {
234  return m_ptypes;
235 }
236 
238 {
239  assert(oid);
240  return m_oids.find(oid) != m_oids.end();
241 }
242 
244 {
245  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::iterator it = m_oids.find(oid);
246 
247  if(it != m_oids.end())
248  m_oids.erase(it);
249 }
250 
252 {
253  assert(rhs);
254 
255  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >& newOids = rhs->m_oids;
256 
257  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::iterator it;
258  for(it = newOids.begin(); it != newOids.end(); ++it)
259  m_oids.find(*it) == m_oids.end() ? add(*it) : delete *it;
260 
261  newOids.clear();
262 
263  setExpressionByInClause();
264 
265  delete rhs;
266  rhs = 0;
267 }
268 
270 {
271  assert(rhs);
272 
273  if(m_oids.empty())
274  return;
275 
276  const std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >& oidsToRemove = rhs->m_oids;
277 
278  if(oidsToRemove.empty())
279  return;
280 
281  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator it;
282  for(it = oidsToRemove.begin(); it != oidsToRemove.end(); ++it)
283  {
284  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::iterator itSearch = m_oids.find(*it);
285 
286  if(itSearch == m_oids.end())
287  continue;
288 
289  delete *itSearch;
290  m_oids.erase(itSearch);
291  }
292 
293  setExpressionByInClause();
294 }
295 
297 {
298  assert(rhs);
299 
300  const std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >& oidsToInsert = rhs->m_oids;
301 
302  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator it;
303  for(it = oidsToInsert.begin(); it != oidsToInsert.end(); ++it)
304  {
305  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::iterator itSearch = m_oids.find(*it);
306 
307  if(itSearch != m_oids.end())
308  {
309  delete *itSearch;
310  m_oids.erase(itSearch);
311  }
312  else
313  m_oids.insert((*it)->clone());
314  }
315 
316  setExpressionByInClause();
317 }
318 
319 std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator te::da::ObjectIdSet::begin() const
320 {
321  return m_oids.begin();
322 }
323 
324 std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator te::da::ObjectIdSet::end() const
325 {
326  return m_oids.end();
327 }
328 
330 {
331  return new ObjectIdSet(*this);
332 }
333 
335 {
336  if(!m_expression)
337  getExpressionByInClause();
338 
339  return m_expByClauseIn;
340 }
const std::vector< int > & getPropertyTypes() const
It returns the property types used to generated the oids.
void add(Expression *arg)
It adds the argument to the function list of arguments.
Definition: Function.cpp:79
bool isExpressionClauseIn() const
The Field class can be used to model an expression that takes part of the output items of a SELECT...
Definition: Field.h:50
Spatial intersects operator.
Definition: ST_Intersects.h:46
te::da::Expression * m_expression
The expression that can be used to retrieve the data set that contains the all indentified elements...
Definition: ObjectIdSet.h:220
Expression * getExpression() const
It returns the expression set for an output select query.
Definition: Field.cpp:80
This class represents an unique id for a data set element.
void addProperty(const std::string &name, std::size_t pos, int type)
It adds a property that will be used to generate the unique ids.
Definition: ObjectIdSet.cpp:75
void setExpression(te::da::Expression *expression, bool isClauseIn)
It set the expression that can be used to retrieve the data set that contains the all indentified ele...
Definition: ObjectIdSet.cpp:89
This is an abstract class that models a query expression.
Definition: Expression.h:47
This class represents a set of unique ids created in the same context. i.e. from the same data set...
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
This class models a literal value.
Definition: Literal.h:53
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
This class represents an unique id for a data set element.
Definition: ObjectId.h:47
void setExpressionByInClause(const std::string source="")
std::size_t size() const
It returns the object id set size.
ObjectIdSet()
Constructor.
Definition: ObjectIdSet.cpp:46
std::set< ObjectId *, te::common::LessCmp< ObjectId * > > m_oids
The set of unique ids.
Definition: ObjectIdSet.h:219
void setExpressionByIntersection(std::string geomAttrName, te::gm::Envelope env, int srid)
Definition: ObjectIdSet.cpp:98
void symDifference(const ObjectIdSet *rhs)
It performs the symmetric difference operation between this ObjectIdSet and the given ObjectIdSet...
void remove(ObjectId *oid)
Removes the object id from set.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator end() const
Returns an iterator for the object ids in container.
void Union(ObjectIdSet *rhs)
It performs the union operation between this ObjectIdSet and the given ObjectIdSet.
void clear()
It clears this object id set.
bool contains(ObjectId *oid) const
It returns if the object id set contains the given oid.
A class that models a literal for Envelope values.
const std::vector< std::string > & getPropertyNames() const
It returns the property names used to generated the oids.
Expression * getExpressionByInClause(const std::string source="") const
void add(ObjectId *oid)
It adds an object id to this object id set.
Definition: ObjectIdSet.cpp:83
A class that represents the IN operator.
Definition: In.h:52
const std::vector< std::size_t > & getPropertyPos() const
It returns the property positions used to generated the oids.
virtual Expression * clone() const =0
It creates a new copy of this expression.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator begin() const
Returns an iterator for the object ids in container.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
ObjectIdSet * clone() const
Expression * getExpression() const
It returns the expression that can be used to retrieve the data set that contains the all indentified...
std::size_t getNumArgs() const
It returns the number of arguments informed to the function.
Definition: Function.cpp:62
void difference(const ObjectIdSet *rhs)
It performs the difference operation between this ObjectIdSet and the given ObjectIdSet.
~ObjectIdSet()
Destructor.
Definition: ObjectIdSet.cpp:68
This class models a string Literal value.
Definition: LiteralString.h:46