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 }
59 
61 {
62 }
63 
64 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)
65 {
66  m_raster = raster;
67 
68  //create output graph
69  m_graph.reset(te::graph::AbstractGraphFactory::make(graphType, dsInfo, gInfo));
70 
71  assert(m_graph);
72 
73  //create graph attrs
75  gProp->setId(0);
77  gProp->setSRID(raster->getSRID());
78 
79  m_graph->addVertexProperty(gProp);
80 
82 
83  t.setTotalSteps(m_raster->getNumberOfRows());
84  t.setMessage(TE_TR("LLD Graph Builder..."));
85  t.useTimer(true);
86 
87  //extract graph
88  for(std::size_t r = 1; r < m_raster->getNumberOfRows(); r++)
89  {
90  for(std::size_t c = 1; c < m_raster->getNumberOfColumns(); c++)
91  {
92  //get pixel value
93  double val = 255.;
94  m_raster->getValue(c, r, val);
95 
96  int value = (int) val;
97 
98  //create vertex
99  int vId;
100 
101  if(getVertexId(r, c, vId) == false)
102  {
103  continue;
104  }
105 
106  Vertex* v = new Vertex(vId);
107  v->setAttributeVecSize(m_graph->getVertexPropertySize());
108 
109  te::gm::Coord2D coord = m_raster->getGrid()->gridToGeo(c, r);
110  te::gm::Point* p = new te::gm::Point(coord.x, coord.y, gProp->getSRID());
111 
112  v->addAttribute(0, p);
113 
114  m_graph->add(v);
115 
116  //value to be ignored
117  if(value == 255 || value == 0)
118  {
119  continue;
120  }
121 
122  //create edge
123 
124  //right
125  if(value & 1)
126  {
127  int vFrom = vId;
128  int vTo;
129  if(getVertexId(r, c + 1, vTo))
130  {
131  int edgeId = getEdgeId();
132 
133  Edge* e = new Edge(edgeId, vFrom, vTo);
134 
135  m_graph->add(e);
136  }
137  }
138 
139  //lower right
140  if(value & 2)
141  {
142  int vFrom = vId;
143  int vTo;
144  if(getVertexId(r + 1, c + 1, vTo))
145  {
146  int edgeId = getEdgeId();
147 
148  Edge* e = new Edge(edgeId, vFrom, vTo);
149 
150  m_graph->add(e);
151  }
152  }
153 
154  //down
155  if(value & 4)
156  {
157  int vFrom = vId;
158  int vTo;
159  if(getVertexId(r + 1, c, vTo))
160  {
161  int edgeId = getEdgeId();
162 
163  Edge* e = new Edge(edgeId, vFrom, vTo);
164 
165  m_graph->add(e);
166  }
167  }
168 
169  //lower left
170  if(value & 8)
171  {
172  int vFrom = vId;
173  int vTo;
174  if(getVertexId(r + 1, c - 1, vTo))
175  {
176  int edgeId = getEdgeId();
177 
178  Edge* e = new Edge(edgeId, vFrom, vTo);
179 
180  m_graph->add(e);
181  }
182  }
183 
184  //left
185  if(value & 16)
186  {
187  int vFrom = vId;
188  int vTo;
189  if(getVertexId(r, c - 1, vTo))
190  {
191  int edgeId = getEdgeId();
192 
193  Edge* e = new Edge(edgeId, vFrom, vTo);
194 
195  m_graph->add(e);
196  }
197  }
198 
199  //upper left
200  if(value & 32)
201  {
202  int vFrom = vId;
203  int vTo;
204  if(getVertexId(r - 1, c - 1, vTo))
205  {
206  int edgeId = getEdgeId();
207 
208  Edge* e = new Edge(edgeId, vFrom, vTo);
209 
210  m_graph->add(e);
211  }
212  }
213 
214  //up
215  if(value & 64)
216  {
217  int vFrom = vId;
218  int vTo;
219  if(getVertexId(r - 1, c, vTo))
220  {
221  int edgeId = getEdgeId();
222 
223  Edge* e = new Edge(edgeId, vFrom, vTo);
224 
225  m_graph->add(e);
226  }
227  }
228 
229  //upper right
230  if(value & 128)
231  {
232  int vFrom = vId;
233  int vTo;
234  if(getVertexId(r - 1, c + 1, vTo))
235  {
236  int edgeId = getEdgeId();
237 
238  Edge* e = new Edge(edgeId, vFrom, vTo);
239 
240  m_graph->add(e);
241  }
242  }
243  }
244 
245  t.pulse();
246  }
247 
248  return true;
249 }
250 
251 bool te::graph::LDDGraphBuilder::getVertexId(int row, int col, int& id)
252 {
253  if( row < 1 || row > (int)m_raster->getNumberOfRows() - 1)
254  {
255  return false;
256  }
257 
258  if( col < 1 || row > (int)m_raster->getNumberOfColumns() - 1)
259  {
260  return false;
261  }
262 
263  if(m_raster)
264  {
265  id = (row * m_raster->getNumberOfColumns()) + col;
266 
267  return true;
268  }
269 
270  return false;
271 }
272 
274 {
275  int id = m_edgeId;
276 
277  m_edgeId++;
278 
279  return id;
280 }
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:346
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 getEdgeId()
Function used to generated the edge id.