All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RAGGraphBuilder.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 RAGGraphBuilder.cpp
22 
23  \brief This class defines the RAG strategy to build a graph,
24 
25  This is strategy is based on adjacency of a region.
26 
27 */
28 
29 // TerraLib
30 #include "../../common/progress/TaskProgress.h"
31 #include "../../common/StringUtils.h"
32 #include "../../common/STLUtils.h"
33 #include "../../common/Translator.h"
34 #include "../../dataaccess/datasource/DataSource.h"
35 #include "../../dataaccess/datasource/DataSourceFactory.h"
36 #include "../../datatype/AbstractData.h"
37 #include "../../geometry/GeometryProperty.h"
38 #include "../../geometry/MultiPolygon.h"
39 #include "../../geometry/Point.h"
40 #include "../../geometry/Polygon.h"
41 #include "../../sam/rtree.h"
42 #include "../core/AbstractGraphFactory.h"
43 #include "../core/Edge.h"
44 #include "../core/Vertex.h"
45 #include "../core/VertexProperty.h"
46 #include "../graphs/Graph.h"
47 #include "../Config.h"
48 #include "../Exception.h"
49 #include "RAGGraphBuilder.h"
50 
51 
53 {
54  m_edgeId = 0;
55 }
56 
58 {
59 }
60 
61 bool te::graph::RAGGraphBuilder::build(const std::string& shapeFileName, const std::string& linkColumn, const int& srid,
62  const std::map<std::string, std::string>& dsInfo, const std::string& graphType, const std::map<std::string, std::string>& gInfo)
63 {
64  //create output graph
65  m_graph.reset(te::graph::AbstractGraphFactory::make(graphType, dsInfo, gInfo));
66 
67  assert(m_graph);
68 
69  if(createVertexObjects(shapeFileName, linkColumn, srid) == false)
70  {
71  return false;
72  }
73 
74  if(createEdgeObjects(shapeFileName, linkColumn) == false)
75  {
76  return false;
77  }
78 
79 
80  return true;
81 }
82 
84 {
85  int id = m_edgeId;
86 
87  m_edgeId++;
88 
89  return id;
90 }
91 
92 
93 std::auto_ptr<te::da::DataSource> te::graph::RAGGraphBuilder::getDataSource(const std::string fileName)
94 {
95  // Creates and connects data source
96  std::map<std::string, std::string> connInfo;
97  connInfo["path"] = fileName;
98  std::auto_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("OGR");
99  ds->setConnectionInfo(connInfo);
100  ds->open();
101 
102  return ds;
103 }
104 
106 {
107  std::vector<std::string> names = ds->getDataSetNames();
108 
109  std::string dsName = names[0];
110 
111  std::auto_ptr<te::da::DataSet> dataset = ds->getDataSet(dsName);
112 
113  return dataset;
114 }
115 
116 boost::ptr_vector<te::dt::Property> te::graph::RAGGraphBuilder::getProperties(te::da::DataSource* ds)
117 {
118  std::vector<std::string> names = ds->getDataSetNames();
119 
120  std::string dsName = names[0];
121 
122  return ds->getProperties(dsName);
123 }
124 
125 bool te::graph::RAGGraphBuilder::createVertexObjects(const std::string& shapeFileName, const std::string& linkColumn, const int& srid)
126 {
127  //get data source
128  std::auto_ptr<te::da::DataSource> ds = getDataSource(shapeFileName);
129 
130  if(ds.get() == 0)
131  {
132  return false;
133  }
134 
135  //get data set
136  std::auto_ptr<te::da::DataSet> dataSet = getDataSet(ds.get());
137 
138  if(dataSet.get() == 0)
139  {
140  return false;
141  }
142 
143  //create graph vertex attrs
144  te::gm::GeometryProperty* gProp = new te::gm::GeometryProperty("coords");
145  gProp->setId(0);
147  gProp->setSRID(srid);
148 
149  m_graph->addVertexProperty(gProp);
150 
151  //get geometry column
152  std::string geomColumn = "";
153  if(getGeometryColumn(shapeFileName, geomColumn) == false)
154  {
155  return false;
156  }
157 
158  //create vertex objects
159  while(dataSet->moveNext())
160  {
161  int id = dataSet->getInt32(linkColumn);
162 
163  Vertex* v = new Vertex(id);
164 
165  v->setAttributeVecSize(1);
166 
167  te::gm::Geometry* g = dataSet->getGeometry(geomColumn).release();
168  g->setSRID(srid);
169 
170  te::dt::AbstractData* ad = 0;
171 
172  if(g->getGeomTypeId() == te::gm::PointType)
173  {
174  ad = g;
175  }
176  else if(g->getGeomTypeId() == te::gm::PolygonType)
177  {
178  te::gm::Point* p = ((te::gm::Polygon*)g)->getCentroid();
179  p->setSRID(srid);
180 
181  ad = p;
182  }
183  else if(g->getGeomTypeId() == te::gm::MultiPolygonType)
184  {
185  te::gm::Polygon* poly = (te::gm::Polygon*)((te::gm::MultiPolygon*)g)->getGeometryN(0);
186 
187  te::gm::Point* p = poly->getCentroid();
188  p->setSRID(srid);
189 
190  ad = p;
191  }
192 
193  v->addAttribute(0, ad);
194 
195  m_graph->add(v);
196  }
197 
198  return true;
199 }
200 
201 bool te::graph::RAGGraphBuilder::createEdgeObjects(const std::string& shapeFileName, const std::string& linkColumn)
202 {
203  //get data source
204  std::auto_ptr<te::da::DataSource> ds = getDataSource(shapeFileName);
205 
206  if(ds.get() == 0)
207  {
208  return false;
209  }
210 
211  //get data set
212  std::auto_ptr<te::da::DataSet> dataSet = getDataSet(ds.get());
213 
214  if(dataSet.get() == 0)
215  {
216  return false;
217  }
218 
219  //get geometry column
220  std::string geomColumn = "";
221  if(getGeometryColumn(shapeFileName, geomColumn) == false)
222  {
223  return false;
224  }
225 
227  std::map<int, te::gm::Geometry*> geomMap;
228  //create tree
229  while(dataSet->moveNext())
230  {
231  int id = dataSet->getInt32(linkColumn);
232  te::gm::Geometry* g = dataSet->getGeometry(geomColumn).release();
233  const te::gm::Envelope* box = g->getMBR();
234 
235  rtree.insert(*box, id);
236 
237  geomMap.insert(std::map<int, te::gm::Geometry*>::value_type(id, g));
238  }
239 
240  dataSet->moveBeforeFirst();
241 
243 
244  task.setTotalSteps(dataSet->size());
245  task.setMessage("RAG Builder - Extracting Edges");
246 
247  //create vertex objects
248  while(dataSet->moveNext())
249  {
250  int vFromId = dataSet->getInt32(linkColumn);
251 
252  std::auto_ptr<te::gm::Geometry> g = dataSet->getGeometry(geomColumn);
253 
254  std::vector<int> results;
255 
256  rtree.search(*g->getMBR(), results);
257 
258  for(size_t t = 0; t < results.size(); ++t)
259  {
260  std::map<int, te::gm::Geometry*>::iterator it = geomMap.find(results[t]);
261 
262  if(it != geomMap.end())
263  {
264  if(g->touches(it->second))
265  {
266  int edgeId = getEdgeId();
267 
268  int vToId = results[t];
269 
270  Edge* e = new Edge(edgeId, vFromId, vToId);
271 
272  m_graph->add(e);
273  }
274  }
275  }
276 
277  task.pulse();
278  }
279 
280  te::common::FreeContents(geomMap);
281  geomMap.clear();
282 
283  return true;
284 }
285 
286 bool te::graph::RAGGraphBuilder::getGeometryColumn(const std::string& shapeFileName, std::string& columnName)
287 {
288  //get data source
289  std::auto_ptr<te::da::DataSource> ds = getDataSource(shapeFileName);
290 
291  if(ds.get() == 0)
292  {
293  return false;
294  }
295 
296  //get properties
297  boost::ptr_vector<te::dt::Property> properties = getProperties(ds.get());
298 
299  if(properties.empty())
300  {
301  return false;
302  }
303 
304  boost::ptr_vector<te::dt::Property>::iterator it = properties.begin();
305  while(it != properties.end())
306  {
307  if(it->getType() == te::dt::GEOMETRY_TYPE)
308  {
309  columnName = it->getName();
310  }
311 
312  ++it;
313  }
314 
315  if(columnName.empty())
316  {
317  return false;
318  }
319 
320  return true;
321 }
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
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.
std::auto_ptr< te::da::DataSet > getDataSet(te::da::DataSource *ds)
Function used to get the data set with the vectorial data.
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.
virtual boost::ptr_vector< te::dt::Property > getProperties(const std::string &datasetName)
It retrieves the properties of the dataset.
Definition: DataSource.cpp:168
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
bool getGeometryColumn(const std::string &shapeFileName, std::string &columnName)
Function used to get the geometry column name.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:118
This class defines the RAG strategy to build a graph,.
int m_edgeId
Attribute used as a index counter for edge objects.
int getEdgeId()
Function used to generated the edge id.
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
bool build(const std::string &shapeFileName, const std::string &linkColumn, const int &srid, const std::map< std::string, std::string > &dsInfo, const std::string &graphType, const std::map< std::string, std::string > &gInfo)
Function used to generated a graph using the RAG strategy.
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void setSRID(int srid)
It sets the Spatial Reference System ID of the Point.
boost::ptr_vector< te::dt::Property > getProperties(te::da::DataSource *ds)
Function used to get the data source properties.
static std::auto_ptr< DataSource > make(const std::string &dsType)
bool createVertexObjects(const std::string &shapeFileName, const std::string &linkColumn, const int &srid)
Function used to create all vertex object based on vectorial data.
virtual std::auto_ptr< DataSet > getDataSet(const std::string &name, te::common::TraverseType travType=te::common::FORWARDONLY, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It gets the dataset identified by the given name. This method always returns a disconnected dataset...
Definition: DataSource.cpp:61
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
std::auto_ptr< te::da::DataSource > getDataSource(const std::string fileName)
Function used to get the data source with the vectorial data.
virtual std::vector< std::string > getDataSetNames()
It gets the dataset names available in the data source.
Definition: DataSource.cpp:144
int search(const te::gm::Envelope &mbr, std::vector< DATATYPE > &report) const
Range search query.
Definition: Index.h:326
virtual ~RAGGraphBuilder()
Virtual destructor.
GeomType getGeomTypeId() const
It returns the geometry subclass type identifier.
Definition: Geometry.h:178
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
void insert(const te::gm::Envelope &mbr, const DATATYPE &data)
It inserts an item into the tree.
Definition: Index.h:313
Point * getCentroid() const
It returns the mathematical centroid for this surface as a point.
RAGGraphBuilder()
Default constructor.
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.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
Definition: Geometry.cpp:104
This abstract class provides the common functions for graph builder classes. Each builder strategy ha...
virtual void setSRID(int srid)=0
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
bool createEdgeObjects(const std::string &shapeFileName, const std::string &linkColumn)
Function used to create all edges object based on vectorial data.