All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Interpolate.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2011 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/serialization/se/Interpolate.cpp
22 
23  \brief Support for Interpolate serialization.
24 */
25 
26 // TerraLib
27 #include "../../se/Interpolate.h"
28 #include "../../xml/Reader.h"
29 #include "../../xml/Writer.h"
30 #include "Interpolate.h"
31 #include "InterpolationPoint.h"
32 #include "ParameterValue.h"
33 #include "Utils.h"
34 
35 // STL
36 #include <cassert>
37 #include <memory>
38 #include <vector>
39 
41 {
42  assert(reader.getNodeType() == te::xml::START_ELEMENT);
43  assert(reader.getElementLocalName() == "Interpolate");
44 
45  assert(reader.hasAttrs());
46 
47  std::auto_ptr<te::se::Interpolate> interpolate(new te::se::Interpolate);
48 
49  // FallBackValue Attribute
50  std::string fbv = reader.getAttr("fallbackValue");
51  assert(!fbv.empty());
52  interpolate->setFallbackValue(fbv);
53 
54  // Mode Attribute
55  std::string mode = reader.getAttr("mode");
56  assert(!mode.empty() && (mode == "linear" || mode == "cosine" || mode == "cubic"));
57  if(mode == "linear")
58  interpolate->setModeType(te::se::Interpolate::LINEAR);
59  else if(mode == "cosine")
60  interpolate->setModeType(te::se::Interpolate::COSINE);
61  else
62  interpolate->setModeType(te::se::Interpolate::CUBIC);
63 
64  // Method Attribute
65  std::string method = reader.getAttr("method");
66  assert(!method.empty() && (method == "numeric" || method == "color"));
67  method == "numeric" ? interpolate->setMethodType(te::se::Interpolate::NUMERIC) : interpolate->setMethodType(te::se::Interpolate::COLOR);
68 
69  reader.next();
70 
71  // LookupValue
72  assert(reader.getElementLocalName() == "LookupValue");
73  reader.next();
74  interpolate->setLookupValue(ReadParameterValue(reader));
75 
76  // InterpolationPoints
77  while(reader.getNodeType() == te::xml::START_ELEMENT &&
78  reader.getElementLocalName() == "InterpolationPoint")
79  {
80  //reader.next();
81  interpolate->add(ReadInterpolationPoint(reader));
82  }
83 
84  assert(reader.getNodeType() == te::xml::END_ELEMENT);
85  reader.next();
86 
87  return interpolate.release();
88 }
89 
90 void te::serialize::Save(const te::se::Interpolate* interpolate, te::xml::Writer& writer)
91 {
92  if(interpolate == 0)
93  return;
94 
95  writer.writeStartElement("se:Interpolate");
96 
97  writer.writeAttribute("fallbackValue", interpolate->getFallbackValue());
98 
99  te::se::Interpolate::ModeType modeType = interpolate->getModeType();
100  switch(modeType)
101  {
103  writer.writeAttribute("mode", "linear");
104  break;
105 
107  writer.writeAttribute("mode", "cosine");
108  break;
109 
111  writer.writeAttribute("mode", "cubic");
112  break;
113  }
114 
115  te::se::Interpolate::MethodType methodType = interpolate->geMethodType();
116  switch(methodType)
117  {
119  writer.writeAttribute("method", "numeric");
120  break;
121 
123  writer.writeAttribute("method", "color");
124  break;
125  }
126 
127  WriteParameterValuePtrHelper("se:LookupValue", interpolate->getLookupValue(), writer);
128 
129  const std::vector<te::se::InterpolationPoint*> ipts = interpolate->getInterpolationPoints();
130  assert(!ipts.empty());
131  for(std::size_t i = 0; i < ipts.size(); ++i)
132  Save(ipts[i], writer);
133 
134  writer.writeEndElement("se:Interpolate");
135 }
TESERIALIZATIONEXPORT te::se::ParameterValue * ReadParameterValue(te::xml::Reader &reader)
Utility methods for Symbology serialization.
TEDATAACCESSEXPORT void Save(const std::string &fileName)
Definition: Serializer.cpp:191
This class models a XML reader object.
Definition: Reader.h:55
void WriteParameterValuePtrHelper(const std::string &elementName, const te::se::ParameterValue *p, te::xml::Writer &writer)
Definition: Utils.cpp:46
virtual bool hasAttrs() const =0
It tells if the element has attributes in the case of an element node.
virtual bool next()=0
It gets the next event to be read.
const MethodType & geMethodType() const
Definition: Interpolate.h:159
ModeType
It controls the ...
Definition: Interpolate.h:97
The transformation of continuous values to a number of values (Interpolate function).
Definition: Interpolate.h:88
virtual void writeStartElement(const std::string &qName)
Definition: Writer.cpp:44
virtual void writeAttribute(const std::string &attName, const std::string &value)
Definition: Writer.cpp:90
virtual void writeEndElement(const std::string &qName)
Definition: Writer.cpp:156
TESERIALIZATIONEXPORT void Save(const te::fe::Filter *filter, te::xml::Writer &writer)
Definition: Filter.cpp:54
const ModeType & getModeType() const
Definition: Interpolate.h:155
const std::string & getFallbackValue() const
Definition: Function.cpp:42
TESERIALIZATIONEXPORT te::se::Interpolate * ReadInterpolate(te::xml::Reader &reader)
Definition: Interpolate.cpp:40
const std::vector< InterpolationPoint * > & getInterpolationPoints() const
Definition: Interpolate.cpp:91
virtual NodeType getNodeType() const =0
It return the type of node read.
Support for InterpolationPoint serialization.
virtual std::string getElementLocalName() const =0
It returns the local part of the element name in the case of an element node.
TESERIALIZATIONEXPORT te::se::InterpolationPoint * ReadInterpolationPoint(te::xml::Reader &reader)
ParameterValue * getLookupValue() const
Definition: Interpolate.h:149
MethodType
It controls the ...
Definition: Interpolate.h:109
The transformation of continuous values to a number of values (Interpolate function).
virtual std::string getAttr(const std::string &name) const =0
It returns the attribute value in the case of an element node with valid attributes.
This class models a XML writer object.
Definition: Writer.h:52