All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AddRasterAttribute.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 AddRasterAttribute.cpp
22 
23  \brief This class defines a function used to add to a graph the raster pixel value as attribute
24 
25 */
26 
27 // Terralib
28 #include "../../common/Translator.h"
29 #include "../../common/progress/TaskProgress.h"
30 #include "../../datatype/SimpleProperty.h"
31 #include "../../datatype/SimpleData.h"
32 #include "../../datatype/Enums.h"
33 #include "../../geometry/Coord2D.h"
34 #include "../../geometry/Point.h"
35 #include "../../raster/Grid.h"
36 #include "../../raster/Raster.h"
37 #include "../core/AbstractGraph.h"
38 #include "../core/Edge.h"
39 #include "../core/GraphMetadata.h"
40 #include "../core/Vertex.h"
41 #include "../core/VertexProperty.h"
42 #include "../graphs/Graph.h"
43 #include "../iterator/SequenceIterator.h"
44 #include "../Exception.h"
45 #include "AddRasterAttribute.h"
46 
47 
48 te::graph::AddRasterAttribute::AddRasterAttribute(te::graph::AbstractGraph* graph, std::string spatialAttributeName, std::string attributeName, te::rst::Raster* raster, int band)
49 {
50  //clear graph cache
51  graph->flush();
52 
53  //add new attribute
55  p->setParent(0);
56  p->setId(0);
57 
58  graph->addVertexProperty(p);
59 
60  // verify what the index of the new property
61  int pIdx = 0;
62 
63  for(int i = 0; i < graph->getVertexPropertySize(); ++ i)
64  {
65  if(graph->getVertexProperty(i)->getName() == attributeName)
66  {
67  pIdx = i;
68  break;
69  }
70  }
71 
72  //iterator for all vertex objects
74 
75  te::graph::Vertex* vertex = it->getFirstVertex();
76 
78 
80  task.setMessage("Add Raster Attribute Operation");
81 
82  int spatialPropertyId = -1;
83 
84  for(int i = 0; i < graph->getMetadata()->getVertexPropertySize(); ++i)
85  {
86  if(graph->getMetadata()->getVertexProperty(i)->getName() == spatialAttributeName)
87  {
88  spatialPropertyId = i;
89  break;
90  }
91  }
92 
93  while(it->isVertexIteratorAfterEnd() == false)
94  {
95  if(vertex)
96  {
97  te::gm::Point* p = dynamic_cast<te::gm::Point*>(vertex->getAttributes()[spatialPropertyId]);
98 
99  if(p)
100  {
101  te::gm::Coord2D coord = raster->getGrid()->geoToGrid(p->getX(), p->getY());
102 
103  double pixelValue;
104 
105  raster->getValue((int)coord.x, (int)coord.y, pixelValue, band);
106 
107  vertex->addAttribute(pIdx, new te::dt::SimpleData<int, te::dt::INT32_TYPE>((int)pixelValue));
108 
109  //set the vertex as durty
110  graph->update(vertex);
111  }
112  }
113 
114  vertex = it->getNextVertex();
115 
116  task.pulse();
117  }
118 
119  delete it;
120 
121  //clear graph cache
122  graph->flush();
123 }
124 
126 {
127 }
virtual te::graph::Vertex * getFirstVertex()
It returns a pointer to the first vertex element of a graph.
virtual void update(Vertex *v)=0
Update the vertex element.
void setMessage(const std::string &message)
Set the task message.
double y
y-coordinate.
Definition: Coord2D.h:114
virtual te::dt::Property * getVertexProperty(int idx)
Get a vertex property given a index.
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 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
std::vector< te::dt::AbstractData * > & getAttributes()
It returns the vector of attributes associated with this element.
Definition: Vertex.cpp:74
virtual te::dt::Property * getVertexProperty(int idx)=0
Get a vertex property given a index.
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
Definition: Grid.cpp:307
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
virtual te::graph::GraphMetadata * getMetadata()=0
Function used to access the graph metadata.
AddRasterAttribute(te::graph::AbstractGraph *graph, std::string spatialAttributeName, std::string attributeName, te::rst::Raster *raster, int band)
Default constructor.
virtual ~AddRasterAttribute()
Virtual destructor.
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
An abstract class for raster data strucutures.
Definition: Raster.h:71
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
This class defines a function used to add to a graph the raster pixel value as attribute.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
Grid * getGrid()
It returns the raster grid.
Definition: Raster.cpp:94
virtual te::graph::Vertex * getNextVertex()
It returns a pointer to the next vertex element of a graph.
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.
Definition: Raster.cpp:228
virtual int getVertexPropertySize()=0
Used to verify the number of properties associated to vertex elements.
virtual size_t getVertexInteratorCount()
It returns the number of elements of this iterator.
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Vertex.cpp:84
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
virtual void flush()=0
Function used to clear the memory cache, all elements was released from memory, if any element was ch...
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:136
virtual bool isVertexIteratorAfterEnd()
Used to check the iterator position.
const std::string & getName() const
It returns the property name.
Definition: Property.h:127
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177