| 1 | /********************************************************************** |
| 2 | jlwarp.cc: warp Jim using GDAL warp algorithm |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2020 European Union (Joint Research Centre) |
| 5 | |
| 6 | This file is part of jiplib. |
| 7 | |
| 8 | jiplib is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | jiplib is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with jiplib. If not, see <https://www.gnu.org/licenses/>. |
| 20 | ***********************************************************************/ |
| 21 | #include <iostream> |
| 22 | #include "ogr_spatialref.h" |
| 23 | #include "gdal_alg.h" |
| 24 | #include "gdalwarper.h" |
| 25 | #include <config_jiplib.h> |
| 26 | #include "Jim.h" |
| 27 | #include "base/Optionjl.h" |
| 28 | |
| 29 | using namespace std; |
| 30 | ///convert Jim image in memory returning Jim image (alias for crop) |
| 31 | std::shared_ptr<Jim> Jim::warp(app::AppFactory& theApp){ |
| 32 | std::shared_ptr<Jim> imgWriter=Jim::createImg(); |
| 33 | warp(*imgWriter, theApp); |
| 34 | return(imgWriter); |
| 35 | } |
| 36 | |
| 37 | // ///convert Jim image in memory returning Jim image (alias for crop) |
| 38 | // void Jim::warpSingle(Jim& imgWriter, app::AppFactory &theApp){ |
| 39 | // Optionjl<std::string> sourceSRS_opt("s_srs", "s_srs", "Source spatial reference for the input file, e.g., epsg:3035 to use European projection and force to European grid"); |
| 40 | // Optionjl<std::string> targetSRS_opt("t_srs", "t_srs", "Target spatial reference for the output file, e.g., epsg:3035 to use European projection and force to European grid"); |
| 41 | // Optionjl<std::string> resample_opt("r", "resample", "resample: GRIORA_NearestNeighbour|GRIORA_Bilinear|GRIORA_Cubic|GRIORA_CubicSpline|GRIORA_Lanczos|GRIORA_Average|GRIORA_Average|GRIORA_Gauss (check http://www.gdal.org/gdal_8h.html#a640ada511cbddeefac67c548e009d5a)","GRIORA_NearestNeighbour"); |
| 42 | // Optionjl<double> nodata_opt("nodata", "nodata", "Nodata value to put in image.",0); |
| 43 | // Optionjl<std::string> warp_opt("wo", "wo", "Warp option(s). Multiple options can be specified."); |
| 44 | // Optionjl<std::string> otype_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image"); |
| 45 | // Optionjl<short> verbose_opt("verbose", "verbose", "verbose output",0,2); |
| 46 | |
| 47 | // bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 48 | // try{ |
| 49 | // doProcess=sourceSRS_opt.retrieveOption(theApp); |
| 50 | // targetSRS_opt.retrieveOption(theApp); |
| 51 | // resample_opt.retrieveOption(theApp); |
| 52 | // nodata_opt.retrieveOption(theApp); |
| 53 | // warp_opt.retrieveOption(theApp); |
| 54 | // otype_opt.retrieveOption(theApp); |
| 55 | // verbose_opt.retrieveOption(theApp); |
| 56 | // } |
| 57 | // catch(std::string predefinedString){ |
| 58 | // std::cout << predefinedString << std::endl; |
| 59 | // } |
| 60 | // if(!doProcess){ |
| 61 | // std::cout << std::endl; |
| 62 | // std::ostringstream helpStream; |
| 63 | // helpStream << "exception thrown due to help info"; |
| 64 | // throw(helpStream.str());//help was invoked, stop processing |
| 65 | // } |
| 66 | |
| 67 | // std::vector<std::string> badKeys; |
| 68 | // theApp.badKeys(badKeys); |
| 69 | // if(badKeys.size()){ |
| 70 | // std::ostringstream errorStream; |
| 71 | // if(badKeys.size()>1) |
| 72 | // errorStream << "Error: unknown keys: "; |
| 73 | // else |
| 74 | // errorStream << "Error: unknown key: "; |
| 75 | // for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 76 | // errorStream << badKeys[ikey] << " "; |
| 77 | // } |
| 78 | // errorStream << std::endl; |
| 79 | // throw(errorStream.str()); |
| 80 | // } |
| 81 | |
| 82 | // GDALDataType theType=getGDALDataType(); |
| 83 | // if(otype_opt.size()){ |
| 84 | // theType=string2GDAL(otype_opt[0]); |
| 85 | // if(theType==GDT_Unknown) |
| 86 | // std::cout << "Warning: unknown output pixel type: " << otype_opt[0] << ", using input type as default" << std::endl; |
| 87 | // } |
| 88 | // if(verbose_opt[0]) |
| 89 | // std::cout << "Output pixel type: " << GDALGetDataTypeName(theType) << std::endl; |
| 90 | |
| 91 | // OGRSpatialReference sourceSpatialRef; |
| 92 | // OGRSpatialReference targetSpatialRef; |
| 93 | // sourceSpatialRef.SetFromUserInput(getProjectionRef().c_str()); |
| 94 | // targetSpatialRef.SetFromUserInput(targetSRS_opt[0].c_str()); |
| 95 | // if(sourceSpatialRef.IsSame(&targetSpatialRef)){ |
| 96 | // if(verbose_opt[0]) |
| 97 | // std::cout << "source SRS is same as target SRS, no warp is needed (just converting): " << GDALGetDataTypeName(theType) << std::endl; |
| 98 | // app::AppFactory convertApp(theApp); |
| 99 | // convertApp.clearOption("t_srs"); |
| 100 | // convertApp.clearOption("resample"); |
| 101 | // Jim::convert(imgWriter,convertApp); |
| 102 | // } |
| 103 | // // char *sourceWKT=getProjectionRef().c_str(); |
| 104 | // char *targetWKT=0; |
| 105 | // targetSpatialRef.exportToWkt(&targetWKT); |
| 106 | // std::vector<double> sourceGT(6); |
| 107 | // std::vector<double> targetGT(6); |
| 108 | // getGeoTransform(sourceGT); |
| 109 | // //todo: warp first band to open imgWriter and consecutive bands in parallel |
| 110 | // for(int iband=0;iband<nrOfBand();++iband){ |
| 111 | // if(verbose_opt[0]) |
| 112 | // std::cout << "warping band " << iband << std::endl; |
| 113 | // const char *pszFormat = "MEM"; |
| 114 | // GDALDriver *poDriverMem = GetGDALDriverManager()->GetDriverByName(pszFormat); |
| 115 | // GDALDataset *poDatasetMem = NULL; |
| 116 | // GDALRasterBand *poBandMem = NULL; |
| 117 | // if( poDriverMem ){ |
| 118 | // poDatasetMem = (GDALDataset *)poDriverMem->Create("memband",nrOfCol(),nrOfRow(),1,getGDALDataType(),NULL); |
| 119 | // if( poDatasetMem ){ |
| 120 | // //printf("Copy MemoryBand to memband\n"); fflush(stdout); |
| 121 | // poDatasetMem->SetGeoTransform(&sourceGT[0]); |
| 122 | // poBandMem = poDatasetMem->GetRasterBand(1); |
| 123 | // if( poBandMem->RasterIO(GF_Write, 0, 0, nrOfCol(), nrOfRow(), m_data[iband], nrOfCol(), nrOfRow(), getGDALDataType(), 0, 0, NULL) == CE_None ){ |
| 124 | // // Quality parameters for warping operation |
| 125 | // m_resample=getGDALResample(resample_opt[0]); |
| 126 | // GDALResampleAlg eResampleAlg = (GDALResampleAlg)m_resample; |
| 127 | // poBandMem->SetNoDataValue(nodata_opt[0]); |
| 128 | // double dfWarpMemoryLimit = 0.0; |
| 129 | // double dfMaxError = 0.0; |
| 130 | // GDALProgressFunc pfnProgress = NULL; |
| 131 | // void *pProgressArg = NULL; |
| 132 | // GDALWarpOptions *psOptions = GDALCreateWarpOptions(); |
| 133 | |
| 134 | // // Create a transformation object from the source to destination coordinate system |
| 135 | // poDatasetMem->SetGeoTransform(&sourceGT[0]); |
| 136 | // void *hTransformArg = GDALCreateGenImgProjTransformer(poDatasetMem, getProjectionRef().c_str(), NULL, targetWKT, TRUE, 1000.0, 0); |
| 137 | // if( hTransformArg != NULL ){ |
| 138 | // // Get approximate output definition |
| 139 | // int nPixels, nLines; |
| 140 | // if( GDALSuggestedWarpOutput(poDatasetMem, GDALGenImgProjTransform, hTransformArg, &targetGT[0], &nPixels, &nLines) == CE_None ){ |
| 141 | // GDALDestroyGenImgProjTransformer(hTransformArg); |
| 142 | |
| 143 | // // Create the output memory band |
| 144 | // GDALDataset *poDatasetOut = (GDALDataset *)poDriverMem->Create("outband",nPixels,nLines,1,theType,NULL); |
| 145 | // if( poDatasetOut != NULL ){ |
| 146 | // // Write out the projection definition |
| 147 | // poDatasetOut->SetProjection(targetWKT); |
| 148 | // poDatasetOut->SetGeoTransform(&targetGT[0]); |
| 149 | |
| 150 | // psOptions->papszWarpOptions = CSLSetNameValue(psOptions->papszWarpOptions,"INIT_DEST",type2string<double>(nodata_opt[0]).c_str()); |
| 151 | // // psOptions->GDALWarpOptions = CSLSetNameValue(psOptions->papszWarpOptions,"eResampleAlg",resample_opt[0]); |
| 152 | // psOptions->papszWarpOptions = CSLSetNameValue(psOptions->papszWarpOptions,"eResampleAlg",resample_opt[0].c_str()); |
| 153 | // // Perform the reprojection |
| 154 | // if( GDALReprojectImage(poDatasetMem, getProjectionRef().c_str(), poDatasetOut, targetWKT, eResampleAlg, dfWarpMemoryLimit, dfMaxError, pfnProgress, pProgressArg, psOptions) == CE_None ){ |
| 155 | // // Copy pixels to pout |
| 156 | // GDALRasterBand *poBandOut = poDatasetOut->GetRasterBand(1); |
| 157 | // if( poBandOut ){ |
| 158 | // if(!iband){ |
| 159 | // // app::AppFactory writerApp; |
| 160 | // // writerApp.setLongOption("ncol",poBandOut->GetXSize()); |
| 161 | // // writerApp.setLongOption("nrow",poBandOut->GetYSize()); |
| 162 | // // writerApp.setLongOption("nband",nrOfBand()); |
| 163 | // // if(otype_opt.size()) |
| 164 | // // writerApp.setLongOption("otype",otype_opt[0]); |
| 165 | // // imgWriter.open(writerApp); |
| 166 | // imgWriter.open(poBandOut->GetXSize(),poBandOut->GetYSize(),nrOfBand(),theType); |
| 167 | // imgWriter.setGeoTransform(targetGT); |
| 168 | // imgWriter.setProjection(targetWKT); |
| 169 | // imgWriter.setNoData(nodata_opt); |
| 170 | // } |
| 171 | // if( poBandOut->RasterIO(GF_Read, 0, 0, imgWriter.nrOfCol(), imgWriter.nrOfRow(), imgWriter.getDataPointer(iband), imgWriter.nrOfCol(), imgWriter.nrOfRow(), imgWriter.getGDALDataType(), 0, 0, NULL) != CE_None ){ |
| 172 | // std::string errorString="Error: could not read band from RasterIO"; |
| 173 | // std::cerr << errorString << std::endl; |
| 174 | // throw(errorString); |
| 175 | // } |
| 176 | // } |
| 177 | // GDALClose(poDatasetOut); |
| 178 | // if(psOptions->papszWarpOptions) CSLDestroy(psOptions->papszWarpOptions); |
| 179 | // psOptions->papszWarpOptions = NULL; |
| 180 | // } |
| 181 | // } |
| 182 | // GDALDestroyWarpOptions(psOptions); |
| 183 | // } |
| 184 | // } |
| 185 | // GDALClose(poDatasetMem); |
| 186 | // } |
| 187 | // } |
| 188 | // } |
| 189 | // } |
| 190 | // } |
| 191 | |
| 192 | ///convert Jim image in memory returning Jim image (alias for crop) |
| 193 | void Jim::warp(Jim& imgWriter, app::AppFactory &theApp) |
| 194 | { |
| 195 | Optionjl<std::string> sourceSRS_opt("s_srs", "s_srs", "Source spatial reference for the input file, e.g., epsg:3035 to use European projection and force to European grid"); |
| 196 | Optionjl<std::string> targetSRS_opt("t_srs", "t_srs", "Target spatial reference for the output file, e.g., epsg:3035 to use European projection and force to European grid"); |
| 197 | Optionjl<std::string> resample_opt("r", "resample", "resample: near, bilinear, cubic, cubicspline, lanczos, average, mode, max, min, med, q1, q3, sum (check https://gdal.org/doxygen/gdalwarper_8h.html#a4775b029869df1f9270ad554c0633843)","near"); |
| 198 | Optionjl<double> nodata_opt("nodata", "nodata", "Nodata value to put in image.",0); |
| 199 | Optionjl<std::string> warp_opt("wo", "wo", "Warp option(s). Multiple options can be specified."); |
| 200 | Optionjl<std::string> otype_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image"); |
| 201 | Optionjl<double> ulx_opt("ulx", "ulx", "Target upper left x value bounding box"); |
| 202 | Optionjl<double> uly_opt("uly", "uly", "Target upper left y value bounding box"); |
| 203 | Optionjl<double> lrx_opt("lrx", "lrx", "Target lower right x value bounding box"); |
| 204 | Optionjl<double> lry_opt("lry", "lry", "Target lower right y value bounding box"); |
| 205 | Optionjl<int> ncol_opt("ncol", "ncol", "force output to be this number of columns"); |
| 206 | Optionjl<int> nrow_opt("nrow", "nrow", "force output to be this number of rows"); |
| 207 | Optionjl<double> dx_opt("dx", "dx", "Output resolution in x (in meter)"); |
| 208 | Optionjl<double> dy_opt("dy", "dy", "Output resolution in y (in meter)"); |
| 209 | Optionjl<short> verbose_opt("verbose", "verbose", "verbose output",0,2); |
| 210 | |
| 211 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 212 | try{ |
| 213 | doProcess=sourceSRS_opt.retrieveOption(theApp); |
| 214 | targetSRS_opt.retrieveOption(theApp); |
| 215 | resample_opt.retrieveOption(theApp); |
| 216 | nodata_opt.retrieveOption(theApp); |
| 217 | warp_opt.retrieveOption(theApp); |
| 218 | otype_opt.retrieveOption(theApp); |
| 219 | ulx_opt.retrieveOption(theApp); |
| 220 | uly_opt.retrieveOption(theApp); |
| 221 | lrx_opt.retrieveOption(theApp); |
| 222 | lry_opt.retrieveOption(theApp); |
| 223 | dx_opt.retrieveOption(theApp); |
| 224 | dy_opt.retrieveOption(theApp); |
| 225 | verbose_opt.retrieveOption(theApp); |
| 226 | } |
| 227 | catch(std::string predefinedString){ |
| 228 | std::cout << predefinedString << std::endl; |
| 229 | } |
| 230 | if(!doProcess){ |
| 231 | std::cout << std::endl; |
| 232 | std::ostringstream helpStream; |
| 233 | helpStream << "exception thrown due to help info"; |
| 234 | throw(helpStream.str());//help was invoked, stop processing |
| 235 | } |
| 236 | |
| 237 | std::vector<std::string> badKeys; |
| 238 | theApp.badKeys(badKeys); |
| 239 | if(badKeys.size()){ |
| 240 | std::ostringstream errorStream; |
| 241 | if(badKeys.size()>1) |
| 242 | errorStream << "Error: unknown keys: "; |
| 243 | else |
| 244 | errorStream << "Error: unknown key: "; |
| 245 | for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 246 | errorStream << badKeys[ikey] << " "; |
| 247 | } |
| 248 | errorStream << std::endl; |
| 249 | throw(errorStream.str()); |
| 250 | } |
| 251 | |
| 252 | GDALDataType theType=getGDALDataType(); |
| 253 | if(otype_opt.size()){ |
| 254 | theType=string2GDAL(otype_opt[0]); |
| 255 | if(theType==GDT_Unknown) |
| 256 | std::cout << "Warning: unknown output pixel type: " << otype_opt[0] << ", using input type as default" << std::endl; |
| 257 | } |
| 258 | if(verbose_opt[0]) |
| 259 | std::cout << "Output pixel type: " << GDALGetDataTypeName(theType) << std::endl; |
| 260 | |
| 261 | OGRSpatialReference sourceSpatialRef; |
| 262 | OGRSpatialReference targetSpatialRef; |
| 263 | #if GDAL_VERSION_MAJOR > 2 |
| 264 | sourceSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 265 | targetSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 266 | #endif |
| 267 | |
| 268 | sourceSpatialRef.SetFromUserInput(getProjectionRef().c_str()); |
| 269 | if(targetSRS_opt.size()) |
| 270 | targetSpatialRef.SetFromUserInput(targetSRS_opt[0].c_str()); |
| 271 | else |
| 272 | targetSpatialRef.SetFromUserInput(getProjectionRef().c_str()); |
| 273 | // if(sourceSpatialRef.IsSame(&targetSpatialRef)){ |
| 274 | // if(verbose_opt[0]) |
| 275 | // std::cout << "source SRS is same as target SRS, no warp is needed (just converting): " << GDALGetDataTypeName(theType) << std::endl; |
| 276 | // app::AppFactory convertApp(theApp); |
| 277 | // convertApp.clearOption("t_srs"); |
| 278 | // convertApp.clearOption("resample"); |
| 279 | // Jim::convert(imgWriter,convertApp); |
| 280 | // } |
| 281 | // char *sourceWKT=getProjectionRef().c_str(); |
| 282 | char *targetWKT=0; |
| 283 | targetSpatialRef.exportToWkt(&targetWKT); |
| 284 | std::vector<double> sourceGT(6); |
| 285 | std::vector<double> targetGT(6); |
| 286 | getGeoTransform(sourceGT); |
| 287 | // for(int iband=0;iband<nrOfBand();++iband){ |
| 288 | // if(verbose_opt[0]) |
| 289 | // std::cout << "warping band " << iband << std::endl; |
| 290 | const char *pszFormat = "MEM"; |
| 291 | GDALDriver *poDriverMem = GetGDALDriverManager()->GetDriverByName(pszFormat); |
| 292 | GDALDataset *poDatasetMem = NULL; |
| 293 | GDALRasterBand *poBandMem = NULL; |
| 294 | if( !poDriverMem ){ |
| 295 | std::string errorString="Error: could get GDAL driver"; |
| 296 | std::cerr << errorString << std::endl; |
| 297 | throw(errorString); |
| 298 | } |
| 299 | poDatasetMem = (GDALDataset *)poDriverMem->Create("memband",nrOfCol(),nrOfRow(),nrOfBand(),getGDALDataType(),NULL); |
| 300 | if( !poDatasetMem ){ |
| 301 | std::string errorString="Error: could get GDAL dataset in memory"; |
| 302 | std::cerr << errorString << std::endl; |
| 303 | throw(errorString); |
| 304 | } |
| 305 | //printf("Copy MemoryBand to memband\n"); fflush(stdout); |
| 306 | poDatasetMem->SetGeoTransform(&sourceGT[0]); |
| 307 | for(int iband=0;iband<nrOfBand();++iband){ |
| 308 | poBandMem = poDatasetMem->GetRasterBand(iband+1); |
| 309 | poBandMem->SetNoDataValue(nodata_opt[0]); |
| 310 | if( poBandMem->RasterIO(GF_Write, 0, 0, nrOfCol(), nrOfRow(), m_data[iband], nrOfCol(), nrOfRow(), getGDALDataType(), 0, 0, NULL) != CE_None ){ |
| 311 | std::string errorString="Error: could not perform RasterIO GF_Write"; |
| 312 | std::cerr << errorString << std::endl; |
| 313 | throw(errorString); |
| 314 | } |
| 315 | } |
| 316 | // Quality parameters for warping operation |
| 317 | GDALResampleAlg eResampleAlg=getGDALResampleAlg(resample_opt[0]); |
| 318 | // GDALResampleAlg eResampleAlg = (GDALResampleAlg)resample; |
| 319 | double dfWarpMemoryLimit = 0.0; |
| 320 | double dfMaxError = 0.0; |
| 321 | GDALProgressFunc pfnProgress = NULL; |
| 322 | void *pProgressArg = NULL; |
| 323 | GDALWarpOptions *psOptions = GDALCreateWarpOptions(); |
| 324 | |
| 325 | // Create a transformation object from the source to destination coordinate system |
| 326 | poDatasetMem->SetGeoTransform(&sourceGT[0]); |
| 327 | void *hTransformArg = GDALCreateGenImgProjTransformer(poDatasetMem, getProjectionRef().c_str(), NULL, targetWKT, TRUE, 1000.0, 0); |
| 328 | if( hTransformArg != NULL ){ |
| 329 | // Get approximate output definition |
| 330 | int nPixels, nLines; |
| 331 | if( GDALSuggestedWarpOutput(poDatasetMem, GDALGenImgProjTransform, hTransformArg, &targetGT[0], &nPixels, &nLines) == CE_None ){ |
| 332 | double dfMinX; |
| 333 | double dfMaxX; |
| 334 | double dfMinY; |
| 335 | double dfMaxY; |
| 336 | GDALDestroyGenImgProjTransformer(hTransformArg); |
| 337 | if(dx_opt.size() && dy_opt.size()){ |
| 338 | if((ulx_opt.size() && uly_opt.size() && lrx_opt.size() && lry_opt.size())){ |
| 339 | dfMinX = ulx_opt[0]; |
| 340 | dfMaxX = lrx_opt[0]; |
| 341 | dfMinY = lry_opt[0]; |
| 342 | dfMaxY = uly_opt[0]; |
| 343 | } |
| 344 | else{ |
| 345 | dfMinX = targetGT[0]; |
| 346 | dfMaxX = targetGT[0] + targetGT[1] * nPixels; |
| 347 | dfMaxY = targetGT[3]; |
| 348 | dfMinY = targetGT[3] + targetGT[5] * nLines; |
| 349 | } |
| 350 | nPixels = (int) ((dfMaxX - dfMinX + (dx_opt[0]/2.0)) / dx_opt[0]); |
| 351 | nLines = (int) ((dfMaxY - dfMinY + (dy_opt[0]/2.0)) / dy_opt[0]); |
| 352 | targetGT[0] = dfMinX; |
| 353 | targetGT[3] = dfMaxY; |
| 354 | targetGT[1] = dx_opt[0]; |
| 355 | targetGT[5] = -dy_opt[0]; |
| 356 | } |
| 357 | else if(ncol_opt.size() && nrow_opt.size()){ |
| 358 | if((ulx_opt.size() && uly_opt.size() && lrx_opt.size() && lry_opt.size())){ |
| 359 | dfMinX = ulx_opt[0]; |
| 360 | dfMaxX = lrx_opt[0]; |
| 361 | dfMinY = lry_opt[0]; |
| 362 | dfMaxY = uly_opt[0]; |
| 363 | } |
| 364 | else{ |
| 365 | dfMinX = targetGT[0]; |
| 366 | dfMaxX = targetGT[0] + targetGT[1] * nPixels; |
| 367 | dfMaxY = targetGT[3]; |
| 368 | dfMinY = targetGT[3] + targetGT[5] * nLines; |
| 369 | } |
| 370 | double dfXRes = (dfMaxX - dfMinX) / ncol_opt[0]; |
| 371 | double dfYRes = (dfMaxY - dfMinY) / nrow_opt[0]; |
| 372 | |
| 373 | targetGT[0] = dfMinX; |
| 374 | targetGT[3] = dfMaxY; |
| 375 | targetGT[1] = dfXRes; |
| 376 | targetGT[5] = -dfYRes; |
| 377 | nPixels = ncol_opt[0]; |
| 378 | nLines = nrow_opt[0]; |
| 379 | } |
| 380 | else if((ulx_opt.size() && uly_opt.size() && lrx_opt.size() && lry_opt.size())){ |
| 381 | double dfXRes = targetGT[1]; |
| 382 | double dfYRes = fabs(targetGT[5]); |
| 383 | |
| 384 | nPixels = (int) ((lrx_opt[0] - ulx_opt[0] + (dfXRes/2.0)) / dfXRes); |
| 385 | nLines = (int) ((uly_opt[0] - lry_opt[0] + (dfYRes/2.0)) / dfYRes); |
| 386 | |
| 387 | targetGT[0] = ulx_opt[0]; |
| 388 | targetGT[3] = uly_opt[0]; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | // Create the output memory band |
| 393 | GDALDataset *poDatasetOut = (GDALDataset *)poDriverMem->Create("outband",nPixels,nLines,nrOfBand(),theType,NULL); |
| 394 | if( poDatasetOut != NULL ){ |
| 395 | // Write out the projection definition |
| 396 | poDatasetOut->SetProjection(targetWKT); |
| 397 | poDatasetOut->SetGeoTransform(&targetGT[0]); |
| 398 | |
| 399 | psOptions->papszWarpOptions = CSLSetNameValue(psOptions->papszWarpOptions,"INIT_DEST",type2string<double>(nodata_opt[0]).c_str()); |
| 400 | // psOptions->papszWarpOptions = CSLSetNameValue(psOptions->papszWarpOptions,"eResampleAlg",resample_opt[0].c_str()); |
| 401 | //test |
| 402 | // psOptions->nOvLevel = -1; |
| 403 | |
| 404 | for(std::vector<std::string>::const_iterator warpoptit=warp_opt.begin();warpoptit!=warp_opt.end();++warpoptit) |
| 405 | psOptions->papszWarpOptions=CSLAddString(psOptions->papszWarpOptions,warpoptit->c_str()); |
| 406 | |
| 407 | // Perform the reprojection |
| 408 | if( GDALReprojectImage(poDatasetMem, getProjectionRef().c_str(), poDatasetOut, targetWKT, eResampleAlg, dfWarpMemoryLimit, dfMaxError, pfnProgress, pProgressArg, psOptions) != CE_None ){ |
| 409 | std::string errorString="Error: could not GDAL reproject image"; |
| 410 | std::cerr << errorString << std::endl; |
| 411 | throw(errorString); |
| 412 | } |
| 413 | // Copy pixels to pout |
| 414 | for(int iband=0;iband<nrOfBand();++iband){ |
| 415 | GDALRasterBand *poBandOut = poDatasetOut->GetRasterBand(iband+1); |
| 416 | if( poBandOut ){ |
| 417 | if(!iband){ |
| 418 | imgWriter.open(poBandOut->GetXSize(),poBandOut->GetYSize(),nrOfBand(),theType); |
| 419 | imgWriter.setGeoTransform(targetGT); |
| 420 | imgWriter.setProjection(targetWKT); |
| 421 | imgWriter.setNoData(nodata_opt); |
| 422 | } |
| 423 | if( poBandOut->RasterIO(GF_Read, 0, 0, imgWriter.nrOfCol(), imgWriter.nrOfRow(), imgWriter.getDataPointer(iband), imgWriter.nrOfCol(), imgWriter.nrOfRow(), imgWriter.getGDALDataType(), 0, 0, NULL) != CE_None ){ |
| 424 | std::string errorString="Error: could not read band from RasterIO"; |
| 425 | std::cerr << errorString << std::endl; |
| 426 | throw(errorString); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | GDALClose(poDatasetOut); |
| 431 | if(psOptions->papszWarpOptions) CSLDestroy(psOptions->papszWarpOptions); |
| 432 | psOptions->papszWarpOptions = NULL; |
| 433 | } |
| 434 | GDALDestroyWarpOptions(psOptions); |
| 435 | } |
| 436 | } |
| 437 | GDALClose(poDatasetMem); |
| 438 | } |