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 
8 // Qt
9 #include <QtCore/QPropertyAnimation>
10 #include <QtGui/QPainter>
11 #include <QtCore/QVector>
12 
14  : te::qt::widgets::ImageItem()
15 {
16 }
17 
18 te::qt::widgets::Eta5kmItem::Eta5kmItem(const QString& title, const QString& folder, te::qt::widgets::MapDisplay* display)
19  : te::qt::widgets::ImageItem(title, folder, display)
20 {
21 }
22 
24 {
25 }
26 
28 {
29  if(getCtlParameters() == false)
30  return false;
31 
32  //m_animationScene->addItem(pi);
33  if (m_animation)
34  delete m_animation;
35  m_animation = new te::qt::widgets::Animation(this, "pos");
36  m_animation->m_spatialExtent = te::gm::Envelope(m_imaRect.x(), m_imaRect.y(), m_imaRect.right(), m_imaRect.bottom());
37  m_animation->setEasingCurve(QEasingCurve::Linear);
38  //m_parallelAnimation->addAnimation(animation);
39 
40  QStringList nameFilter;
41  nameFilter.append("*.bin");
42  QStringList files = m_dir.entryList(nameFilter, QDir::Files, QDir::Name);
43  size_t count = files.count();
44 
45  // get time extent
46  te::dt::TimeInstant ti = getTime(m_dir.path() + "/" + files.first());
47  te::dt::TimeInstant tf = getTime(m_dir.path() + "/" + files.last());
48  // the file contains 3 days of data
49  tf = te::dt::TimeInstant(tf.getTimeInstant() + boost::posix_time::seconds(60 * 60 * 24 * 3 - 3600));
50  m_animation->m_temporalExtent = te::dt::TimePeriod(ti, tf);
51 
52  // set data
53  te::dt::TimeInstant tnext, t;
54  QString sn, fname;
55  for (size_t i = 0; i < count; ++i)
56  {
57  QString f = files[(int)i];
58  QFileInfo fi(m_dir.path() + "/" + f);
59  QString baseName = fi.baseName();
60  t = getTime(f);
61 
62  if (i != count - 1)
63  tnext = getTime(files[(int)i + 1]);
64  else
65  tnext = te::dt::TimeInstant(t.getTimeInstant() + boost::posix_time::seconds(60 * 60 * 24 * 3));
66 
67  size_t n = 1;
68  while ((t < tnext || t == tnext) && n <= 72)
69  {
70  m_time.push_back(t);
71  t = te::dt::TimeInstant(t.getTimeInstant() + boost::posix_time::seconds(60 * 60));
72  sn.setNum(n++);
73  fname = m_dir.path() + "/" + baseName + "_" + sn + "." + fi.suffix();
74  m_files.push_back(fname);
75  }
76  }
77 
78  generateRoute();
79  setLUT();
80 
81  return true;
82 }
83 
85 {
86  char buf[2000];
87  QString file(m_dir.path() + "/Prec5km.ctl");
88  FILE* fp = fopen(file.toStdString().c_str(), "r");
89  if (fp == 0)
90  return false;
91 
92  fread(buf, 2000, sizeof(char), fp);
93  fclose(fp);
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 
107  // get UNDEF value
108  size_t pos = ss.indexOf("undef ", Qt::CaseInsensitive) + strlen("undef ");
109  ss.remove(0, (int)pos);
110  pos = ss.indexOf("xdef", Qt::CaseInsensitive);
111  s = ss.left((int)pos);
112  m_undef = atof(s.toStdString().c_str());
113  ss.remove(0, (int)pos);
114 
115  // get XDEF ulx and resX values
116  pos = ss.indexOf("xdef ", Qt::CaseInsensitive) + strlen("xdef ");
117  ss.remove(0, (int)pos);
118  pos = ss.indexOf(" ");
119  s = ss.left((int)pos);
120  m_ncols = atoi(s.toStdString().c_str());
121  ss.remove(0, (int)pos);
122 
123  pos = ss.indexOf("linear ", Qt::CaseInsensitive) + strlen("linear ");
124  ss.remove(0, (int)pos);
125  pos = ss.indexOf(" ");
126  s = ss.left((int)pos);
127  double llx = atof(s.toStdString().c_str());
128  ss.remove(0, (int)pos);
129  pos = ss.indexOf("ydef ", Qt::CaseInsensitive);
130  s = ss.left((int)pos);
131  double resX = atof(s.toStdString().c_str());
132  ss.remove(0, (int)pos);
133 
134  // get YDEF uly and resY values
135  pos = ss.indexOf("ydef ", Qt::CaseInsensitive) + strlen("ydef ");
136  ss.remove(0, (int)pos);
137  pos = ss.indexOf(" ");
138  s = ss.left((int)pos);
139  m_nlines = atoi(s.toStdString().c_str());
140  ss.remove(0, (int)pos);
141 
142  pos = ss.indexOf("linear ", Qt::CaseInsensitive) + strlen("linear ");
143  ss.remove(0, (int)pos);
144  pos = ss.indexOf(" ");
145  s = ss.left((int)pos);
146  double lly = atof(s.toStdString().c_str());
147  ss.remove(0, (int)pos);
148  pos = ss.indexOf("zdef ", Qt::CaseInsensitive);
149  s = ss.left((int)pos);
150  double resY = atof(s.toStdString().c_str());
151  ss.remove(0, (int)pos);
152 
153  double w = (double)m_ncols * resX;
154  double h = (double)m_nlines * resY;
155  m_imaRect = QRectF(llx, lly, w, h);
156 
157  return true;
158 }
159 
161 {
162  if (m_image)
163  delete m_image;
164  m_image = 0;
165 
166  QString path = m_dir.absolutePath() + "/";
167  QString file = m_currentImageFile;
168  QFileInfo fi(file);
169  QString baseName = fi.baseName();
170 
171  if (m_suffix == ".bin" && baseName.contains("Prec5km"))
172  {
173  QString auxFile(file);
174  size_t pos = auxFile.indexOf(baseName);
175  auxFile.remove(0, (int)pos);
176  pos = auxFile.indexOf("_");
177  size_t pp = auxFile.indexOf(".bin");
178  int offset = atoi(auxFile.mid((int)pos + 1, (int)pp - (int)pos + 1).toStdString().c_str());
179  size_t fileSize = m_nlines * m_ncols * 4 + 8; // dado é float e desprepreza 4 bytes iniciais e 4 bytes finais
180  offset *= (int)fileSize;
181  auxFile.remove((int)pos, auxFile.length() - (int)pos);
182  auxFile = path + auxFile + m_suffix;
183 
184  size_t nchars = m_ncols * 4;
185  uchar* buf = new uchar[nchars];
186  FILE* fp = fopen(auxFile.toStdString().c_str(), "rb");
187  fseek(fp, offset, SEEK_SET);
188  fseek(fp, 4, SEEK_CUR); // despreza 4 bytes da primeira linha
189  m_image = new QImage((int)m_ncols, (int)m_nlines, QImage::Format_ARGB32);
190  m_image->fill(Qt::transparent);
191 
192  uchar uc[5];
193  uc[4] = 0;
194  for (size_t j = 0; j < m_nlines; ++j)
195  {
196  uchar* u = m_image->scanLine((int)m_nlines - 1 - (int)j); // origem bottom left
197  fread(buf, nchars, sizeof(char), fp);
198 
199  for (size_t i = 0; i < m_ncols; i++)
200  {
201  uc[0] = *(buf + (i << 2));
202  uc[1] = *(buf + (1 + (i << 2)));
203  uc[2] = *(buf + (2 + (i << 2)));
204  uc[3] = *(buf + (3 + (i << 2)));
205  float b = *(float*)uc;
206  if (b != m_undef)
207  {
208  uchar a = (uchar)(b * 10000. + .5);
209  QRgb* v = (QRgb*)(u + (i << 2));
210  *v = qRgba(m_lut[a].red(), m_lut[a].green(), m_lut[a].blue(), 255);
211  }
212  }
213  }
214  fclose(fp);
215  delete[]buf;
216  }
217 }
218 
219 void drawCurrentImage(QPainter* p)
220 {
221 }
222 
224 {
225  QString file(fileName);
226  int ind = file.indexOf("Prec5km") + (int)strlen("Prec5km");
227  file.remove(0, ind);
228  QString ano = file.mid(0, 4);
229  QString mes = file.mid(4, 2);
230  QString dia = file.mid(6, 2);
231  QString hour = file.mid(8, 2);
232 
233  te::dt::Date date(ano.toInt(), mes.toInt(), dia.toInt());
234  te::dt::TimeDuration dur(hour.toInt() + 1, 0, 0); // tem deslocamento de 1 hora
235  return te::dt::TimeInstant(date, dur);
236 }
237 
239 {
240  std::vector<std::pair<int, QColor> > tab;
241  int i = 0;
242  tab.push_back(std::pair<int, QColor>(i += 1, QColor(2, 1, 201, 255)));
243  tab.push_back(std::pair<int, QColor>(i += 1, QColor(2, 18, 213, 255)));
244  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 24, 220, 255)));
245  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 36, 227, 255)));
246  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 54, 240, 255)));
247  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 71, 254, 255)));
248  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 90, 252, 255)));
249  tab.push_back(std::pair<int, QColor>(i += 1, QColor(3, 106, 251, 255)));
250  tab.push_back(std::pair<int, QColor>(i += 1, QColor(3, 130, 253, 255)));
251  tab.push_back(std::pair<int, QColor>(i += 1, QColor(3, 148, 250, 255)));
252  tab.push_back(std::pair<int, QColor>(i += 1, QColor(3, 160, 250, 255)));
253  tab.push_back(std::pair<int, QColor>(i += 1, QColor(4, 174, 250, 255)));
254  tab.push_back(std::pair<int, QColor>(i += 1, QColor(4, 186, 250, 255)));
255  tab.push_back(std::pair<int, QColor>(i += 1, QColor(5, 200, 251, 255)));
256  tab.push_back(std::pair<int, QColor>(i += 1, QColor(5, 214, 246, 255)));
257  tab.push_back(std::pair<int, QColor>(i += 1, QColor(3, 227, 241, 255)));
258  tab.push_back(std::pair<int, QColor>(i += 1, QColor(3, 240, 237, 255)));
259  tab.push_back(std::pair<int, QColor>(i += 1, QColor(2, 254, 233, 255)));
260  tab.push_back(std::pair<int, QColor>(i += 1, QColor(2, 254, 212, 255)));
261  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 190, 255)));
262  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 170, 255)));
263  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 160, 255)));
264  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 151, 255)));
265  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 105, 255)));
266  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 72, 255)));
267  tab.push_back(std::pair<int, QColor>(i += 1, QColor(1, 254, 37, 255)));
268  tab.push_back(std::pair<int, QColor>(i += 1, QColor(2, 254, 4, 255)));
269  tab.push_back(std::pair<int, QColor>(i += 1, QColor(25, 254, 4, 255)));
270  tab.push_back(std::pair<int, QColor>(i += 1, QColor(56, 254, 3, 255)));
271  tab.push_back(std::pair<int, QColor>(i += 1, QColor(71, 254, 3, 255)));
272  tab.push_back(std::pair<int, QColor>(i += 1, QColor(99, 254, 2, 255)));
273  tab.push_back(std::pair<int, QColor>(i += 1, QColor(125, 254, 2, 255)));
274  tab.push_back(std::pair<int, QColor>(i += 1, QColor(150, 254, 2, 255)));
275  tab.push_back(std::pair<int, QColor>(i += 1, QColor(175, 254, 2, 255)));
276  tab.push_back(std::pair<int, QColor>(i += 1, QColor(200, 255, 1, 255)));
277  tab.push_back(std::pair<int, QColor>(i += 1, QColor(209, 255, 1, 255)));
278  tab.push_back(std::pair<int, QColor>(i += 1, QColor(218, 255, 1, 255)));
279  tab.push_back(std::pair<int, QColor>(i += 1, QColor(229, 255, 1, 255)));
280  tab.push_back(std::pair<int, QColor>(i += 1, QColor(237, 255, 0, 255)));
281  tab.push_back(std::pair<int, QColor>(i += 1, QColor(240, 248, 0, 255)));
282  tab.push_back(std::pair<int, QColor>(i += 1, QColor(244, 241, 0, 255)));
283  tab.push_back(std::pair<int, QColor>(i += 1, QColor(250, 237, 0, 255)));
284  tab.push_back(std::pair<int, QColor>(i += 1, QColor(251, 232, 2, 255)));
285  tab.push_back(std::pair<int, QColor>(i += 1, QColor(251, 226, 2, 255)));
286  tab.push_back(std::pair<int, QColor>(i += 1, QColor(252, 215, 2, 255)));
287  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 208, 2, 255)));
288  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 199, 2, 255)));
289  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 194, 2, 255)));
290  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 190, 2, 255)));
291  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 184, 2, 255)));
292  tab.push_back(std::pair<int, QColor>(i += 1, QColor(252, 179, 2, 255)));
293  tab.push_back(std::pair<int, QColor>(i += 1, QColor(252, 172, 2, 255)));
294  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 164, 2, 255)));
295  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 155, 2, 255)));
296  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 147, 4, 255)));
297  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 136, 4, 255)));
298  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 125, 3, 255)));
299  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 112, 3, 255)));
300  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 99, 2, 255)));
301  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 75, 2, 255)));
302  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 50, 2, 255)));
303  tab.push_back(std::pair<int, QColor>(i += 1, QColor(253, 25, 2, 255)));
304  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 1, 2, 255)));
305  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 1, 12, 255)));
306  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 1, 25, 255)));
307  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 1, 37, 255)));
308  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 0, 49, 255)));
309  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 2, 37, 255)));
310  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 3, 25, 255)));
311  tab.push_back(std::pair<int, QColor>(i += 1, QColor(254, 4, 12, 255)));
312  tab.push_back(std::pair<int, QColor>(i += 1, QColor(255, 5, 1, 255)));
313 
315 }
virtual ~Eta5kmItem()
Destructor It destructs a Eta5km Item.
Definition: Eta5kmItem.cpp:23
te::dt::TimeInstant getTime(QString file)
Get time of the raster data.
Definition: Eta5kmItem.cpp:223
bool getCtlParameters()
Get control parameters.
Definition: Eta5kmItem.cpp:84
A widget to control the display of a set of layers.
Definition: MapDisplay.h:66
void loadCurrentImage()
Load current image.
Definition: Eta5kmItem.cpp:160
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
This file defines a class for a Animation Scene.
This file defines a class for a Eta5kmItem.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
A base class for date data types.
Definition: Date.h:53
Eta5kmItem()
Empty Constructor.
Definition: Eta5kmItem.cpp:13
URI C++ Library.
A class to represent time period.
Definition: TimePeriod.h:54
This file defines a class for a Trajectory Animation.
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:238
void drawCurrentImage(QPainter *p)
Definition: Eta5kmItem.cpp:219
bool loadData()
Load temporal raster data.
Definition: Eta5kmItem.cpp:27
void setLUT(const std::vector< std::pair< int, QColor > > &tab)
Sets the LUT. tab The LUT information.
Definition: ImageItem.cpp:441