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