CoverageSeries.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 Coverage.cpp
22 
23  \brief This file contains an abstract class to represent a coverage.
24 */
25 
26 //TerraLib
27 #include "../../../geometry/Polygon.h"
28 #include "../../../geometry/Point.h"
29 #include "../../../geometry/Utils.h"
30 #include "../../../datatype/DateTime.h"
31 #include "../../../datatype/DateTimeInstant.h"
32 #include "../../../datatype/DateTimeUtils.h"
33 
34 //ST
35 #include "CoverageSeries.h"
37 #include "../interpolator/AbstractCoverageSeriesInterp.h"
38 #include "../interpolator/NearestCoverageAtTimeInterp.h"
39 
41  :
42 
43  m_interpolator(&NearestCoverageAtTimeInterp::getInstance()),
44  m_cvtype(te::st::UNKNOWN)
45 
46 {
47 }
48 
51  m_observations(obs),
52  m_interpolator(interp),
53  m_cvtype(t),
54  m_sextent(se)
55 {
56 }
57 
59 {
60  return m_observations;
61 }
62 
64 {
65  return m_cvtype;
66 }
67 
69 {
70  return m_sextent.get();
71 }
72 
73 std::unique_ptr<te::dt::DateTimePeriod> te::st::CoverageSeries::getTemporalExtent() const
74 {
75  te::dt::DateTime* bt = m_observations.begin()->first.get();
76  te::dt::DateTime* et = m_observations.rbegin()->first.get();
77  //This function does not take the ownership of the given times
78  return std::unique_ptr<te::dt::DateTimePeriod>(te::dt::GetTemporalExtent(bt, et));
79 }
80 
82 {
83  te::dt::DateTimeShrPtr t(time);
84  CoverageShrPtr c(cv);
86  add(obs);
87 }
88 
90 {
91  m_observations.insert(o);
92 }
93 
94 std::size_t te::st::CoverageSeries::size() const
95 {
96  return m_observations.size();
97 }
98 
100 {
102 }
103 
105 {
107 }
108 
110 {
111  te::dt::DateTimeShrPtr aux(static_cast<te::dt::DateTime*>(t->clone()));
112  CoverageSeriesObservationSet::const_iterator itcs = m_observations.find(aux);
113  CoverageSeriesIterator it(itcs);
114  return it;
115 }
116 
117 std::unique_ptr<te::st::Coverage> te::st::CoverageSeries::getCoverage(te::dt::DateTime* t) const
118 {
119  te::dt::DateTimeShrPtr aux(static_cast<te::dt::DateTime*>(t->clone()));
120  CoverageSeriesObservationSet::const_iterator it = m_observations.find(aux);
121  if(it!=m_observations.end())
122  return std::unique_ptr<te::st::Coverage>(it->second->clone());
123 
124  return std::unique_ptr<te::st::Coverage>(m_interpolator->estimate(*this,t));
125 }
126 
127 std::unique_ptr<te::st::TimeSeries>
129 {
130  std::unique_ptr<te::st::TimeSeries> result(new te::st::TimeSeries());
131  result->setLocation(static_cast<te::gm::Geometry*>(l.clone()));
132 
133  CoverageSeriesObservationSet::const_iterator it = m_observations.begin();
134  while(it!=m_observations.end())
135  {
136  te::dt::DateTime* dt = static_cast<te::dt::DateTime*>(it->first->clone());
137  std::unique_ptr<te::dt::AbstractData> value(it->second->getValue(l,p));
138  result->add(dt, value.release());
139  ++it;
140  }
141  return result;
142 }
143 
144 void te::st::CoverageSeries::getTimeSeries(const te::gm::Point& l, boost::ptr_vector<TimeSeries>& r) const
145 {
146  CoverageSeriesObservationSet::const_iterator it = m_observations.begin();
147  if(it==m_observations.end())
148  return;
149 
150  unsigned int numts = it->second->getNumberOfProperties();
151  for(unsigned int i=0; i<numts; ++i)
152  {
153  std::unique_ptr<te::st::TimeSeries> ts(new te::st::TimeSeries());
154  ts->setLocation(static_cast<te::gm::Geometry*>(l.clone()));
155  r.push_back(ts.release());
156  }
157 
158  while(it!=m_observations.end())
159  {
160  boost::ptr_vector<te::dt::AbstractData> values;
161  it->second->getValue(l,values);
162  for(unsigned int i=0; i<numts; ++i)
163  r[i].add(static_cast<te::dt::DateTime*>(it->first->clone()), &values[i]);
164  values.release();
165  ++it;
166  }
167  return;
168 }
169 
170 void te::st::CoverageSeries::getTimeSeries(const te::gm::Polygon& l, unsigned int p, boost::ptr_vector<TimeSeries>& r) const
171 {
172  CoverageSeriesObservationSet::const_iterator it = m_observations.begin();
173  if(it==m_observations.end())
174  return;
175 
176  //==== First iteration: creates the time series and add the first values
177  //all values inside the polygon at time dt
178  boost::ptr_vector<te::dt::AbstractData> values;
179  it->second->getValue(l,p,values);
180 
181  for(unsigned int i=0; i<values.size(); ++i)
182  {
183  std::unique_ptr<te::st::TimeSeries> result(new te::st::TimeSeries());
184  result->setLocation(static_cast<te::gm::Geometry*>(l.clone()));
185  result->add(static_cast<te::dt::DateTime*>(it->first->clone()), &values[i]);
186  r.push_back(result.release());
187  }
188  values.release();
189  ++it;
190 
191  //==== Next iterations: add values into the time series
192  while(it!=m_observations.end())
193  {
194  //all values inside the polygon at time dt
195  it->second->getValue(l,p,values);
196 
197  for(unsigned int i=0; i<values.size(); ++i)
198  r[i].add(static_cast<te::dt::DateTime*>(it->first->clone()), &values[i]);
199 
200  values.release();
201  ++it;
202  }
203  return;
204 }
205 
207  boost::ptr_vector<TimeSeries>& r) const
208 {
209  std::unique_ptr<te::gm::Geometry> geom(te::gm::GetGeomFromEnvelope(&e, 0));
210  te::gm::Polygon* pol = static_cast<te::gm::Polygon*>(geom.get());
211  getTimeSeries(*pol, p, r);
212  return;
213 }
214 
215 void te::st::CoverageSeries::getTimeSeries(const te::gm::Polygon& l, boost::ptr_vector<TimeSeries>& r) const
216 {
217  CoverageSeriesObservationSet::const_iterator it = m_observations.begin();
218  if(it==m_observations.end())
219  return;
220 
221  //==== First iteration: creates the time series and add the first values
222  //all values inside the polygon at time dt
223  boost::ptr_vector<te::dt::AbstractData> values;
224  it->second->getValue(l,values);
225 
226  for(unsigned int i=0; i<values.size(); ++i)
227  {
228  std::unique_ptr<te::st::TimeSeries> result(new te::st::TimeSeries());
229  result->setLocation(static_cast<te::gm::Geometry*>(l.clone()));
230  result->add(static_cast<te::dt::DateTime*>(it->first->clone()), &values[i]);
231  r.push_back(result.release());
232  }
233  values.release();
234  ++it;
235 
236  //==== Next iterations: add values into the time series
237  while(it!=m_observations.end())
238  {
239  //all values inside the polygon at time dt
240  it->second->getValue(l,values);
241 
242  for(unsigned int i=0; i<values.size(); ++i)
243  r[i].add(static_cast<te::dt::DateTime*>(it->first->clone()), &values[i]);
244 
245  values.release();
246  ++it;
247  }
248  return;
249 }
250 
251 void te::st::CoverageSeries::getTimeSeries(const te::gm::Envelope& e, boost::ptr_vector<TimeSeries>& r) const
252 {
253  std::unique_ptr<te::gm::Geometry> geom(te::gm::GetGeomFromEnvelope(&e, 0));
254  te::gm::Polygon* pol = static_cast<te::gm::Polygon*>(geom.get());
255  getTimeSeries(*pol, r);
256  return;
257 }
258 
259 std::unique_ptr<te::st::TimeSeries> te::st::CoverageSeries::getTimeSeries(const te::gm::Point& l, const te::dt::DateTime& t,
260  te::dt::TemporalRelation tr, unsigned int p) const
261 {
262  std::unique_ptr<te::st::CoverageSeries> aux = getPatch(t,tr);
263  return aux->getTimeSeries(l,p);
264 }
265 
267  te::dt::TemporalRelation tr, boost::ptr_vector<TimeSeries>& r) const
268 {
269  std::unique_ptr<te::st::CoverageSeries> aux = getPatch(t,tr);
270  return aux->getTimeSeries(l,r);
271 }
272 
274  te::dt::TemporalRelation tr, boost::ptr_vector<TimeSeries>& r) const
275 {
276  std::unique_ptr<te::st::CoverageSeries> aux = getPatch(t,tr);
277  return aux->getTimeSeries(l,p,r);
278 }
279 
280 
282  te::dt::TemporalRelation tr, boost::ptr_vector<TimeSeries>& r) const
283 {
284  std::unique_ptr<te::st::CoverageSeries> aux = getPatch(t,tr);
285  return aux->getTimeSeries(e,p,r);
286 }
287 
288 
290  te::dt::TemporalRelation tr, boost::ptr_vector<TimeSeries>& r) const
291 {
292  std::unique_ptr<te::st::CoverageSeries> aux = getPatch(t,tr);
293  return aux->getTimeSeries(l,r);
294 }
295 
297  te::dt::TemporalRelation tr, boost::ptr_vector<TimeSeries>& r) const
298 {
299  std::unique_ptr<te::st::CoverageSeries> aux = getPatch(t,tr);
300  return aux->getTimeSeries(e,r);
301 }
302 
303 std::unique_ptr<te::st::CoverageSeries>
305 {
306  std::unique_ptr<te::st::CoverageSeries> cs (new CoverageSeries());
307  //Note: the end iterator of a patch points to the position AFTER the last required observation
308  CoverageSeriesObservationSet::const_iterator itb = m_observations.end();
309  CoverageSeriesObservationSet::const_iterator ite = m_observations.end();
310 
311  te::dt::DateTimeShrPtr shrdt(static_cast<te::dt::DateTime*>(dt.clone()));
312 
313  if(r==te::dt::AFTER) //2
314  {
315  itb = m_observations.upper_bound(shrdt);
316  }
317  else if(r==(te::dt::AFTER | te::dt::EQUALS)) // 2 OU 8 = 10
318  {
319  itb = m_observations.find(shrdt);
320  if(itb==m_observations.end())
321  itb = m_observations.upper_bound(shrdt);
322  }
323  else if(r==te::dt::BEFORE) // 1
324  {
325  itb = m_observations.begin();
326  ite = m_observations.find(shrdt);
327  if(ite==m_observations.end())
328  ite = m_observations.upper_bound(shrdt);
329  }
330  else if(r==(te::dt::BEFORE | te::dt::EQUALS)) // 1 OU 8 = 9
331  {
332  itb = m_observations.begin();
333  ite = m_observations.upper_bound(shrdt);
334  }
335  else if(r==te::dt::DURING) //4
336  {
337  te::dt::DateTimePeriod* auxt = static_cast<te::dt::DateTimePeriod*>(shrdt.get());
340  itb = m_observations.find(t1);
341  if(itb==m_observations.end())
342  itb = m_observations.upper_bound(t1);
343  ite = m_observations.upper_bound(t2);
344  }
345  else if(r==te::dt::EQUALS) //8
346  {
347  std::pair<CoverageSeriesObservationSet::const_iterator, CoverageSeriesObservationSet::const_iterator> itPair;
348  itPair = m_observations.equal_range(shrdt);
349  itb = itPair.first;
350  ite = itPair.second;
351  if(ite!= m_observations.end())
352  ++ite;
353  }
354 
355  while(itb != ite)
356  {
357  cs->add(*itb);
358  ++itb;
359  }
360 
361  return cs;
362 }
363 
364 std::unique_ptr<te::st::CoverageSeries>
366 {
367  return std::unique_ptr<te::st::CoverageSeries>();
368 }
369 
370 std::unique_ptr<te::st::CoverageSeries>
372 {
373  return std::unique_ptr<te::st::CoverageSeries>();
374 }
375 
376 
377 std::unique_ptr<te::st::CoverageSeries>
379  const te::dt::DateTime& /*dt*/, te::dt::TemporalRelation /*r*/) const
380 {
381  return std::unique_ptr<te::st::CoverageSeries>();
382 }
383 
384 
385 std::unique_ptr<te::st::CoverageSeries>
387  const te::dt::DateTime& /*dt*/, te::dt::TemporalRelation /*tr*/) const
388 {
389  return std::unique_ptr<te::st::CoverageSeries>();
390 }
391 
static boost::posix_time::ptime bt
An abstract class for interpolation function or interpolator that estimate coverages at non-observed ...
std::unique_ptr< te::dt::DateTimePeriod > getTemporalExtent() const
It returns the temporal extent of the coverage series.
This file contains a class to represent a CoverageSeries observation.
CoverageSeriesIterator end() const
It returns an iterator that points to the end of the time series.
CoverageSeriesIterator begin() const
It returns an iterator that points to the first observation of the point coverage.
std::map< te::dt::DateTimeShrPtr, CoverageShrPtr, CompareShrDateTime > CoverageSeriesObservationSet
boost::shared_ptr< te::st::Coverage > CoverageShrPtr
Definition: Coverage.h:318
std::pair< te::dt::DateTimeShrPtr, CoverageShrPtr > CoverageSeriesObservation
CoverageType
An enum for the types of coverage.
TemporalRelation
Temporal relations between date and time (Source: Allen, 1991).
std::unique_ptr< te::gm::Geometry > m_sextent
SpatialRelation
Spatial relations between geometric objects.
AbstractCoverageSeriesInterp * m_interpolator
The coverage series observations.
virtual std::unique_ptr< Coverage > estimate(const CoverageSeries &cvs, te::dt::DateTime *time) const =0
It estimates a coverage of a coverage series at a given non-observed time .
boost::shared_ptr< DateTime > DateTimeShrPtr
Definition: DateTime.h:126
virtual te::dt::AbstractData * clone() const
It clones the linestring.
A class to traverse the observations of a CoverageSeries.
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
virtual te::dt::AbstractData * clone() const
It clones the point.
URI C++ Library.
Definition: Attributes.h:37
const CoverageSeriesObservationSet & getObservations() const
It returns the coverage series observations.
std::unique_ptr< te::st::Coverage > getCoverage(te::dt::DateTime *t) const
It returns the coverage associated to a given date and time.
static te::dt::TimeDuration dt(20, 30, 50, 11)
std::unique_ptr< te::st::TimeSeries > getTimeSeries(const te::gm::Point &l, unsigned int p=0) const
It returns a time series of the p-th property associated to a given location.
te::gm::Polygon * p
virtual AbstractData * clone() const =0
It returns a clone of this object.
CoverageSeriesIterator at(te::dt::DateTime *t) const
It returns an iterator that points to an observation at a given time.
CoverageSeries()
A constructor.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
This file contains a class to represent a coverage series.
CoverageSeriesObservationSet m_observations
std::size_t size() const
It returns the size of the coverage series observations.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
An abstract class to represent a period of date and time.
A class to represent time series.
Definition: TimeSeries.h:66
CoverageType getType() const
It returns the type of the internal coverages.
te::gm::Geometry * getSpatialExtent() const
It returns the spatial extent of the coverage series.
An abstract class to represent a coverage.
Definition: Coverage.h:63
virtual DateTimeInstant * getInitialInstant() const =0
It gets the initial date time instant.
It is an interpolation function the estimates the nearest coverage at a given non-observed time...
TEDATATYPEEXPORT DateTimePeriod * GetTemporalExtent(const DateTime *t1, const DateTime *t2)
It returns the temporal extent of two date and time types.
virtual ~CoverageSeries()
Virtual destructor.
std::unique_ptr< CoverageSeries > getPatch(const te::dt::DateTime &dt, te::dt::TemporalRelation r=te::dt::DURING) const
It returns a subset or patch of the coverage series considering a given temporal restriction.
virtual DateTimeInstant * getFinalInstant() const =0
It gets the final date time instant.
void add(te::dt::DateTime *time, te::st::Coverage *cv)
It adds an observation (time and coverage) into the coverage series.
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.