All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.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 terralib/sa/core/Utils.cpp
22 
23  \brief Utilitary function for spatial analysis module.
24 */
25 
26 // TerraLib
27 #include "../../dataaccess/dataset/DataSet.h"
28 #include "../../dataaccess/dataset/DataSetType.h"
29 #include "../../dataaccess/datasource/DataSource.h"
30 #include "../../dataaccess/datasource/DataSourceFactory.h"
31 #include "../../dataaccess/datasource/DataSourceInfoManager.h"
32 #include "../../dataaccess/datasource/DataSourceManager.h"
33 #include "../../dataaccess/utils/Utils.h"
34 #include "../../datatype/AbstractData.h"
35 #include "../../datatype/SimpleProperty.h"
36 #include "../../geometry/GeometryProperty.h"
37 #include "../../geometry/MultiPolygon.h"
38 #include "../../geometry/Point.h"
39 #include "../../geometry/Polygon.h"
40 #include "../../graph/core/AbstractGraph.h"
41 #include "../../graph/core/GraphMetadata.h"
42 #include "../../graph/core/Vertex.h"
43 #include "../../maptools/AbstractLayer.h"
44 #include "../../maptools/DataSetLayer.h"
45 #include "../../raster/Grid.h"
46 #include "../../raster/RasterProperty.h"
47 #include "../../se/Utils.h"
49 #include "Utils.h"
50 
51 // STL
52 #include <cassert>
53 
54 // Boost
55 #include <boost/uuid/random_generator.hpp>
56 #include <boost/uuid/uuid_io.hpp>
57 
58 int te::sa::AssociateGPMVertexAttribute(te::sa::GeneralizedProximityMatrix* gpm, te::da::DataSource* ds, std::string dataSetName, std::string attrLink, std::string attr, int dataType, int srid, int subType)
59 {
60  assert(ds);
61 
62  std::auto_ptr<te::da::DataSet> dataSet = ds->getDataSet(dataSetName);
63 
64  return AssociateGPMVertexAttribute(gpm, dataSet.get(), attrLink, attr, dataType, srid, subType);
65 }
66 
67 int te::sa::AssociateGPMVertexAttribute(te::sa::GeneralizedProximityMatrix* gpm, te::da::DataSet* dataSet, std::string attrLink, std::string attr, int dataType, int srid, int subType)
68 {
69  assert(dataSet);
70  assert(gpm);
71 
72  te::graph::AbstractGraph* graph = gpm->getGraph();
73 
74  //add graph attr
75  int attrGraphIdx = te::sa::AddGraphVertexAttribute(graph, attr, dataType, srid, subType);
76 
77  //get the number of attributes from graph
78  int attrSize = graph->getMetadata()->getVertexPropertySize();
79 
80  //dataset iterator
81  dataSet->moveBeforeFirst();
82 
83  while(dataSet->moveNext())
84  {
85  te::dt::AbstractData* ad = dataSet->getValue(attr).release();
86 
87  std::string strIdx = dataSet->getAsString(attrLink);
88 
89  int idx = atoi(strIdx.c_str());
90 
91  te::graph::Vertex* v = graph->getVertex(idx);
92 
93  if(v)
94  {
95  //resize attr vector
96  v->setAttributeVecSize(attrSize);
97 
98  //add attribute
99  v->addAttribute(attrGraphIdx, ad);
100  }
101  }
102 
103  return attrGraphIdx;
104 }
105 
106 int te::sa::AddGraphVertexAttribute(te::graph::AbstractGraph* graph, std::string attrName, int dataType, int srid, int subType)
107 {
108  assert(graph);
109 
110  //add new attribute
111  te::dt::Property* p = 0;
112 
113  if(dataType == te::dt::GEOMETRY_TYPE)
114  {
115  p = new te::gm::GeometryProperty(attrName, srid, (te::gm::GeomType)subType);
116  }
117  else
118  {
119  p = new te::dt::SimpleProperty(attrName, dataType);
120  }
121 
122  p->setParent(0);
123  p->setId(0);
124 
125  graph->addVertexProperty(p);
126 
127  // verify what the index of the new property
128  int idx = 0;
129 
130  for(int i = 0; i < graph->getVertexPropertySize(); ++ i)
131  {
132  if(graph->getVertexProperty(i)->getName() == attrName)
133  {
134  idx = i;
135  break;
136  }
137  }
138 
139  return idx;
140 }
141 
142 int te::sa::AddGraphEdgeAttribute(te::graph::AbstractGraph* graph, std::string attrName, int dataType)
143 {
144  assert(graph);
145 
146  //add new attribute
147  te::dt::SimpleProperty* p = new te::dt::SimpleProperty(attrName, dataType);
148  p->setParent(0);
149  p->setId(0);
150 
151  graph->addEdgeProperty(p);
152 
153  // verify what the index of the new property
154  int idx = 0;
155 
156  for(int i = 0; i < graph->getEdgePropertySize(); ++ i)
157  {
158  if(graph->getEdgeProperty(i)->getName() == attrName)
159  {
160  idx = i;
161  break;
162  }
163  }
164 
165  return idx;
166 }
167 
168 bool te::sa::GetGraphVertexAttrIndex(te::graph::AbstractGraph* graph, std::string attrName, int& index)
169 {
170  assert(graph);
171 
172  for(int i = 0; i < graph->getVertexPropertySize(); ++ i)
173  {
174  if(graph->getVertexProperty(i)->getName() == attrName)
175  {
176  index = i;
177  return true;
178  }
179  }
180 
181  return false;
182 }
183 
184 bool te::sa::GetGraphEdgeAttrIndex(te::graph::AbstractGraph* graph, std::string attrName, int& index)
185 {
186  assert(graph);
187 
188  for(int i = 0; i < graph->getEdgePropertySize(); ++ i)
189  {
190  if(graph->getEdgeProperty(i)->getName() == attrName)
191  {
192  index = i;
193  return true;
194  }
195  }
196 
197  return false;
198 }
199 
201 {
202  assert(ad);
203 
204  std::string strValue = ad->toString();
205 
206  return atof(strValue.c_str());
207 }
208 
210 {
211  double distance = std::numeric_limits<double>::max();
212 
213  std::auto_ptr<te::gm::Point> point(new te::gm::Point(coord.x, coord.y));
214 
215  point->setSRID(geom->getSRID());
216 
217  if(geom->getGeomTypeId() == te::gm::PointType)
218  {
219  te::gm::Point* p = ((te::gm::Point*)geom);
220 
221  distance = p->distance(point.get());
222  }
223  else if(geom->getGeomTypeId() == te::gm::PolygonType)
224  {
225  te::gm::Point* p = ((te::gm::Polygon*)geom)->getCentroid();
226 
227  distance = p->distance(point.get());
228 
229  delete p;
230  }
231  else if(geom->getGeomTypeId() == te::gm::MultiPolygonType)
232  {
233  te::gm::Point* p = ((te::gm::MultiPolygon*)geom)->getCentroid();
234 
235  distance = p->distance(point.get());
236 
237  delete p;
238  }
239 
240  return distance;
241 }
242 
244 {
245  assert(geom);
246 
247  te::gm::Coord2D coord;
248 
249  if(geom->getGeomTypeId() == te::gm::PointType)
250  {
251  te::gm::Point* p = ((te::gm::Point*)geom);
252 
253  coord.x = p->getX();
254  coord.y = p->getY();
255  }
256  else if(geom->getGeomTypeId() == te::gm::PolygonType)
257  {
258  te::gm::Point* p = ((te::gm::Polygon*)geom)->getCentroid();
259 
260  coord.x = p->getX();
261  coord.y = p->getY();
262 
263  delete p;
264  }
265  else if(geom->getGeomTypeId() == te::gm::MultiPolygonType)
266  {
267  te::gm::Point* p = ((te::gm::MultiPolygon*)geom)->getCentroid();
268 
269  coord.x = p->getX();
270  coord.y = p->getY();
271 
272  delete p;
273  }
274 
275  return coord;
276 }
277 
279 {
280  assert(geom);
281 
282  if(geom->getGeomTypeId() == te::gm::PolygonType)
283  {
284  return ((te::gm::Polygon*)geom)->getArea();
285  }
286  else if(geom->getGeomTypeId() == te::gm::MultiPolygonType)
287  {
288  return ((te::gm::MultiPolygon*)geom)->getArea();
289  }
290 
291  return 0.;
292 }
int getSRID() const
It returns the Spatial Reference System ID associated to this geometric object.
Definition: Geometry.h:189
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.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
Utility functions for the data access module.
double y
y-coordinate.
Definition: Coord2D.h:114
An atomic property like an integer or double.
virtual int getVertexPropertySize()
Used to verify the number of properties associated to vertex elements.
double x
x-coordinate.
Definition: Coord2D.h:113
This class defines a Generalized Proximity Matrix.
virtual void addEdgeProperty(te::dt::Property *p)=0
Add a new property associated to the edge element.
TESAEXPORT te::gm::Coord2D GetCentroidCoord(te::gm::Geometry *geom)
Function used to get centroid coord of a geometry.
Definition: Utils.cpp:243
virtual std::string toString() const =0
It returns the data value in a string notation.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:118
TESAEXPORT int AddGraphVertexAttribute(te::graph::AbstractGraph *graph, std::string attrName, int dataType, int srid=TE_UNKNOWN_SRS, int subType=te::gm::UnknownGeometryType)
Function used to create the vertex attribute metadata in the graph of the gpm.
Definition: Utils.cpp:106
virtual te::dt::Property * getVertexProperty(int idx)=0
Get a vertex property given a index.
It models a property definition.
Definition: Property.h:59
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
void setId(unsigned int id)
It sets the property identifier.
Definition: Property.h:118
virtual te::graph::GraphMetadata * getMetadata()=0
Function used to access the graph metadata.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
TESAEXPORT double GetArea(te::gm::Geometry *geom)
Function used to get area of a geometry.
Definition: Utils.cpp:278
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:150
A point with x and y coordinate values.
Definition: Point.h:50
virtual void addVertexProperty(te::dt::Property *p)=0
Add a new property associated to the vertex element.
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
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
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
Definition: DataSet.cpp:218
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
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
virtual int getEdgePropertySize()=0
Used to verify the number of properties associated to edge elements.
virtual int getVertexPropertySize()=0
Used to verify the number of properties associated to vertex elements.
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
virtual te::dt::Property * getEdgeProperty(int idx)=0
Get a edge property given a index.
TESAEXPORT bool GetGraphVertexAttrIndex(te::graph::AbstractGraph *graph, std::string attrName, int &index)
Function used to get the vertex attribute index in the graph of the gpm.
Definition: Utils.cpp:168
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
TESAEXPORT double CalculateDistance(te::gm::Geometry *geom, te::gm::Coord2D &coord)
Function used to calculate the distance from a coord to the center of a geometry. ...
Definition: Utils.cpp:209
TESAEXPORT int AddGraphEdgeAttribute(te::graph::AbstractGraph *graph, std::string attrName, int dataType)
Function used to create the edge attribute metadata in the graph of the gpm.
Definition: Utils.cpp:142
TESAEXPORT double GetDataValue(te::dt::AbstractData *ad)
Function used to get the numeric value from a gpm property.
Definition: Utils.cpp:200
TESAEXPORT int AssociateGPMVertexAttribute(te::sa::GeneralizedProximityMatrix *gpm, te::da::DataSource *ds, std::string dataSetName, std::string attrLink, std::string attr, int dataType, int srid=TE_UNKNOWN_SRS, int subType=te::gm::UnknownGeometryType)
Function used to set a an attribute valeu from a dataset to the vertex objects from a gpm...
Definition: Utils.cpp:58
virtual te::graph::Vertex * getVertex(int id)=0
It returns the vertex element if it's exist.
virtual double distance(const Geometry *const rhs) const
It returns the shortest distance between any two points in the two geometry objects.
Definition: Geometry.cpp:453
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:136
TESAEXPORT bool GetGraphEdgeAttrIndex(te::graph::AbstractGraph *graph, std::string attrName, int &index)
Function used to get the edge attribute index in the graph of the gpm.
Definition: Utils.cpp:184
virtual std::auto_ptr< te::dt::AbstractData > getValue(std::size_t i) const
Method for retrieving any other type of data value stored in the data source.
Definition: DataSet.cpp:151
const std::string & getName() const
It returns the property name.
Definition: Property.h:127
This class defines the GPM class.
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177