src/terralib/sa/core/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::unique_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 = nullptr;
112 
113  if(dataType == te::dt::GEOMETRY_TYPE)
114  {
115  p = new te::gm::GeometryProperty(attrName, srid, static_cast<te::gm::GeomType>(subType));
116  }
117  else
118  {
119  p = new te::dt::SimpleProperty(attrName, dataType);
120  }
121 
122  p->setParent(nullptr);
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(nullptr);
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  te::gm::Point point(coord.x, coord.y);
212 
213  point.setSRID(geom->getSRID());
214 
215  return point.distance(geom);
216 }
217 
219 {
220  assert(geom);
221 
222  if(geom->getGeomTypeId() == te::gm::PolygonType)
223  {
224  return (static_cast<te::gm::Polygon*>(geom))->getArea();
225  }
226  else if(geom->getGeomTypeId() == te::gm::MultiPolygonType)
227  {
228  return (static_cast<te::gm::MultiPolygon*>(geom))->getArea();
229  }
230 
231  return 0.;
232 }
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.
double y
y-coordinate.
Definition: Coord2D.h:114
void setSRID(int srid) _NOEXCEPT_OP(true)
It sets the Spatial Reference System ID of the Point.
An atomic property like an integer or double.
virtual double distance(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns the shortest distance between any two points in the two geometry objects.
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.
GeomType getGeomTypeId() const _NOEXCEPT_OP(true)
It returns the geometry subclass type identifier.
virtual void addEdgeProperty(te::dt::Property *p)=0
Add a new property associated to the edge element.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
static te::dt::Date ds(2010, 01, 01)
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
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.
virtual std::string toString() const =0
It returns the data value in a string notation.
int getSRID() const _NOEXCEPT_OP(true)
It returns the Spatial Reference System ID associated to this geometric object.
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
te::gm::Polygon * p
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
TESAEXPORT int AddGraphVertexAttribute(te::graph::AbstractGraph *graph, std::string attrName, int dataType, int srid=TE_UNKNOWN_SRS, int subType=static_cast< int >(te::gm::UnknownGeometryType))
Function used to create the vertex attribute metadata in the graph of the gpm.
Utility functions for the data access module.
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=static_cast< int >(te::gm::UnknownGeometryType))
Function used to set a an attribute valeu from a dataset to the vertex objects from a gpm...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
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.
virtual std::unique_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...
virtual te::dt::Property * getEdgeProperty(int idx)=0
Get a edge property given a index.
virtual std::unique_ptr< te::dt::AbstractData > getValue(std::size_t i) const
Method for retrieving any other type of data value stored in the data source.
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.
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. ...
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.
TESAEXPORT double GetDataValue(te::dt::AbstractData *ad)
Function used to get the numeric value from a gpm property.
virtual te::graph::Vertex * getVertex(int id)=0
It returns the vertex element if it&#39;s exist.
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.
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