All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Categorize.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/Categorize.cpp
22 
23  \brief Support for Categorize serialization.
24 */
25 
26 // TerraLib
27 #include "../../se/Categorize.h"
28 #include "../../xml/Reader.h"
29 #include "../../xml/Writer.h"
30 #include "Categorize.h"
31 #include "ParameterValue.h"
32 #include "Utils.h"
33 
34 // STL
35 #include <cassert>
36 #include <memory>
37 #include <vector>
38 
40 {
41  assert(reader.getNodeType() == te::xml::START_ELEMENT);
42  assert(reader.getElementLocalName() == "Categorize");
43 
44  assert(reader.hasAttrs());
45 
46  std::auto_ptr<te::se::Categorize> c(new te::se::Categorize);
47 
48  // FallBackValue Attribute
49  std::string fbv = reader.getAttr("fallbackValue");
50  assert(!fbv.empty());
51  c->setFallbackValue(fbv);
52 
53  // ThreshholdsBelongTo Attribute
54  std::string tbt = reader.getAttr("threshholdsBelongTo");
55  assert(!tbt.empty() && (tbt == "preceding" || tbt == "succeeding"));
56  tbt == "preceding" ? c->setThresholdsBelongTo(te::se::Categorize::PRECEDING) : c->setThresholdsBelongTo(te::se::Categorize::SUCCEEDING);
57 
58  reader.next();
59 
60  // LookupValue
61  assert(reader.getElementLocalName() == "LookupValue");
62  reader.next();
63  c->setLookupValue(ReadParameterValue(reader));
64 
65  // Value
66  assert(reader.getElementLocalName() == "Value");
67  reader.next();
68  c->addValue(ReadParameterValue(reader));
69 
70  // Threshold + Value
71  while(reader.getNodeType() == te::xml::START_ELEMENT &&
72  reader.getElementLocalName() == "Threshold")
73  {
74  reader.next();
75  c->addThreshold(ReadParameterValue(reader));
76 
77  assert(reader.getElementLocalName() == "Value");
78  reader.next();
79  c->addValue(ReadParameterValue(reader));
80  }
81 
82  assert(reader.getNodeType() == te::xml::END_ELEMENT);
83  reader.next();
84 
85  return c.release();
86 }
87 
89 {
90  if(c == 0)
91  return;
92 
93  writer.writeStartElement("se:Categorize");
94 
95  writer.writeAttribute("fallbackValue", c->getFallbackValue());
96 
98  switch(type)
99  {
101  break;
102 
104  writer.writeAttribute("threshholdsBelongTo", "preceding");
105  break;
106 
108  writer.writeAttribute("threshholdsBelongTo", "succeeding");
109  break;
110  }
111 
112  WriteParameterValuePtrHelper("se:LookupValue", c->getLookupValue(), writer);
113  WriteParameterValuePtrHelper("se:Value", c->getValue(), writer);
114 
115  std::vector<te::se::ParameterValue*> thresholds = c->getThresholds();
116  std::vector<te::se::ParameterValue*> values = c->getThresholdValues();
117  assert(thresholds.size() == values.size()-1);
118 
119  for(std::size_t i = 0; i < values.size(); ++i)
120  {
121  WriteParameterValuePtrHelper("se:Value", values[i], writer);
122 
123  if(i != values.size()-1)
124  WriteParameterValuePtrHelper("se:Threshold", thresholds[i], writer);
125  }
126 
127  writer.writeEndElement("se:Categorize");
128 }
TESERIALIZATIONEXPORT te::se::ParameterValue * ReadParameterValue(te::xml::Reader &reader)
Utility methods for Symbology serialization.
const std::vector< ParameterValue * > & getThresholds() const
Definition: Categorize.cpp:109
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
The transformation of continuous values to distinct values (Categorize function). ...
Definition: Categorize.h:90
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.
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
const ThresholdsBelongToType & getThresholdsBelongTo() const
Definition: Categorize.h:147
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
ParameterValue * getValue() const
Definition: Categorize.h:139
const std::string & getFallbackValue() const
Definition: Function.cpp:42
TESERIALIZATIONEXPORT te::se::Categorize * ReadCategorize(te::xml::Reader &reader)
Definition: Categorize.cpp:39
const std::vector< ParameterValue * > & getThresholdValues() const
Definition: Categorize.cpp:114
virtual NodeType getNodeType() const =0
It return the type of node read.
The transformation of continuous values to distinct values (Categorize function). ...
virtual std::string getElementLocalName() const =0
It returns the local part of the element name in the case of an element node.
ParameterValue * getLookupValue() const
Definition: Categorize.h:135
ThresholdsBelongToType
It controls the interval order.
Definition: Categorize.h:99
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