All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Raster.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 terralib/grib/Raster.cpp
22 
23  \brief A raster class for Grib.
24 */
25 
26 // TerraLib
27 #include "../common/STLUtils.h"
28 #include "../common/Translator.h"
29 #include "../geometry/Coord2D.h"
30 #include "../geometry/Envelope.h"
31 #include "../raster/BandProperty.h"
32 #include "../raster/Grid.h"
33 #include "Band.h"
34 #include "Exception.h"
35 #include "Raster.h"
36 #include "Enums.h"
37 
38 // STL
39 #include <cassert>
40 #include <cmath>
41 
42 // Boost
43 #include <boost/format.hpp>
44 
45 grib_context* te::grib::Raster::sm_context(0);
46 
48  : m_file(0)
49 {
50 // get the context
51  if(!sm_context)
52  sm_context = grib_context_get_default();
53 }
54 
55 /*
56 te::grib::Raster::Raster(te::rst::Grid* grid, te::common::AccessPolicy p)
57  : te::rst::Raster(grid, p),
58  m_file(0)
59 {
60  throw Exception(TR_GRIB("Not implemented yet!"));
61 }
62 */
63 
65  : te::rst::Raster(rhs),
66  m_file(0)
67 {
68  throw Exception(TR_GRIB("Not implemented yet!"));
69 }
70 
72 {
73  close();
74 }
75 
76 const double TeEARTHRADIUS = 6378160.;
77 
78 void te::grib::Raster::open(const std::map<std::string, std::string>& rinfo, te::common::AccessPolicy p)
79 {
80 // assure that raster is closed
81  close();
82 
83 // extract file name
84  std::map<std::string, std::string>::const_iterator it = rinfo.find("URI");
85 
86  if ( it == rinfo.end() )
87  throw Exception(TR_GRIB("Missing Grib filename"));
88 
89 // open file
90  std::string file_name = it->second;
91 
92  if(p == te::common::RAccess)
93  m_file = fopen(file_name.c_str(), "r");
94  else
95  m_file = fopen(file_name.c_str(), "rw");
96 
97  if(m_file == 0)
98  throw Exception((boost::format(TR_GRIB("Grib file can not be opened: %1%")) %file_name).str());
99 
100  int err = 0;
101 
102 // for each message extract the equivalent band information
103  grib_handle* handle = 0;
104 
105  int bandId = 0;
106 
107  while((handle = grib_handle_new_from_file(sm_context, m_file, &err)) != 0)
108  {
109  Band* band = new Band(this, bandId, handle);
110 
111  m_bands.push_back(band);
112 
113  ++bandId;
114  }
115 
116  if(err)
117  throw Exception((boost::format(TR_GRIB("Can not create Grib handle for file %1% due to the following error: %2%.")) % file_name % Band::getErrMsg(err)).str());
118 
119 // get grid info from the first message (band)
120 
121 // find projection type
122  int gtype = m_bands[0]->getLong("dataRepresentationType");
123  if(gtype == SATELLITE_REP) //Satellite
124  setGridSatelliteRep();
125  else if (gtype == LATLNG_REP) //Latlong
126  setGridLatLngRep();
127  else if (gtype == REDUCEDGG_REP) //Reduced gaussian grid
128  setGridReducedGGRep();
129  else // if projection is not known, let's throw an exception! note: may be -1 could be used instead!
130  {
131  throw Exception((boost::format(TR_GRIB("Could not determine the data projection in the grib file: %1%.")) % file_name).str());
132  }
133 }
134 
135 void te::grib::Raster::open(void* h, const std::map<std::string, std::string>& rinfo, te::common::AccessPolicy p, void (*deleter)(void*))
136 {
137  throw Exception(TR_GRIB("Not implemented yet!"));
138  // http://www.dpi.inpe.br/terralib5/wiki/doku.php?id=wiki:designimplementation:dataaccess:drivers:memory
139 }
140 
141 std::map<std::string, std::string> te::grib::Raster::getInfo() const
142 {
143  return std::map<std::string, std::string>();
144 }
145 
147 {
148  return m_bands.size();
149 }
150 
151 int te::grib::Raster::getBandDataType(std::size_t i) const
152 {
153  assert(i < m_bands.size());
154 
155  te::rst::BandProperty* bp = m_bands[i]->getProperty();
156 
157  return bp->getType();
158 }
159 
160 const te::rst::Band* te::grib::Raster::getBand(std::size_t i) const
161 {
162  return m_bands[i];
163 }
164 
166 {
167  return m_bands[i];
168 }
169 
170 const te::rst::Band& te::grib::Raster::operator[](std::size_t i) const
171 {
172  return *m_bands[i];
173 }
174 
176 {
177  return *m_bands[i];
178 }
179 
181 {
182  throw Exception(TR_GRIB("Not implemented yet!"));
183 }
184 
186 {
187  te::common::FreeContents(m_bands);
188 
189  m_bands.clear();
190 
191  if(m_file)
192  {
193  fclose(m_file);
194  m_file = 0;
195  }
196 
197  delete m_grid;
198  m_grid = 0;
199 }
200 
202 {
203  // number of rows and cols
204  long nx = m_bands[0]->getLong("numberOfPointsAlongXAxis");
205  long ny = m_bands[0]->getLong("numberOfPointsAlongYAxis");
206 
207  // altitude in number of radius of earth
208  double altitude = m_bands[0]->getDouble("NrInRadiusOfEarth") * 0.000001;
209 
210  double pri = 2. * asin(1. / altitude) / m_bands[0]->getDouble("dy");
211  double prj = 2.0* asin(1. / altitude) / m_bands[0]->getDouble("dx");
212 
213  // resolution
214  double resx = (double)(std::atan(std::tan(pri) * (altitude - 1.) ) * TeEARTHRADIUS);
215  double resy = (double)(std::atan(std::tan(prj) * (altitude - 1.) ) * TeEARTHRADIUS);
216 
217  // offset in image coordinates
218  double offx = m_bands[0]->getDouble("xCoordinateOfOriginOfSectorImage");
219  double offy = m_bands[0]->getDouble("yCoordinateOfOriginOfSectorImage");
220 
221  // bounding box coordinates
222  double ulx = offx * resx;
223  double uly = -(offy * resy);
224  double lrx = (offx + nx - 1) * resx;
225  double lry = -(offy + ny - 1) * resy;
226 
227  // find srid = ???
228  int srid = -1;
229 
230  // bounding box
231  te::gm::Envelope* e = new te::gm::Envelope(ulx, lry, lrx, uly);
232 
233  // upper-left corner
234  //te::gm::Coord2D ulc(ulx, uly);
235 
236  // grid
237  m_grid = new te::rst::Grid(nx, ny, resx, resy, e, srid);
238 
239  return;
240 }
241 
243 {
244  // number of rows and cols
245  long nx = m_bands[0]->getLong("numberOfPointsAlongAParallel");
246  long ny = m_bands[0]->getLong("numberOfPointsAlongAMeridian");
247 
248  // bounding box coordinates
249  double north = m_bands[0]->getDouble("latitudeOfFirstGridPointInDegrees");
250  double west = m_bands[0]->getDouble("longitudeOfFirstGridPointInDegrees");
251  double south = m_bands[0]->getDouble("latitudeOfLastGridPointInDegrees");;
252  double east = m_bands[0]->getDouble("longitudeOfLastGridPointInDegrees");;
253  //longitudesSanityCheck(west, east);
254 
255  // resolution
256  double resx = (east-west) / (nx-1);
257  double resy = (north-south) / (ny-1);
258 
259  // find srid = ???
260  int srid = -1;
261 
262  // bounding box
263  te::gm::Envelope* e = new te::gm::Envelope(west, south, east, north);
264 
265  // upper-left corner
266  //te::gm::Coord2D ulc(west, north);
267 
268  // grid
269  m_grid = new te::rst::Grid(nx, ny, resx, resy, e, srid);
270 
271  return;
272 }
273 
275 {
276 
277 }
const te::rst::Band * getBand(std::size_t i) const
Returns the raster i-th band.
Definition: Raster.cpp:160
te::dt::AbstractData * clone() const
It returns a clone of this object.
Definition: Raster.cpp:180
A raster band description.
Definition: BandProperty.h:61
~Raster()
Virtual destructor.
Definition: Raster.cpp:71
const double TeEARTHRADIUS
Definition: Raster.cpp:76
An exception class for GRIB.
static grib_context * sm_context
Grib API context.
Definition: Raster.h:119
void setGridLatLngRep()
Definition: Raster.cpp:242
A raster class for GRIB format.
AccessPolicy
Supported data access policies (can be used as bitfield).
Definition: Enums.h:40
General enumerations.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
Opens a raster.
Definition: Raster.cpp:78
std::map< std::string, std::string > getInfo() const
It returns additional information about the raster.
Definition: Raster.cpp:141
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
A raster band description.
Definition: Band.h:63
void setGridSatelliteRep()
Definition: Raster.cpp:201
Band implemntatin for GRIB.
Definition: Band.h:48
A raster class for GRIB format.
Definition: Raster.h:51
static std::string getErrMsg(int errCode)
Definition: Band.cpp:181
void setGridReducedGGRep()
Definition: Raster.cpp:274
int getType() const
It returns the data type of the elements in the band.
Definition: BandProperty.h:113
std::size_t getNumberOfBands() const
Returns the number of bands (dimension of cells attribute values) in the raster.
Definition: Raster.cpp:146
const te::rst::Band & operator[](std::size_t i) const
Access band in i position.
Definition: Raster.cpp:170
int getBandDataType(std::size_t i) const
Returns the data type in a particular band (or dimension).
Definition: Raster.cpp:151
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
Band implemntatin for GRIB.
#define TR_GRIB(message)
It marks a string in order to get translated. This is a special mark used in the DataAccess module of...
Definition: Config.h:69