All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QueryEncoder.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/maptools/QueryEncoder.cpp
22 
23  \brief A visitor that converts a OGC Filter Expression to TerraLib Expression.
24  */
25 
26 // TerraLib
27 #include "../dataaccess/query_h.h"
28 #include "../fe.h"
29 #include "../gml/Envelope.h"
30 #include "QueryEncoder.h"
31 
32 std::map<std::string, std::string> te::map::QueryEncoder::sm_fnameMap;
33 
35  : m_expression(0)
36 {
37 }
38 
40 {
41 }
42 
44 {
45  te::fe::AbstractOp* ops = f->getOp();
46  if(ops)
47  {
48  ops->accept(*this);
49  return m_expression;
50  }
51  else
52  {
53  te::da::In* in = new te::da::In(""); // TODO: What is the PropertyName?
54  // ids
55  size_t nids = f->getNumberOfOids();
56  for(size_t i = 0; i < nids; ++i)
57  {
58  te::fe::ObjectId* objId = f->getOid(i);
59  in->add(new te::da::LiteralString(objId->getId()));
60  }
61  return in;
62  }
63 
64  return 0;
65 }
66 
68 {
69  // no need
70 }
71 
73 {
74  // no need
75 }
76 
78 {
79  // no need
80 }
81 
83 {
84  // no need
85 }
86 
88 {
89  te::da::Expression* l = 0;
90  te::gml::Envelope* e = visited.getEnvelope();
91  if(e)
93 
94  te::fe::PropertyName* p = visited.getProperty();
95  assert(p);
96  p->accept(*this);
97 
98  te::da::ST_Intersects* f = new te::da::ST_Intersects(l, m_expression);
99 
100  m_expression = f;
101 }
102 
104 {
105  visited.getFirst()->accept(*this);
106  te::da::Expression* e1 = m_expression;
107 
108  visited.getSecond()->accept(*this);
109  te::da::Expression* e2 = m_expression;
110 
111  te::da::Function* f = new te::da::Function(sm_fnameMap[visited.getName()]);
112  f->add(e1);
113  f->add(e2);
114 
115  m_expression = f;
116 }
117 
119 {
120  te::da::Function* f = new te::da::Function(sm_fnameMap[visited.getName()]);
121  size_t n = visited.size();
122  for(size_t i = 0; i < n; ++i)
123  {
124  visited.getOp(i)->accept(*this);
125  te::da::Expression* e = m_expression;
126  f->add(e);
127  }
128  m_expression = f;
129 }
130 
132 {
133  te::da::Expression* l = 0;
134  te::gml::Envelope* e = visited.getEnvelope();
135  if(e)
136  {
137  l = new te::da::LiteralEnvelope(e->getCoordinates(), e->getSRID());
138  }
139  else
140  {
141  te::gm::Geometry* g = visited.getGeometry();
142  if(g)
143  l = new te::da::LiteralGeom(g);
144  }
145 
146  te::fe::PropertyName* p = visited.getProperty();
147  assert(p);
148  p->accept(*this);
149 
150  te::da::BinaryFunction* f = new te::da::BinaryFunction(sm_fnameMap[visited.getName()], m_expression, l);
151 
152  m_expression = f;
153 }
154 
156 {
157  te::fe::PropertyName* p = visited.getProperty();
158  assert(p);
159  p->accept(*this);
160 
161  te::gm::Geometry* g = visited.getGeometry();
162  te::common::Distance* d = visited.getDistance();
163 
164  te::da::ST_DistanceBuffer* dbuffer = new te::da::ST_DistanceBuffer(sm_fnameMap[visited.getName()], m_expression, g, d);
165 
166  m_expression = dbuffer;
167 }
168 
170 {
171  visited.getExpression()->accept(*this);
172  te::da::Expression* v = m_expression;
173 
174  visited.getLowerBoundary()->accept(*this);
175  te::da::Expression* e1 = m_expression;
176 
177  visited.getUpperBoundary()->accept(*this);
178  te::da::Expression* e2 = m_expression;
179 
180  te::da::GreaterThan* gt = new te::da::GreaterThan(v, e1);
181  te::da::LessThan* lt = new te::da::LessThan(v, e2);
182 
183  te::da::And* and_op = new te::da::And(gt, lt);
184 
185  m_expression = and_op;
186 }
187 
189 {
190  te::fe::Literal* l = visited.getLiteral();
191  assert(l);
192  std::string pattern = l->getValue();
193  std::string wildCard = visited.getWildCard();
194  std::string singleChar = visited.getSingleChar();
195  std::string escapeChar = visited.getEscapeChar();
196 
197  te::fe::PropertyName* p = visited.getPropertyName();
198  assert(p);
199  p->accept(*this);
200 
201  te::da::Like* like = new te::da::Like(m_expression, pattern, wildCard, singleChar, escapeChar);
202 
203  m_expression = like;
204 }
205 
207 {
208  te::fe::PropertyName* p = visited.getPropertyName();
209  assert(p);
210  p->accept(*this);
211 
212  te::da::IsNull* f = new te::da::IsNull(m_expression);
213 
214  m_expression = f;
215 }
216 
218 {
219  visited.getOp()->accept(*this);
220 
221  te::da::UnaryOp* op = new te::da::UnaryOp(sm_fnameMap[visited.getName()], m_expression);
222 
223  m_expression = op;
224 }
225 
227 {
228  // no need
229 }
230 
232 {
233  visited.getFirst()->accept(*this);
234  te::da::Expression* e1 = m_expression;
235 
236  visited.getSecond()->accept(*this);
237  te::da::Expression* e2 = m_expression;
238 
239  te::da::BinaryFunction* f = new te::da::BinaryFunction(sm_fnameMap[visited.getName()], e1, e2);
240 
241  m_expression = f;
242 }
243 
245 {
246  // no need
247 }
248 
250 {
251  te::da::Literal* l = new te::da::LiteralString(visited.getValue());
252  m_expression = l;
253 }
254 
256 {
258  m_expression = p;
259 }
260 
262 {
263  // BinaryComparisonOp
269 
270  // BinaryLogicOp
273 
274  // BinarySpatialOp
282 
283  // DistanceBuffer
286 
287  // UnaryLogicOp
289 
290  // BinaryOperator
295 }
It models the inequality operator greater than (>).
Definition: GreaterThan.h:46
const std::string & getName() const
It returns the property name.
static const char * sm_propertyIsLessThan
Definition: Globals.h:54
static const char * sm_touches
Definition: Globals.h:76
static const char * sm_mul
Definition: Globals.h:69
void add(Expression *arg)
It adds the argument to the function list of arguments.
Definition: Function.cpp:79
const char * getName() const
It returns the operator name.
Definition: AbstractOp.h:82
te::gm::Envelope * getCoordinates() const
Definition: Envelope.cpp:47
te::gm::Geometry * getGeometry() const
It returns the geometry.
te::common::Distance * getDistance() const
It returns the distance.
static const char * sm_dWithin
Definition: Globals.h:83
const std::string & getId() const
It returns the feature identification value.
Definition: ObjectId.cpp:49
static const std::string sm_Add
Definition: FunctionNames.h:63
PropertyName * getPropertyName() const
It returns the property name.
static const std::string sm_LessThan
Definition: FunctionNames.h:59
Distance buffer operator.
Spatial intersects operator.
Definition: ST_Intersects.h:46
static const std::string sm_ST_Equals
Definition: FunctionNames.h:84
static const std::string sm_ST_Overlaps
Definition: FunctionNames.h:90
ObjectId * getOid(size_t i) const
It returns a specified object identifier.
Definition: Filter.cpp:75
te::gm::Geometry * getGeometry() const
It returns the geometry.
static const char * sm_propertyIsGreaterThanOrEqualTo
Definition: Globals.h:57
static const char * sm_propertyIsGreaterThan
Definition: Globals.h:55
A class that models the name of any property of an object.
Definition: PropertyName.h:50
static const char * sm_crosses
Definition: Globals.h:79
static const std::string sm_ST_Touches
Definition: FunctionNames.h:93
static const std::string sm_ST_Intersects
Definition: FunctionNames.h:86
static const char * sm_or
Definition: Globals.h:63
A convenient and more compact way of encoding the very common bounding box constraint based on an env...
Definition: BBOXOp.h:71
A spatial operator determines whether its geometric arguments satisfy the stated spatial relationship...
Definition: SpatialOp.h:49
PropertyName * getPropertyName() const
It returns the property name.
const std::string & getWildCard() const
It returns the wild character.
A visitor that converts a OGC Filter Expression to TerraLib Expression.
static const char * sm_intersects
Definition: Globals.h:80
A class for binary spatial operators.
~QueryEncoder()
Virtual destructor.
It models the inequality operator less than (<).
Definition: LessThan.h:46
static const char * sm_disjoint
Definition: Globals.h:75
The PropertyIsBetween element is defined as a compact way of encoding a range check.
static const char * sm_within
Definition: Globals.h:77
A comparison operator is used to form expressions that evaluate the mathematical comparison between t...
Definition: ComparisonOp.h:49
te::da::Expression * getExpression(const te::fe::Filter *f)
It converts the OGC Filter Expression to a TerraLib Expression.
AbstractOp * getOp() const
It returns main filter operation.
Definition: Filter.cpp:56
static const std::string sm_EqualTo
Definition: FunctionNames.h:55
PropertyName * getProperty() const
It returns the property name.
std::size_t size() const
It returns the number of operands.
int getSRID() const
Definition: Envelope.cpp:60
static const char * sm_propertyIsEqualTo
Definition: Globals.h:52
A logical operator that can be used to combine one conditional expressions.
Definition: UnaryLogicOp.h:46
The PropertyIsNull class encodes an operator that checks to see if the value of its content is NULL...
Boolean logic operator: AND.
Definition: And.h:46
This is an abstract class that models a query expression.
Definition: Expression.h:47
te::gml::Envelope * getEnvelope() const
It returns the envelope.
Definition: BBOXOp.cpp:62
Literal * getLiteral() const
It returns the literal value.
QueryEncoder()
Default constructor.
static const char * sm_div
Definition: Globals.h:70
static std::map< std::string, std::string > sm_fnameMap
A map that associates Filter Operator names to Query Functions names.
Definition: QueryEncoder.h:147
const std::string & getSingleChar() const
It returns the single wild character.
Spatial Distance Buffer operator.
static const std::string sm_GreaterThanOrEqualTo
Definition: FunctionNames.h:58
Expression * getUpperBoundary() const
It returns the upper boundary expression.
static const std::string sm_ST_Crosses
Definition: FunctionNames.h:76
A function is a named procedure that performs a distinct computation.
Definition: Function.h:54
It is intended to encode a character string comparison operator with pattern matching.
This class can be used to represent binary operation expressions.
static const std::string sm_Mul
Definition: FunctionNames.h:65
This class models a literal value.
Definition: Literal.h:53
This class is used to encode the name of any property of an object.
Definition: PropertyName.h:54
size_t getNumberOfOids() const
It returns the size of the list of object identifiers.
Definition: Filter.cpp:81
PropertyName * getProperty() const
It returns the property name.
static const char * sm_beyond
Definition: Globals.h:84
This is an abstract class that models a Filter Encoding expression.
Definition: Expression.h:50
Expression * getSecond() const
It returns the second operand.
Expression * getFirst() const
It returns the first operand.
It is intended to encode a character string comparison operator with pattern matching.
Definition: Like.h:43
AbstractOp * getOp(std::size_t i) const
It returns a specified operand.
An object identifier is meant to represent a unique identifier for an object instance within the cont...
Definition: ObjectId.h:58
static const std::string sm_And
Definition: FunctionNames.h:51
static void initialize()
Static method to initialize the QueryEncoder.
static const std::string sm_ST_Beyond
Definition: FunctionNames.h:70
Expression * getSecond() const
It returns the second operand.
A class that models a Function expression.
Definition: Function.h:47
const std::string & getEscapeChar() const
It returns the escape character.
Tells if a value is NULL.
Definition: IsNull.h:46
A filter is any valid predicate expression.
Definition: Filter.h:52
AbstractOp * getOp() const
It returns the operand.
static const std::string sm_Div
Definition: FunctionNames.h:66
Envelope defines an extent using a pair of positions defining opposite corners in arbitrary dimension...
Definition: Envelope.h:52
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
static const std::string sm_LessThanOrEqualTo
Definition: FunctionNames.h:60
static const std::string sm_Sub
Definition: FunctionNames.h:64
static const char * sm_add
Definition: Globals.h:67
An abstract interface for operators.
Definition: AbstractOp.h:47
PropertyName * getProperty() const
It returns the property name.
Definition: BBOXOp.cpp:51
A class for binary comparison operators.
static const std::string sm_ST_Disjoint
Definition: FunctionNames.h:78
static const char * sm_and
Definition: Globals.h:62
te::gml::Envelope * getEnvelope() const
It returns the envelope.
This class can be used to represent literal values.
Definition: Literal.h:56
static const char * sm_sub
Definition: Globals.h:68
A logical operator can be used to combine one or more conditional expressions.
Definition: LogicOp.h:52
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
static const std::string sm_ST_Within
Definition: FunctionNames.h:95
static const char * sm_equals
Definition: Globals.h:74
A class that models a literal for Envelope values.
A base class for unary operators.
Definition: UnaryOp.h:43
void visit(const te::fe::AbstractOp &visited)
A logical operator can be used to combine two or more conditional expressions.
Definition: BinaryLogicOp.h:58
A class that represents the IN operator.
Definition: In.h:52
static const char * sm_overlaps
Definition: Globals.h:78
const std::string & getValue() const
It returns the literal value.
Definition: Literal.cpp:38
static const char * sm_not
Definition: Globals.h:65
Expression * getFirst() const
It returns the first operand.
static const std::string sm_Not
Definition: FunctionNames.h:50
static const std::string sm_Or
Definition: FunctionNames.h:52
const char * getName() const
It returns the operator name.
static const std::string sm_ST_DWithin
Definition: FunctionNames.h:81
static const std::string sm_GreaterThan
Definition: FunctionNames.h:57
Expression * getLowerBoundary() const
It returns the lower boundary expression.
A base class for binary functions.
static const char * sm_propertyIsLessThanOrEqualTo
Definition: Globals.h:56
A class that models a literal for Geometry values.
Definition: LiteralGeom.h:46
Expression * getExpression() const
It returns the between expression.
A given distance has a measurement and a unit-of-measure.
Definition: Distance.h:44
This class models a string Literal value.
Definition: LiteralString.h:46