src/terralib/terralib4/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 "../core/encoding/CharEncoding.h"
30 #include "../core/translator/Translator.h"
31 #include "../core/uri/URI.h"
32 #include "../core/uri/Utils.h"
33 #include "../dataaccess/dataset/DataSetType.h"
34 #include "../datatype/NumericProperty.h"
35 #include "../datatype/Property.h"
36 #include "../datatype/StringProperty.h"
37 #include "../geometry/Enums.h"
38 #include "../geometry/Envelope.h"
39 #include "../geometry/GeometryProperty.h"
40 #include "../raster/BandProperty.h"
41 #include "../raster/Grid.h"
42 #include "../raster/RasterProperty.h"
43 #include "Utils.h"
44 
45 // TerraLib 4.x
46 #include <terralib4/kernel/TeBox.h>
47 #include <terralib4/kernel/TeDatabaseFactoryParams.h>
48 #include <terralib4/kernel/TeDecoderDatabase.h>
49 #include <terralib4/kernel/TeProjection.h>
50 #include <terralib4/kernel/TeTable.h>
51 #include <terralib4/kernel/TeRasterParams.h>
52 
53 // Boost
54 #include <boost/lexical_cast.hpp>
55 
56 std::unique_ptr<te::dt::Property> terralib4::Convert2T5(const TeAttributeRep& attRep)
57 {
58  std::string* defaultValue = attRep.defaultValue_.empty() ? 0 : new std::string(attRep.defaultValue_);
59 
60  bool isRequired = !attRep.null_;
61 
62  std::string attrRepNameUtf8 = Convert2Utf8(attRep.name_);
63 
64  bool changed;
65  std::string normalName = te::common::ReplaceSpecialChars(attrRepNameUtf8, changed);
66 
67  switch(attRep.type_)
68  {
69  case TeSTRING:
70  if(attRep.numChar_ == 0)
71  return std::unique_ptr<te::dt::Property>(new te::dt::StringProperty(normalName, te::dt::STRING, 0, isRequired, defaultValue));
72  else
73  return std::unique_ptr<te::dt::Property>(new te::dt::StringProperty(normalName, te::dt::VAR_STRING, attRep.numChar_, isRequired, defaultValue));
74 
75  case TeCHARACTER:
76  return std::unique_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::CHAR_TYPE, isRequired, defaultValue));
77 
78  case TeBOOLEAN:
79  return std::unique_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::BOOLEAN_TYPE, isRequired, defaultValue));
80 
81  case TeINT:
82  return std::unique_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::INT32_TYPE, isRequired, defaultValue));
83 
84  case TeUNSIGNEDINT:
85  return std::unique_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::UINT32_TYPE, isRequired, defaultValue));
86 
87  case TeREAL:
88  return std::unique_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::DOUBLE_TYPE, isRequired, defaultValue));
89 
90  case TeDATETIME:
91  return std::unique_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::TIME_INSTANT, isRequired, defaultValue));
92 
93  case TeBLOB:
94  return std::unique_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::BYTE_ARRAY_TYPE, isRequired, defaultValue));
95 
96  case TePOINTTYPE:
97  return std::unique_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::PointType, isRequired, defaultValue));
98 
99  case TeLINE2DTYPE:
100  return std::unique_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::LineStringType, isRequired, defaultValue));
101 
102  case TePOLYGONTYPE:
103  return std::unique_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::PolygonType, isRequired, defaultValue));
104 
105  case TeCELLTYPE:
106  return std::unique_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::PolygonType, isRequired, defaultValue));
107 
108  case TePOINTSETTYPE:
109  return std::unique_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::MultiPointType, isRequired, defaultValue));
110 
111  case TeLINESETTYPE:
112  return std::unique_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::MultiLineStringType, isRequired, defaultValue));
113 
114  case TePOLYGONSETTYPE:
115  return std::unique_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::MultiPolygonType, isRequired, defaultValue));
116 
117  case TeCELLSETTYPE:
118  return std::unique_ptr<te::dt::Property>(new te::gm::GeometryProperty(normalName, 0, te::gm::MultiPolygonType, isRequired, defaultValue));
119 
120  case TeRASTERTYPE:
121  return std::unique_ptr<te::dt::Property>(new te::rst::RasterProperty(normalName, isRequired));
122 
123  case TeUNKNOWN:
124  return std::unique_ptr<te::dt::Property>(new te::dt::SimpleProperty(normalName, te::dt::UNKNOWN_TYPE, isRequired));
125 
126  case TeNODETYPE:
127  case TeNODESETTYPE:
128  case TeTEXTTYPE:
129  case TeTEXTSETTYPE:
130  case TeOBJECT:
131  default:
132  throw te::common::Exception(TE_TR("The informed attribute representation is not supported!"));
133  }
134 }
135 
136 std::unique_ptr<TeDatabaseFactoryParams> terralib4::Convert2T4DatabaseParams(const std::string& dsInfo)
137 {
138  te::core::URI uri(dsInfo);
139 
140  std::map<std::string, std::string> kvp = te::core::Expand(uri.query());
141  std::map<std::string, std::string>::const_iterator it = kvp.begin();
142  std::map<std::string, std::string>::const_iterator itend = kvp.end();
143 
144  std::unique_ptr<TeDatabaseFactoryParams> fparams(new TeDatabaseFactoryParams());
145 
146  if(uri.isValid())
147  {
148  fparams->dbms_name_ = Convert2Latin1(kvp["T4_DRIVER"]);
149  fparams->database_ = Convert2Latin1(uri.path());
150  fparams->host_ = Convert2Latin1(uri.host());
151  fparams->user_ = Convert2Latin1(uri.user());
152  fparams->password_ = Convert2Latin1(uri.password());
153  }
154 
155  return fparams;
156 }
157 
158 int terralib4::Convert2T5(TeAttrDataType type)
159 {
160  switch(type)
161  {
162  case TeSTRING:
163  return te::dt::STRING_TYPE;
164 
165  case TeREAL:
166  return te::dt::DOUBLE_TYPE;
167 
168  case TeINT:
169  return te::dt::INT32_TYPE;
170 
171  case TeDATETIME:
172  return te::dt::DATETIME_TYPE;
173 
174  case TeBLOB:
176 
177  case TeCHARACTER:
178  return te::dt::CHAR_TYPE;
179 
180  case TeUNSIGNEDINT:
181  return te::dt::UINT32_TYPE;
182 
183  case TePOINTTYPE:
184  case TeNODETYPE:
185  case TeLINE2DTYPE:
186  case TePOLYGONTYPE:
187  case TeCELLTYPE:
188  case TePOINTSETTYPE:
189  case TeNODESETTYPE:
190  case TeLINESETTYPE:
191  case TePOLYGONSETTYPE:
192  case TeCELLSETTYPE:
193  return te::dt::GEOMETRY_TYPE;
194 
195  case TeRASTERTYPE:
196  return te::dt::RASTER_TYPE;
197 
198  case TeBOOLEAN:
199  return te::dt::BOOLEAN_TYPE;
200 
201  case TeUNKNOWN:
202  case TeOBJECT:
203  case TeTEXTTYPE:
204  case TeTEXTSETTYPE:
205  default:
206  return te::dt::UNKNOWN_TYPE;
207  }
208 }
209 
211 {
212  switch(type)
213  {
214  case TePOINTTYPE:
215  case TeNODETYPE:
216  return te::gm::PointType;
217 
218  case TeLINE2DTYPE:
219  return te::gm::LineStringType;
220 
221  case TePOLYGONTYPE:
222  case TeCELLTYPE:
223  return te::gm::PolygonType;
224 
225  case TePOINTSETTYPE:
226  case TeNODESETTYPE:
227  return te::gm::MultiPointType;
228 
229  case TeLINESETTYPE:
231 
232  case TePOLYGONSETTYPE:
233  case TeCELLSETTYPE:
235 
236  default:
238  }
239 }
240 
242 {
243  switch(type)
244  {
245  case TePOINTS:
246  case TeNODES:
247  return te::gm::PointType;
248 
249  case TeLINES:
250  return te::gm::LineStringType;
251 
252  case TePOLYGONS:
253  case TeCELLS:
254  return te::gm::PolygonType;
255 
256  default:
258  }
259 }
260 
261 int terralib4::Convert2T5(TeDataType dt)
262 {
263  switch(dt)
264  {
265  case TeBIT:
266  return te::dt::BIT_TYPE;
267 
268  case TeUNSIGNEDCHAR:
269  return te::dt::UCHAR_TYPE;
270 
271  case TeCHAR:
272  return te::dt::CHAR_TYPE;
273 
274  case TeUNSIGNEDSHORT:
275  return te::dt::UINT16_TYPE;
276 
277  case TeSHORT:
278  return te::dt::INT16_TYPE;
279 
280  case TeINTEGER:
281  return te::dt::INT32_TYPE;
282 
283  case TeUNSIGNEDLONG:
284  return te::dt::UINT32_TYPE;
285 
286  case TeLONG:
287  return te::dt::INT32_TYPE; // see TerraLib 4.x => TeRasterParams.cpp elementSize method => sizeof(long) => 4bytes!
288 
289  case TeFLOAT:
290  return te::dt::FLOAT_TYPE;
291 
292  case TeDOUBLE:
293  return te::dt::DOUBLE_TYPE;
294 
295  default:
296  return te::dt::UNKNOWN_TYPE;
297  }
298 }
299 
300 TeAttrDataType terralib4::Convert2T4(int type)
301 {
302  switch(type)
303  {
304  case te::dt::STRING_TYPE:
305  return TeSTRING;
306 
307  case te::dt::DOUBLE_TYPE:
309  return TeREAL;
310 
311  case te::dt::INT16_TYPE:
312  case te::dt::INT32_TYPE:
313  case te::dt::INT64_TYPE:
314  return TeINT;
315 
317  return TeDATETIME;
318 
320  return TeBLOB;
321 
322  case te::dt::CHAR_TYPE:
323  return TeCHARACTER;
324 
325  case te::dt::UINT16_TYPE:
326  case te::dt::UINT32_TYPE:
327  case te::dt::UINT64_TYPE:
328  return TeUNSIGNEDINT;
329 
330  case te::dt::RASTER_TYPE:
331  return TeRASTERTYPE;
332 
334  return TeBOOLEAN;
335 
337  default:
338  return TeUNKNOWN;
339  }
340 }
341 
343 {
344  switch(type)
345  {
346  case te::gm::PointType:
347  return TePOINTTYPE;
348 
350  return TeLINE2DTYPE;
351 
352  case te::gm::PolygonType:
353  return TePOLYGONTYPE;
354 
356  return TePOINTSETTYPE;
357 
359  return TeLINESETTYPE;
360 
362  return TePOLYGONSETTYPE;
363 
364  default:
365  return TeUNKNOWN;
366  }
367 }
368 
369 std::unique_ptr<te::gm::Envelope> terralib4::Convert2T5(TeBox box)
370 {
371  std::unique_ptr<te::gm::Envelope> env(new te::gm::Envelope(box.x1(), box.y1(), box.x2(), box.y2()));
372 
373  return env;
374 }
375 
376 std::unique_ptr<te::da::DataSetType> terralib4::Convert2T5(TeTable table)
377 {
378  TeAttributeList attrList = table.attributeList();
379 
380  std::string tableNameUtf8 = Convert2Utf8(table.name());
381 
382  std::unique_ptr<te::da::DataSetType> newDst(new te::da::DataSetType(tableNameUtf8));
383 
384  for(std::size_t i = 0; i < attrList.size(); ++i)
385  {
386  TeAttributeRep attr = attrList[i].rep_;
387 
388  std::unique_ptr<te::dt::Property> prop = terralib4::Convert2T5(attr);
389 
390  newDst->add(prop.release());
391  }
392 
393  return newDst;
394 }
395 
397 {
398  std::unique_ptr<te::rst::RasterProperty> rproperty(new te::rst::RasterProperty("raster"));
399 
400  unsigned int ncols = rparams.ncols_;
401  unsigned int nrows = rparams.nlines_;
402  std::unique_ptr<te::gm::Envelope> mbr(Convert2T5(rparams.boundingBox()));
403 
404  int srid = rparams.projection()->epsgCode();
405  if(srid == 4979)
406  srid = 4326;
407 
408  te::gm::Coord2D* cord = new te::gm::Coord2D(rparams.boundingBox().x1_-(rparams.resx_/2), rparams.boundingBox().y2_+(rparams.resy_/2));
409  std::unique_ptr<te::rst::Grid> grid(new te::rst::Grid(ncols, nrows, rparams.resx_, rparams.resy_, cord, srid));
410 
411  rproperty->set(grid.release());
412 
413  std::vector<te::rst::BandProperty::ColorEntry> palette;
414  if(rparams.photometric_[0] == TeRasterParams::TePallete)
415  {
416  std::size_t size = rparams.lutr_.size();
417 
418  for(std::size_t j = 0; j < size; ++j)
419  {
421  c.c1 = rparams.lutr_[j];
422  c.c2 = rparams.lutg_[j];
423  c.c3 = rparams.lutb_[j];
424  palette.push_back(c);
425  }
426  }
427 
428  for(int i = 0; i != rparams.nBands(); ++i)
429  {
430  te::rst::BandProperty* bp = new te::rst::BandProperty(i, Convert2T5(rparams.dataType_[i]));
431 
432  bp->m_blkh = rparams.blockHeight_;
433  bp->m_blkw = rparams.blockWidth_;
434  bp->m_noDataValue = rparams.dummy_[i];
435  bp->m_valuesOffset = rparams.offset_;
436 
437  if(!palette.empty())
438  bp->m_palette.assign(palette.begin(), palette.end());
439 
440  bp->m_nblocksx = (ncols + bp->m_blkw - 1) / bp->m_blkw;
441  bp->m_nblocksy = (nrows + bp->m_blkh - 1) / bp->m_blkh;
442 
443  rproperty->add(bp);
444  }
445 
446  return rproperty.release();
447 }
448 
449 void terralib4::CheckDecimalSeparator(std::string& value)
450 {
451  bool isDotSeparator = false;
452 
453  HKEY hk;
454  DWORD DataSize = 2;
455  DWORD Type = REG_SZ;
456  char buf[2];
457 
458  std::string key = "Control Panel\\International";
459  std::string sepDecimal = "sDecimal";
460  std::string sepDecimalResult = "";
461 
462  if (RegOpenKeyExA(HKEY_CURRENT_USER, key.c_str(), 0, KEY_READ, &hk) == ERROR_SUCCESS)
463  {
464  memset(buf, 0, 2);
465  DataSize = 2;
466  //decimal separator
467  if (RegQueryValueExA(hk, sepDecimal.c_str(), NULL, &Type, (LPBYTE)buf, &DataSize) == ERROR_SUCCESS)
468  sepDecimalResult = buf;
469 
470  RegCloseKey(hk);
471 
472  if (sepDecimalResult == ".")
473  isDotSeparator = true;
474  }
475 
476  if (!isDotSeparator)
477  {
478  std::replace(value.begin(), value.end(), ',', '.');
479  }
480 }
481 
482 te::gm::GeomType terralib4::GetMinimalRepresentation(const std::vector<TeGeometry*>& geoms)
483 {
484  std::map<TeGeomRep, int> auxMap;
485 
486  for (std::size_t i = 0; i < geoms.size(); ++i)
487  {
488  TeGeomRep geomRep = geoms[i]->elemType();
489 
490  std::map<TeGeomRep, int>::iterator it = auxMap.find(geomRep);
491 
492  if (it != auxMap.end())
493  it->second += 1;
494  else
495  auxMap[geomRep] = 1;
496  }
497 
498  if (auxMap.size() > 1)
500 
501  TeGeomRep geomRep = auxMap.begin()->first;
502  if (auxMap.begin()->second == 1)
503  {
504  return Convert2T5GeomType(geomRep);
505  }
506  else
507  {
508  return GetCollection(geomRep);
509  }
510 
511 }
512 
514 {
515  switch (rep)
516  {
517  case TeCELLS:
518  case TePOLYGONS:
520 
521  case TeLINES:
523 
524  case TeNODES:
525  case TePOINTS:
526  return te::gm::MultiPointType;
527 
528  default:
530  }
531 }
532 
533 std::string terralib4::Convert2Utf8(const std::string& str)
534 {
536 }
537 
538 std::string terralib4::Convert2Latin1(const std::string& str)
539 {
541 }
TeAttrDataType Convert2T4GeomType(te::gm::GeomType type)
std::unique_ptr< TeDatabaseFactoryParams > Convert2T4DatabaseParams(const std::string &dsInfo)
It converts a data source information to a TerraLib 4.x database params.
std::string path() const
Retrieving the path.
Definition: URI.cpp:118
Geometric property.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
An atomic property like an integer or double.
std::vector< ColorEntry > m_palette
The color palette.
Definition: BandProperty.h:142
TETERRALIB4EXPORT std::string Convert2Utf8(const std::string &str)
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
te::gm::GeomType GetCollection(TeGeomRep rep)
std::string password() const
Retrieving the password information.
Definition: URI.cpp:103
bool isValid() const
Return if the given URI is valid or not.
Definition: URI.cpp:133
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<double>::max().
Definition: BandProperty.h:136
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
static std::string fromUTF8(const std::string &src)
Convert a string in UTF-8 to the current locale encoding.
std::string ReplaceSpecialChars(const std::string &str, bool &changed)
It replace special characters of a string.
Definition: StringUtils.h:376
Raster property.
std::string query() const
Retrieving the query.
Definition: URI.cpp:123
std::unique_ptr< te::dt::Property > Convert2T5(const TeAttributeRep &attRep)
It creates a valid TerraLib 5 property given a valid TerraLib 4.x attribute representation.
te::gm::GeomType Convert2T5GeomType(TeAttrDataType type)
An Envelope defines a 2D rectangular region.
int m_blkw
Block width (pixels).
Definition: BandProperty.h:143
static te::dt::TimeDuration dt(20, 30, 50, 11)
te::gm::GeomType GetMinimalRepresentation(const std::vector< TeGeometry * > &geoms)
std::string host() const
Retrieving the host.
Definition: URI.cpp:108
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. ...
TETERRALIB4EXPORT std::string Convert2Latin1(const std::string &str)
void CheckDecimalSeparator(std::string &value)
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
short c1
gray, red, cyan or hue.
Definition: BandProperty.h:72
This file contains utility functions used to manipulate data from a URI.
TECOREEXPORT std::map< std::string, std::string > Expand(const std::string &query_str)
Split a query string into its components.
static std::string toUTF8(const std::string &src)
Convert a string from a current locale encoding to UTF-8.
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.
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
short c2
green, magenta, or lightness.
Definition: BandProperty.h:73
std::string user() const
Retrieving the user information.
Definition: URI.cpp:98