All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GeneralizedProximityMatrix.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 GeneralizedProximityMatrix.cpp
22 
23  \brief This class defines the GPM class.
24 */
25 
26 // Terralib Includes
27 #include "../../datatype/SimpleProperty.h"
28 #include "../../graph/core/AbstractGraph.h"
29 #include "../../graph/core/GraphMetadata.h"
30 #include "../../graph/core/Vertex.h"
31 #include "../../graph/iterator/MemoryIterator.h"
32 #include "../../memory/DataSet.h"
33 #include "../../memory/DataSetItem.h"
35 
36 //STL Includes
37 #include <cassert>
38 
40  m_dataSetName(""),
41  m_attributeName("")
42 {
43 }
44 
46 {
47 }
48 
49 void te::sa::GeneralizedProximityMatrix::setDataSetName(const std::string& dataSetName)
50 {
51  m_dataSetName = dataSetName;
52 }
53 
55 {
56  return m_dataSetName;
57 }
58 
59 void te::sa::GeneralizedProximityMatrix::setAttributeName( const std::string& attrName)
60 {
61  m_attributeName = attrName;
62 }
63 
65 {
66  return m_attributeName;
67 }
68 
70 {
71  m_graph.reset(graph);
72 }
73 
75 {
76  return m_graph.get();
77 }
78 
80 {
81  assert(m_graph.get());
82  assert(ds.get());
83 
84  //create datasetype
85  std::auto_ptr<te::da::DataSetType> dsType = createDataSetType(dataSetName);
86 
87  //create dataset
88  std::auto_ptr<te::mem::DataSet> dataSet = createDataSet(dsType.get());
89 
90  //save gpm vertex into datasource
91  if(dsType.get() && dataSet.get())
92  {
93  dataSet->moveBeforeFirst();
94 
95  std::map<std::string, std::string> options;
96 
97  ds->createDataSet(dsType.get(), options);
98 
99  ds->add(dataSetName, dataSet.get(), options);
100  }
101 }
102 
103 std::auto_ptr<te::da::DataSetType> te::sa::GeneralizedProximityMatrix::createDataSetType(std::string dataSetName)
104 {
105  std::auto_ptr<te::da::DataSetType> dataSetType(new te::da::DataSetType(dataSetName));
106 
107  //create index property
109  dataSetType->add(idxProperty);
110 
111  //create primary key
112  std::string pkName = TE_SA_GPM_ATTR_PK_NAME;
113  pkName+= "_" + dataSetName;
114  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pkName, dataSetType.get());
115  pk->add(idxProperty);
116 
117  //create all gpm properties
118  for(int i = 0; i < m_graph->getMetadata()->getVertexPropertySize(); ++i)
119  {
120  te::dt::Property* prop = m_graph->getMetadata()->getVertexProperty(i);
121 
122  if(prop->getName() == TE_SA_GEOMETRY_ATTR_NAME)
123  continue;
124 
125  te::dt::Property* newProp = prop->clone();
126  newProp->setId(0);
127  newProp->setParent(0);
128 
129  dataSetType->add(newProp);
130  }
131 
132  return dataSetType;
133 }
134 
136 {
137  std::auto_ptr<te::mem::DataSet> outDataset(new te::mem::DataSet(dsType));
138 
139  //get property map
140  std::map<int, std::string> propMap = getGPMPropertyMap();
141 
142  //create graph vertex iterator
143  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(m_graph.get()));
144 
145  te::graph::Vertex* v = it->getFirstVertex();
146 
147  while(!it->isVertexIteratorAfterEnd())
148  {
149  //create dataset item
150  te::mem::DataSetItem* outDSetItem = new te::mem::DataSetItem(outDataset.get());
151 
152  //get vertex info
153  int idx = v->getId();
154 
155  //set index information
156  outDSetItem->setInt32("index", idx);
157 
158  //set the other attributes
159  std::vector<te::dt::AbstractData*> adVec = v->getAttributes();
160 
161  std::map<int, std::string>::iterator itMap = propMap.begin();
162 
163  while(itMap != propMap.end())
164  {
165  te::dt::AbstractData* adClone = adVec[itMap->first]->clone();
166 
167  outDSetItem->setValue(itMap->second, adClone);
168 
169  ++itMap;
170  }
171 
172  //add item into dataset
173  outDataset->add(outDSetItem);
174 
175  v = it->getNextVertex();
176  }
177 
178  return outDataset;
179 }
180 
182 {
183  std::map<int, std::string> propMap;
184 
185  for(int i = 0; i < m_graph->getMetadata()->getVertexPropertySize(); ++i)
186  {
187  te::dt::Property* prop = m_graph->getMetadata()->getVertexProperty(i);
188 
189  if(prop->getName() == TE_SA_GEOMETRY_ATTR_NAME)
190  continue;
191 
192  propMap.insert(std::map<int, std::string>::value_type(i, prop->getName()));
193  }
194 
195  return propMap;
196 }
#define TE_SA_GEOMETRY_ATTR_NAME
Definition: Config.h:44
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
std::auto_ptr< te::mem::DataSet > createDataSet(te::da::DataSetType *dsType)
An atomic property like an integer or double.
#define TE_SA_GPM_ATTR_PK_NAME
Definition: Config.h:35
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
A class that models the description of a dataset.
Definition: DataSetType.h:72
virtual Property * clone() const =0
It returns a clone of the object.
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
std::vector< te::dt::AbstractData * > & getAttributes()
It returns the vector of attributes associated with this element.
Definition: Vertex.cpp:74
It models a property definition.
Definition: Property.h:59
std::map< int, std::string > getGPMPropertyMap()
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
void setId(unsigned int id)
It sets the property identifier.
Definition: Property.h:118
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
void setDataSetName(const std::string &dataSetName)
std::auto_ptr< te::da::DataSetType > createDataSetType(std::string dataSetName)
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
virtual ~GeneralizedProximityMatrix()
Virtual destructor.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
void toDataSource(te::da::DataSourcePtr ds, std::string dataSetName)
Function used to export the all vertex attributes from gpm graph to a datasource. ...
int getId()
It returns the vertex id.
Definition: Vertex.cpp:69
virtual AbstractData * clone() const =0
It returns a clone of this object.
void setAttributeName(const std::string &attrName)
void setGraph(te::graph::AbstractGraph *graph)
const std::string & getName() const
It returns the property name.
Definition: Property.h:127
This class defines the GPM class.
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177