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 void te::sa::GeneralizedProximityMatrix::setDataSetName(const std::string& dataSetName)
48 {
49  m_dataSetName = dataSetName;
50 }
51 
53 {
54  return m_dataSetName;
55 }
56 
57 void te::sa::GeneralizedProximityMatrix::setAttributeName( const std::string& attrName)
58 {
59  m_attributeName = attrName;
60 }
61 
63 {
64  return m_attributeName;
65 }
66 
68 {
69  m_graph.reset(graph);
70 }
71 
73 {
74  return m_graph.get();
75 }
76 
78 {
79  assert(m_graph.get());
80  assert(ds.get());
81 
82  //create datasetype
83  std::unique_ptr<te::da::DataSetType> dsType = createDataSetType(dataSetName);
84 
85  //create dataset
86  std::unique_ptr<te::mem::DataSet> dataSet = createDataSet(dsType.get());
87 
88  //save gpm vertex into datasource
89  if(dsType.get() && dataSet.get())
90  {
91  dataSet->moveBeforeFirst();
92 
93  std::map<std::string, std::string> options;
94 
95  ds->createDataSet(dsType.get(), options);
96 
97  ds->add(dataSetName, dataSet.get(), options);
98  }
99 }
100 
101 std::unique_ptr<te::da::DataSetType> te::sa::GeneralizedProximityMatrix::createDataSetType(std::string dataSetName)
102 {
103  std::unique_ptr<te::da::DataSetType> dataSetType(new te::da::DataSetType(dataSetName));
104 
105  //create index property
107  dataSetType->add(idxProperty);
108 
109  //create primary key
110  std::string pkName = TE_SA_GPM_ATTR_PK_NAME;
111  pkName+= "_" + dataSetName;
112  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pkName, dataSetType.get());
113  pk->add(idxProperty);
114 
115  //create all gpm properties
116  for(int i = 0; i < m_graph->getMetadata()->getVertexPropertySize(); ++i)
117  {
118  te::dt::Property* prop = m_graph->getMetadata()->getVertexProperty(i);
119 
120  if(prop->getName() == TE_SA_GEOMETRY_ATTR_NAME)
121  continue;
122 
123  te::dt::Property* newProp = prop->clone();
124  newProp->setId(0);
125  newProp->setParent(nullptr);
126 
127  dataSetType->add(newProp);
128  }
129 
130  return dataSetType;
131 }
132 
134 {
135  std::unique_ptr<te::mem::DataSet> outDataset(new te::mem::DataSet(dsType));
136 
137  //get property map
138  std::map<int, std::string> propMap = getGPMPropertyMap();
139 
140  //create graph vertex iterator
141  std::unique_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(m_graph.get()));
142 
143  te::graph::Vertex* v = it->getFirstVertex();
144 
145  while(!it->isVertexIteratorAfterEnd())
146  {
147  //create dataset item
148  te::mem::DataSetItem* outDSetItem = new te::mem::DataSetItem(outDataset.get());
149 
150  //get vertex info
151  int idx = v->getId();
152 
153  //set index information
154  outDSetItem->setInt32("index", idx);
155 
156  //set the other attributes
157  std::vector<te::dt::AbstractData*> adVec = v->getAttributes();
158 
159  std::map<int, std::string>::iterator itMap = propMap.begin();
160 
161  while(itMap != propMap.end())
162  {
163  te::dt::AbstractData* adClone = adVec[itMap->first]->clone();
164 
165  outDSetItem->setValue(itMap->second, adClone);
166 
167  ++itMap;
168  }
169 
170  //add item into dataset
171  outDataset->add(outDSetItem);
172 
173  v = it->getNextVertex();
174  }
175 
176  return outDataset;
177 }
178 
180 {
181  std::map<int, std::string> propMap;
182 
183  for(int i = 0; i < m_graph->getMetadata()->getVertexPropertySize(); ++i)
184  {
185  te::dt::Property* prop = m_graph->getMetadata()->getVertexProperty(i);
186 
187  if(prop->getName() == TE_SA_GEOMETRY_ATTR_NAME)
188  continue;
189 
190  propMap.insert(std::map<int, std::string>::value_type(i, prop->getName()));
191  }
192 
193  return propMap;
194 }
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
An atomic property like an integer or double.
#define TE_SA_GEOMETRY_ATTR_NAME
boost::shared_ptr< DataSource > DataSourcePtr
A class that models the description of a dataset.
Definition: DataSetType.h:72
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
static te::dt::Date ds(2010, 01, 01)
std::vector< te::dt::AbstractData * > & getAttributes()
It returns the vector of attributes associated with this element.
Definition: Vertex.cpp:74
std::unique_ptr< te::da::DataSetType > createDataSetType(std::string dataSetName)
virtual Property * clone() const =0
It returns a clone of the object.
It models a property definition.
Definition: Property.h:59
std::unique_ptr< te::mem::DataSet > createDataSet(te::da::DataSetType *dsType)
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...
void setDataSetName(const std::string &dataSetName)
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
virtual AbstractData * clone() const =0
It returns a clone of this object.
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
#define TE_SA_GPM_ATTR_PK_NAME
virtual ~GeneralizedProximityMatrix()
Virtual destructor.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
std::unique_ptr< te::graph::AbstractGraph > m_graph
Graph that represents the gpm.
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
std::string m_attributeName
Attribute used to identify the attr from dataset associated to this gmp.
void setAttributeName(const std::string &attrName)
std::string m_dataSetName
Attribute used to identify the dataset associated to this gpm.
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