Eta5kmItem.cpp
Go to the documentation of this file.
1 // TerraLib
2 #include "Eta5kmItem.h"
3 #include "AnimationScene.h"
4 #include "Animation.h"
5 #include "../canvas/MapDisplay.h"
6 #include "../canvas/Canvas.h"
7 #include "../../../core/encoding/CharEncoding.h"
8 
9 // Qt
10 #include <QtCore/QPropertyAnimation>
11 #include <QtGui/QPainter>
12 #include <QtCore/QVector>
13 
15 
16 {
17 }
18 
19 te::qt::widgets::Eta5kmItem::Eta5kmItem(const QString& title, const QString& folder, te::qt::widgets::MapDisplay* display)
20  : te::qt::widgets::ImageItem(title, folder, display)
21 {
22 }
23 
25 
27 {
28  if (getCtlParameters() == false)
29  return false;
30 
31  //m_animationScene->addItem(pi);
32  if (m_animation)
33  delete m_animation;
34  m_animation = new te::qt::widgets::Animation(this, "pos");
36  m_animation->setEasingCurve(QEasingCurve::Linear);
37  //m_parallelAnimation->addAnimation(animation);
38 
39  QStringList nameFilter;
40  nameFilter.append("*.bin");
41  QStringList files = m_dir.entryList(nameFilter, QDir::Files, QDir::Name);
42  size_t count = files.count();
43 
44  // get time extent
45  te::dt::TimeInstant ti = getTime(m_dir.path() + "/" + files.first());
46  te::dt::TimeInstant tf = getTime(m_dir.path() + "/" + files.last());
47  // the file contains 3 days of data
48  tf = te::dt::TimeInstant(tf.getTimeInstant() + boost::posix_time::seconds(60 * 60 * 24 * 3 - 3600));
50 
51  // set data
52  te::dt::TimeInstant tnext, t;
53  QString sn, fname;
54  for (size_t i = 0; i < count; ++i)
55  {
56  QString f = files[(int)i];
57  QFileInfo fi(m_dir.path() + "/" + f);
58  QString baseName = fi.baseName();
59  t = getTime(f);
60 
61  if (i != count - 1)
62  tnext = getTime(files[(int)i + 1]);
63  else
64  tnext = te::dt::TimeInstant(t.getTimeInstant() + boost::posix_time::seconds(60 * 60 * 24 * 3));
65 
66  size_t n = 1;
67  while ((t < tnext || t == tnext) && n <= 72)
68  {
69  m_time.push_back(t);
70  t = te::dt::TimeInstant(t.getTimeInstant() + boost::posix_time::seconds(60 * 60));
71  sn.setNum(n++);
72  fname = m_dir.path() + "/" + baseName + "_" + sn + "." + fi.suffix();
73  m_files.push_back(fname);
74  }
75  }
76 
77  generateRoute();
78  setLUT();
79 
80  return true;
81 }
82 
84 {
85  char buf[500];
86  QString file(m_dir.path() + "/Prec5km.ctl");
87  FILE* fp = fopen(te::core::CharEncoding::fromUTF8(file.toUtf8().data()).c_str(), "r");
88  if (fp == nullptr)
89  return false;
90 
91  size_t n = fread(buf, sizeof(char), 500, fp);
92  fclose(fp);
93  buf[n] = 0;
94  QString ss(QString(buf).simplified());
95 
96  // validation
97  if (!(ss.contains("undef ", Qt::CaseInsensitive) && ss.contains("xdef", Qt::CaseInsensitive)
98  && ss.contains("ydef", Qt::CaseInsensitive) && ss.contains("linear", Qt::CaseInsensitive)
99  && ss.contains("zdef", Qt::CaseInsensitive)))
100  return false;
101 
102  QString s;
103 
104  // CHUTE SRID 4326 WGS84
105  //m_SRID = 4326;
106  // get undef
107  int pos = ss.indexOf("undef ", Qt::CaseInsensitive) + static_cast<int>(strlen("undef "));
108  ss.remove(0, pos);
109  pos = ss.indexOf("srid ", Qt::CaseInsensitive);
110  if (pos != -1)
111  {
112  s = ss.left(pos);
113  m_undef = static_cast<float>(atof(s.toUtf8().data()));
114  ss.remove(0, pos);
115 
116  // get srid
117  pos = ss.indexOf("srid ", Qt::CaseInsensitive) + static_cast<int>(strlen("srid "));
118  ss.remove(0, pos);
119  pos = ss.indexOf("xdef ", Qt::CaseInsensitive);
120  s = ss.left(pos);
121  m_SRID = atoi(s.toUtf8().data());
122  ss.remove(0, pos);
123  }
124  else
125  m_SRID = 4326;
126 
127  // get XDEF ulx and resX values
128  pos = ss.indexOf("xdef ", Qt::CaseInsensitive) + static_cast<int>(strlen("xdef "));
129  ss.remove(0, pos);
130  pos = ss.indexOf(" ");
131  s = ss.left(pos);
132  m_ncols = static_cast<size_t>(atoi(s.toUtf8().data()));
133  ss.remove(0, pos);
134 
135  pos = ss.indexOf("linear ", Qt::CaseInsensitive) + static_cast<int>(strlen("linear "));
136  ss.remove(0, pos);
137  pos = ss.indexOf(" ");
138  s = ss.left((int)pos);
139  double llx = atof(s.toUtf8().data());
140  ss.remove(0, (int)pos);
141  pos = ss.indexOf("ydef ", Qt::CaseInsensitive);
142  s = ss.left((int)pos);
143  double resX = atof(s.toUtf8().data());
144  ss.remove(0, (int)pos);
145 
146  // get YDEF uly and resY values
147  pos = static_cast<int>(ss.indexOf("ydef ", Qt::CaseInsensitive) + strlen("ydef "));
148  ss.remove(0, (int)pos);
149  pos = ss.indexOf(" ");
150  s = ss.left((int)pos);
151  m_nlines = atoi(s.toUtf8().data());
152  ss.remove(0, (int)pos);
153 
154  pos = static_cast<int>(ss.indexOf("linear ", Qt::CaseInsensitive) + strlen("linear "));
155  ss.remove(0, (int)pos);
156  pos = ss.indexOf(" ");
157  s = ss.left((int)pos);
158  double lly = atof(s.toUtf8().data());
159  ss.remove(0, (int)pos);
160  pos = ss.indexOf("zdef ", Qt::CaseInsensitive);
161  s = ss.left((int)pos);
162  double resY = atof(s.toUtf8().data());
163  ss.remove(0, (int)pos);
164 
165  double w = (double)m_ncols * resX;
166  double h = (double)m_nlines * resY;
167  m_imaRect = QRectF(llx, lly, w, h);
168 
169  // get static representation
170  if (m_dir.exists("staticRepresentation.png"))
171  m_staticRepresentation = QImage(m_dir.path() + "/staticRepresentation.png");
172 
173  return true;
174 }
175 
177 {
178  if (m_image)
179  delete m_image;
180  m_image = nullptr;
181 
182  if (m_currentImageFile.isEmpty())
183  {
184  if (m_staticRepresentation.isNull())
185  {
186  QRect r = getRect();
187  m_image = new QImage(r.size(), QImage::Format_ARGB32);
188  m_image->fill(QColor(0, 0, 255, 100).rgba());
189  QPainter p(m_image);
190  p.setPen(QPen(QColor(255, 0, 0)));
191 
192  QFont font(p.font());
193  int ps = 7;
194  int w = (int)((double)m_image->width() / 1.2);
195  int h = (int)((double)m_image->width() / 5.);
196 
197  font.setPointSize(ps);
198  QFontMetrics fm(font);
199  QRectF rec(fm.boundingRect(m_title));
200 
201  while (rec.width() < w && rec.height() < h)
202  {
203  ++ps;
204  font.setPointSize(ps);
205  QFontMetrics fm(font);
206  rec = fm.boundingRect(m_title);
207  }
208  rec.moveCenter(m_image->rect().center());
209  p.setFont(font);
210  p.drawText(rec.toRect(), Qt::AlignLeft, m_title);
211  }
212  else
213  m_image = new QImage(m_staticRepresentation);
214  }
215  else
216  {
217  QString path = m_dir.absolutePath() + "/";
218  QString file = m_currentImageFile;
219  QFileInfo fi(file);
220  QString baseName = fi.baseName();
221 
222  if (m_suffix == ".bin" && baseName.contains("Prec5km"))
223  {
224  QString auxFile(file);
225  size_t pos = auxFile.indexOf(baseName);
226  auxFile.remove(0, (int)pos);
227  pos = auxFile.indexOf("_");
228  size_t pp = auxFile.indexOf(".bin");
229  int offset = atoi(auxFile.mid((int)pos + 1, (int)pp - (int)pos + 1).toUtf8().data());
230  size_t fileSize = m_nlines * m_ncols * 4 + 8; // dado � float e desprepreza 4 bytes iniciais e 4 bytes finais
231  offset *= (int)fileSize;
232  auxFile.remove((int)pos, auxFile.length() - (int)pos);
233  auxFile = path + auxFile + m_suffix;
234 
235  size_t nchars = m_ncols * 4;
236  uchar* buf = new uchar[nchars];
237  FILE* fp = fopen(te::core::CharEncoding::fromUTF8(auxFile.toUtf8().data()).c_str(), "rb");
238  fseek(fp, offset, SEEK_SET);
239  fseek(fp, 4, SEEK_CUR); // despreza 4 bytes da primeira linha
240  m_image = new QImage((int)m_ncols, (int)m_nlines, QImage::Format_ARGB32);
241  m_image->fill(Qt::transparent);
242 
243  uchar uc[5];
244  uc[4] = 0;
245  for (size_t j = 0; j < m_nlines; ++j)
246  {
247  uchar* u = m_image->scanLine((int)m_nlines - 1 - (int)j); // origem bottom left
248  fread(buf, nchars, sizeof(char), fp);
249 
250  for (size_t i = 0; i < m_ncols; i++)
251  {
252  uc[0] = *(buf + (i << 2));
253  uc[1] = *(buf + (1 + (i << 2)));
254  uc[2] = *(buf + (2 + (i << 2)));
255  uc[3] = *(buf + (3 + (i << 2)));
256  float b = *(reinterpret_cast<float*>(uc));
257  if (b != m_undef)
258  {
259  uchar a = (uchar)(b * 10000. + .5);
260  QRgb* v = (QRgb*)(u + (i << 2));
261  *v = qRgba(m_lut[a].red(), m_lut[a].green(), m_lut[a].blue(), 255);
262  }
263  }
264  }
265  fclose(fp);
266  delete[]buf;
267  }
268  }
269 }
270 
271 //void drawCurrentImage(QPainter* p)
272 //{
273 //}
274 
276 {
277  QString file(fileName);
278  int ind = file.indexOf("Prec5km") + (int)strlen("Prec5km");
279  file.remove(0, ind);
280  QString ano = file.mid(0, 4);
281  QString mes = file.mid(4, 2);
282  QString dia = file.mid(6, 2);
283  QString hour = file.mid(8, 2);
284 
285  te::dt::Date date(ano.toInt(), mes.toInt(), dia.toInt());
286  te::dt::TimeDuration dur(hour.toInt() + 1, 0, 0); // tem deslocamento de 1 hora
287  return te::dt::TimeInstant(date, dur);
288 }
289 
291 {
292  std::vector<std::pair<int, QColor> > tab;
293  int i = 0;
294  tab.push_back(std::pair<int, QColor>(i += 1, QColor(2, 1, 201, 255)));
295  tab.push_back(std::pair<int, QColor>(i += 1, QColor(2, 18, 213, 255)));
296  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 24, 220, 255)));
297  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 36, 227, 255)));
298  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 54, 240, 255)));
299  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 71, 254, 255)));
300  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 90, 252, 255)));
301  tab.push_back(std::pair<int, QColor>(i += 1, QColor(3, 106, 251, 255)));
302  tab.push_back(std::pair<int, QColor>(i += 1, QColor(3, 130, 253, 255)));
303  tab.push_back(std::pair<int, QColor>(i += 1, QColor(3, 148, 250, 255)));
304  tab.push_back(std::pair<int, QColor>(i += 1, QColor(3, 160, 250, 255)));
305  tab.push_back(std::pair<int, QColor>(i += 1, QColor(4, 174, 250, 255)));
306  tab.push_back(std::pair<int, QColor>(i += 1, QColor(4, 186, 250, 255)));
307  tab.push_back(std::pair<int, QColor>(i += 1, QColor(5, 200, 251, 255)));
308  tab.push_back(std::pair<int, QColor>(i += 1, QColor(5, 214, 246, 255)));
309  tab.push_back(std::pair<int, QColor>(i += 1, QColor(3, 227, 241, 255)));
310  tab.push_back(std::pair<int, QColor>(i += 1, QColor(3, 240, 237, 255)));
311  tab.push_back(std::pair<int, QColor>(i += 1, QColor(2, 254, 233, 255)));
312  tab.push_back(std::pair<int, QColor>(i += 1, QColor(2, 254, 212, 255)));
313  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 190, 255)));
314  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 170, 255)));
315  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 160, 255)));
316  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 151, 255)));
317  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 105, 255)));
318  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 72, 255)));
319  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 37, 255)));
320  tab.push_back(std::pair<int, QColor>(i += 1, QColor(2, 254, 4, 255)));
321  tab.push_back(std::pair<int, QColor>(i += 1, QColor(25, 254, 4, 255)));
322  tab.push_back(std::pair<int, QColor>(i += 1, QColor(56, 254, 3, 255)));
323  tab.push_back(std::pair<int, QColor>(i += 1, QColor(71, 254, 3, 255)));
324  tab.push_back(std::pair<int, QColor>(i += 1, QColor(99, 254, 2, 255)));
325  tab.push_back(std::pair<int, QColor>(i += 1, QColor(125, 254, 2, 255)));
326  tab.push_back(std::pair<int, QColor>(i += 1, QColor(150, 254, 2, 255)));
327  tab.push_back(std::pair<int, QColor>(i += 1, QColor(175, 254, 2, 255)));
328  tab.push_back(std::pair<int, QColor>(i += 1, QColor(200, 255, 1, 255)));
329  tab.push_back(std::pair<int, QColor>(i += 1, QColor(209, 255, 1, 255)));
330  tab.push_back(std::pair<int, QColor>(i += 1, QColor(218, 255, 1, 255)));
331  tab.push_back(std::pair<int, QColor>(i += 1, QColor(229, 255, 1, 255)));
332  tab.push_back(std::pair<int, QColor>(i += 1, QColor(237, 255, 0, 255)));
333  tab.push_back(std::pair<int, QColor>(i += 1, QColor(240, 248, 0, 255)));
334  tab.push_back(std::pair<int, QColor>(i += 1, QColor(244, 241, 0, 255)));
335  tab.push_back(std::pair<int, QColor>(i += 1, QColor(250, 237, 0, 255)));
336  tab.push_back(std::pair<int, QColor>(i += 1, QColor(251, 232, 2, 255)));
337  tab.push_back(std::pair<int, QColor>(i += 1, QColor(251, 226, 2, 255)));
338  tab.push_back(std::pair<int, QColor>(i += 1, QColor(252, 215, 2, 255)));
339  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 208, 2, 255)));
340  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 199, 2, 255)));
341  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 194, 2, 255)));
342  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 190, 2, 255)));
343  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 184, 2, 255)));
344  tab.push_back(std::pair<int, QColor>(i += 1, QColor(252, 179, 2, 255)));
345  tab.push_back(std::pair<int, QColor>(i += 1, QColor(252, 172, 2, 255)));
346  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 164, 2, 255)));
347  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 155, 2, 255)));
348  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 147, 4, 255)));
349  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 136, 4, 255)));
350  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 125, 3, 255)));
351  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 112, 3, 255)));
352  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 99, 2, 255)));
353  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 75, 2, 255)));
354  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 50, 2, 255)));
355  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 25, 2, 255)));
356  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 1, 2, 255)));
357  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 1, 12, 255)));
358  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 1, 25, 255)));
359  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 1, 37, 255)));
360  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 0, 49, 255)));
361  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 2, 37, 255)));
362  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 3, 25, 255)));
363  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 4, 12, 255)));
364  tab.push_back(std::pair<int, QColor>(i += 1, QColor(255, 5, 1, 255)));
365 
367 }
virtual ~Eta5kmItem()
Destructor It destructs a Eta5km Item.
QRectF m_imaRect
Image location.
Definition: ImageItem.h:203
te::dt::TimeInstant getTime(QString file)
Get time of the raster data.
Definition: Eta5kmItem.cpp:275
std::map< uchar, QColor > m_lut
LUT.
Definition: ImageItem.h:204
te::gm::Envelope m_spatialExtent
Spatial extent.
Definition: Animation.h:109
bool getCtlParameters()
Get control parameters.
Definition: Eta5kmItem.cpp:83
A widget to control the display of a set of layers.
void loadCurrentImage()
Load current image.
Definition: Eta5kmItem.cpp:176
static std::string fromUTF8(const std::string &src)
Convert a string in UTF-8 to the current locale encoding.
const boost::posix_time::ptime & getTimeInstant() const
It returns the boost time instant type.
Definition: TimeInstant.h:92
A class to represent time instant.
Definition: TimeInstant.h:55
Animation * m_animation
The animation this item.
int b
Definition: TsRtree.cpp:32
This file defines a class for a Animation Scene.
This file defines a class for a Eta5kmItem.
An Envelope defines a 2D rectangular region.
A base class for date data types.
Definition: Date.h:53
Eta5kmItem()
Empty Constructor.
Definition: Eta5kmItem.cpp:14
URI C++ Library.
Definition: Attributes.h:37
size_t m_ncols
number of colunms.
Definition: ImageItem.h:194
QVector< QString > m_files
The input files.
Definition: ImageItem.h:196
te::gm::Polygon * p
QRect getRect()
It gets image rect in device coordinate.
Definition: ImageItem.cpp:491
float m_undef
undef value.
Definition: ImageItem.h:195
QImage m_staticRepresentation
Static representation of the animation.
Definition: ImageItem.h:206
QString m_title
The icon item title.
QDir m_dir
The image data directory.
Definition: ImageItem.h:192
size_t m_nlines
number of lines.
Definition: ImageItem.h:193
QString m_currentImageFile
Image to be displayed on paint event animation.
Definition: ImageItem.h:198
A class to represent time period.
Definition: TimePeriod.h:54
This file defines a class for a Trajectory Animation.
QImage * m_image
current image
Definition: ImageItem.h:199
void generateRoute()
It generate the raster route.
Definition: ImageItem.cpp:528
te::dt::TimePeriod m_temporalExtent
Total temporal extent.
Definition: Animation.h:110
A class to represent time duration with nano-second/micro-second resolution.
Definition: TimeDuration.h:51
void setLUT()
Sets the default LUT.
Definition: Eta5kmItem.cpp:290
QString m_suffix
File suffix.
Definition: ImageItem.h:201
bool loadData()
Load temporal raster data.
Definition: Eta5kmItem.cpp:26
int m_SRID
The input route srid.
file(WRITE ${CMAKE_BINARY_DIR}/config_qhelp.cmake"configure_file (${TERRALIB_ABSOLUTE_ROOT_DIR}/doc/qhelp/help.qhcp.in ${CMAKE_BINARY_DIR}/share/terraview/help/help.qhcp @ONLY)") add_custom_command(OUTPUT del_dir COMMAND $
void setLUT(const std::vector< std::pair< int, QColor > > &tab)
Sets the LUT. tab The LUT information.
Definition: ImageItem.cpp:504
QVector< te::dt::TimeInstant > m_time
The input time.