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