All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GPMConstructorAdjacencyStrategy.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_calcDistance = true;
45 }
46 
48  m_calcDistance(calcDistance)
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  if(m_calcDistance)
66  {
67  createDistanceAttribute(m_gpm);
68  }
69 
70  //create tree
72  std::map<int, te::gm::Geometry*> geomMap;
73 
74  dataSet->moveBeforeFirst();
75 
76  while(dataSet->moveNext())
77  {
78  std::string strId = dataSet->getAsString(m_gpm->getAttributeName());
79 
80  int id = atoi(strId.c_str());
81 
82  te::gm::Geometry* g = dataSet->getGeometry(geomPos).release();
83  const te::gm::Envelope* box = g->getMBR();
84 
85  rtree.insert(*box, id);
86 
87  geomMap.insert(std::map<int, te::gm::Geometry*>::value_type(id, g));
88  }
89 
90  //create task
92 
93  task.setTotalSteps(dataSet->size());
94  task.setMessage(TE_TR("Creating Edge Objects."));
95 
96  //create edges objects
97  dataSet->moveBeforeFirst();
98 
99  while(dataSet->moveNext())
100  {
101  std::string strIdFrom = dataSet->getAsString(m_gpm->getAttributeName());
102 
103  int vFromId = atoi(strIdFrom.c_str());
104 
105  std::auto_ptr<te::gm::Geometry> g = dataSet->getGeometry(geomPos);
106 
107  std::vector<int> results;
108 
109  rtree.search(*g->getMBR(), results);
110 
111  for(size_t t = 0; t < results.size(); ++t)
112  {
113  std::map<int, te::gm::Geometry*>::iterator it = geomMap.find(results[t]);
114 
115  if(it != geomMap.end())
116  {
117  if(g->touches(it->second) || g->intersects(it->second))
118  {
119  int edgeId = getEdgeId();
120 
121  int vToId = results[t];
122 
123  te::graph::Edge* e = new te::graph::Edge(edgeId, vFromId, vToId);
124 
125  if(m_calcDistance)
126  {
127  te::graph::Vertex* vFrom = m_gpm->getGraph()->getVertex(vFromId);
128  te::gm::Point* pFrom = dynamic_cast<te::gm::Point*>(vFrom->getAttributes()[0]);
129 
130  te::graph::Vertex* vTo = m_gpm->getGraph()->getVertex(vToId);
131  te::gm::Point* pTo = dynamic_cast<te::gm::Point*>(vTo->getAttributes()[0]);
132 
133  double dist = pFrom->distance(pTo);
134 
136 
137  e->setAttributeVecSize(1);
138  e->addAttribute(0, sd);
139  }
140 
141  m_gpm->getGraph()->add(e);
142  }
143  }
144  }
145 
146  if(!task.isActive())
147  {
148  te::common::FreeContents(geomMap);
149  geomMap.clear();
150 
151  throw te::common::Exception(TE_TR("Operation canceled by the user."));
152  }
153 
154 
155  task.pulse();
156  }
157 
158  te::common::FreeContents(geomMap);
159  geomMap.clear();
160 }
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
virtual void constructStrategy()
Build the edges using specific strategy.
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
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
This class defines a an adjacency strategy class for a GPM constructor.
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
virtual ~GPMConstructorAdjacencyStrategy()
Virtual destructor.
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
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
This class defines the GPM class.