All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  m_observations(),
42  m_interpolator(&NearestCoverageAtTimeInterp::getInstance()),
43  m_cvtype(te::st::UNKNOWN),
44  m_sextent(0)
45 {
46 }
47 
50  m_observations(obs),
51  m_interpolator(interp),
52  m_cvtype(t),
53  m_sextent(se)
54 {
55 }
56 
58 {
59  return m_observations;
60 }
61 
63 {
64  return m_cvtype;
65 }
66 
68 {
69  return m_sextent.get();
70 }
71 
72 std::auto_ptr<te::dt::DateTimePeriod> te::st::CoverageSeries::getTemporalExtent() const
73 {
74  te::dt::DateTime* bt = m_observations.begin()->first.get();
75  te::dt::DateTime* et = m_observations.rbegin()->first.get();
76  //This function does not take the ownership of the given times
77  return std::auto_ptr<te::dt::DateTimePeriod>(te::dt::GetTemporalExtent(bt, et));
78 }
79 
81 {
82  te::dt::DateTimeShrPtr t(time);
83  CoverageShrPtr c(cv);
85  add(obs);
86 }
87 
89 {
90  m_observations.insert(o);
91 }
92 
93 std::size_t te::st::CoverageSeries::size() const
94 {
95  return m_observations.size();
96 }
97 
99 {
100  return te::st::CoverageSeriesIterator(m_observations.begin());
101 }
102 
104 {
105  return te::st::CoverageSeriesIterator(m_observations.end());
106 }
107 
109 {
110  te::dt::DateTimeShrPtr aux(static_cast<te::dt::DateTime*>(t->clone()));
111  CoverageSeriesObservationSet::const_iterator itcs = m_observations.find(aux);
112  CoverageSeriesIterator it(itcs);
113  return it;
114 }
115 
116 std::auto_ptr<te::st::Coverage> te::st::CoverageSeries::getCoverage(te::dt::DateTime* t) const
117 {
118  te::dt::DateTimeShrPtr aux(static_cast<te::dt::DateTime*>(t->clone()));
119  CoverageSeriesObservationSet::const_iterator it = m_observations.find(aux);
120  if(it!=m_observations.end())
121  return std::auto_ptr<te::st::Coverage>(it->second->clone());
122 
123  return std::auto_ptr<te::st::Coverage>(m_interpolator->estimate(*this,t));
124 }
125 
126 std::auto_ptr<te::st::TimeSeries>
128 {
129  std::auto_ptr<te::st::TimeSeries> result(new te::st::TimeSeries());
130  result->setLocation(static_cast<te::gm::Geometry*>(l.clone()));
131 
132  CoverageSeriesObservationSet::const_iterator it = m_observations.begin();
133  while(it!=m_observations.end())
134  {
135  te::dt::DateTime* dt = static_cast<te::dt::DateTime*>(it->first->clone());
136  std::auto_ptr<te::dt::AbstractData> value(it->second->getValue(l,p));
137  result->add(dt, value.release());
138  ++it;
139  }
140  return result;
141 }
142 
143 void te::st::CoverageSeries::getTimeSeries(const te::gm::Point& l, boost::ptr_vector<TimeSeries>& r) const
144 {
145  CoverageSeriesObservationSet::const_iterator it = m_observations.begin();
146  if(it==m_observations.end())
147  return;
148 
149  unsigned int numts = it->second->getNumberOfProperties();
150  for(unsigned int i=0; i<numts; ++i)
151  {
152  std::auto_ptr<te::st::TimeSeries> ts(new te::st::TimeSeries());
153  ts->setLocation(static_cast<te::gm::Geometry*>(l.clone()));
154  r.push_back(ts);
155  }
156 
157  while(it!=m_observations.end())
158  {
159  boost::ptr_vector<te::dt::AbstractData> values;
160  it->second->getValue(l,values);
161  for(unsigned int i=0; i<numts; ++i)
162  r[i].add(static_cast<te::dt::DateTime*>(it->first->clone()), &values[i]);
163  values.release();
164  ++it;
165  }
166  return;
167 }
168 
169 void te::st::CoverageSeries::getTimeSeries(const te::gm::Polygon& l, unsigned int p, boost::ptr_vector<TimeSeries>& r) const
170 {
171  CoverageSeriesObservationSet::const_iterator it = m_observations.begin();
172  if(it==m_observations.end())
173  return;
174 
175  //==== First iteration: creates the time series and add the first values
176  //all values inside the polygon at time dt
177  boost::ptr_vector<te::dt::AbstractData> values;
178  it->second->getValue(l,p,values);
179 
180  for(unsigned int i=0; i<values.size(); ++i)
181  {
182  std::auto_ptr<te::st::TimeSeries> result(new te::st::TimeSeries());
183  result->setLocation(static_cast<te::gm::Geometry*>(l.clone()));
184  result->add(static_cast<te::dt::DateTime*>(it->first->clone()), &values[i]);
185  r.push_back(result);
186  }
187  values.release();
188  ++it;
189 
190  //==== Next iterations: add values into the time series
191  while(it!=m_observations.end())
192  {
193  //all values inside the polygon at time dt
194  it->second->getValue(l,p,values);
195 
196  for(unsigned int i=0; i<values.size(); ++i)
197  r[i].add(static_cast<te::dt::DateTime*>(it->first->clone()), &values[i]);
198 
199  values.release();
200  ++it;
201  }
202  return;
203 }
204 
206  boost::ptr_vector<TimeSeries>& r) const
207 {
208  std::auto_ptr<te::gm::Geometry> geom(te::gm::GetGeomFromEnvelope(&e, 0));
209  te::gm::Polygon* pol = static_cast<te::gm::Polygon*>(geom.get());
210  getTimeSeries(*pol, p, r);
211  return;
212 }
213 
214 void te::st::CoverageSeries::getTimeSeries(const te::gm::Polygon& l, boost::ptr_vector<TimeSeries>& r) const
215 {
216  CoverageSeriesObservationSet::const_iterator it = m_observations.begin();
217  if(it==m_observations.end())
218  return;
219 
220  //==== First iteration: creates the time series and add the first values
221  //all values inside the polygon at time dt
222  boost::ptr_vector<te::dt::AbstractData> values;
223  it->second->getValue(l,values);
224 
225  for(unsigned int i=0; i<values.size(); ++i)
226  {
227  std::auto_ptr<te::st::TimeSeries> result(new te::st::TimeSeries());
228  result->setLocation(static_cast<te::gm::Geometry*>(l.clone()));
229  result->add(static_cast<te::dt::DateTime*>(it->first->clone()), &values[i]);
230  r.push_back(result);
231  }
232  values.release();
233  ++it;
234 
235  //==== Next iterations: add values into the time series
236  while(it!=m_observations.end())
237  {
238  //all values inside the polygon at time dt
239  it->second->getValue(l,values);
240 
241  for(unsigned int i=0; i<values.size(); ++i)
242  r[i].add(static_cast<te::dt::DateTime*>(it->first->clone()), &values[i]);
243 
244  values.release();
245  ++it;
246  }
247  return;
248 }
249 
250 void te::st::CoverageSeries::getTimeSeries(const te::gm::Envelope& e, boost::ptr_vector<TimeSeries>& r) const
251 {
252  std::auto_ptr<te::gm::Geometry> geom(te::gm::GetGeomFromEnvelope(&e, 0));
253  te::gm::Polygon* pol = static_cast<te::gm::Polygon*>(geom.get());
254  getTimeSeries(*pol, r);
255  return;
256 }
257 
258 std::auto_ptr<te::st::TimeSeries> te::st::CoverageSeries::getTimeSeries(const te::gm::Point& l, const te::dt::DateTime& t,
259  te::dt::TemporalRelation tr, unsigned int p) const
260 {
261  std::auto_ptr<te::st::CoverageSeries> aux = getPatch(t,tr);
262  return aux->getTimeSeries(l,p);
263 }
264 
266  te::dt::TemporalRelation tr, boost::ptr_vector<TimeSeries>& r) const
267 {
268  std::auto_ptr<te::st::CoverageSeries> aux = getPatch(t,tr);
269  return aux->getTimeSeries(l,r);
270 }
271 
273  te::dt::TemporalRelation tr, boost::ptr_vector<TimeSeries>& r) const
274 {
275  std::auto_ptr<te::st::CoverageSeries> aux = getPatch(t,tr);
276  return aux->getTimeSeries(l,p,r);
277 }
278 
279 
281  te::dt::TemporalRelation tr, boost::ptr_vector<TimeSeries>& r) const
282 {
283  std::auto_ptr<te::st::CoverageSeries> aux = getPatch(t,tr);
284  return aux->getTimeSeries(e,p,r);
285 }
286 
287 
289  te::dt::TemporalRelation tr, boost::ptr_vector<TimeSeries>& r) const
290 {
291  std::auto_ptr<te::st::CoverageSeries> aux = getPatch(t,tr);
292  return aux->getTimeSeries(l,r);
293 }
294 
296  te::dt::TemporalRelation tr, boost::ptr_vector<TimeSeries>& r) const
297 {
298  std::auto_ptr<te::st::CoverageSeries> aux = getPatch(t,tr);
299  return aux->getTimeSeries(e,r);
300 }
301 
302 std::auto_ptr<te::st::CoverageSeries>
304 {
305  std::auto_ptr<te::st::CoverageSeries> cs (new CoverageSeries());
306  //Note: the end iterator of a patch points to the position AFTER the last required observation
307  CoverageSeriesObservationSet::const_iterator itb = m_observations.end();
308  CoverageSeriesObservationSet::const_iterator ite = m_observations.end();
309 
310  te::dt::DateTimeShrPtr shrdt(static_cast<te::dt::DateTime*>(dt.clone()));
311 
312  if(r==te::dt::AFTER) //2
313  {
314  itb = m_observations.upper_bound(shrdt);
315  }
316  else if(r==(te::dt::AFTER | te::dt::EQUALS)) // 2 OU 8 = 10
317  {
318  itb = m_observations.find(shrdt);
319  if(itb==m_observations.end())
320  itb = m_observations.upper_bound(shrdt);
321  }
322  else if(r==te::dt::BEFORE) // 1
323  {
324  itb = m_observations.begin();
325  ite = m_observations.find(shrdt);
326  if(ite==m_observations.end())
327  ite = m_observations.upper_bound(shrdt);
328  }
329  else if(r==(te::dt::BEFORE | te::dt::EQUALS)) // 1 OU 8 = 9
330  {
331  itb = m_observations.begin();
332  ite = m_observations.upper_bound(shrdt);
333  }
334  else if(r==te::dt::DURING) //4
335  {
336  te::dt::DateTimePeriod* auxt = static_cast<te::dt::DateTimePeriod*>(shrdt.get());
339  itb = m_observations.find(t1);
340  if(itb==m_observations.end())
341  itb = m_observations.upper_bound(t1);
342  ite = m_observations.upper_bound(t2);
343  }
344  else if(r==te::dt::EQUALS) //8
345  {
346  std::pair<CoverageSeriesObservationSet::const_iterator, CoverageSeriesObservationSet::const_iterator> itPair;
347  itPair = m_observations.equal_range(shrdt);
348  itb = itPair.first;
349  ite = itPair.second;
350  if(ite!= m_observations.end())
351  ++ite;
352  }
353 
354  while(itb != ite)
355  {
356  cs->add(*itb);
357  ++itb;
358  }
359 
360  return cs;
361 }
362 
363 std::auto_ptr<te::st::CoverageSeries>
365 {
366  return std::auto_ptr<te::st::CoverageSeries>();
367 }
368 
369 std::auto_ptr<te::st::CoverageSeries>
371 {
372  return std::auto_ptr<te::st::CoverageSeries>();
373 }
374 
375 
376 std::auto_ptr<te::st::CoverageSeries>
378  const te::dt::DateTime& /*dt*/, te::dt::TemporalRelation /*r*/) const
379 {
380  return std::auto_ptr<te::st::CoverageSeries>();
381 }
382 
383 
384 std::auto_ptr<te::st::CoverageSeries>
386  const te::dt::DateTime& /*dt*/, te::dt::TemporalRelation /*tr*/) const
387 {
388  return std::auto_ptr<te::st::CoverageSeries>();
389 }
390 
392 {
393 
394 }
395 
396 
An abstract class for interpolation function or interpolator that estimate coverages at non-observed ...
std::auto_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.
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
virtual DateTimeInstant * getInitialInstant() const =0
It gets the initial date time instant.
CoverageType
An enum for the types of coverage.
Definition: Enums.h:43
TemporalRelation
Temporal relations between date and time (Source: Allen, 1991).
Definition: Enums.h:140
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
boost::shared_ptr< DateTime > DateTimeShrPtr
Definition: DateTime.h:126
virtual te::dt::AbstractData * clone() const
It clones the linestring.
Definition: Polygon.cpp:52
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.
Definition: Envelope.h:51
virtual te::dt::AbstractData * clone() const
It clones the point.
Definition: Point.cpp:71
const CoverageSeriesObservationSet & getObservations() const
It returns the coverage series observations.
virtual DateTimeInstant * getFinalInstant() const =0
It gets the final date time instant.
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.
Definition: Geometry.h:73
This file contains a class to represent a coverage series.
std::auto_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.
std::size_t size() const
It returns the size of the coverage series observations.
A class to represent a coverage series.
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
It is an interpolation function the estimates the nearest coverage at a given non-observed time...
std::auto_ptr< te::dt::DateTimePeriod > getTemporalExtent() const
It returns the temporal extent of the coverage series.
TEDATATYPEEXPORT DateTimePeriod * GetTemporalExtent(const DateTime *t1, const DateTime *t2)
It returns the temporal extent of two date and time types.
virtual ~CoverageSeries()
Virtual destructor.
virtual AbstractData * clone() const =0
It returns a clone of this object.
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.
Definition: Utils.cpp:38
std::auto_ptr< te::st::Coverage > getCoverage(te::dt::DateTime *t) const
It returns the coverage associated to a given date and time.