All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 {
40 }
41 
43 {
44 }
45 
46 std::auto_ptr<te::dt::AbstractData>
48 {
49  TimeSeriesObservationSet::const_iterator itlower, itupper, itbegin, itend;
50  const TimeSeriesObservationSet& tsObs = ts.getObservations();
51 
52  TimeSeriesObservation item(static_cast<te::dt::DateTime*>(time->clone()),0);
53  itlower = tsObs.lower_bound(item);
54  itupper = tsObs.upper_bound(item);
55 
56  //If both iterator points to the same position (the first element after item), we have to get
57  //the first element before item
58  if(itlower==itupper && itlower!=tsObs.begin())
59  --itlower;
60 
61  if(itlower!=tsObs.end() && itupper!=tsObs.end())
62  {
63  long distLower = GetDistance(itlower->getTime(),time);
64  long distUpper = GetDistance(itupper->getTime(),time);
65  if(distLower<distUpper)
66  return std::auto_ptr<te::dt::AbstractData>(itlower->getValue()->clone());
67  else
68  return std::auto_ptr<te::dt::AbstractData>(itupper->getValue()->clone());
69  }
70 
71  if(itlower!=tsObs.end() && itupper==tsObs.end())
72  return std::auto_ptr<te::dt::AbstractData>(itlower->getValue()->clone());
73 
74  return std::auto_ptr<te::dt::AbstractData>(0);
75 }
76 
77 
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.
std::auto_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.
const TimeSeriesObservationSet & getObservations() const
It returns the time series observations.
Definition: TimeSeries.cpp:133
A class to represent time series.
Definition: TimeSeries.h:66
virtual AbstractData * clone() const =0
It returns a clone of this object.
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