NearestValueAtTimeInterp.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 NearestValueAtTimeInterp.cpp
22 
23  \brief This file contains an interpolation function that estimates the
24  nearest value at time of a time series.
25 */
26 
27 
28 // TerraLib
29 #include "../../../datatype/DateTime.h"
30 #include "../../../datatype/DateTimeUtils.h"
31 #include "../../../datatype/AbstractData.h"
32 
33 // ST
34 #include "../timeseries/TimeSeries.h"
35 #include "../timeseries/TimeSeriesObservation.h"
37 
39 
41 
42 std::unique_ptr<te::dt::AbstractData>
44 {
45  TimeSeriesObservationSet::const_iterator itlower, itupper, itbegin, itend;
46  const TimeSeriesObservationSet& tsObs = ts.getObservations();
47 
48  TimeSeriesObservation item(static_cast<te::dt::DateTime*>(time->clone()),nullptr);
49  itlower = tsObs.lower_bound(item);
50  itupper = tsObs.upper_bound(item);
51 
52  //If both iterator points to the same position (the first element after item), we have to get
53  //the first element before item
54  if(itlower==itupper && itlower!=tsObs.begin())
55  --itlower;
56 
57  if(itlower!=tsObs.end() && itupper!=tsObs.end())
58  {
59  long distLower = GetDistance(itlower->getTime(),time);
60  long distUpper = GetDistance(itupper->getTime(),time);
61  if(distLower<distUpper)
62  return std::unique_ptr<te::dt::AbstractData>(itlower->getValue()->clone());
63  else
64  return std::unique_ptr<te::dt::AbstractData>(itupper->getValue()->clone());
65  }
66 
67  if(itlower!=tsObs.end() && itupper==tsObs.end())
68  return std::unique_ptr<te::dt::AbstractData>(itlower->getValue()->clone());
69 
70  return std::unique_ptr<te::dt::AbstractData>(nullptr);
71 }
72 
73 
virtual ~NearestValueAtTimeInterp()
Virtual destructor.
TEDATATYPEEXPORT long GetDistance(const te::dt::DateTime *t1, const te::dt::DateTime *t2)
It returns the distance between two datetime types.
A class to represent an observation (time and value) of a time series.
virtual AbstractData * clone() const =0
It returns a clone of this object.
const TimeSeriesObservationSet & getObservations() const
It returns the time series observations.
Definition: TimeSeries.cpp:125
std::unique_ptr< te::dt::AbstractData > estimate(const TimeSeries &ts, te::dt::DateTime *time) const
It estimates a value at a given non-observed time of a time series.
A class to represent time series.
Definition: TimeSeries.h:66
NearestValueAtTimeInterp()
Constructor.
boost::multi_index_container< TimeSeriesObservation, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::identity< TimeSeriesObservation > >, boost::multi_index::ordered_non_unique< boost::multi_index::const_mem_fun< TimeSeriesObservation, double,&TimeSeriesObservation::getDouble > >, boost::multi_index::ordered_non_unique< boost::multi_index::const_mem_fun< TimeSeriesObservation, std::string,&TimeSeriesObservation::getString > > > > TimeSeriesObservationSet