GPMWeightsInverseDistanceStrategy.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 GPMWeightsInverseDistanceStrategy.h
22 
23  \brief This class defines a class to calculates a weight for a GPM using Inverse Distance strategy.
24 */
25 
26 // Terralib Includes
27 #include "../../datatype/SimpleData.h"
28 #include "../../graph/core/AbstractGraph.h"
29 #include "../../graph/core/Edge.h"
30 #include "../../graph/core/GraphMetadata.h"
31 #include "../../graph/core/Vertex.h"
32 #include "../../graph/iterator/MemoryIterator.h"
35 
37  bool normalize, double a, double factor)
38  : m_normalize(normalize), m_a(a), m_factor(factor)
39 {
41 }
42 
45 
47 {
48  //create weight property
49  int weightIdx = createWeightAttribute(gpm);
50 
51  int nEdgeAttrs = gpm->getGraph()->getMetadata()->getEdgePropertySize();
52 
53  //check if the gpm has the distance attribute
54  int distanceIdx;
55  bool hasDistance = getDistanceAttributeIndex(gpm->getGraph()->getMetadata(), distanceIdx);
56 
57  //iterate over all vertex
61 
62  while(v)
63  {
64  std::set<int> neighbours = v->getSuccessors();
65  std::set<int>::iterator itNeighbours = neighbours.begin();
66 
67  //calculate weight
68  double tot = 0.;
69  std::vector<double> weightVec;
70 
71  while(itNeighbours != neighbours.end())
72  {
73  double weight = 0.;
74 
75  te::graph::Edge* e = g->getEdge(*itNeighbours);
76 
77  if(e)
78  {
79  //get distance attribute
80  if(hasDistance)
81  {
82  double distance = getDistanceAttributeValue(e, distanceIdx);
83 
84  if(distance != 0.)
85  weight += m_a * (1. / distance);
86  }
87 
88  //extra information calculate in TerraLib 4
89  //if (attr.WasNetworkObjectsDistanceComputed())
90  //if ((d_net = attr.NetworkObjectsDistance()) != 0.0)
91  //w += (params_.b_)*1/d_net;
92 
93  //if (attr.WasNetworkMinimumPathComputed())
94  //if ((d_conn = attr.NetworkMinimumPath()) != 0.0)
95  //w += (params_.c_)*1/d_conn;
96  }
97 
98  if(weight == 0.)
99  weight = 1.;
100 
101  weightVec.push_back(weight * m_factor);
102 
103  tot += weight;
104 
105  ++itNeighbours;
106  }
107 
108  itNeighbours = neighbours.begin();
109  std::vector<double>::iterator itWeights = weightVec.begin();
110 
111  while(itNeighbours != neighbours.end())
112  {
113  te::graph::Edge* e = g->getEdge(*itNeighbours);
114 
115  if(e)
116  {
117  double weight = *itWeights;
118 
119  if(m_normalize && tot != 0.)
120  {
121  weight = weight / tot;
122  }
123 
124  e->setAttributeVecSize(nEdgeAttrs);
125 
127  }
128 
129  ++itWeights;
130  ++itNeighbours;
131  }
132 
133  v = it->getNextVertex();
134  }
135 }
virtual te::graph::Vertex * getFirstVertex()
It returns a pointer to the first vertex element of a graph.
GPMWeightsInverseDistanceStrategy(bool normalize, double a=1.0, double factor=1.0)
Default constructor.
void setAttributeVecSize(int size)
This function is used to set the number of attributes associated with the edge elements.
Definition: Edge.cpp:86
virtual te::graph::Vertex * getNextVertex()
It returns a pointer to the next vertex element of a graph.
virtual ~GPMWeightsInverseDistanceStrategy()
Virtual destructor.
This class defines a Generalized Proximity Matrix.
bool getDistanceAttributeIndex(te::graph::GraphMetadata *gm, int &index)
Function used to get the distance attribute index.
GPMWeightsStrategyType m_type
Weight Type.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
virtual te::graph::GraphMetadata * getMetadata()=0
Function used to access the graph metadata.
int createWeightAttribute(GeneralizedProximityMatrix *gpm)
Added to the edge a new attribute for weight information and return the attr index.
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
double getDistanceAttributeValue(te::graph::Edge *e, const int index)
Function used to get the distance value from a edge element.
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
std::set< int > & getSuccessors()
Returns the Successors vector.
Definition: Vertex.cpp:106
This class defines a class to calculates a weight for a GPM using Inverse Distance strategy...
virtual int getEdgePropertySize()
Used to verify the number of properties associated to edge elements.
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
virtual void calculate(GeneralizedProximityMatrix *gpm)
This class defines the GPM class.