All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GPMConstructorDistanceStrategy.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 GPMConstructorAdjacencyStrategy.cpp
22 
23  \brief This class defines a an adjacency strategy class for a GPM constructor.
24 */
25 
26 // TerraLib Includes
27 #include "../../common/Exception.h"
28 #include "../../common/Translator.h"
29 #include "../../common/STLUtils.h"
30 #include "../../common/progress/TaskProgress.h"
31 #include "../../dataaccess/datasource/DataSource.h"
32 #include "../../dataaccess/utils/Utils.h"
33 #include "../../datatype/SimpleData.h"
34 #include "../../datatype/SimpleProperty.h"
35 #include "../../geometry/Point.h"
36 #include "../../graph/core/Edge.h"
37 #include "../../graph/core/Vertex.h"
40 
42 {
44  m_distance = 0.;
45 }
46 
48  m_distance(distance)
49 {
51 }
52 
54 {
55 }
56 
58 {
59  //get input information
60  std::auto_ptr<te::da::DataSet> dataSet = m_ds->getDataSet(m_gpm->getDataSetName());
61 
62  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(dataSet.get());
63 
64  //create distance attribute
65  createDistanceAttribute(m_gpm);
66 
67  //create tree
69  std::map<int, te::gm::Geometry*> geomMap;
70 
71  dataSet->moveBeforeFirst();
72 
73  while(dataSet->moveNext())
74  {
75  std::string strId = dataSet->getAsString(m_gpm->getAttributeName());
76 
77  int id = atoi(strId.c_str());
78 
79  te::gm::Geometry* g = dataSet->getGeometry(geomPos).release();
80  const te::gm::Envelope* box = g->getMBR();
81 
82  rtree.insert(*box, id);
83 
84  geomMap.insert(std::map<int, te::gm::Geometry*>::value_type(id, g));
85  }
86 
87  //create task
89 
90  task.setTotalSteps(dataSet->size());
91  task.setMessage(TE_TR("Creating Edge Objects."));
92 
93  //create edge objects
94  dataSet->moveBeforeFirst();
95 
96  while(dataSet->moveNext())
97  {
98  std::string strIdFrom = dataSet->getAsString(m_gpm->getAttributeName());
99 
100  int vFromId = atoi(strIdFrom.c_str());
101 
102  std::auto_ptr<te::gm::Geometry> g = dataSet->getGeometry(geomPos);
103 
104  std::vector<int> results;
105 
106  te::gm::Envelope ext(*g->getMBR());
107 
108  ext.m_llx -= m_distance;
109  ext.m_lly -= m_distance;
110  ext.m_urx += m_distance;
111  ext.m_ury += m_distance;
112 
113  rtree.search(ext, results);
114 
115  for(size_t t = 0; t < results.size(); ++t)
116  {
117  std::map<int, te::gm::Geometry*>::iterator it = geomMap.find(results[t]);
118 
119  if(it != geomMap.end())
120  {
121  int vToId = results[t];
122 
123  te::graph::Vertex* vFrom = m_gpm->getGraph()->getVertex(vFromId);
124  te::gm::Point* pFrom = dynamic_cast<te::gm::Point*>(vFrom->getAttributes()[0]);
125 
126  te::graph::Vertex* vTo = m_gpm->getGraph()->getVertex(vToId);
127  te::gm::Point* pTo = dynamic_cast<te::gm::Point*>(vTo->getAttributes()[0]);
128 
129  double dist = pFrom->distance(pTo);
130 
131  if(dist <= m_distance)
132  {
133  int edgeId = getEdgeId();
134 
135  te::graph::Edge* e = new te::graph::Edge(edgeId, vFromId, vToId);
136 
138 
139  e->setAttributeVecSize(1);
140  e->addAttribute(0, sd);
141 
142  m_gpm->getGraph()->add(e);
143  }
144  }
145  }
146 
147  if(!task.isActive())
148  {
149  te::common::FreeContents(geomMap);
150  geomMap.clear();
151 
152  throw te::common::Exception(TE_TR("Operation canceled by the user."));
153  }
154 
155  task.pulse();
156  }
157 
158  te::common::FreeContents(geomMap);
159  geomMap.clear();
160 }
virtual void constructStrategy()
Build the edges using specific strategy.
void setMessage(const std::string &message)
Set the task message.
void setAttributeVecSize(int size)
This function is used to set the number of attributes associated with the edge elements.
Definition: Edge.cpp:86
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
std::vector< te::dt::AbstractData * > & getAttributes()
It returns the vector of attributes associated with this element.
Definition: Vertex.cpp:74
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
bool isActive() const
Verify if the task is active.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
void setTotalSteps(int value)
Set the task total stepes.
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
Definition: Utils.cpp:462
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
int search(const te::gm::Envelope &mbr, std::vector< DATATYPE > &report) const
Range search query.
Definition: Index.h:326
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
GPMConstructorStrategyType m_type
Constructor Type.
This class defines a an Abstract class for a GPM constructor.
void insert(const te::gm::Envelope &mbr, const DATATYPE &data)
It inserts an item into the tree.
Definition: Index.h:313
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Edge.cpp:91
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
This class defines a an distance strategy class for a GPM constructor.
virtual double distance(const Geometry *const rhs) const
It returns the shortest distance between any two points in the two geometry objects.
Definition: Geometry.cpp:453
virtual ~GPMConstructorDistanceStrategy()
Virtual destructor.
This class defines the GPM class.