All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SpatialRestrictionVisitor.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/query/SpatialRestrictionVisitor.cpp
22 
23  \brief A visitor that retrieves spatial restrictions from a Query hierarchy.
24 */
25 
26 // TerraLib
27 #include "../../common/STLUtils.h"
28 #include "../../common/Translator.h"
29 #include "../../geometry/Envelope.h"
30 #include "../../geometry/Geometry.h"
31 #include "../../geometry/Utils.h"
32 #include "../Exception.h"
33 #include "Expression.h"
34 #include "Function.h"
35 #include "FunctionNames.h"
36 #include "LiteralEnvelope.h"
37 #include "LiteralGeom.h"
38 #include "PropertyName.h"
39 #include "Select.h"
41 #include "Where.h"
42 
43 // STL
44 #include <cassert>
45 
47  : m_index(0),
48  m_type(te::gm::UNKNOWN_SPATIAL_RELATION),
49  m_geometry(0),
50  m_pname(""),
51  m_isFromEnvelope(false),
52  m_function(0)
53 {
54 }
55 
57 {
58  delete m_geometry;
59 }
60 
62  : m_index(0)
63 {
64  initialize();
65 }
66 
68 {
69  te::common::FreeContents(m_spatialRestrictions);
70 }
71 
73 {
74  if(isSpatialRestrictionFunction(visited))
75  {
76  addSpatialRestriction(visited);
77  return;
78  }
79 
80  std::size_t size = visited.getNumArgs();
81  for(std::size_t i = 0; i < size; ++i)
82  {
83  assert(visited[i]);
84  visited[i]->accept(*this);
85  }
86 }
87 
89 {
90  Where* restriction = visited.getWhere();
91 
92  if(!restriction)
93  return;
94 
95  Expression* e = restriction->getExp();
96  if(e)
97  e->accept(*this);
98 }
99 
101 {
102  return !m_spatialRestrictions.empty();
103 }
104 
105 const std::vector<te::da::SpatialRestriction*>& te::da::SpatialRestrictionVisitor::getSpatialRestrictions() const
106 {
107  return m_spatialRestrictions;
108 }
109 
111 {
112  // Initializes the map of spatial restriction functions
114  m_spatialFunctions[FunctionNames::sm_ST_Disjoint ] = te::gm::DISJOINT;
115  m_spatialFunctions[FunctionNames::sm_ST_Touches ] = te::gm::TOUCHES;
116  m_spatialFunctions[FunctionNames::sm_ST_Overlaps ] = te::gm::OVERLAPS;
117  m_spatialFunctions[FunctionNames::sm_ST_Crosses ] = te::gm::CROSSES;
118  m_spatialFunctions[FunctionNames::sm_ST_Within ] = te::gm::WITHIN;
119  m_spatialFunctions[FunctionNames::sm_ST_Contains ] = te::gm::CONTAINS;
120  m_spatialFunctions[FunctionNames::sm_ST_Equals ] = te::gm::EQUALS;
121 
122  /* TODO: not mapped values - UNKNOWN_SPATIAL_RELATION = 0, COVERS = 128, COVEREDBY = 256 */
123 }
124 
126 {
127  return m_spatialFunctions.find(f.getName()) != m_spatialFunctions.end();
128 }
129 
131 {
132  assert(isSpatialRestrictionFunction(f));
133 
134  return m_spatialFunctions.find(f.getName())->second;
135 }
136 
138 {
139  assert(f.getNumArgs() == 2);
140  assert(f.getArg(0));
141  assert(f.getArg(1));
142 
143  // Try the first argument
144  te::da::LiteralEnvelope* lenv = dynamic_cast<te::da::LiteralEnvelope*>(f.getArg(0));
145  if(lenv)
146  return true;
147 
148  // Try the second argument
149  lenv = dynamic_cast<te::da::LiteralEnvelope*>(f.getArg(1));
150  if(lenv)
151  return true;
152 
153  return false;
154 }
155 
157 {
158  assert(isSpatialRestrictionFunction(f));
159  assert(f.getNumArgs() == 2);
160  assert(f.getArg(0));
161  assert(f.getArg(1));
162 
163  bool hasEnvelope = true;
164 
165  // Try the first argument
166  te::da::LiteralEnvelope* lenv = dynamic_cast<te::da::LiteralEnvelope*>(f.getArg(0));
167  if(lenv == 0)
168  {
169  // Try the second argument
170  lenv = dynamic_cast<te::da::LiteralEnvelope*>(f.getArg(1));
171  if(lenv == 0)
172  hasEnvelope = false;
173  }
174 
175  if(hasEnvelope)
176  {
177  te::gm::Envelope* e = lenv->getValue();
178  assert(e);
179  return te::gm::GetGeomFromEnvelope(e, lenv->getSRID());
180  }
181 
182  // Here there is not a LiteralEnvelope. Try LiteralGeom!
183  te::da::LiteralGeom* lgeom = dynamic_cast<te::da::LiteralGeom*>(f.getArg(0));
184  if(lgeom == 0)
185  {
186  // Try the second argument
187  lgeom = dynamic_cast<te::da::LiteralGeom*>(f.getArg(1));
188  if(lgeom == 0)
189  return 0;
190  }
191 
192  te::dt::AbstractData* data = lgeom->getValue();
193  assert(data);
194 
195  te::gm::Geometry* geom = dynamic_cast<te::gm::Geometry*>(data);
196  assert(geom);
197 
198  return dynamic_cast<te::gm::Geometry*>(geom->clone());
199 }
200 
202 {
203  assert(f.getNumArgs() == 2);
204  assert(f.getArg(0));
205  assert(f.getArg(1));
206 
207  // Try the first argument
208  te::da::PropertyName* pname = dynamic_cast<te::da::PropertyName*>(f.getArg(0));
209  if(pname)
210  return pname->getName();
211 
212  // Try the second argument
213  pname = dynamic_cast<te::da::PropertyName*>(f.getArg(1));
214  if(pname)
215  return pname->getName();
216 
217  return "";
218 }
219 
221 {
222  // Gets the geometry restriction
223  te::gm::Geometry* geom = getGeometryRestriction(f);
224  if(geom == 0)
225  throw Exception(TE_TR("The spatial function not have a literal geometry or literal envelope!"));
226 
227  // Gets the property name
228  std::string pname = getPropertyName(f);
229  if(pname.empty())
230  throw Exception(TE_TR("The spatial function not have a property name!"));
231 
232  // Creates the spatial restriction
233  SpatialRestriction* restriction = new SpatialRestriction;
234  restriction->m_index = m_index++;
235  restriction->m_geometry = geom;
236  restriction->m_pname = pname;
237  restriction->m_type = getSpatialRelation(f);
238  restriction->m_isFromEnvelope = isFromEnvelope(f);
239  restriction->m_function = dynamic_cast<const Function*>(&f);
240 
241  // Adds the spatial restriction
242  m_spatialRestrictions.push_back(restriction);
243 }
Expression * getArg(std::size_t i) const
It returns the i-th function argument.
Definition: Function.cpp:67
te::gm::SpatialRelation m_type
The spatial restriction type.
const std::string & getName() const
It returns the property name.
Definition: PropertyName.h:80
A visitor that retrieves spatial restrictions from a Query hierarchy.
bool isSpatialRestrictionFunction(const Function &f) const
A class that models a Function expression.
static const std::string sm_ST_Equals
Definition: FunctionNames.h:84
std::size_t m_index
Internal index of the spatial restriction.
const std::vector< te::da::SpatialRestriction * > & getSpatialRestrictions() const
static const std::string sm_ST_Overlaps
Definition: FunctionNames.h:90
A class that models the name of any property of an object.
A class that models the name of any property of an object.
Definition: PropertyName.h:50
static const std::string sm_ST_Touches
Definition: FunctionNames.h:93
static const std::string sm_ST_Intersects
Definition: FunctionNames.h:86
SpatialRestriction()
Default constructor.
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
static const std::string sm_ST_Contains
Definition: FunctionNames.h:74
This is an abstract class that models a query expression.
Definition: Expression.h:47
A class that models a literal for Envelope values.
bool isFromEnvelope(const Function &f) const
static const std::string sm_ST_Crosses
Definition: FunctionNames.h:76
te::gm::Geometry * getGeometryRestriction(const Function &f) const
const Function * m_function
The function that represents the spatial restriction.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
A struct that represents a spatial restriction.
bool m_isFromEnvelope
A flag that indicates if the spatial restriction uses an envelope.
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
This is an abstract class that models a query expression.
virtual ~SpatialRestrictionVisitor()
Virtual destructor.
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
Expression * getExp() const
Definition: Where.cpp:60
A class that models a Function expression.
Definition: Function.h:47
A Select models a query to be used when retrieving data from a data source.
const std::string & getName() const
It returns the function name.
Definition: Function.h:79
std::string m_pname
The property name of the spatial restriction.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
int getSRID() const
It returns the envelope SRS id.
A class that can be used to model a filter expression that can be applied to a query.
Where * getWhere() const
It returns the filter condition.
Definition: Select.cpp:952
static const std::string sm_ST_Disjoint
Definition: FunctionNames.h:78
te::gm::Geometry * m_geometry
The geometry of the spatial restriction.
virtual void visit(const Expression &visited)
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
A class that models a literal for Envelope values.
A static class with global function name definitions.
std::string getPropertyName(const Function &f) const
A class that models a literal for Geometry values.
virtual AbstractData * clone() const =0
It returns a clone of this object.
te::gm::SpatialRelation getSpatialRelation(const Function &f) const
te::dt::AbstractData * getValue() const
It returns the value associated to the literal.
Definition: Literal.cpp:65
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
std::size_t getNumArgs() const
It returns the number of arguments informed to the function.
Definition: Function.cpp:62
te::gm::Envelope * getValue() const
It returns the associated envelope value.
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
Definition: Utils.cpp:38
A class that models a literal for Geometry values.
Definition: LiteralGeom.h:46