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