forked from gerasimosmichalitsianos/pansharpen_c_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resample.cpp
163 lines (138 loc) · 5.31 KB
/
resample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <cstring>
#include "unistd.h"
#include "gdal_priv.h"
#include "cpl_conv.h"
#include "gdalwarper.h"
#include "ogr_spatialref.h"
#include "resample.h"
typedef std::string String;
/* *******************************************************************
* String resample(char*,char*):
* This function resamples an input low-resolution 1-band
* Geotiff file to new dimensions as specified by an input
* higher-resolution (panchromatic) Geotiff file. Bicubic
* resampling is used here.
*
* Args:
* char* : low-resolution 1-band Geotiff filename string.
* char* : higher-resolution 1-band Geotiff filename string.
* Returns:
* String (std::string): Out filename for resampled Geotiff file.
*
*/
String resample(
char* srcfname, // low-res 1-band Geotiff filename (i.e. NIR, Red, Green, Blue)
char* dstfname // high-res (pan) Geotiff filename
) {
GDALAllRegister();
GDALDatasetH srcDataset;
const char* srcProjection;
/* open up low-resolution Geotiff file dataset */
srcDataset = GDALOpen( srcfname , GA_ReadOnly );
CPLAssert( srcDataset != NULL );
/* read projection from low-res. Geotiff. Also read its
* data-type. For LANDSAT8 its unsigned int16.
*/
srcProjection = GDALGetProjectionRef( srcDataset ) ;
CPLAssert( srcProjection != NULL && strlen( srcProjection ) > 0) ;
GDALDataType sourceDatatype;
sourceDatatype = GDALGetRasterDataType( GDALGetRasterBand(srcDataset,1) );
/* create output filename for bicubic resampled 1-band Geotiff. Just
* replace the .tif extension with '_resampled.tif'.
*/
String outfname;
int len = ((String)srcfname).length();
String extension("_resampled.tif");
outfname = ((String)srcfname).substr(0,len-4)+extension;
/* remove output resampled Geotiff file if it already exists */
int deleted;
if( access( outfname.c_str() , F_OK ) != -1) {
deleted = remove(outfname.c_str());
}
/* open the destination (high-res) file (i.e. panchromatic image file) ,
* read following parameters:
* (1) ncols : output resampled number of columns (samples)
* (2) nrows : output resampled number of rows (lines)
* (3) output geotransform for resampled low-res geotiff : array of 6 DOUBLE elements
* (4) output projection string
*/
GDALDatasetH dstDataset;
int dstnrows, dstncols;
double dstGeotransform[6];
const char* dstProjection;
dstDataset = GDALOpen( dstfname , GA_ReadOnly);
dstncols = GDALGetRasterXSize( dstDataset );
dstnrows = GDALGetRasterYSize( dstDataset );
GDALGetGeoTransform(dstDataset, dstGeotransform);
dstProjection = GDALGetProjectionRef( dstDataset );
/* create geotransform object ... pass in following parameters:
* (1) srcDataset : open GDAL dataset from low-res file
* (2) srcProjection : projection from low-res file
* (3) destination projection : projection of panchromatic (high-res.)
* among other arguments.
*/
void *handleTransformArg;
handleTransformArg = GDALCreateGenImgProjTransformer( srcDataset, srcProjection,
NULL, dstProjection, FALSE, 0, 1);
CPLAssert( handleTransformArg != NULL);
/* define geotransform (from source low-res dataset)
* as well as its dimensions
*/
double srcGeoTransform[6];
int outnrows = 0;
int outncols = 0;
CPLErr eErr;
/* Gather some information about the output dimensions and map projection
* information for the upcoming GDAL warp transformation.
*/
eErr = GDALSuggestedWarpOutput( srcDataset,
GDALGenImgProjTransform,
handleTransformArg,
srcGeoTransform,
&outncols ,
&outnrows );
CPLAssert( eErr == CE_None);
GDALDestroyGenImgProjTransformer( handleTransformArg );
/* begin creating output dataset. first create objects
* for the output driver handle and output GDAL dataset object.
*/
GDALDriverH outHandleDriver;
GDALDatasetH outDataset;
/* create output dataset. This will have the same dimensions
* as the input high-resolution Geotiff datasat (i.e. a 1-band Geotiff file
* holding a panchromatic band). This output dataset will also inherit
* its geotransform and projection string from this input high-res. dataset.
*/
outHandleDriver = GDALGetDriverByName("GTiff");
outDataset = GDALCreate( outHandleDriver ,
outfname.c_str() ,
dstncols, dstnrows , 1 ,
sourceDatatype, NULL);
GDALSetProjection( outDataset , dstProjection) ;
GDALSetGeoTransform( outDataset, dstGeotransform) ;
GDALWarpOptions *options;
/* project input source (low-res.) Geotiff dataset to same higher
* dimensions as input high-res. panchromatic image. To this end we
* use the GDALReprojectImage() method with the following arguments:
*
* (1) input low-resolution Geotiff GDAL dataset object from GDALOpen().
* (2) input source (low-res.) projection string from GetProjectionRef().
* (3) output high-resolution resampled Geotiff dataset object.
* (4) destination or output projection from high-res. Geotiff.
* (5) Resampling method.
* ...
*/
eErr = GDALReprojectImage( srcDataset ,
srcProjection,
outDataset ,
dstProjection ,
GRA_Cubic, 0.0, 0.0, NULL,NULL,NULL);
// alternatively: GRA_Cubic, 0.0, 0.0, GDALTermProgress, NULL, NULL);
CPLAssert( eErr == CE_None) ;
GDALClose(outDataset); /* this line is important. Close output dataset! */
return outfname;
}