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 
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 
26 {
27  if (getCtlParameters() == false)
28  return false;
29 
30  //m_animationScene->addItem(pi);
31  if (m_animation)
32  delete m_animation;
33  m_animation = new te::qt::widgets::Animation(this, "pos");
35  m_animation->setEasingCurve(QEasingCurve::Linear);
36  //m_parallelAnimation->addAnimation(animation);
37 
38  QStringList nameFilter;
39  nameFilter.append("*.bin");
40  QStringList files = m_dir.entryList(nameFilter, QDir::Files, QDir::Name);
41  size_t count = files.count();
42 
43  // get time extent
44  te::dt::TimeInstant ti = getTime(m_dir.path() + "/" + files.first());
45  te::dt::TimeInstant tf = getTime(m_dir.path() + "/" + files.last());
47 
48  // set data
50  for (size_t i = 0; i < count; ++i)
51  {
52  QString f = files[(int)i];
53  t = getTime(f);
54  m_time.push_back(t);
55  m_files.push_back(f);
56  }
57 
58  generateRoute();
59  setLUT();
60  return true;
61 }
62 
64 {
65  char buf[500];
66  QString file(m_dir.path() + "/racc.ctl");
67  FILE* fp = fopen(te::core::CharEncoding::fromUTF8(file.toUtf8().data()).c_str(), "r");
68  if (fp == nullptr)
69  return false;
70 
71  size_t n = fread(buf, sizeof(char), 500, fp);
72  fclose(fp);
73  buf[n] = 0;
74  QString s, ss(QString(buf).simplified());
75 
76  // validation
77  if (!(ss.contains("undef ", Qt::CaseInsensitive) && ss.contains("title", Qt::CaseInsensitive) &&
78  ss.contains("xdef", Qt::CaseInsensitive) && ss.contains("ydef", Qt::CaseInsensitive) &&
79  ss.contains("linear", Qt::CaseInsensitive) && ss.contains("zdef", Qt::CaseInsensitive)))
80  return false;
81 
82  // CHUTE SRID 4326 WGS84
83  //m_SRID = 4326;
84 
85  // get UNDEF value
86  size_t pos = ss.indexOf("UNDEF ", Qt::CaseInsensitive) + strlen("UNDEF ");
87  ss.remove(0, (int)pos);
88  if (pos != std::string::npos)
89  {
90  pos = ss.indexOf("SRID", Qt::CaseInsensitive);
91  s = ss.left((int)pos);
92  m_undef = atoi(s.toUtf8().data());
93  ss.remove(0, (int)pos);
94 
95  // get SRID
96  pos = ss.indexOf("SRID ", Qt::CaseInsensitive) + (int)strlen("SRID ");
97  ss.remove(0, (int)pos);
98  pos = ss.indexOf("XDEF ", Qt::CaseInsensitive);
99  s = ss.left((int)pos);
100  m_SRID = atoi(s.toUtf8().data());
101  ss.remove(0, (int)pos);
102  }
103  else
104  m_SRID = 4326;
105 
106  // get XDEF ulx and resX values
107  pos = ss.indexOf("XDEF ", Qt::CaseInsensitive) + strlen("XDEF ");
108  ss.remove(0, (int)pos);
109  pos = ss.indexOf(" ");
110  s = ss.left((int)pos);
111  m_ncols = atoi(s.toUtf8().data());
112  ss.remove(0, (int)pos);
113 
114  pos = ss.indexOf("LINEAR ", Qt::CaseInsensitive) + strlen("LINEAR ");
115  ss.remove(0, (int)pos);
116  pos = ss.indexOf(" ");
117  s = ss.left((int)pos);
118  double llx = atof(s.toUtf8().data()) - 360.;
119  ss.remove(0, (int)pos);
120  pos = ss.indexOf("YDEF ", Qt::CaseInsensitive);
121  s = ss.left((int)pos);
122  double resX = atof(s.toUtf8().data());
123  ss.remove(0, (int)pos);
124 
125  // get YDEF uly and resY values
126  pos = ss.indexOf("YDEF ", Qt::CaseInsensitive) + strlen("YDEF ");
127  ss.remove(0, (int)pos);
128  pos = ss.indexOf(" ");
129  s = ss.left((int)pos);
130  m_nlines = atoi(s.toUtf8().data());
131  ss.remove(0, (int)pos);
132 
133  pos = ss.indexOf("LINEAR ", Qt::CaseInsensitive) + strlen("LINEAR ");
134  ss.remove(0, (int)pos);
135  pos = ss.indexOf(" ");
136  s = ss.left((int)pos);
137  double lly = atof(s.toUtf8().data());
138  ss.remove(0, (int)pos);
139  pos = ss.indexOf("ZDEF ", Qt::CaseInsensitive);
140  s = ss.left((int)pos);
141  double resY = atof(s.toUtf8().data());
142  ss.remove(0, (int)pos);
143 
144  double w = (double)m_ncols * resX;
145  double h = (double)m_nlines * resY;
146  m_imaRect = QRectF(llx, lly, w, h);
147 
148  // get static representation
149  if (m_dir.exists("staticRepresentation.png"))
150  m_staticRepresentation = QImage(m_dir.path() + "/staticRepresentation.png");
151 
152  return true;
153 }
154 
156 {
157  if (m_image)
158  delete m_image;
159  m_image = nullptr;
160 
161  if (m_currentImageFile.isEmpty())
162  {
163  if (m_staticRepresentation.isNull())
164  {
165  QRect r = getRect();
166  m_image = new QImage(r.size(), QImage::Format_ARGB32);
167  m_image->fill(QColor(0, 0, 255, 100).rgba());
168  QPainter p(m_image);
169  p.setPen(QPen(QColor(255, 0, 0)));
170 
171  QFont font(p.font());
172  int ps = 7;
173  int w = (int)((double)m_image->width() / 1.2);
174  int h = (int)((double)m_image->width() / 5.);
175 
176  font.setPointSize(ps);
177  QFontMetrics fm(font);
178  QRectF rec(fm.boundingRect(m_title));
179 
180  while (rec.width() < w && rec.height() < h)
181  {
182  ++ps;
183  font.setPointSize(ps);
184  QFontMetrics fm(font);
185  rec = fm.boundingRect(m_title);
186  }
187  rec.moveCenter(m_image->rect().center());
188  p.setFont(font);
189  p.drawText(rec.toRect(), Qt::AlignLeft, m_title);
190  }
191  else
192  m_image = new QImage(m_staticRepresentation);
193  }
194  else
195  {
196  QString path = m_dir.absolutePath() + "/";
197  QString file = m_currentImageFile;
198  QFileInfo fi(file);
199  QString baseName = fi.baseName();
200 
201  if (m_suffix == ".bin" && baseName == "racc")
202  {
203  size_t nchars = m_ncols * 2;
204  uchar* buf = new uchar[nchars];
205  FILE* fp = fopen(te::core::CharEncoding::fromUTF8(file.toUtf8().data()).c_str(), "rb");
206  m_image = new QImage((int)m_ncols, (int)m_nlines, QImage::Format_ARGB32);
207  m_image->fill(Qt::transparent);
208 
209  uchar uc[3];
210  uc[2] = 0;
211  for (size_t j = 0; j < m_nlines; ++j)
212  {
213  uchar* u = m_image->scanLine((int)j);
214  fread(buf, nchars, sizeof(char), fp);
215  for (size_t i = 0; i < m_ncols; i++)
216  {
217  uc[0] = *(buf + (i << 1));
218  uc[1] = *(buf + (1 + (i << 1)));
219  ushort b = *(ushort*)uc;
220  if (b != m_undef)
221  {
222  b = (b + 5) / 10;
223  QRgb* v = (QRgb*)(u + (i << 2));
224  *v = qRgba(m_lut[b].red(), m_lut[b].green(), m_lut[b].blue(), 255);
225  }
226  }
227  }
228  fclose(fp);
229  delete[]buf;
230  }
231  }
232 }
233 
235 {
236  QString file(fileName);
237  int ind = file.indexOf("racc.") + (int)strlen("racc.");
238  file.remove(0, ind);
239  QString ano = "20" + file.mid(0, 2);
240  QString mes = file.mid(2, 2);
241  QString dia = file.mid(4, 2);
242  QString hour = file.mid(7, 2);
243  QString min = file.mid(9, 2);
244 
245  te::dt::Date date(ano.toInt(), mes.toInt(), dia.toInt());
246  te::dt::TimeDuration dur(hour.toInt(), min.toInt(), 0);
247  return te::dt::TimeInstant(date, dur);
248 }
249 
251 {
252  std::vector<std::pair<int, QColor> > tab;
253  int i = 0;
254  tab.push_back(std::pair<int, QColor>(i += 3, QColor(2, 1, 201, 255)));
255  tab.push_back(std::pair<int, QColor>(i += 3, QColor(1, 71, 254, 255)));
256  tab.push_back(std::pair<int, QColor>(i += 3, QColor(3, 148, 250, 255)));
257  tab.push_back(std::pair<int, QColor>(i += 3, QColor(5, 200, 251, 255)));
258  tab.push_back(std::pair<int, QColor>(i += 3, QColor(2, 254, 233, 255)));
259  tab.push_back(std::pair<int, QColor>(i += 3, QColor(1, 254, 151, 255)));
260  tab.push_back(std::pair<int, QColor>(i += 3, QColor(2, 254, 4, 255)));
261  tab.push_back(std::pair<int, QColor>(i += 3, QColor(99, 254, 2, 255)));
262  tab.push_back(std::pair<int, QColor>(i += 3, QColor(200, 255, 1, 255)));
263  tab.push_back(std::pair<int, QColor>(i += 3, QColor(237, 255, 0, 255)));
264  tab.push_back(std::pair<int, QColor>(i += 3, QColor(251, 232, 2, 255)));
265  tab.push_back(std::pair<int, QColor>(i += 3, QColor(254, 199, 2, 255)));
266  tab.push_back(std::pair<int, QColor>(i += 3, QColor(252, 179, 2, 255)));
267  tab.push_back(std::pair<int, QColor>(i += 3, QColor(254, 147, 4, 255)));
268  tab.push_back(std::pair<int, QColor>(i += 3, QColor(253, 99, 2, 255)));
269  tab.push_back(std::pair<int, QColor>(i += 3, QColor(254, 1, 2, 255)));
270  tab.push_back(std::pair<int, QColor>(i += 3, QColor(254, 0, 49, 255)));
271  tab.push_back(std::pair<int, QColor>(i += 3, QColor(255, 5, 1, 255)));
272 
274 }
HidroItem()
Empty Constructor.
Definition: HidroItem.cpp:13
QRectF m_imaRect
Image location.
Definition: ImageItem.h:203
void setLUT()
Sets the default LUT.
Definition: HidroItem.cpp:250
std::map< uchar, QColor > m_lut
LUT.
Definition: ImageItem.h:204
te::gm::Envelope m_spatialExtent
Spatial extent.
Definition: Animation.h:109
A widget to control the display of a set of layers.
static std::string fromUTF8(const std::string &src)
Convert a string in UTF-8 to the current locale encoding.
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.
te::dt::TimeInstant getTime(QString file)
Get time of the raster data.
Definition: HidroItem.cpp:234
An Envelope defines a 2D rectangular region.
A base class for date data types.
Definition: Date.h:53
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 loadCurrentImage()
Get current image.
Definition: HidroItem.cpp:155
bool getCtlParameters()
Get control parameters.
Definition: HidroItem.cpp:63
virtual ~HidroItem()
Destructor It destructs a Hidro Item.
QString m_suffix
File suffix.
Definition: ImageItem.h:201
bool loadData()
Load temporal raster data.
Definition: HidroItem.cpp:25
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.