All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LDDGraphBuilder.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 LDDGraphBuilder.cpp
22 
23  \brief This class defines the LDD strategy to build a graph.
24 
25  This strategy is based on Serio Rosim method, using this
26  "mask" is possible extract a graph from a LDD image.
27 
28  -------------------
29  | 32 | 64 | 128 |
30  -------------------
31  | 16 | * | 1 |
32  -------------------
33  | 8 | 4 | 2 |
34  -------------------
35 
36 */
37 
38 // TerraLib
39 #include "../../common/Translator.h"
40 #include "../../common/progress/TaskProgress.h"
41 #include "../../geometry/Coord2D.h"
42 #include "../../geometry/GeometryProperty.h"
43 #include "../../geometry/Point.h"
44 #include "../../raster/Grid.h"
45 #include "../../raster/Raster.h"
46 #include "../core/AbstractGraphFactory.h"
47 #include "../core/Edge.h"
48 #include "../core/GraphMetadata.h"
49 #include "../core/Vertex.h"
50 #include "../core/VertexProperty.h"
51 #include "../graphs/Graph.h"
52 #include "../Config.h"
53 #include "../Exception.h"
54 #include "LDDGraphBuilder.h"
55 
57 {
58  m_edgeId = 0;
59 }
60 
62 {
63 }
64 
65 bool te::graph::LDDGraphBuilder::build(te::rst::Raster* raster, const std::map<std::string, std::string>& dsInfo, const std::string& graphType, const std::map<std::string, std::string>& gInfo)
66 {
67  m_raster = raster;
68 
69  //create output graph
70  m_graph.reset(te::graph::AbstractGraphFactory::make(graphType, dsInfo, gInfo));
71 
72  assert(m_graph);
73 
74  //create graph attrs
76  gProp->setId(0);
78  gProp->setSRID(raster->getSRID());
79 
80  m_graph->addVertexProperty(gProp);
81 
83 
84  t.setTotalSteps(m_raster->getNumberOfRows());
85  t.setMessage(TE_TR("LLD Graph Builder..."));
86  t.useTimer(true);
87 
88  //extract graph
89  for(std::size_t r = 1; r < m_raster->getNumberOfRows(); r++)
90  {
91  for(std::size_t c = 1; c < m_raster->getNumberOfColumns(); c++)
92  {
93  //get pixel value
94  double val = 255.;
95  m_raster->getValue(c, r, val);
96 
97  int value = (int) val;
98 
99  //create vertex
100  int vId;
101 
102  if(getVertexId(r, c, vId) == false)
103  {
104  continue;
105  }
106 
107  Vertex* v = new Vertex(vId);
108  v->setAttributeVecSize(m_graph->getVertexPropertySize());
109 
110  te::gm::Coord2D coord = m_raster->getGrid()->gridToGeo(c, r);
111  te::gm::Point* p = new te::gm::Point(coord.x, coord.y, gProp->getSRID());
112 
113  v->addAttribute(0, p);
114 
115  m_graph->add(v);
116 
117  //value to be ignored
118  if(value == 255 || value == 0)
119  {
120  continue;
121  }
122 
123  //create edge
124 
125  //right
126  if(value & 1)
127  {
128  int vFrom = vId;
129  int vTo;
130  if(getVertexId(r, c + 1, vTo))
131  {
132  int edgeId = getEdgeId();
133 
134  Edge* e = new Edge(edgeId, vFrom, vTo);
135 
136  m_graph->add(e);
137  }
138  }
139 
140  //lower right
141  if(value & 2)
142  {
143  int vFrom = vId;
144  int vTo;
145  if(getVertexId(r + 1, c + 1, vTo))
146  {
147  int edgeId = getEdgeId();
148 
149  Edge* e = new Edge(edgeId, vFrom, vTo);
150 
151  m_graph->add(e);
152  }
153  }
154 
155  //down
156  if(value & 4)
157  {
158  int vFrom = vId;
159  int vTo;
160  if(getVertexId(r + 1, c, vTo))
161  {
162  int edgeId = getEdgeId();
163 
164  Edge* e = new Edge(edgeId, vFrom, vTo);
165 
166  m_graph->add(e);
167  }
168  }
169 
170  //lower left
171  if(value & 8)
172  {
173  int vFrom = vId;
174  int vTo;
175  if(getVertexId(r + 1, c - 1, vTo))
176  {
177  int edgeId = getEdgeId();
178 
179  Edge* e = new Edge(edgeId, vFrom, vTo);
180 
181  m_graph->add(e);
182  }
183  }
184 
185  //left
186  if(value & 16)
187  {
188  int vFrom = vId;
189  int vTo;
190  if(getVertexId(r, c - 1, vTo))
191  {
192  int edgeId = getEdgeId();
193 
194  Edge* e = new Edge(edgeId, vFrom, vTo);
195 
196  m_graph->add(e);
197  }
198  }
199 
200  //upper left
201  if(value & 32)
202  {
203  int vFrom = vId;
204  int vTo;
205  if(getVertexId(r - 1, c - 1, vTo))
206  {
207  int edgeId = getEdgeId();
208 
209  Edge* e = new Edge(edgeId, vFrom, vTo);
210 
211  m_graph->add(e);
212  }
213  }
214 
215  //up
216  if(value & 64)
217  {
218  int vFrom = vId;
219  int vTo;
220  if(getVertexId(r - 1, c, vTo))
221  {
222  int edgeId = getEdgeId();
223 
224  Edge* e = new Edge(edgeId, vFrom, vTo);
225 
226  m_graph->add(e);
227  }
228  }
229 
230  //upper right
231  if(value & 128)
232  {
233  int vFrom = vId;
234  int vTo;
235  if(getVertexId(r - 1, c + 1, vTo))
236  {
237  int edgeId = getEdgeId();
238 
239  Edge* e = new Edge(edgeId, vFrom, vTo);
240 
241  m_graph->add(e);
242  }
243  }
244  }
245 
246  t.pulse();
247  }
248 
249  return true;
250 }
251 
252 bool te::graph::LDDGraphBuilder::getVertexId(int row, int col, int& id)
253 {
254  if( row < 1 || row > (int)m_raster->getNumberOfRows() - 1)
255  {
256  return false;
257  }
258 
259  if( col < 1 || row > (int)m_raster->getNumberOfColumns() - 1)
260  {
261  return false;
262  }
263 
264  if(m_raster)
265  {
266  id = (row * m_raster->getNumberOfColumns()) + col;
267 
268  return true;
269  }
270 
271  return false;
272 }
273 
275 {
276  int id = m_edgeId;
277 
278  m_edgeId++;
279 
280  return id;
281 }
void setAttributeVecSize(int size)
This function is used to set the number of attributes associated with the vertex elements.
Definition: Vertex.cpp:79
Geometric property.
bool getVertexId(int row, int col, int &id)
Function used to generated the vertex id based on raster coordenate.
void setMessage(const std::string &message)
Set the task message.
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
void setGeometryType(GeomType t)
It sets the geometry subtype.
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
void useTimer(bool flag)
Used to define if task use progress timer information.
virtual ~LDDGraphBuilder()
Virtual destructor.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
void setTotalSteps(int value)
Set the task total stepes.
void setId(unsigned int id)
It sets the property identifier.
Definition: Property.h:118
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
int getSRID() const
It returns the spatial reference system identifier associated to this property.
A point with x and y coordinate values.
Definition: Point.h:50
An abstract class for raster data strucutures.
Definition: Raster.h:71
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
int getSRID() const
Returns the raster spatial reference system identifier.
Definition: Raster.cpp:203
LDDGraphBuilder()
Default constructor.
bool build(te::rst::Raster *raster, const std::map< std::string, std::string > &dsInfo, const std::string &graphType, const std::map< std::string, std::string > &gInfo)
Function used to build the output graph based on input parameters.
This class defines the LDD strategy to build a graph.
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Vertex.cpp:84
static AbstractGraph * make()
It creates and returns an empty graph with default graph type.
This abstract class provides the common functions for graph builder classes. Each builder strategy ha...
int m_edgeId
Attribute used as a index counter for edge objects.
int getEdgeId()
Function used to generated the edge id.