GribExample.cpp
Go to the documentation of this file.
1 // Examples
2 #include "RasterExamples.h"
3 
4 // TerraLib
5 #include <terralib/common.h>
6 #include <terralib/geometry.h>
7 #include <terralib/raster.h>
8 
9 // STL
10 #include <exception>
11 #include <iostream>
12 #include <memory>
13 #include <map>
14 #include <string>
15 
16 #if TE_EXAMPLE_USE_GRIB
17 
18 // GDAL
19 #include <gdal_priv.h>
20 #include <gdalwarper.h>
21 #include <ogr_spatialref.h>
22 
23 void ReprojectRaster(GDALDataset* iraster,
24  std::string outfile,
25  std::string outWKT)
26 {
27  GDALDataType eDT = GDALGetRasterDataType(GDALGetRasterBand(iraster, 1));
28 
29  std::string dName = "GTiff";
30 
31  GDALDriverH hDriver = GDALGetDriverByName(dName.c_str());
32 
33  if(hDriver == 0)
34  return;
35 
36  const char *pszSrcWKT = GDALGetProjectionRef(iraster);
37 
38  if(pszSrcWKT == 0 || strlen(pszSrcWKT) == 0)
39  return;
40 
41  char *pszDstWKT = (char*) outWKT.c_str();
42 
43  if(pszDstWKT == 0 || strlen(pszDstWKT) == 0)
44  return;
45 
46  void* hTransformArg = GDALCreateGenImgProjTransformer(iraster, 0, NULL,
47  pszDstWKT, FALSE, 0, 1);
48  if (hTransformArg == 0)
49  return;
50 
51  /* Get approximate output georeferenced bounds and resolution for file */
52  double adfDstGeoTransform[6];
53 
54  int nPixels=0, nLines=0;
55 
56  if(GDALSuggestedWarpOutput(iraster, GDALGenImgProjTransform, hTransformArg,
57  adfDstGeoTransform, &nPixels, &nLines) != CE_None)
58  return;
59 
60  int nBands = GDALGetRasterCount(iraster);
61 
62  GDALDestroyGenImgProjTransformer(hTransformArg);
63 
64  /* Create the output file */
65  GDALDatasetH hDstDS = GDALCreate(hDriver, outfile.c_str(), nPixels,
66  nLines, nBands, eDT, NULL);
67  if (hDstDS == 0)
68  return;
69 
70  /* Write out the projection definition */
71  GDALSetProjection(hDstDS, pszDstWKT);
72  GDALSetGeoTransform(hDstDS, adfDstGeoTransform);
73 
74  /* Copy the color table, if required */
75  GDALColorTableH hCT;
76 
77  hCT = GDALGetRasterColorTable(GDALGetRasterBand(iraster,1));
78 
79  if(hCT != NULL)
80  GDALSetRasterColorTable(GDALGetRasterBand(hDstDS,1), hCT);
81 
82  /* Define warp options */
83  GDALWarpOptions *psWarpOptions = GDALCreateWarpOptions();
84  psWarpOptions->hSrcDS = iraster;
85  psWarpOptions->hDstDS = hDstDS;
86  psWarpOptions->nBandCount = nBands;
87  psWarpOptions->panSrcBands = (int*)CPLMalloc(sizeof(int)*psWarpOptions->nBandCount);
88  psWarpOptions->panDstBands = (int*)CPLMalloc(sizeof(int)*psWarpOptions->nBandCount);
89  for (int b = 0; b < psWarpOptions->nBandCount; b++)
90  {
91  psWarpOptions->panSrcBands[b] = b+1;
92  psWarpOptions->panDstBands[b] = b+1;
93  }
94 
95  /* Establish reprojection transformer */
96  psWarpOptions->pTransformerArg = GDALCreateGenImgProjTransformer(iraster, GDALGetProjectionRef(iraster),
97  hDstDS, GDALGetProjectionRef(hDstDS),
98  FALSE, 0.0, 1);
99  psWarpOptions->pfnTransformer = GDALGenImgProjTransform;
100 
101  /* Initialize and execute the warp operation */
102  GDALWarpOperation oOperation;
103  oOperation.Initialize(psWarpOptions);
104  oOperation.WarpRegion(0, 0, GDALGetRasterXSize(hDstDS), GDALGetRasterYSize(hDstDS));
105 
106  /* Close transformer and images */
107  GDALDestroyGenImgProjTransformer(psWarpOptions->pTransformerArg);
108  GDALDestroyWarpOptions(psWarpOptions);
109 
110  GDALClose(hDstDS);
111 
112 
113  return;
114 }
115 #endif
117 {
118 #if TE_EXAMPLE_USE_GRIB
119  try
120  {
121 // open raster for read
122  std::unique_ptr<te::rst::Raster> raster(te::rst::RasterFactory::open("GRIB",
123  "URI",
124  "/home/ecmwf/terralib5/trunk/examples/raster/Z500.grb"));
125 // "/home/ecmwf/terralib5/trunk/examples/raster/image.grb"));
126 
127 
128  te::rst::Grid* grid = raster->getGrid();
129 
130  int nrows = grid->getNumberOfRows();
131  int ncols = grid->getNumberOfColumns();
132 
133  double* gt = (double*)(grid->getGeoreference());
134 
135  void* buff = raster->getBand(0)->read(0, 0);
136  double* buffd = (double*)buff;
137 
138 #if 1
139  char* buffbyte = new char[nrows*ncols];
140  for(int i = 0; i < nrows*ncols; ++i)
141  {
142  buffbyte[i] = (char)(buffd[i]);
143  }
144 #endif
145 
146  boost::int64_t buffaddress = (boost::int64_t)buffbyte;
147 // boost::int64_t buffaddress = (boost::int64_t)buffd;
148 
149  std::string memraster = "MEM:::DATAPOINTER=";
150  memraster += te::common::Convert2String(buffaddress);
151  memraster += ",PIXELS=";
152  memraster += te::common::Convert2String(ncols);
153  memraster += ",LINES=";
154  memraster += te::common::Convert2String(nrows);
155  //memraster += ",BANDS=1,DATATYPE=float64";
156  memraster += ",BANDS=1,DATATYPE=byte";
157 
158  GDALAllRegister();
159 
160  GDALDataset* gds = (GDALDataset*)GDALOpen(memraster.c_str(), GA_ReadOnly);
161 
162  if(gds == 0)
163  throw te::rst::Exception("caca!");
164 
165  double xc = grid->getExtent()->getLowerLeftX() + grid->getExtent()->getWidth() / 2.0;
166  double yc = grid->getExtent()->getLowerLeftY() + grid->getExtent()->getHeight() / 2.0;
167 
168 #if 0
169  // projecao satellite
170  std::string cpprojsrs = "+proj=geos +lon_0=0 +h=35785831 +x_0=";
171  cpprojsrs += te::common::Convert2String(xc);
172  cpprojsrs += " +y_0=";
173  cpprojsrs += te::common::Convert2String(yc);
174  cpprojsrs += " +ellps=WGS84 +units=m +no_defs";
175  const char* projsrs = cpprojsrs.c_str();
176 
177  OGRSpatialReference iSRS;
178  iSRS.importFromProj4(projsrs);
179 #else
180  // projecao latlong
181  OGRSpatialReference iSRS;
182 
183  // lat/long
184  iSRS.importFromEPSG(4326);
185 #endif
186 
187  char* iWKT = NULL;
188  iSRS.exportToWkt(&iWKT);
189 
190  gds->SetProjection(iWKT);
191 
192  gds->SetGeoTransform(gt);
193 
194  OGRSpatialReference oSRS;
195 
196  // lat/long
197  oSRS.importFromEPSG(4326);
198 
199  // polar stereografica norte
200  //oSRS.importFromEPSG(3995);
201  //oSRS.importFromProj4(projsrs);
202 
203  // polar stereografica norte
204  //oSRS.importFromProj4("+proj=stere +lat_0=90 +lat_ts=90 +lon_0=0 +k=0.994 +x_0=2000000 +y_0=2000000 +ellps=WGS84 +datum=WGS84 +units=m +no_defs");
205 
206  char* oWKT = NULL;
207  oSRS.exportToWkt(&oWKT);
208 
209  ReprojectRaster(gds,
210  "/home/ecmwf/terralib5/trunk/examples/raster/Z500_ll.tif",
211  oWKT);
212 
213  GDALClose(gds);
214 
215  }
216  catch(const std::exception& e)
217  {
218  std::cout << std::endl << "An exception has occurred in Grib example: " << e.what() << std::endl;
219  }
220  catch(...)
221  {
222  std::cout << std::endl << "An unknown exception has occurred in Grib example!" << std::endl;
223  }
224 
225 #endif // TE_EXAMPLE_USE_GRIB
226 
227  return;
228 }
unsigned int getNumberOfRows() const
Returns the grid number of rows.
void ReprojectRaster()
Creates a new raster file with a new projection.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
double getWidth() const
It returns the envelope width.
void GribExample()
An example to use the Grib driver.
int b
Definition: TsRtree.cpp:32
const double * getGeoreference() const
Returns a list of 6 coefficients describing an affine transformation to georeference a grid...
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
These routines show how to use the raster module and the GDAL data source module. ...
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
This file contains include headers for the TerraLib Common Runtime module.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56
This file contains include headers for the Vector Geometry model of TerraLib.
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
unsigned int nLines
double getHeight() const
It returns the envelope height.
static Raster * open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
It opens a raster with the given parameters and default raster driver.