HidroItem.cpp
Go to the documentation of this file.
1 // TerraLib
2 #include "HidroItem.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::HidroItem::HidroItem(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  m_animation->m_temporalExtent = te::dt::TimePeriod(ti, tf);
49 
50  // set data
52  for (size_t i = 0; i < count; ++i)
53  {
54  QString f = files[(int)i];
55  t = getTime(f);
56  m_time.push_back(t);
57  m_files.push_back(f);
58  }
59 
60  generateRoute();
61  setLUT();
62  return true;
63 }
64 
66 {
67  char buf[2000];
68  QString file(m_dir.path() + "/racc.ctl");
69  FILE* fp = fopen(file.toStdString().c_str(), "r");
70  if(fp == 0)
71  return false;
72 
73  fread(buf, 2000, sizeof(char), fp);
74  fclose(fp);
75  QString s, ss(QString(buf).simplified());
76 
77  // validation
78  if(!(ss.contains("undef ", Qt::CaseInsensitive) && ss.contains("title", Qt::CaseInsensitive) &&
79  ss.contains("xdef", Qt::CaseInsensitive) && ss.contains("ydef", Qt::CaseInsensitive) &&
80  ss.contains("linear", Qt::CaseInsensitive) && ss.contains("zdef", Qt::CaseInsensitive)))
81  return false;
82 
83  // CHUTE SRID 4326 WGS84
84  m_SRID = 4326;
85 
86  // get UNDEF value
87  size_t pos = ss.indexOf("UNDEF ", Qt::CaseInsensitive) + strlen("UNDEF ");
88  ss.remove(0, (int)pos);
89  pos = ss.indexOf("TITLE", Qt::CaseInsensitive);
90  s = ss.left((int)pos);
91  m_undef = atoi(s.toStdString().c_str());
92  ss.remove(0, (int)pos);
93 
94  // get XDEF ulx and resX values
95  pos = ss.indexOf("XDEF ", Qt::CaseInsensitive) + strlen("XDEF ");
96  ss.remove(0, (int)pos);
97  pos = ss.indexOf(" ");
98  s = ss.left((int)pos);
99  m_ncols = atoi(s.toStdString().c_str());
100  ss.remove(0, (int)pos);
101 
102  pos = ss.indexOf("LINEAR ", Qt::CaseInsensitive) + strlen("LINEAR ");
103  ss.remove(0, (int)pos);
104  pos = ss.indexOf(" ");
105  s = ss.left((int)pos);
106  double llx = atof(s.toStdString().c_str()) - 360.;
107  ss.remove(0, (int)pos);
108  pos = ss.indexOf("YDEF ", Qt::CaseInsensitive);
109  s = ss.left((int)pos);
110  double resX = atof(s.toStdString().c_str());
111  ss.remove(0, (int)pos);
112 
113  // get YDEF uly and resY values
114  pos = ss.indexOf("YDEF ", Qt::CaseInsensitive) + strlen("YDEF ");
115  ss.remove(0, (int)pos);
116  pos = ss.indexOf(" ");
117  s = ss.left((int)pos);
118  m_nlines = atoi(s.toStdString().c_str());
119  ss.remove(0, (int)pos);
120 
121  pos = ss.indexOf("LINEAR ", Qt::CaseInsensitive) + strlen("LINEAR ");
122  ss.remove(0, (int)pos);
123  pos = ss.indexOf(" ");
124  s = ss.left((int)pos);
125  double lly = atof(s.toStdString().c_str());
126  ss.remove(0, (int)pos);
127  pos = ss.indexOf("ZDEF ", Qt::CaseInsensitive);
128  s = ss.left((int)pos);
129  double resY = atof(s.toStdString().c_str());
130  ss.remove(0, (int)pos);
131 
132  double w = (double)m_ncols * resX;
133  double h = (double)m_nlines * resY;
134  m_imaRect = QRectF(llx, lly, w, h);
135 
136  return true;
137 }
138 
140 {
141  if (m_image)
142  delete m_image;
143  m_image = 0;
144 
145  QString path = m_dir.absolutePath() + "/";
146  QString file = m_currentImageFile;
147  QFileInfo fi(file);
148  QString baseName = fi.baseName();
149 
150  if (m_suffix == ".bin" && baseName == "racc")
151  {
152  size_t nchars = m_ncols * 2;
153  uchar* buf = new uchar[nchars];
154  FILE* fp = fopen(file.toStdString().c_str(), "rb");
155  m_image = new QImage((int)m_ncols, (int)m_nlines, QImage::Format_ARGB32);
156  m_image->fill(Qt::transparent);
157 
158  uchar uc[3];
159  uc[2] = 0;
160  for (size_t j = 0; j < m_nlines; ++j)
161  {
162  uchar* u = m_image->scanLine((int)j);
163  fread(buf, nchars, sizeof(char), fp);
164  for (size_t i = 0; i < m_ncols; i++)
165  {
166  uc[0] = *(buf + (i << 1));
167  uc[1] = *(buf + (1 + (i << 1)));
168  ushort b = *(ushort*)uc;
169  if (b != m_undef)
170  {
171  b = (b + 5) / 10;
172  QRgb* v = (QRgb*)(u + (i << 2));
173  *v = qRgba(m_lut[b].red(), m_lut[b].green(), m_lut[b].blue(), 255);
174  }
175  }
176  }
177  fclose(fp);
178  delete[]buf;
179  }
180 }
181 
183 {
184  QString file(fileName);
185  int ind = file.indexOf("racc.") + (int)strlen("racc.");
186  file.remove(0, ind);
187  QString ano = "20" + file.mid(0, 2);
188  QString mes = file.mid(2, 2);
189  QString dia = file.mid(4, 2);
190  QString hour = file.mid(7, 2);
191  QString min = file.mid(9, 2);
192 
193  te::dt::Date date(ano.toInt(), mes.toInt(), dia.toInt());
194  te::dt::TimeDuration dur(hour.toInt(), min.toInt(), 0);
195  return te::dt::TimeInstant(date, dur);
196 }
197 
199 {
200  std::vector<std::pair<int, QColor> > tab;
201  int i = 0;
202  tab.push_back(std::pair<int, QColor>(i += 3, QColor(2, 1, 201, 255)));
203  tab.push_back(std::pair<int, QColor>(i += 3, QColor(1, 71, 254, 255)));
204  tab.push_back(std::pair<int, QColor>(i += 3, QColor(3, 148, 250, 255)));
205  tab.push_back(std::pair<int, QColor>(i += 3, QColor(5, 200, 251, 255)));
206  tab.push_back(std::pair<int, QColor>(i += 3, QColor(2, 254, 233, 255)));
207  tab.push_back(std::pair<int, QColor>(i += 3, QColor(1, 254, 151, 255)));
208  tab.push_back(std::pair<int, QColor>(i += 3, QColor(2, 254, 4, 255)));
209  tab.push_back(std::pair<int, QColor>(i += 3, QColor(99, 254, 2, 255)));
210  tab.push_back(std::pair<int, QColor>(i += 3, QColor(200, 255, 1, 255)));
211  tab.push_back(std::pair<int, QColor>(i += 3, QColor(237, 255, 0, 255)));
212  tab.push_back(std::pair<int, QColor>(i += 3, QColor(251, 232, 2, 255)));
213  tab.push_back(std::pair<int, QColor>(i += 3, QColor(254, 199, 2, 255)));
214  tab.push_back(std::pair<int, QColor>(i += 3, QColor(252, 179, 2, 255)));
215  tab.push_back(std::pair<int, QColor>(i += 3, QColor(254, 147, 4, 255)));
216  tab.push_back(std::pair<int, QColor>(i += 3, QColor(253, 99, 2, 255)));
217  tab.push_back(std::pair<int, QColor>(i += 3, QColor(254, 1, 2, 255)));
218  tab.push_back(std::pair<int, QColor>(i += 3, QColor(254, 0, 49, 255)));
219  tab.push_back(std::pair<int, QColor>(i += 3, QColor(255, 5, 1, 255)));
220 
222 }
HidroItem()
Empty Constructor.
Definition: HidroItem.cpp:13
void setLUT()
Sets the default LUT.
Definition: HidroItem.cpp:198
A widget to control the display of a set of layers.
Definition: MapDisplay.h:66
A class to represent time instant.
Definition: TimeInstant.h:55
This file defines a class for a Animation Scene.
te::dt::TimeInstant getTime(QString file)
Get time of the raster data.
Definition: HidroItem.cpp:182
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
A base class for date data types.
Definition: Date.h:53
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 loadCurrentImage()
Get current image.
Definition: HidroItem.cpp:139
bool getCtlParameters()
Get control parameters.
Definition: HidroItem.cpp:65
virtual ~HidroItem()
Destructor It destructs a Hidro Item.
Definition: HidroItem.cpp:23
bool loadData()
Load temporal raster data.
Definition: HidroItem.cpp:27
void setLUT(const std::vector< std::pair< int, QColor > > &tab)
Sets the LUT. tab The LUT information.
Definition: ImageItem.cpp:441