16 #if TE_EXAMPLE_USE_GRIB 19 #include <gdal_priv.h> 20 #include <gdalwarper.h> 21 #include <ogr_spatialref.h> 27 GDALDataType eDT = GDALGetRasterDataType(GDALGetRasterBand(iraster, 1));
29 std::string dName =
"GTiff";
31 GDALDriverH hDriver = GDALGetDriverByName(dName.c_str());
36 const char *pszSrcWKT = GDALGetProjectionRef(iraster);
38 if(pszSrcWKT == 0 || strlen(pszSrcWKT) == 0)
41 char *pszDstWKT = (
char*) outWKT.c_str();
43 if(pszDstWKT == 0 || strlen(pszDstWKT) == 0)
46 void* hTransformArg = GDALCreateGenImgProjTransformer(iraster, 0, NULL,
47 pszDstWKT, FALSE, 0, 1);
48 if (hTransformArg == 0)
52 double adfDstGeoTransform[6];
56 if(GDALSuggestedWarpOutput(iraster, GDALGenImgProjTransform, hTransformArg,
57 adfDstGeoTransform, &nPixels, &
nLines) != CE_None)
60 int nBands = GDALGetRasterCount(iraster);
62 GDALDestroyGenImgProjTransformer(hTransformArg);
65 GDALDatasetH hDstDS = GDALCreate(hDriver, outfile.c_str(), nPixels,
66 nLines, nBands, eDT, NULL);
71 GDALSetProjection(hDstDS, pszDstWKT);
72 GDALSetGeoTransform(hDstDS, adfDstGeoTransform);
77 hCT = GDALGetRasterColorTable(GDALGetRasterBand(iraster,1));
80 GDALSetRasterColorTable(GDALGetRasterBand(hDstDS,1), hCT);
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++)
91 psWarpOptions->panSrcBands[
b] =
b+1;
92 psWarpOptions->panDstBands[
b] =
b+1;
96 psWarpOptions->pTransformerArg = GDALCreateGenImgProjTransformer(iraster, GDALGetProjectionRef(iraster),
97 hDstDS, GDALGetProjectionRef(hDstDS),
99 psWarpOptions->pfnTransformer = GDALGenImgProjTransform;
102 GDALWarpOperation oOperation;
103 oOperation.Initialize(psWarpOptions);
104 oOperation.WarpRegion(0, 0, GDALGetRasterXSize(hDstDS), GDALGetRasterYSize(hDstDS));
107 GDALDestroyGenImgProjTransformer(psWarpOptions->pTransformerArg);
108 GDALDestroyWarpOptions(psWarpOptions);
118 #if TE_EXAMPLE_USE_GRIB 124 "/home/ecmwf/terralib5/trunk/examples/raster/Z500.grb"));
135 void* buff = raster->getBand(0)->read(0, 0);
136 double* buffd = (
double*)buff;
139 char* buffbyte =
new char[nrows*ncols];
140 for(
int i = 0; i < nrows*ncols; ++i)
142 buffbyte[i] = (char)(buffd[i]);
146 boost::int64_t buffaddress = (boost::int64_t)buffbyte;
149 std::string memraster =
"MEM:::DATAPOINTER=";
151 memraster +=
",PIXELS=";
153 memraster +=
",LINES=";
156 memraster +=
",BANDS=1,DATATYPE=byte";
160 GDALDataset* gds = (GDALDataset*)GDALOpen(memraster.c_str(), GA_ReadOnly);
163 throw te::rst::Exception(
"caca!");
170 std::string cpprojsrs =
"+proj=geos +lon_0=0 +h=35785831 +x_0=";
172 cpprojsrs +=
" +y_0=";
174 cpprojsrs +=
" +ellps=WGS84 +units=m +no_defs";
175 const char* projsrs = cpprojsrs.c_str();
177 OGRSpatialReference iSRS;
178 iSRS.importFromProj4(projsrs);
181 OGRSpatialReference iSRS;
184 iSRS.importFromEPSG(4326);
188 iSRS.exportToWkt(&iWKT);
190 gds->SetProjection(iWKT);
192 gds->SetGeoTransform(gt);
194 OGRSpatialReference oSRS;
197 oSRS.importFromEPSG(4326);
207 oSRS.exportToWkt(&oWKT);
210 "/home/ecmwf/terralib5/trunk/examples/raster/Z500_ll.tif",
216 catch(
const std::exception& e)
218 std::cout << std::endl <<
"An exception has occurred in Grib example: " << e.what() << std::endl;
222 std::cout << std::endl <<
"An unknown exception has occurred in Grib example!" << std::endl;
225 #endif // TE_EXAMPLE_USE_GRIB 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.
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.
This file contains include headers for the Vector Geometry model of TerraLib.
A rectified grid is the spatial support for raster data.
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.