All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.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/terralib4/Utils.cpp
22 
23  \brief Utilitary functions for dealing with TerraLib 5 and 4.x conversion.
24 */
25 
26 // TerraLib 5
27 #include "../common/Exception.h"
28 #include "../common/StringUtils.h"
29 #include "../common/Translator.h"
30 #include "../dataaccess/dataset/DataSetType.h"
31 #include "../datatype/NumericProperty.h"
32 #include "../datatype/Property.h"
33 #include "../datatype/StringProperty.h"
34 #include "../geometry/Enums.h"
35 #include "../geometry/Envelope.h"
36 #include "../geometry/GeometryProperty.h"
37 #include "../raster/BandProperty.h"
38 #include "../raster/Grid.h"
39 #include "../raster/RasterProperty.h"
40 #include "Utils.h"
41 
42 // TerraLib 4.x
43 #include <terralib4/kernel/TeBox.h>
44 #include <terralib4/kernel/TeDatabaseFactoryParams.h>
45 #include <terralib4/kernel/TeDecoderDatabase.h>
46 #include <terralib4/kernel/TeProjection.h>
47 #include <terralib4/kernel/TeTable.h>
48 #include <terralib4/kernel/TeRasterParams.h>
49 
50 // Boost
51 #include <boost/lexical_cast.hpp>
52 
53 std::auto_ptr<te::dt::Property> terralib4::Convert2T5(const TeAttributeRep& attRep)
54 {
55  std::string* defaultValue = attRep.defaultValue_.empty() ? 0 : new std::string(attRep.defaultValue_);
56 
57  bool isRequired = !attRep.null_;
58 
59  bool changed;
60  std::string normalName = te::common::ReplaceSpecialChars(attRep.name_, changed);
61 
62  switch(attRep.type_)
63  {
64  case TeSTRING:
65  if(attRep.numChar_ == 0)
66  return std::auto_ptr<te::dt::Property>(new te::dt::StringProperty(normalName, te::dt::STRING, 0, isRequired, defaultValue));
67  else
68  return std::auto_ptr<te::dt::Property>(new te::dt::StringProperty(normalName, te::dt::VAR_STRING, attRep.numChar_, isRequired, defaultValue));
69 
70  case TeCHARACTER:
71  return std::auto_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::CHAR_TYPE, isRequired, defaultValue));
72 
73  case TeBOOLEAN:
74  return std::auto_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::BOOLEAN_TYPE, isRequired, defaultValue));
75 
76  case TeINT:
77  return std::auto_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::INT32_TYPE, isRequired, defaultValue));
78 
79  case TeUNSIGNEDINT:
80  return std::auto_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::UINT32_TYPE, isRequired, defaultValue));
81 
82  case TeREAL:
83  return std::auto_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::DOUBLE_TYPE, isRequired, defaultValue));
84 
85  case TeDATETIME:
86  return std::auto_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::TIME_INSTANT, isRequired, defaultValue));
87 
88  case TeBLOB:
89  return std::auto_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::BYTE_ARRAY_TYPE, isRequired, defaultValue));
90 
91  case TePOINTTYPE:
92  return std::auto_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::PointType, isRequired, defaultValue));
93 
94  case TeLINE2DTYPE:
95  return std::auto_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::LineStringType, isRequired, defaultValue));
96 
97  case TePOLYGONTYPE:
98  return std::auto_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::PolygonType, isRequired, defaultValue));
99 
100  case TeCELLTYPE:
101  return std::auto_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::PolygonType, isRequired, defaultValue));
102 
103  case TePOINTSETTYPE:
104  return std::auto_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::MultiPointType, isRequired, defaultValue));
105 
106  case TeLINESETTYPE:
107  return std::auto_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::MultiLineStringType, isRequired, defaultValue));
108 
109  case TePOLYGONSETTYPE:
110  return std::auto_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::MultiPolygonType, isRequired, defaultValue));
111 
112  case TeCELLSETTYPE:
113  return std::auto_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::MultiPolygonType, isRequired, defaultValue));
114 
115  case TeRASTERTYPE:
116  return std::auto_ptr<te::dt::Property>(new te::rst::RasterProperty(normalName, isRequired));
117 
118  case TeUNKNOWN:
119  return std::auto_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::UNKNOWN_TYPE, isRequired));
120 
121  case TeNODETYPE:
122  case TeNODESETTYPE:
123  case TeTEXTTYPE:
124  case TeTEXTSETTYPE:
125  case TeOBJECT:
126  default:
127  throw te::common::Exception(TE_TR("The informed attribute representation is not supported!"));
128  }
129 }
130 
131 std::auto_ptr<TeDatabaseFactoryParams> terralib4::Convert2T4DatabaseParams(const std::map<std::string, std::string>& dsInfo)
132 {
133  std::auto_ptr<TeDatabaseFactoryParams> fparams(new TeDatabaseFactoryParams());
134 
135  fparams->dbms_name_ = dsInfo.at("T4_DRIVER");
136  fparams->database_ = dsInfo.at("T4_DB_NAME");
137  fparams->host_ = dsInfo.at("T4_HOST");
138  //fparams->port_ = boost::lexical_cast<int>(dsInfo.at("T4_PORT"));
139  fparams->user_ = dsInfo.at("T4_USER");
140  fparams->password_ = dsInfo.at("T4_PASSWORD");
141 
142  return fparams;
143 }
144 
145 int terralib4::Convert2T5(TeAttrDataType type)
146 {
147  switch(type)
148  {
149  case TeSTRING:
150  return te::dt::STRING_TYPE;
151 
152  case TeREAL:
153  return te::dt::DOUBLE_TYPE;
154 
155  case TeINT:
156  return te::dt::INT32_TYPE;
157 
158  case TeDATETIME:
159  return te::dt::DATETIME_TYPE;
160 
161  case TeBLOB:
163 
164  case TeCHARACTER:
165  return te::dt::CHAR_TYPE;
166 
167  case TeUNSIGNEDINT:
168  return te::dt::UINT32_TYPE;
169 
170  case TePOINTTYPE:
171  case TeNODETYPE:
172  case TeLINE2DTYPE:
173  case TePOLYGONTYPE:
174  case TeCELLTYPE:
175  case TePOINTSETTYPE:
176  case TeNODESETTYPE:
177  case TeLINESETTYPE:
178  case TePOLYGONSETTYPE:
179  case TeCELLSETTYPE:
180  return te::dt::GEOMETRY_TYPE;
181 
182  case TeRASTERTYPE:
183  return te::dt::RASTER_TYPE;
184 
185  case TeBOOLEAN:
186  return te::dt::BOOLEAN_TYPE;
187 
188  case TeUNKNOWN:
189  case TeOBJECT:
190  case TeTEXTTYPE:
191  case TeTEXTSETTYPE:
192  default:
193  return te::dt::UNKNOWN_TYPE;
194  }
195 }
196 
198 {
199  switch(type)
200  {
201  case TePOINTTYPE:
202  case TeNODETYPE:
203  return te::gm::PointType;
204 
205  case TeLINE2DTYPE:
206  return te::gm::LineStringType;
207 
208  case TePOLYGONTYPE:
209  case TeCELLTYPE:
210  return te::gm::PolygonType;
211 
212  case TePOINTSETTYPE:
213  case TeNODESETTYPE:
214  return te::gm::MultiPointType;
215 
216  case TeLINESETTYPE:
218 
219  case TePOLYGONSETTYPE:
220  case TeCELLSETTYPE:
222 
223  default:
225  }
226 }
227 
229 {
230  switch(type)
231  {
232  case TePOINTS:
233  case TeNODES:
234  return te::gm::PointType;
235 
236  case TeLINES:
237  return te::gm::LineStringType;
238 
239  case TePOLYGONS:
240  case TeCELLS:
241  return te::gm::PolygonType;
242 
243  default:
245  }
246 }
247 
248 int terralib4::Convert2T5(TeDataType dt)
249 {
250  switch(dt)
251  {
252  case TeBIT:
253  return te::dt::BIT_TYPE;
254 
255  case TeUNSIGNEDCHAR:
256  return te::dt::UCHAR_TYPE;
257 
258  case TeCHAR:
259  return te::dt::CHAR_TYPE;
260 
261  case TeUNSIGNEDSHORT:
262  return te::dt::UINT16_TYPE;
263 
264  case TeSHORT:
265  return te::dt::INT16_TYPE;
266 
267  case TeINTEGER:
268  return te::dt::INT32_TYPE;
269 
270  case TeUNSIGNEDLONG:
271  return te::dt::UINT32_TYPE;
272 
273  case TeLONG:
274  return te::dt::INT32_TYPE; // see TerraLib 4.x => TeRasterParams.cpp elementSize method => sizeof(long) => 4bytes!
275 
276  case TeFLOAT:
277  return te::dt::FLOAT_TYPE;
278 
279  case TeDOUBLE:
280  return te::dt::DOUBLE_TYPE;
281 
282  default:
283  return te::dt::UNKNOWN_TYPE;
284  }
285 }
286 
287 TeAttrDataType terralib4::Convert2T4(int type)
288 {
289  switch(type)
290  {
291  case te::dt::STRING_TYPE:
292  return TeSTRING;
293 
294  case te::dt::DOUBLE_TYPE:
296  return TeREAL;
297 
298  case te::dt::INT16_TYPE:
299  case te::dt::INT32_TYPE:
300  case te::dt::INT64_TYPE:
301  return TeINT;
302 
304  return TeDATETIME;
305 
307  return TeBLOB;
308 
309  case te::dt::CHAR_TYPE:
310  return TeCHARACTER;
311 
312  case te::dt::UINT16_TYPE:
313  case te::dt::UINT32_TYPE:
314  case te::dt::UINT64_TYPE:
315  return TeUNSIGNEDINT;
316 
317  case te::dt::RASTER_TYPE:
318  return TeRASTERTYPE;
319 
321  return TeBOOLEAN;
322 
324  default:
325  return TeUNKNOWN;
326  }
327 }
328 
330 {
331  switch(type)
332  {
333  case te::gm::PointType:
334  return TePOINTTYPE;
335 
337  return TeLINE2DTYPE;
338 
339  case te::gm::PolygonType:
340  return TePOLYGONTYPE;
341 
343  return TePOINTSETTYPE;
344 
346  return TeLINESETTYPE;
347 
349  return TePOLYGONSETTYPE;
350 
351  default:
352  return TeUNKNOWN;
353  }
354 }
355 
356 std::auto_ptr<te::gm::Envelope> terralib4::Convert2T5(TeBox box)
357 {
358  std::auto_ptr<te::gm::Envelope> env(new te::gm::Envelope(box.x1(), box.y1(), box.x2(), box.y2()));
359 
360  return env;
361 }
362 
363 std::auto_ptr<te::da::DataSetType> terralib4::Convert2T5(TeTable table)
364 {
365  TeAttributeList attrList = table.attributeList();
366 
367  std::auto_ptr<te::da::DataSetType> newDst(new te::da::DataSetType(table.name()));
368 
369  for(std::size_t i = 0; i < attrList.size(); ++i)
370  {
371  TeAttributeRep attr = attrList[i].rep_;
372 
373  std::auto_ptr<te::dt::Property> prop = terralib4::Convert2T5(attr);
374 
375  newDst->add(prop.release());
376  }
377 
378  return newDst;
379 }
380 
382 {
383  std::auto_ptr<te::rst::RasterProperty> rproperty(new te::rst::RasterProperty("raster"));
384 
385  unsigned int ncols = rparams.ncols_;
386  unsigned int nrows = rparams.nlines_;
387  std::auto_ptr<te::gm::Envelope> mbr(Convert2T5(rparams.boundingBox()));
388 
389  int srid = rparams.projection()->epsgCode();
390  if(srid == 4979)
391  srid = 4326;
392 
393  te::gm::Coord2D* cord = new te::gm::Coord2D(rparams.boundingBox().x1_-(rparams.resx_/2), rparams.boundingBox().y2_+(rparams.resy_/2));
394  std::auto_ptr<te::rst::Grid> grid(new te::rst::Grid(ncols, nrows, rparams.resx_, rparams.resy_, cord, srid));
395 
396  rproperty->set(grid.release());
397 
398  std::vector<te::rst::BandProperty::ColorEntry> palette;
399  if(rparams.photometric_[0] == TeRasterParams::TePallete)
400  {
401  std::size_t size = rparams.lutr_.size();
402 
403  for(std::size_t j = 0; j < size; ++j)
404  {
406  c.c1 = rparams.lutr_[j];
407  c.c2 = rparams.lutg_[j];
408  c.c3 = rparams.lutb_[j];
409  palette.push_back(c);
410  }
411  }
412 
413  for(int i = 0; i != rparams.nBands(); ++i)
414  {
415  te::rst::BandProperty* bp = new te::rst::BandProperty(i, Convert2T5(rparams.dataType_[i]));
416 
417  bp->m_blkh = rparams.blockHeight_;
418  bp->m_blkw = rparams.blockWidth_;
419  bp->m_noDataValue = rparams.dummy_[i];
420  bp->m_valuesOffset = rparams.offset_;
421 
422  if(!palette.empty())
423  bp->m_palette.assign(palette.begin(), palette.end());
424 
425  bp->m_nblocksx = (ncols + bp->m_blkw - 1) / bp->m_blkw;
426  bp->m_nblocksy = (nrows + bp->m_blkh - 1) / bp->m_blkh;
427 
428  rproperty->add(bp);
429  }
430 
431  return rproperty.release();
432 }
433 
TeAttrDataType Convert2T4GeomType(te::gm::GeomType type)
Definition: Utils.cpp:329
Geometric property.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
An atomic property like an integer or double.
std::vector< ColorEntry > m_palette
The color palette.
Definition: BandProperty.h:142
A raster band description.
Definition: BandProperty.h:61
A class that models the description of a dataset.
Definition: DataSetType.h:72
int m_nblocksx
The number of blocks in x.
Definition: BandProperty.h:145
int m_nblocksy
The number of blocks in y.
Definition: BandProperty.h:146
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
Definition: BandProperty.h:136
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
std::string ReplaceSpecialChars(const std::string &str, bool &changed)
It replace special characters of a string.
Definition: StringUtils.h:537
Raster property.
std::auto_ptr< te::dt::Property > Convert2T5(const TeAttributeRep &attRep)
It creates a valid TerraLib 5 property given a valid TerraLib 4.x attribute representation.
Definition: Utils.cpp:53
std::auto_ptr< TeDatabaseFactoryParams > Convert2T4DatabaseParams(const std::map< std::string, std::string > &dsInfo)
It converts a data source information to a TerraLib 4.x database params.
Definition: Utils.cpp:131
te::gm::GeomType Convert2T5GeomType(TeAttrDataType type)
Definition: Utils.cpp:197
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
int m_blkw
Block width (pixels).
Definition: BandProperty.h:143
The type for string types: FIXED_STRING, VAR_STRING or STRING.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
short c1
gray, red, cyan or hue.
Definition: BandProperty.h:72
short c3
blue, yellow, or saturation.
Definition: BandProperty.h:74
An structure to represent a color tuple.
Definition: BandProperty.h:70
std::complex< double > m_valuesOffset
Offset is the values (real and imaginary) to add to grid values for this sample dimension, default is 0.
Definition: BandProperty.h:137
int m_blkh
Block height (pixels).
Definition: BandProperty.h:144
TeAttrDataType Convert2T4(int type)
It converts a Terralib 5 data type to Terralib 4.x data type.
Definition: Utils.cpp:287
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
Utilitary functions for dealing with TerraLib 5 and 4.x conversion.
short c2
green, magenta, or lightness.
Definition: BandProperty.h:73