examples/graph/Functions.cpp
Go to the documentation of this file.
1 //TerraLib
9 #include <terralib/raster/Grid.h>
11 #include "GraphExamples.h"
12 
13 // STL Includes
14 #include <iostream>
15 
16 
17 
18 std::unique_ptr<te::rst::Raster> OpenRaster(const std::string& pathName, const int& srid)
19 {
20  std::cout << std::endl << "Open Raster: " << pathName << std::endl;
21 
22  //set connection info
23  std::map<std::string, std::string> rinfo;
24  rinfo["URI"] = pathName;
25 
26  // open input raster
28  raster->getGrid()->setSRID(srid);
29 
30  std::unique_ptr<te::rst::Raster> rst(raster);
31 
32  return rst;
33 }
34 
35 std::unique_ptr<te::da::DataSource> OpenDataSource(std::string connInfo, std::string dsType)
36 {
37  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make(dsType, connInfo);
38  ds->open();
39 
40  return ds;
41 }
42 
43 std::unique_ptr<te::da::DataSource> OpenOGRDataSource(const std::string& pathName)
44 {
45  std::string connInfo("file://" + pathName);
46  return OpenDataSource(connInfo, "OGR");
47 }
48 
49 std::unique_ptr<te::gm::Envelope> getDataSetExtent(te::da::DataSource* ds, std::string dataSetName)
50 {
51  std::unique_ptr<te::da::DataSet> dataSet = ds->getDataSet(dataSetName);
52 
53  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(dataSet.get());
54 
55  return dataSet->getExtent(geomPos);
56 }
57 
58 int AssociateGraphVertexAttribute(te::da::DataSource* ds, std::string dataSetName, int idIdx, int attrIdx, boost::shared_ptr<te::graph::AbstractGraph> graph, int dataType, std::string attrName)
59 {
60  assert(ds);
61 
62  //add graph attr
63  int attrGraphIdx = AddGraphVertexAttribute(graph, attrName, dataType);
64 
65  //get the number of attributes from graph
66  int attrSize = graph->getMetadata()->getVertexPropertySize();
67 
68  //dataset iterator
69  std::unique_ptr<te::da::DataSet> dataSet = ds->getDataSet(dataSetName);
70 
71  dataSet->moveBeforeFirst();
72 
73  while(dataSet->moveNext())
74  {
75  te::dt::AbstractData* ad = dataSet->getValue(attrIdx).release();
76 
77  int idx = dataSet->getInt32(idIdx);
78 
79  te::graph::Vertex* v = graph->getVertex(idx);
80 
81  if(v)
82  {
83  //resize attr vector
84  v->setAttributeVecSize(attrSize);
85 
86  //add attribute
87  v->addAttribute(attrGraphIdx, ad);
88  }
89  }
90 
91  return attrGraphIdx;
92 }
93 
94 int AddGraphVertexAttribute(boost::shared_ptr<te::graph::AbstractGraph> graph, std::string attrName, int dataType)
95 {
96  //add new attribute
97  te::dt::SimpleProperty* p = new te::dt::SimpleProperty(attrName, dataType);
98  p->setParent(0);
99  p->setId(0);
100 
101  graph->addVertexProperty(p);
102 
103  // verify what the index of the new property
104  int idx = 0;
105 
106  for(int i = 0; i < graph->getVertexPropertySize(); ++ i)
107  {
108  if(graph->getVertexProperty(i)->getName() == attrName)
109  {
110  idx = i;
111  break;
112  }
113  }
114 
115  return idx;
116 }
117 
118 int AddGraphEdgeAttribute(boost::shared_ptr<te::graph::AbstractGraph> graph, std::string attrName, int dataType)
119 {
120  //add new attribute
121  te::dt::SimpleProperty* p = new te::dt::SimpleProperty(attrName, dataType);
122  p->setParent(0);
123  p->setId(0);
124 
125  graph->addEdgeProperty(p);
126 
127  // verify what the index of the new property
128  int idx = 0;
129 
130  for(int i = 0; i < graph->getEdgePropertySize(); ++ i)
131  {
132  if(graph->getEdgeProperty(i)->getName() == attrName)
133  {
134  idx = i;
135  break;
136  }
137  }
138 
139  return idx;
140 }
std::unique_ptr< te::da::DataSource > OpenDataSource(std::string connInfo, std::string dsType)
int AssociateGraphVertexAttribute(te::da::DataSource *ds, std::string dataSetName, int idIdx, int attrIdx, boost::shared_ptr< te::graph::AbstractGraph > graph, int dataType, std::string attrName)
Auxiliar functions used to create a vertex attribute in a graph and get the value from a dataset...
void setAttributeVecSize(int size)
This function is used to set the number of attributes associated with the vertex elements.
Definition: Vertex.cpp:79
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
std::unique_ptr< te::rst::Raster > OpenRaster(const std::string &pathName, const int &srid)
Auxiliar functions for load a raster.
int AddGraphVertexAttribute(boost::shared_ptr< te::graph::AbstractGraph > graph, std::string attrName, int dataType)
Auxiliar functions used to create a vertex attribute in a graph, return the property idx...
An atomic property like an integer or double.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
static te::dt::Date ds(2010, 01, 01)
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Class used to define the graph metadata informations.
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
Abstract class used to define the main functions of graph struct. All graph implementations must used...
This is the abstract factory for Rasters.
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
An abstract class for raster data strucutures.
int AddGraphEdgeAttribute(boost::shared_ptr< te::graph::AbstractGraph > graph, std::string attrName, int dataType)
Auxiliar functions used to create a edge attribute in a graph, return the property idx...
A factory for data sources.
te::gm::Polygon * p
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Grid * getGrid()
It returns the raster grid.
Utility functions for the data access module.
virtual std::unique_ptr< DataSet > getDataSet(const std::string &name, te::common::TraverseType travType=te::common::FORWARDONLY, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It gets the dataset identified by the given name. This method always returns a disconnected dataset...
A rectified grid is the spatial support for raster data.
std::unique_ptr< te::gm::Envelope > getDataSetExtent(te::da::DataSource *ds, std::string dataSetName)
Auxiliar functions used to get a dataset extent.
An atomic property like an integer or double.
void setSRID(int srid)
Just sets the grid spatial reference system identifier.
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Vertex.cpp:84
These routines show how to use the Graph module.
std::unique_ptr< te::da::DataSource > OpenOGRDataSource(const std::string &pathName)
Auxiliar functions for load a org data source.
static Raster * open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
It opens a raster with the given parameters and default raster driver.
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177