CreateMSTGraph.cpp
Go to the documentation of this file.
1 //TerraLib
2 #include "../Config.h"
3 #include "GraphExamples.h"
4 #include "DisplayWindow.h"
5 
13 #include <terralib/graph/Globals.h>
14 #include <terralib/se.h>
18 #include <terralib/sa/core/Utils.h>
19 
20 
21 // STL Includes
22 #include <iostream>
23 
24 // BOOST Includes
25 #include <boost/shared_ptr.hpp>
26 
27 // Qt
28 #include <QApplication>
29 
31 void CreateWeightAttribute(te::graph::AbstractGraph* graph, int weightAttrIdx, std::vector<int> attrsIdx);
32 double CalculateWeight(std::vector<int> attrsIdx, te::graph::Vertex* vFrom, te::graph::Vertex* vTo);
33 
34 void CreateMSTGraph(bool draw)
35 {
36  std::cout << std::endl << "Create MST Graph..." << std::endl;
37 
38  //load data set
39  std::string data_dir = TERRALIB_DATA_DIR;
40  std::unique_ptr<te::da::DataSource> ds = OpenOGRDataSource(data_dir + "/graph/up_pol_rec_pol.shp");
41 
42  std::string dataSetName = "up_pol_rec_pol";
43  std::unique_ptr<te::da::DataSet> dataSet = ds->getDataSet(dataSetName);
44  std::unique_ptr<te::da::DataSetType> dataSetType = ds->getDataSetType(dataSetName);
45 
46  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(dataSet.get());
47 
48  //load gpm
49  std::unique_ptr<te::sa::GeneralizedProximityMatrix> gpm;
50 
52 
53  gpm.reset(swe.importFromGAL(data_dir + "/graph/up_pol_rec_gal.gal", ds.get()));
54 
55  gpm->getGraph()->getMetadata()->setSRID(29193);
56  gpm->getGraph()->getMetadata()->setEnvelope(*dataSet->getExtent(geomPos));
57 
58 
59  //calculate the mst
60  std::vector<int> attrsIdx;
61 
62  std::string attrName = "Attr1";
63 
64  std::size_t idx = dataSetType->getPropertyPosition(attrName);
65  int type = dataSet->getPropertyDataType(idx);
66  int gpmIdx = te::sa::AssociateGPMVertexAttribute(gpm.get(), dataSet.get(), "objet_id_1", attrName, type);
67 
68  attrsIdx.push_back(gpmIdx);
69 
71 
72  CreateWeightAttribute(gpm->getGraph(), edgeWeightIdx, attrsIdx);
73 
74  te::sa::MinimumSpanningTree mst(gpm->getGraph());
75 
76  te::graph::AbstractGraph* graph = mst.kruskal(edgeWeightIdx);
77 
78  graph->getMetadata()->setSRID(29193);
79  graph->getMetadata()->setEnvelope(*dataSet->getExtent(geomPos));
80 
81  if(draw)
82  {
83  // set visual
85 
86  //start qApp
87  int argc = 0;
88  QApplication app(argc, 0);
89 
90  DisplayWindow* w = new DisplayWindow();
91  w->addVectorialLayer(data_dir + "/graph/up_pol_rec_pol.shp", 29193);
92  w->addGraph(graph, *dataSet->getExtent(geomPos), style);
93  w->show();
94 
95  app.exec();
96  }
97 
98  delete graph;
99 }
100 
102 {
103  te::se::Stroke* stroke = te::se::CreateStroke("#000000", "1.0");
104  te::se::LineSymbolizer* lineSymbolizer = te::se::CreateLineSymbolizer(stroke);
105 
106  te::se::Fill* markFill = te::se::CreateFill("#009900", "1.0");
107  te::se::Stroke* markStroke = te::se::CreateStroke("#000000", "1");
108  te::se::Mark* mark = te::se::CreateMark("circle", markStroke, markFill);
109  te::se::Graphic* graphic = te::se::CreateGraphic(mark, "4", "", "");
110  te::se::PointSymbolizer* pointSymbolizer = te::se::CreatePointSymbolizer(graphic);
111 
112  te::se::Rule* rule = new te::se::Rule;
113  rule->push_back(lineSymbolizer);
114  rule->push_back(pointSymbolizer);
115 
117  style->push_back(rule);
118 
119  return style;
120 }
121 
122 void CreateWeightAttribute(te::graph::AbstractGraph* graph, int weightAttrIdx, std::vector<int> attrsIdx)
123 {
124  int size = graph->getMetadata()->getEdgePropertySize();
125 
127 
128  te::graph::Edge* edge = iterator->getFirstEdge();
129 
130  while(edge)
131  {
132  te::graph::Vertex* vFrom = graph->getVertex(edge->getIdFrom());
133  te::graph::Vertex* vTo = graph->getVertex(edge->getIdTo());
134 
135  if(vFrom && vTo)
136  {
137  double weight = CalculateWeight(attrsIdx, vFrom, vTo);
138 
139  edge->setAttributeVecSize(size);
140  edge->addAttribute(weightAttrIdx, new te::dt::SimpleData<double, te::dt::DOUBLE_TYPE>(weight));
141  }
142 
143  edge = iterator->getNextEdge();
144  }
145 }
146 
147 double CalculateWeight(std::vector<int> attrsIdx, te::graph::Vertex* vFrom, te::graph::Vertex* vTo)
148 {
149  double weight = 0.;
150 
151  for(std::size_t t = 0; t < attrsIdx.size(); ++t)
152  {
153  double valueFrom = te::sa::GetDataValue(vFrom->getAttributes()[attrsIdx[t]]);
154  double valueTo = te::sa::GetDataValue(vTo->getAttributes()[attrsIdx[t]]);
155 
156  weight += (valueTo - valueFrom) * (valueTo - valueFrom);
157  }
158 
159  return sqrt(weight);
160 }
void CreateMSTGraph(bool draw)
Creates a MST GRAPH.
virtual te::graph::Edge * getNextEdge()
It returns a pointer to the next edge element of a graph.
void setAttributeVecSize(int size)
This function is used to set the number of attributes associated with the edge elements.
Definition: Edge.cpp:86
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
A Mark specifies a geometric shape and applies coloring to it.
Definition: Mark.h:84
Utilitary function for spatial analysis module.
An static class with global definitions for the TerraLib Graph Module.
#define TE_SA_SKATER_ATTR_WEIGHT_NAME
double CalculateWeight(std::vector< int > attrsIdx, te::graph::Vertex *vFrom, te::graph::Vertex *vTo)
A PointSymbolizer specifies the rendering of a graphic Symbolizer at a point.
This file contains include headers for TerraLib Symbology Encoding module.
void push_back(const std::string &semanticTypeIdentifier)
Definition: Style.cpp:75
static te::dt::Date ds(2010, 01, 01)
te::sa::GeneralizedProximityMatrix * importFromGAL(std::string pathFileName, te::da::DataSource *ds=0)
Function used to import a gpm from a Spatial Weights File GAL Format.
te::se::Style * getMSTGraphStyle()
std::vector< te::dt::AbstractData * > & getAttributes()
It returns the vector of attributes associated with this element.
Definition: Vertex.cpp:74
TESEEXPORT LineSymbolizer * CreateLineSymbolizer(Stroke *stroke)
Creates a line symbolizer.
A Graphic is a graphic symbol with an inherent shape, color(s), and possibly size.
Definition: Graphic.h:66
void push_back(Symbolizer *s)
Definition: Rule.cpp:138
This class defines functions used to load and save gpm&#39;s using GAL and GWT formats, both formats use a &#39; &#39; as separator.
Class used to define the graph metadata informations.
This file contains a class that represents the Minimum Spanning Tree operation.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
Abstract class used to define the main functions of graph struct. All graph implementations must used...
virtual te::graph::GraphMetadata * getMetadata()=0
Function used to access the graph metadata.
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.
The FeatureTypeStyle defines the styling that is to be applied to a dataset that can be viewed as a f...
A simple main window to show example of TerraLib Qt Tools.
Definition: DisplayWindow.h:80
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
void addGraph(te::graph::AbstractGraph *graph, te::gm::Envelope extent, te::se::Style *s=0)
TESEEXPORT Graphic * CreateGraphic(Mark *mark, const std::string &size, const std::string &rotation, const std::string &opacity)
Creates a graphic.
A Fill specifies the pattern for filling an area geometry.
Definition: Fill.h:59
virtual void setSRID(int srid)
Used to set the SRID of the geometry elements associated with this graph.
int getIdFrom()
It returns the vertex origin identification.
Definition: Edge.cpp:71
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Utility functions for the data access module.
TESAEXPORT int AssociateGPMVertexAttribute(te::sa::GeneralizedProximityMatrix *gpm, te::da::DataSource *ds, std::string dataSetName, std::string attrLink, std::string attr, int dataType, int srid=TE_UNKNOWN_SRS, int subType=static_cast< int >(te::gm::UnknownGeometryType))
Function used to set a an attribute valeu from a dataset to the vertex objects from a gpm...
TESEEXPORT Mark * CreateMark(const std::string &wellKnownName, Stroke *stroke, Fill *fill)
Creates a mark.
void addVectorialLayer(std::string path, int srid)
virtual te::graph::Edge * getFirstEdge()
It returns a pointer to the first edge element of a graph.
TESEEXPORT Stroke * CreateStroke(const std::string &color, const std::string &width)
Creates a stroke.
void CreateWeightAttribute(te::graph::AbstractGraph *graph, int weightAttrIdx, std::vector< int > attrsIdx)
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:76
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
A LineSymbolizer is used to style a stroke along a linear geometry type, such as a string of line seg...
virtual int getEdgePropertySize()
Used to verify the number of properties associated to edge elements.
This class defines functions used to load and save gpm&#39;s using GAL and GWT formats, both formats use a &#39; &#39; as separator.
A simple main window to show example of TerraLib Graph.
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
TESAEXPORT int AddGraphEdgeAttribute(te::graph::AbstractGraph *graph, std::string attrName, int dataType)
Function used to create the edge attribute metadata in the graph of the gpm.
TESAEXPORT double GetDataValue(te::dt::AbstractData *ad)
Function used to get the numeric value from a gpm property.
This file contains several implementations for atomic data types (integers, floats, strings and others).
These routines show how to use the Graph module.
virtual te::graph::Vertex * getVertex(int id)=0
It returns the vertex element if it&#39;s exist.
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Edge.cpp:91
std::unique_ptr< te::da::DataSource > OpenOGRDataSource(const std::string &pathName)
Auxiliar functions for load a org data source.
TESEEXPORT PointSymbolizer * CreatePointSymbolizer(Graphic *graphic)
Creates a point symbolizer.
TESEEXPORT Fill * CreateFill(const std::string &color, const std::string &opacity)
Creates a fill.
int getIdTo()
It returns the vertex destiny identification.
Definition: Edge.cpp:76
This class defines the GPM class.