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