All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
BoxIterator.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 BoxIterator.cpp
22 
23  \brief
24 */
25 
26 // Terralib Includes
27 #include "../../common/Translator.h"
28 #include "../../common/StringUtils.h"
29 #include "../../dataaccess/dataset/DataSet.h"
30 #include "../../dataaccess/datasource/DataSource.h"
31 #include "../../dataaccess/query_h.h"
32 #include "../../datatype/Property.h"
33 #include "../../geometry/Envelope.h"
34 #include "../core/AbstractGraph.h"
35 #include "../core/GraphMetadata.h"
36 #include "../Config.h"
37 #include "../Exception.h"
38 #include "../Globals.h"
39 #include "BoxIterator.h"
40 
42  te::graph::AbstractIterator(g),
43  m_extent(0)
44 {
45 }
46 
48  te::graph::AbstractIterator(g),
49  m_extent(e)
50 {
51 }
52 
53 
55 {
56  delete m_extent;
57 }
58 
60 {
61  if(m_graph == 0 || m_graph->getMetadata() == 0)
62  {
63  throw Exception(TE_TR("Invalid graph pointer."));
64  }
65 
66  if(m_vertexQuery.get())
67  {
68  if(m_vertexQuery->moveFirst())
69  {
70  int id = m_vertexQuery->getInt32(Globals::sm_tableVertexModelAttrId);
71 
72  return m_graph->getVertex(id);
73  }
74  }
75 
76  //get the vertex geometry name
77  std::string geometryAttrName = "";
78 
79  for(int i = 0; i < m_graph->getMetadata()->getVertexPropertySize(); ++i)
80  {
81  if(m_graph->getMetadata()->getVertexProperty(i)->getType() == te::dt::GEOMETRY_TYPE)
82  {
83  geometryAttrName = m_graph->getMetadata()->getVertexProperty(i)->getName();
84 
85  break;
86  }
87  }
88 
89  //create a query to get eache vertex id
90  std::string tableName = m_graph->getMetadata()->getVertexTableName();
91 
93  te::da::Fields* fields = new te::da::Fields;
94  fields->push_back(f);
95 
96  te::da::FromItem* t = new te::da::DataSetName(tableName);
97  te::da::From* from = new te::da::From;
98  from->push_back(t);
99 
100  int srid = m_graph->getMetadata()->getSRID();
101  te::da::LiteralEnvelope* lEnv = new te::da::LiteralEnvelope(*m_extent, srid);
102 
103  std::string vAttr = tableName + "." + geometryAttrName;
104 
105  te::da::Field* fvAttr = new te::da::Field(vAttr);
106 
107  te::da::ST_Intersects* intersects = new te::da::ST_Intersects(fvAttr->getExpression(), lEnv);
108 
109  te::da::Where* wh = new te::da::Where(intersects);
110 
112  te::da::OrderBy* ob = new te::da::OrderBy();
113  ob->push_back(obItem);
114 
115  te::da::Select select(fields, from, wh, ob);
116 
117  m_vertexQuery.reset(0);
118  m_vertexQuery = m_graph->getMetadata()->getDataSource()->query(select);
119 
120  if(m_vertexQuery.get() == 0)
121  {
122  throw Exception(TE_TR("Iterator not initialized."));
123  }
124 
125  int id;
126 
127  if(m_vertexQuery->moveNext())
128  {
129  id = m_vertexQuery->getInt32(Globals::sm_tableVertexModelAttrId);
130  }
131  else
132  {
133  return 0;
134  }
135 
136  return m_graph->getVertex(id);
137 }
138 
140 {
141  if(m_graph == 0 || m_graph->getMetadata() == 0)
142  {
143  throw Exception(TE_TR("Invalid graph pointer."));
144  }
145 
146  if(m_edgeQuery.get())
147  {
148  if(m_edgeQuery->moveFirst())
149  {
150  int id = m_edgeQuery->getInt32(Globals::sm_tableEdgeModelAttrId);
151 
152  return m_graph->getEdge(id);
153  }
154  }
155 
156  //get the vertex geometry name
157  std::string geometryAttrName = "";
158 
159  for(int i = 0; i < m_graph->getMetadata()->getVertexPropertySize(); ++i)
160  {
161  if(m_graph->getMetadata()->getVertexProperty(i)->getType() == te::dt::GEOMETRY_TYPE)
162  {
163  geometryAttrName = m_graph->getMetadata()->getVertexProperty(i)->getName();
164 
165  break;
166  }
167  }
168 
169  //create a query to get eache edge id
170  std::string edgeTable = m_graph->getMetadata()->getEdgeTableName();
171  std::string vertexAttrTalbe = m_graph->getMetadata()->getVertexTableName();
172 
174  te::da::Fields* fields = new te::da::Fields;
175  fields->push_back(f);
176 
177  te::da::From* from = new te::da::From;
178  te::da::FromItem* fi1 = new te::da::DataSetName(edgeTable, "edges");
179  from->push_back(fi1);
180  te::da::FromItem* fi2 = new te::da::DataSetName(vertexAttrTalbe, "v1");
181  from->push_back(fi2);
182  te::da::FromItem* fi3 = new te::da::DataSetName(vertexAttrTalbe, "v2");
183  from->push_back(fi3);
184 
185  std::string vertexFrom = "edges.";
187 
188  std::string vertexTo = "edges.";
190 
191  std::string v1Id = "v1.";
193 
194  std::string v2Id = "v2.";
196 
197  //where
198  te::da::Field* fvf = new te::da::Field(vertexFrom);
199  te::da::Field* fv1id = new te::da::Field(v1Id);
200  te::da::Expression* exp1 = new te::da::EqualTo(fvf->getExpression(), fv1id->getExpression());
201 
202  te::da::Field* fvt = new te::da::Field(vertexTo);
203  te::da::Field* fv2id = new te::da::Field(v2Id);
204  te::da::Expression* exp2 = new te::da::EqualTo(fvt->getExpression(), fv2id->getExpression());
205 
206  te::da::And* anda = new te::da::And(exp1, exp2);
207 
208  std::string v1Attr = "v1." + geometryAttrName;
209  std::string v2Attr = "v2." + geometryAttrName;
210 
211  int srid = m_graph->getMetadata()->getSRID();
212  te::da::LiteralEnvelope* lenv1 = new te::da::LiteralEnvelope(*m_extent, srid);
213  te::da::LiteralEnvelope* lenv2 = new te::da::LiteralEnvelope(*m_extent, srid);
214  te::da::Field* fv1attr = new te::da::Field(v1Attr);
215  te::da::Field* fv2attr = new te::da::Field(v2Attr);
216  te::da::ST_Intersects* intersects1 = new te::da::ST_Intersects(fv1attr->getExpression(), lenv1);
217  te::da::ST_Intersects* intersects2 = new te::da::ST_Intersects(fv2attr->getExpression(), lenv2);
218  te::da::Or* ora = new te::da::Or(intersects1, intersects2);
219 
220  te::da::And* andd = new te::da::And(anda, ora);
221 
222  te::da::Where* wh = new te::da::Where(andd);
223 
225  te::da::OrderBy* ob = new te::da::OrderBy();
226  ob->push_back(obItem);
227 
228  //select
229  te::da::Select select(fields, from, wh, ob);
230 
231  m_edgeQuery.reset(0);
232  m_edgeQuery = m_graph->getMetadata()->getDataSource()->query(select);
233 
234  if(m_edgeQuery.get() == 0)
235  {
236  throw Exception(TE_TR("Iterator not initialized."));
237  }
238 
239  int id;
240 
241  if(m_edgeQuery->moveNext())
242  {
243  id = m_edgeQuery->getInt32(Globals::sm_tableEdgeModelAttrId);
244  }
245  else
246  {
247  return 0;
248  }
249 
250  return m_graph->getEdge(id);
251 }
252 
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
static const std::string sm_tableVertexModelAttrId
Attribute id.
Definition: Globals.h:97
virtual ~BoxIterator()
Virtual destructor.
Definition: BoxIterator.cpp:54
Spatial intersects operator.
Definition: ST_Intersects.h:46
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
Expression * getExpression() const
It returns the expression set for an output select query.
Definition: Field.cpp:80
boost::ptr_vector< OrderByItem > OrderBy
A class that can be used to model an ORDER BY clause.
Definition: OrderBy.h:37
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
This class defines a commun interface to represents a graph iterator class. The main diferency to ano...
virtual te::graph::Vertex * getFirstVertex()
It returns a pointer to the first vertex element of a graph.
Definition: BoxIterator.cpp:59
Boolean logic operator: AND.
Definition: And.h:46
This is an abstract class that models a query expression.
Definition: Expression.h:47
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
static const std::string sm_tableEdgeModelAttrId
Attribute Id.
Definition: Globals.h:91
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
Definition: Or.h:46
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
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
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
virtual te::graph::Edge * getFirstEdge()
It returns a pointer to the first edge element of a graph.
BoxIterator(te::graph::AbstractGraph *g)
Default constructor.
Definition: BoxIterator.cpp:41
It models the comparison operator.
Definition: EqualTo.h:46
This class defines a commun interface to represents a graph iterator class. The main diferency to ano...
A class that models a literal for Envelope values.
static const std::string sm_tableEdgeModelAttrVFrom
Attribute Vertex From.
Definition: Globals.h:92
A class that can be used in an ORDER BY clause to sort the items of a resulting query.
Definition: OrderByItem.h:53
static const std::string sm_tableEdgeModelAttrVTo
Attribute Vertex To.
Definition: Globals.h:93