All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Serializer.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/raster/serialization/xml/Serializer.cpp
22 
23  \brief Support for serialization of Raster information.
24 */
25 
26 // TerraLib
27 #include "../../../geometry/Envelope.h"
28 #include "../../../geometry/serialization/xml/Serializer.h"
29 #include "../../../xml/AbstractWriter.h"
30 #include "../../../xml/AbstractWriterFactory.h"
31 #include "../../../xml/Reader.h"
32 #include "../../Band.h"
33 #include "../../BandProperty.h"
34 #include "../../Grid.h"
35 #include "../../Raster.h"
36 #include "Serializer.h"
37 
38 // STL
39 #include <cassert>
40 #include <fstream>
41 #include <memory>
42 
43 // Boost
44 #include <boost/lexical_cast.hpp>
45 
46 void te::serialize::xml::ReadRasterInfo(std::map<std::string, std::string>& rinfo, te::xml::Reader& reader)
47 {
48  assert(reader.getNodeType() == te::xml::START_ELEMENT);
49  assert(reader.getElementLocalName() == "Info");
50 
51  reader.next();
52 
53  while(reader.getNodeType() == te::xml::START_ELEMENT &&
54  reader.getElementLocalName() == "kvp")
55  {
56  std::string k = reader.getAttr("k");
57  std::string v = reader.getAttr("v");
58 
59  rinfo[k] = v;
60 
61  reader.next();
62 
63  assert(reader.getNodeType() == te::xml::END_ELEMENT);
64  reader.next();
65  }
66 }
67 
68 void te::serialize::xml::SaveRasterInfo(std::map<std::string, std::string>& rinfo, te::xml::AbstractWriter& writer)
69 {
70  if(rinfo.empty())
71  return;
72 
73  writer.writeStartElement("Info");
74 
75  std::map<std::string, std::string>::const_iterator it = rinfo.begin();
76  std::map<std::string, std::string>::const_iterator itend = rinfo.end();
77 
78  while(it != itend)
79  {
80  writer.writeStartElement("kvp");
81  writer.writeAttribute("k", it->first);
82  writer.writeAttribute("v", it->second);
83  writer.writeEndElement("kvp");
84 
85  ++it;
86  }
87 
88  writer.writeEndElement("Info");
89 }
90 
91 void te::serialize::xml::Save(const te::rst::Raster* raster, const std::string& fileName)
92 {
93  std::fstream ostr(fileName.c_str(), std::ios_base::out);
94 
95  Save(raster, ostr);
96 
97  ostr.close();
98 }
99 
100 void te::serialize::xml::Save(const te::rst::Raster* raster, std::ostream& ostr)
101 {
102  std::auto_ptr<te::xml::AbstractWriter> w(te::xml::AbstractWriterFactory::make());
103 
104  Save(raster, *w.get());
105 }
106 
108 {
109  writer.writeStartDocument("UTF-8", "no");
110 
111  writer.writeStartElement("Raster");
112 
113  writer.writeAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
114  writer.writeAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema-instance");
115  writer.writeAttribute("xmlns", "http://www.terralib.org/schemas/raster");
116  writer.writeAttribute("xsd:schemaLocation", "http://www.terralib.org/schemas/raster C:/Users/gribeiro/Documents/terralib5/trunk/myschemas/terralib/raster/raster.xsd");
117  writer.writeAttribute("version", "5.0.0");
118  writer.writeAttribute("release", "2011-01-01");
119 
120  writer.writeElement("Name", raster->getName());
121 
122  Save(raster->getGrid(), writer);
123 
124  writer.writeStartElement("Bands");
125 
126  for(std::size_t i = 0; i < raster->getNumberOfBands(); ++i)
127  Save(raster->getBand(i)->getProperty(), writer);
128 
129  writer.writeEndElement("Bands");
130 
131  std::map<std::string, std::string> rinfo = raster->getInfo();
132 
133  SaveRasterInfo(rinfo, writer);
134 
135  writer.writeEndElement("Raster");
136 }
137 
139 {
140  assert(reader.getNodeType() == te::xml::START_ELEMENT);
141  assert(reader.getElementLocalName() == "GeoTransform");
142 
143  double* geo = new double[6];
144 
145  geo[0] = reader.getAttrAsDouble(0);
146  geo[1] = reader.getAttrAsDouble(1);
147  geo[2] = reader.getAttrAsDouble(2);
148  geo[3] = reader.getAttrAsDouble(3);
149  geo[4] = reader.getAttrAsDouble(4);
150  geo[5] = reader.getAttrAsDouble(5);
151 
152  reader.next();
153 
154  assert(reader.getNodeType() == te::xml::END_ELEMENT);
155  reader.next();
156 
157  return geo;
158 }
159 
161 {
162  assert(reader.getNodeType() == te::xml::START_ELEMENT);
163  assert(reader.getElementLocalName() == "Grid");
164 
165  reader.next();
166 
167  /* Extent Element */
168  assert(reader.getNodeType() == te::xml::START_ELEMENT);
169  assert(reader.getElementLocalName() == "Extent");
170  std::auto_ptr<te::gm::Envelope> e(ReadExtent(reader));
171 
172  /* NumCols Element */
173  assert(reader.getNodeType() == te::xml::START_ELEMENT);
174  assert(reader.getElementLocalName() == "NumCols");
175  reader.next();
176  assert(reader.getNodeType() == te::xml::VALUE);
177  unsigned int ncols = reader.getElementValueAsInt32();
178  reader.next();
179  assert(reader.getNodeType() == te::xml::END_ELEMENT);
180 
181  /* NumRows Element */
182  reader.next();
183  assert(reader.getNodeType() == te::xml::START_ELEMENT);
184  assert(reader.getElementLocalName() == "NumRows");
185  reader.next();
186  assert(reader.getNodeType() == te::xml::VALUE);
187  unsigned int nrows = reader.getElementValueAsInt32();
188  reader.next();
189  assert(reader.getNodeType() == te::xml::END_ELEMENT);
190 
191  /* SRID Element */
192  reader.next();
193  assert(reader.getNodeType() == te::xml::START_ELEMENT);
194  assert(reader.getElementLocalName() == "SRID");
195  reader.next();
196  assert(reader.getNodeType() == te::xml::VALUE);
197  int srid = reader.getElementValueAsInt32();
198  reader.next();
199  assert(reader.getNodeType() == te::xml::END_ELEMENT);
200 
201  /* GeoTransform Element */
202  reader.next();
203  double* geo = ReadGeoTransform(reader);
204 
205  te::rst::Grid* grid = new te::rst::Grid(ncols, nrows);
206 
207  grid->setGeoreference(geo, srid);
208 
209  delete [] geo;
210 
211  assert(reader.getNodeType() == te::xml::END_ELEMENT);
212  reader.next();
213 
214  return grid;
215 }
216 
218 {
219  writer.writeStartElement("Grid");
220 
221  const te::gm::Envelope* e = grid->getExtent();
222 
223  SaveExtent(*e, writer);
224 
225  writer.writeElement("NumCols", grid->getNumberOfColumns());
226  writer.writeElement("NumRows", grid->getNumberOfRows());
227  writer.writeElement("SRID", grid->getSRID());
228 
229  writer.writeStartElement("GeoTransform");
230  writer.writeAttribute("c1", grid->getGeoreference()[0]);
231  writer.writeAttribute("c2", grid->getGeoreference()[1]);
232  writer.writeAttribute("c3", grid->getGeoreference()[2]);
233  writer.writeAttribute("c4", grid->getGeoreference()[3]);
234  writer.writeAttribute("c5", grid->getGeoreference()[4]);
235  writer.writeAttribute("c6", grid->getGeoreference()[5]);
236  writer.writeEndElement("GeoTransform");
237 
238  writer.writeEndElement("Grid");
239 }
240 
241 std::vector<te::rst::BandProperty*> te::serialize::xml::ReadBandPropertyVector(te::xml::Reader& reader)
242 {
243  assert(reader.getNodeType() == te::xml::START_ELEMENT);
244  assert(reader.getElementLocalName() == "Bands");
245 
246  std::vector<te::rst::BandProperty*> bands;
247 
248  reader.next();
249  assert(reader.getNodeType() == te::xml::START_ELEMENT);
250  assert(reader.getElementLocalName() == "Band");
251 
252  while(reader.getNodeType() == te::xml::START_ELEMENT &&
253  reader.getElementLocalName() == "Band")
254  {
256  bands.push_back(bp);
257  }
258 
259  assert(reader.getNodeType() == te::xml::END_ELEMENT);
260  reader.next();
261 
262  return bands;
263 }
264 
266 {
267  assert(reader.getNodeType() == te::xml::START_ELEMENT);
268  assert(reader.getElementLocalName() == "Band");
269 
270  int num = reader.getAttrAsInt32(0);
271  int datatype = reader.getAttrAsInt32(1);
272  double noData = reader.getAttrAsDouble(2);
273 
274  reader.next();
275  assert(reader.getNodeType() == te::xml::START_ELEMENT);
276  assert(reader.getElementLocalName() == "BlockInfo");
277 
278  int blkw = reader.getAttrAsInt32(0);
279  int blkh = reader.getAttrAsInt32(1);
280  int nblocksx = reader.getAttrAsInt32(2);
281  int nblocksy = reader.getAttrAsInt32(3);
282 
283  reader.next();
284  assert(reader.getNodeType() == te::xml::END_ELEMENT); // End of BlockInfo Element
285 
286  reader.next();
287  assert(reader.getNodeType() == te::xml::END_ELEMENT); // End of Band Element
288 
289  reader.next();
290 
291  te::rst::BandProperty* bp = new te::rst::BandProperty(num, datatype);
292 
293  bp->m_blkw = blkw;
294  bp->m_blkh = blkh;
295  bp->m_nblocksx = nblocksx;
296  bp->m_nblocksy = nblocksy;
297  bp->m_noDataValue = noData;
298 
299  return bp;
300 }
301 
303 {
304  writer.writeStartElement("Band");
305 
306  writer.writeAttribute("num", boost::lexical_cast<std::string>(bp->m_idx));
307  writer.writeAttribute("datatype", bp->m_type);
308  writer.writeAttribute("no_data", bp->m_noDataValue);
309 
310  writer.writeStartElement("BlockInfo");
311  writer.writeAttribute("blkw", bp->m_blkw);
312  writer.writeAttribute("blkh", bp->m_blkh);
313  writer.writeAttribute("nblocksx", bp->m_nblocksx);
314  writer.writeAttribute("nblocksy", bp->m_nblocksy);
315  writer.writeEndElement("BlockInfo");
316 
317  //writer.writeElement(ostr, "Description", bp->m_description);
318 
319  writer.writeEndElement("Band");
320 }
321 
322 
323 
unsigned int getNumberOfRows() const
Returns the grid number of rows.
Definition: Grid.cpp:209
virtual std::map< std::string, std::string > getInfo() const =0
It returns additional information about the raster.
std::size_t m_idx
The band index.
Definition: BandProperty.h:132
virtual boost::int32_t getElementValueAsInt32() const
It returns the element data value in the case of VALUE node.
Definition: Reader.cpp:32
This class models a XML reader object.
Definition: Reader.h:55
TERASTEREXPORT te::rst::Grid * ReadGrid(te::xml::Reader &reader)
Definition: Serializer.cpp:160
A raster band description.
Definition: BandProperty.h:61
virtual void writeStartElement(const std::string &qName)=0
int getSRID() const
Returns the grid spatial reference system identifier.
Definition: Grid.cpp:265
int m_nblocksx
The number of blocks in x.
Definition: BandProperty.h:145
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
int m_nblocksy
The number of blocks in y.
Definition: BandProperty.h:146
static te::xml::AbstractWriter * make()
It creates a new XML writer using the dafault implementation.
void setGeoreference(const te::gm::Coord2D &ulLocation, int srid, double resX, double resY)
Sets the information needed to georeference the grid.
Definition: Grid.cpp:214
int m_type
The data type of the elements in the band.
Definition: BandProperty.h:133
TERASTEREXPORT void ReadRasterInfo(std::map< std::string, std::string > &rinfo, te::xml::Reader &reader)
Definition: Serializer.cpp:46
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
Definition: BandProperty.h:136
This class models a XML writer object.
TERASTEREXPORT double * ReadGeoTransform(te::xml::Reader &reader)
Definition: Serializer.cpp:138
virtual std::string getElementLocalName() const =0
It returns the local part of the element name in the case of an element node.
virtual boost::int32_t getAttrAsInt32(const std::string &name) const
It returns the attribute value in the case of an element node with valid attributes.
Definition: Reader.cpp:49
TERASTEREXPORT te::rst::BandProperty * ReadBandProperty(te::xml::Reader &reader)
Definition: Serializer.cpp:265
virtual void writeElement(const std::string &qName, const std::string &value)=0
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
An abstract class for raster data strucutures.
Definition: Raster.h:71
Auxiliary classes and functions to read geometry information from a XML document. ...
const double * getGeoreference() const
Returns a list of 6 coefficients describing an affine transformation to georeference a grid...
Definition: Grid.cpp:248
BandProperty * getProperty()
Returns the band property.
Definition: Band.cpp:428
int m_blkw
Block width (pixels).
Definition: BandProperty.h:143
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
TEGEOMEXPORT std::auto_ptr< te::gm::Envelope > ReadExtent(te::xml::Reader &reader)
Definition: Serializer.cpp:34
TEDATAACCESSEXPORT void Save(const std::string &fileName)
Definition: Serializer.cpp:201
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
Definition: Grid.cpp:193
TEGEOMEXPORT void SaveExtent(const te::gm::Envelope &e, te::xml::AbstractWriter &writer)
Definition: Serializer.cpp:52
Grid * getGrid()
It returns the raster grid.
Definition: Raster.cpp:94
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.
const std::string & getName() const
Returns the raster name.
Definition: Raster.cpp:79
virtual void writeAttribute(const std::string &attName, const std::string &value)=0
virtual NodeType getNodeType() const =0
It return the type of node read.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
Definition: Grid.cpp:275
TERASTEREXPORT std::vector< te::rst::BandProperty * > ReadBandPropertyVector(te::xml::Reader &reader)
Definition: Serializer.cpp:241
virtual double getAttrAsDouble(const std::string &name) const
It returns the attribute value in the case of an element node with valid attributes.
Definition: Reader.cpp:69
virtual void writeEndElement(const std::string &qName)=0
virtual void writeStartDocument(const std::string &encoding, const std::string &standalone)=0
int m_blkh
Block height (pixels).
Definition: BandProperty.h:144
TERASTEREXPORT void SaveRasterInfo(std::map< std::string, std::string > &rinfo, te::xml::AbstractWriter &writer)
Definition: Serializer.cpp:68
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
virtual bool next()=0
It gets the next event to be read.