| 1 | /********************************************************************** |
| 2 | Jim.cc: class to read raster files using GDAL API library |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2022 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 | /* |
| 22 | 2017-05-03 Kris Vanhoof (vhoofk): Fix rounding issues in image size calculation |
| 23 | */ |
| 24 | #include <iostream> |
| 25 | #include "ogr_spatialref.h" |
| 26 | #include "gdal_alg.h" |
| 27 | #include "gdalwarper.h" |
| 28 | #include <config_jiplib.h> |
| 29 | #include "Jim.h" |
| 30 | #include "VectorOgr.h" |
| 31 | #include "base/Optionjl.h" |
| 32 | #include "algorithms/StatFactory.h" |
| 33 | #include "Json_compat.h" |
| 34 | |
| 35 | using namespace std; |
| 36 | //Forgetting to place these commands will show itself as an ugly segmentation fault (crash) as soon as any C-API subroutine is actually called |
| 37 | //see also: https://docs.scipy.org/doc/numpy-1.10.0/user/c-info.how-to-extend.html |
| 38 | // and https://stackoverflow.com/questions/47026900/pyarray-check-gives-segmentation-fault-with-cython-c |
| 39 | //just called only once |
| 40 | // int init_numpy(){ |
| 41 | // return _import_array(); // PyError if not successful |
| 42 | // // return 0; |
| 43 | // } |
| 44 | |
| 45 | // const static int numpy_initialized = init_numpy(); |
| 46 | |
| 47 | // void parse_ndarraray(PyObject *obj) { // would be called every time |
| 48 | // if (PyArray_Check(obj)) { |
| 49 | // cout << "PyArray_Check Passed" << endl; |
| 50 | // } else { |
| 51 | // cout << "PyArray_Check Failed" << endl; |
| 52 | // } |
| 53 | // } |
| 54 | |
| 55 | Jim::Jim(){ |
| 56 | reset(); |
| 57 | } |
| 58 | |
| 59 | size_t Jim::getDataTypeSizeBytes(int band) const { |
| 60 | #if MIALLIB == 1 |
| 61 | switch (getDataType()){ |
| 62 | case JDT_UInt64: |
| 63 | case JDT_Int64: |
| 64 | return(static_cast<size_t>(8)); |
| 65 | case JDT_Word: |
| 66 | return(static_cast<size_t>(1)); |
| 67 | default:{ |
| 68 | GDALDataType theType=getGDALDataType(band); |
| 69 | if(theType==GDT_Unknown){ |
| 70 | std::string errorString="Error: data type not supported"; |
| 71 | throw(errorString); |
| 72 | } |
| 73 | return static_cast<size_t>(GDALGetDataTypeSizeBytes(getGDALDataType(band))); |
| 74 | } |
| 75 | } |
| 76 | #else |
| 77 | GDALDataType theType=getGDALDataType(band); |
| 78 | if(theType==GDT_Unknown){ |
| 79 | std::string errorString="Error: data type not supported"; |
| 80 | throw(errorString); |
| 81 | } |
| 82 | return static_cast<size_t>(GDALGetDataTypeSizeBytes(getGDALDataType(band))); |
| 83 | #endif |
| 84 | } |
| 85 | |
| 86 | /// convert single plane multiband image to single band image with multiple planes |
| 87 | void Jim::d_band2plane(){ |
| 88 | //temporary buffer |
| 89 | // m_data.resize(nrOfBand()+1); |
| 90 | if(nrOfPlane()>1){ |
| 91 | std::string errorString="Error: band2plane only supported for single plane image"; |
| 92 | throw(errorString); |
| 93 | } |
| 94 | size_t oldnband=nrOfBand(); |
| 95 | for(size_t iband=1;iband<oldnband;++iband){ |
| 96 | m_nplane+=1; |
| 97 | void* newpointer=realloc(m_data[0],static_cast<size_t>(nrOfCol())*nrOfRow()*nrOfPlane()*getDataTypeSizeBytes()); |
| 98 | if(! newpointer){ |
| 99 | m_nplane-=1; |
| 100 | std::string errorString="Error: not sufficient memory for reallocation in d_band2plane"; |
| 101 | throw(errorString); |
| 102 | } |
| 103 | m_data[0]=newpointer; |
| 104 | memcpy((uint_least8_t*)(m_data[0])+static_cast<size_t>(getDataTypeSizeBytes())*nrOfCol()*nrOfRow()*iband,(uint_least8_t*)(m_data[iband]),static_cast<size_t>(getDataTypeSizeBytes())*nrOfCol()*nrOfRow()); |
| 105 | free(m_data[iband]); |
| 106 | m_data[iband]=0; |
| 107 | m_nband-=1; |
| 108 | #if MIALLIB == 1 |
| 109 | if(m_mia.size()>iband) |
| 110 | delete(m_mia[iband]); |
| 111 | #endif |
| 112 | } |
| 113 | m_data.erase(m_data.begin()+1,m_data.end()); |
| 114 | if(nrOfPlane()!=oldnband){ |
| 115 | std::string errorString="Error: nrOfPlane()!=oldnband in d_band2plane"; |
| 116 | throw(errorString); |
| 117 | } |
| 118 | if(nrOfBand()!=1){ |
| 119 | std::string errorString="Error: nrOfBand()!=1 in d_band2plane"; |
| 120 | throw(errorString); |
| 121 | } |
| 122 | m_mia.clear(); |
| 123 | } |
| 124 | |
| 125 | #if MIALLIB == 1 |
| 126 | IMAGE* Jim::getMIA(int band){ |
| 127 | try{ |
| 128 | if(getBlockSize()!=nrOfRow()){ |
| 129 | std::ostringstream s; |
| 130 | s << "Error: increase memory to support MIA library functions (now at " << 100.0*getBlockSize()/nrOfRow() << "%)"; |
| 131 | throw(s.str()); |
| 132 | } |
| 133 | if(nrOfBand()<=band){ |
| 134 | std::ostringstream s; |
| 135 | s << "Error: illegal band number in getMIA"; |
| 136 | throw(s.str()); |
| 137 | } |
| 138 | if(m_mia.size()<band+1) |
| 139 | m_mia.resize(band+1); |
| 140 | if(m_mia[band]){ |
| 141 | //test |
| 142 | // std::cout << "Warning: deleting m_mia[" << band << "]" << std::endl; |
| 143 | delete(m_mia[band]); |
| 144 | } |
| 145 | m_mia[band]=new(IMAGE); |
| 146 | m_mia[band]->p_im=m_data[band];/* Pointer to image data */ |
| 147 | m_mia[band]->DataType=getMIADataType(); |
| 148 | m_mia[band]->nx=nrOfCol(); |
| 149 | m_mia[band]->ny=nrOfRow(); |
| 150 | m_mia[band]->nz=nrOfPlane(); |
| 151 | m_mia[band]->NByte=(mia_size_t)m_mia[band]->nx * m_mia[band]->ny * m_mia[band]->nz * getDataTypeSizeBytes();//assumes image data type is not of bit type!!! |
| 152 | //todo: remove m_mia[band]->vol and only rely on the getVolume function |
| 153 | m_mia[band]->vol=0;//use getVolume() function |
| 154 | m_mia[band]->lut=0; |
| 155 | //USHORT *lut; /* Pointer to colour map */ |
| 156 | //mia->g=getgetDataType();//not used |
| 157 | return m_mia[band]; |
| 158 | } |
| 159 | catch(std::string errorString){ |
| 160 | std::cerr << errorString << std::endl; |
| 161 | throw; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * set jim data pointer to the m_mia data pointer |
| 167 | * |
| 168 | * @param band the band for which the MIA image pointer needs to be set |
| 169 | * |
| 170 | * @return CE_None if successful |
| 171 | */ |
| 172 | CPLErr Jim::setMIA(int band){ |
| 173 | try{ |
| 174 | if(m_mia.size()<band+1){ |
| 175 | std::ostringstream s; |
| 176 | std::cerr << "Error: illegal band number when setting MIA in Jim"; |
| 177 | throw(s.str()); |
| 178 | } |
| 179 | if(m_ncol!=m_mia[band]->nx){ |
| 180 | // std::cerr << "Warning: x dimension of image (" << m_ncol << ") does not match MIA (" << m_mia[band]->nx << "), adapting m_ncol" << std::endl; |
| 181 | m_ncol=m_mia[band]->nx; |
| 182 | } |
| 183 | if(m_nrow!=m_mia[band]->ny){ |
| 184 | // std::cerr << "Warning: y dimension of image (" << m_nrow << ") does not match MIA (" << m_mia[band]->ny << "), adapting m_nrow" << std::endl; |
| 185 | m_nrow=m_mia[band]->ny; |
| 186 | m_blockSize=m_nrow; |
| 187 | } |
| 188 | if(m_mia[band]->nz!=m_nplane){ |
| 189 | // std::cerr << "Warning: z dimension of image (" << m_nplane << ") does not match MIA (" << m_mia[band]->nz << "), adapting m_nplane" << std::endl; |
| 190 | m_nplane=m_mia[band]->nz; |
| 191 | } |
| 192 | if(m_nband<=band){ |
| 193 | std::ostringstream s; |
| 194 | std::string errorString="Error: band exceeds number of bands in target image"; |
| 195 | throw(errorString); |
| 196 | } |
| 197 | // if(m_nband>1&&m_dataType!=MIA2GDALDataType(m_mia[band]->DataType)){ |
| 198 | if( (m_dataType!=MIA2JIPLIBDataType(m_mia[band]->DataType))){ |
| 199 | if(nrOfBand() > 1) |
| 200 | std::cerr << "Warning: changing data type of multiband image, make sure to set all bands" << std::endl; |
| 201 | m_dataType=MIA2JIPLIBDataType(m_mia[band]->DataType); |
| 202 | } |
| 203 | m_data[band]=(void *)m_mia[band]->p_im; |
| 204 | m_mia[band]->p_im=0;//needed because the destructor of IMAGE (created in mia_tmp.i) will delete the image!!! |
| 205 | m_begin[band]=0; |
| 206 | m_end[band]=m_begin[band]+getBlockSize(); |
| 207 | } |
| 208 | catch(std::string errorString){ |
| 209 | std::cerr << errorString << std::endl; |
| 210 | throw; |
| 211 | } |
| 212 | catch(...){ |
| 213 | throw; |
| 214 | } |
| 215 | return(CE_None); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * set Jim attributes from external MIA image |
| 220 | * |
| 221 | * @param mia the MIA image pointer to be set |
| 222 | * @param band the band for which the MIA image pointer needs to be set |
| 223 | * |
| 224 | * @return C_None if successful |
| 225 | */ |
| 226 | CPLErr Jim::setMIA(IMAGE* mia, int band){ |
| 227 | try{ |
| 228 | if(nrOfBand()){ |
| 229 | if(m_ncol!=mia->nx){ |
| 230 | std::ostringstream s; |
| 231 | s << "Error: x dimension of image (" << m_ncol << ") does not match MIA (" << mia->nx << ")"; |
| 232 | throw(s.str()); |
| 233 | } |
| 234 | if(m_nrow!=mia->ny){ |
| 235 | std::ostringstream s; |
| 236 | s << "Error: y dimension of image (" << m_nrow << ") does not match MIA (" << mia->ny << ")"; |
| 237 | throw(s.str()); |
| 238 | } |
| 239 | if(m_nplane!=mia->nz){ |
| 240 | std::string errorString="Error: number of planes of images do not match"; |
| 241 | throw(errorString); |
| 242 | } |
| 243 | if(m_dataType!=MIA2JIPLIBDataType(m_mia[band]->DataType)){ |
| 244 | std::string errorString="Error: inconsistent data types for multiband image"; |
| 245 | throw(errorString); |
| 246 | } |
| 247 | } |
| 248 | if(m_mia.size()<band+1){ |
| 249 | m_mia.resize(band+1); |
| 250 | m_nband=band+1; |
| 251 | } |
| 252 | if(m_data.size()<band+1){ |
| 253 | m_data.resize(band+1); |
| 254 | m_begin.resize(band+1); |
| 255 | m_end.resize(band+1); |
| 256 | } |
| 257 | m_nplane=mia->nz; |
| 258 | m_ncol=mia->nx; |
| 259 | m_nrow=mia->ny; |
| 260 | m_blockSize=m_nrow; |
| 261 | m_mia[band]=mia; |
| 262 | // setExternalData(true);//todo: need to fix memory leak when setMIA used for single band only! (either create vector<bool> m_externalData or only allow for setMIA all bands) |
| 263 | this->setMIA(band); |
| 264 | } |
| 265 | catch(std::string errorString){ |
| 266 | std::cerr << errorString << std::endl; |
| 267 | throw; |
| 268 | } |
| 269 | catch(...){ |
| 270 | throw; |
| 271 | } |
| 272 | return(CE_None); |
| 273 | } |
| 274 | |
| 275 | int Jim::getMIADataType(){ |
| 276 | switch (getDataType()){ |
| 277 | case GDT_Byte: |
| 278 | return t_UCHAR; |
| 279 | case GDT_UInt16: |
| 280 | return t_USHORT; |
| 281 | case GDT_Int16: |
| 282 | return t_SHORT; |
| 283 | case GDT_UInt32: |
| 284 | return t_UINT32; |
| 285 | case GDT_Int32: |
| 286 | return t_INT32; |
| 287 | case GDT_Float32: |
| 288 | return t_FLOAT; |
| 289 | case GDT_Float64: |
| 290 | return t_DOUBLE; |
| 291 | case JDT_UInt64: |
| 292 | return t_UINT64; |
| 293 | case JDT_Int64: |
| 294 | return t_INT64; |
| 295 | case JDT_Word: |
| 296 | return t_FOURBITPERPIXEL; |
| 297 | case t_UNSUPPORTED: |
| 298 | return GDT_Unknown; |
| 299 | default: |
| 300 | return GDT_Unknown; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | int Jim::JIPLIB2MIADataType(int aJIPLIBDataType){ |
| 305 | //function exists, but introduced for naming consistency |
| 306 | if(aJIPLIBDataType==JDT_UInt64) |
| 307 | return(t_UINT64); |
| 308 | else if(aJIPLIBDataType==JDT_Int64) |
| 309 | return(t_INT64); |
| 310 | else if(aJIPLIBDataType==JDT_Word) |
| 311 | return(t_FOURBITPERPIXEL); |
| 312 | else |
| 313 | return(GDAL2MIALDataType(aJIPLIBDataType)); |
| 314 | } |
| 315 | |
| 316 | ///convert a GDAL to MIA data type |
| 317 | int Jim::GDAL2MIADataType(GDALDataType aGDALDataType){ |
| 318 | //function exists, but introduced for naming consistency |
| 319 | return(GDAL2MIALDataType(aGDALDataType)); |
| 320 | } |
| 321 | |
| 322 | ///convert a MIA data type to GDAL data type |
| 323 | int Jim::MIA2JIPLIBDataType(int aMIADataType){ |
| 324 | |
| 325 | switch (aMIADataType){ |
| 326 | case t_UCHAR: |
| 327 | return GDT_Byte; |
| 328 | case t_USHORT: |
| 329 | return GDT_UInt16; |
| 330 | case t_SHORT: |
| 331 | return GDT_Int16; |
| 332 | case t_UINT32: |
| 333 | return GDT_UInt32; |
| 334 | case t_INT32: |
| 335 | return GDT_Int32; |
| 336 | case t_FLOAT: |
| 337 | return GDT_Float32; |
| 338 | case t_DOUBLE: |
| 339 | return GDT_Float64; |
| 340 | case t_UINT64: |
| 341 | return JDT_UInt64; |
| 342 | case t_INT64: |
| 343 | return JDT_Int64; |
| 344 | case t_FOURBITPERPIXEL: |
| 345 | return JDT_Word; |
| 346 | case t_UNSUPPORTED: |
| 347 | return GDT_Unknown; |
| 348 | default: |
| 349 | return GDT_Unknown; |
| 350 | } |
| 351 | } |
| 352 | #endif |
| 353 | |
| 354 | void Jim::reset() |
| 355 | { |
| 356 | m_gds=0; |
| 357 | m_ncol=0; |
| 358 | m_nrow=0; |
| 359 | m_nband=0; |
| 360 | m_nplane=1;//todo: check if better set to 0 or 1? |
| 361 | m_dataType=GDT_Unknown; |
| 362 | m_gt.resize(6); |
| 363 | for(int index=0;index<6;++index){m_gt[index]=0;}; |
| 364 | m_noDataValues.clear(); |
| 365 | m_projection=std::string(); |
| 366 | m_scale.clear(); |
| 367 | m_offset.clear(); |
| 368 | m_blockSize=0; |
| 369 | m_begin.clear(); |
| 370 | m_end.clear(); |
| 371 | m_coptions.clear(); |
| 372 | m_ooptions.clear(); |
| 373 | // m_writeMode=false; |
| 374 | m_access=READ_ONLY; |
| 375 | m_resample=GRIORA_NearestNeighbour; |
| 376 | m_filename.clear(); |
| 377 | freeMem(); |
| 378 | // m_data.clear(); |
| 379 | //is there any reason why we cannot set external data to false? |
| 380 | m_externalData=false; |
| 381 | #if MIALLIB == 1 |
| 382 | for(int iband=0;iband<m_mia.size();++iband) |
| 383 | delete(m_mia[iband]); |
| 384 | m_mia.clear(); |
| 385 | #endif |
| 386 | } |
| 387 | |
| 388 | // Jim::Jim(PyObject* npArray, bool copyData){ |
| 389 | // reset(); |
| 390 | // if(npArray){ |
| 391 | // if(PyArray_Check(npArray)){ |
| 392 | // PyArrayObject *obj=(PyArrayObject *)npArray; |
| 393 | // GDALDataType jDataType; |
| 394 | // int dim=obj->nd; |
| 395 | // int ncol=(dim==3)? obj->dimensions[2] : obj->dimensions[1]; |
| 396 | // int nrow=(dim==3)? obj->dimensions[1] : obj->dimensions[0]; |
| 397 | // int nplane=(dim==3)? obj->dimensions[0] : 1; |
| 398 | // switch(obj->descr->type_num){ |
| 399 | // case NPY_UINT8: |
| 400 | // jDataType=GDT_Byte; |
| 401 | // break; |
| 402 | // case NPY_UINT16: |
| 403 | // jDataType=GDT_UInt16; |
| 404 | // break; |
| 405 | // case NPY_INT16: |
| 406 | // jDataType=GDT_Int16; |
| 407 | // break; |
| 408 | // case NPY_UINT32: |
| 409 | // jDataType=GDT_UInt32; |
| 410 | // break; |
| 411 | // case NPY_INT32: |
| 412 | // jDataType=GDT_Int32; |
| 413 | // break; |
| 414 | // case NPY_FLOAT32: |
| 415 | // jDataType=GDT_Float32; |
| 416 | // break; |
| 417 | // case NPY_FLOAT64: |
| 418 | // jDataType=GDT_Float64; |
| 419 | // break; |
| 420 | // // case NPY_UINT64: |
| 421 | // // jDataType=; |
| 422 | // // break; |
| 423 | // // case NPY_INT64: |
| 424 | // // jDataType=; |
| 425 | // // break; |
| 426 | // default: |
| 427 | // std::string errorString="Error: Unknown data type"; |
| 428 | // throw(errorString); |
| 429 | // } |
| 430 | // GDALDataType datatype=GDT_Unknown; |
| 431 | // open(obj->data,ncol,nrow,nplane,jDataType,copyData); |
| 432 | // } |
| 433 | // else{ |
| 434 | // std::string errorString="Error: not a numpy object"; |
| 435 | // throw(errorString); |
| 436 | // } |
| 437 | // } |
| 438 | // else{ |
| 439 | // std::string errorString="Error: pointer to numpy object 0"; |
| 440 | // throw(errorString); |
| 441 | // } |
| 442 | // } |
| 443 | |
| 444 | Jim::Jim(void* dataPointer, int ncol, int nrow, const GDALDataType& dataType){ |
| 445 | reset(); |
| 446 | open(dataPointer,ncol,nrow,dataType); |
| 447 | } |
| 448 | |
| 449 | Jim::Jim(std::vector<void*> dataPointers, int ncol, int nrow, const GDALDataType& dataType){ |
| 450 | reset(); |
| 451 | open(dataPointers,ncol,nrow,dataType); |
| 452 | } |
| 453 | |
| 454 | ///constructor opening an image in memory using an external data pointer |
| 455 | Jim::Jim(void* dataPointer, int ncol, int nrow, int nplane, const GDALDataType& dataType){ |
| 456 | reset(); |
| 457 | open(dataPointer,ncol,nrow,nplane,dataType); |
| 458 | } |
| 459 | ///constructor opening a multiband image in memory using an external data pointer |
| 460 | Jim::Jim(std::vector<void*> dataPointers, int ncol, int nrow, int nplane, const GDALDataType& dataType){ |
| 461 | reset(); |
| 462 | open(dataPointers,ncol,nrow,nplane,dataType); |
| 463 | } |
| 464 | |
| 465 | Jim::Jim(const std::string& filename, const Jim& imgSrc, unsigned int memory, const std::vector<std::string>& options){ |
| 466 | reset(); |
| 467 | open(filename,imgSrc,memory,options); |
| 468 | } |
| 469 | |
| 470 | #if MIALLIB == 1 |
| 471 | ///constructor input image |
| 472 | Jim::Jim(IMAGE *mia) : m_nplane(1){ |
| 473 | reset(); |
| 474 | if(mia) |
| 475 | setMIA(mia,0); |
| 476 | } |
| 477 | #endif |
| 478 | |
| 479 | ///constructor output image |
| 480 | Jim::Jim(const std::string& filename, bool readData, unsigned int memory){ |
| 481 | reset(); |
| 482 | open(filename,readData,memory); |
| 483 | } |
| 484 | |
| 485 | ///constructor output image |
| 486 | Jim::Jim(Jim& imgSrc, bool copyData){ |
| 487 | reset(); |
| 488 | if(imgSrc.isInit()) |
| 489 | open(imgSrc,copyData); |
| 490 | } |
| 491 | ///constructor output image |
| 492 | Jim::Jim(const std::string& filename, int ncol, int nrow, int nband, const GDALDataType& dataType, const std::string& imageType, unsigned int memory, const std::vector<std::string>& options){ |
| 493 | reset(); |
| 494 | open(filename, ncol, nrow, nband, dataType, imageType, memory, options); |
| 495 | } |
| 496 | ///constructor output image with nplane |
| 497 | Jim::Jim(int ncol, int nrow, int nband, int nplane, const GDALDataType& dataType){ |
| 498 | reset(); |
| 499 | open(ncol,nrow,nband,nplane,dataType); |
| 500 | } |
| 501 | ///constructor output image |
| 502 | Jim::Jim(int ncol, int nrow, int nband, const GDALDataType& dataType){ |
| 503 | reset(); |
| 504 | open(ncol, nrow, nband, dataType); |
| 505 | } |
| 506 | //test |
| 507 | // Jim::Jim(app::AppFactory &theApp): m_nplane(1), Jim(theApp){}; |
| 508 | Jim::Jim(app::AppFactory &theApp){ |
| 509 | reset(); |
| 510 | open(theApp); |
| 511 | } |
| 512 | |
| 513 | |
| 514 | ///destructor |
| 515 | Jim::~Jim(void){ |
| 516 | #if MIALLIB == 1 |
| 517 | if(m_mia.size()){ |
| 518 | for(int iband=0;iband<m_mia.size();++iband) |
| 519 | if(m_mia[iband]) |
| 520 | delete(m_mia[iband]); |
| 521 | m_mia.clear(); |
| 522 | } |
| 523 | #endif |
| 524 | // Jim::reset(); |
| 525 | close(); |
| 526 | } |
| 527 | /** |
| 528 | * @param memory Available memory to cache image raster data (in MB) |
| 529 | **/ |
| 530 | CPLErr Jim::initMem(unsigned int memory) |
| 531 | { |
| 532 | if(memory<=0) |
| 533 | m_blockSize=nrOfRow(); |
| 534 | else{ |
| 535 | m_blockSize=static_cast<unsigned int>(memory)*1000000/nrOfBand()/nrOfCol()/getDataTypeSizeBytes(); |
| 536 | if(getBlockSizeY(0)) |
| 537 | m_blockSize-=m_blockSize%getBlockSizeY(0); |
| 538 | } |
| 539 | if(m_blockSize<1) |
| 540 | m_blockSize=1; |
| 541 | if(m_blockSize>nrOfRow()) |
| 542 | m_blockSize=nrOfRow(); |
| 543 | m_begin.resize(nrOfBand()); |
| 544 | m_end.resize(nrOfBand()); |
| 545 | freeMem(); |
| 546 | m_data.resize(nrOfBand()); |
| 547 | //todo check if needed when externalData is true |
| 548 | for(int iband=0;iband<nrOfBand();++iband){ |
| 549 | m_data[iband]=(void *) calloc(static_cast<size_t>(nrOfPlane())*nrOfCol()*m_blockSize,getDataTypeSizeBytes()); |
| 550 | if(!(m_data[iband])){ |
| 551 | std::string errorString="Error: could not allocate memory in initMem"; |
| 552 | throw(errorString); |
| 553 | } |
| 554 | } |
| 555 | return(CE_None); |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * @param memory Available memory to cache image raster data (in MB) |
| 560 | **/ |
| 561 | void Jim::freeMem() |
| 562 | { |
| 563 | // if(m_data.size()&&m_filename.size()){ |
| 564 | for(int iband=0;iband<m_data.size();++iband){ |
| 565 | if(m_data.size()>iband){ |
| 566 | if(m_externalData) |
| 567 | m_data[iband]=0; |
| 568 | else |
| 569 | free(m_data[iband]); |
| 570 | // delete(m_data[iband]); |
| 571 | // CPLFree(m_data[iband]); |
| 572 | } |
| 573 | } |
| 574 | m_data.clear(); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * @param imgSrc Use this source image as a template to copy image attributes |
| 579 | **/ |
| 580 | // Jim& Jim::operator=(Jim& imgSrc) |
| 581 | // { |
| 582 | // bool copyData=true; |
| 583 | // //check for assignment to self (of the form v=v) |
| 584 | // if(this==&imgSrc) |
| 585 | // return *this; |
| 586 | // else{ |
| 587 | // open(imgSrc,copyData); |
| 588 | // return *this; |
| 589 | // } |
| 590 | // } |
| 591 | |
| 592 | // ///assignment operator for in memory image |
| 593 | // std::shared_ptr<Jim> Jim::operator=(std::shared_ptr<Jim> imgSrc){ |
| 594 | // std::shared_ptr<Jim> imgWriter=std::make_shared<Jim>(); |
| 595 | // *imgWriter=*imgSrc; |
| 596 | // return(imgWriter); |
| 597 | // } |
| 598 | |
| 599 | /** |
| 600 | * @param dataPointer External pointer to which the image data should be written in memory |
| 601 | * @param ncol The number of columns in the image |
| 602 | * @param nrow The number of rows in the image |
| 603 | * @param dataType The data type of the image (one of the GDAL supported datatypes: GDT_Byte, GDT_[U]Int[16|32], GDT_Float[32|64]) |
| 604 | **/ |
| 605 | CPLErr Jim::open(void* dataPointer, int ncol, int nrow, const GDALDataType& dataType){ |
| 606 | m_ncol=ncol; |
| 607 | m_nrow=nrow; |
| 608 | m_nband=1; |
| 609 | m_dataType=dataType; |
| 610 | m_data.resize(m_nband); |
| 611 | m_begin.resize(m_nband); |
| 612 | m_end.resize(m_nband); |
| 613 | m_blockSize=nrow;//memory contains entire image and has been read already |
| 614 | if(dataPointer){ |
| 615 | for(int iband=0;iband<m_nband;++iband){ |
| 616 | // m_data[iband]=dataPointer+iband*ncol*nrow*getDataTypeSizeBytes(); |
| 617 | m_data[iband]=(uint8_t*)dataPointer; |
| 618 | m_begin[iband]=0; |
| 619 | m_end[iband]=m_begin[iband]+m_blockSize; |
| 620 | } |
| 621 | // m_externalData=true; |
| 622 | return(CE_None); |
| 623 | } |
| 624 | else |
| 625 | return(CE_Failure); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * @param dataPointers External pointers to which the image data should be written in memory |
| 630 | * @param ncol The number of columns in the image |
| 631 | * @param nrow The number of rows in the image |
| 632 | * @param dataType The data type of the image (one of the GDAL supported datatypes: GDT_Byte, GDT_[U]Int[16|32], GDT_Float[32|64]) |
| 633 | **/ |
| 634 | CPLErr Jim::open(std::vector<void*> dataPointers, int ncol, int nrow, const GDALDataType& dataType){ |
| 635 | m_ncol=ncol; |
| 636 | m_nrow=nrow; |
| 637 | m_nband=dataPointers.size(); |
| 638 | m_dataType=dataType; |
| 639 | m_data.resize(m_nband); |
| 640 | m_begin.resize(m_nband); |
| 641 | m_end.resize(m_nband); |
| 642 | m_blockSize=nrow;//memory contains entire image and has been read already |
| 643 | if(dataPointers.size()){ |
| 644 | for(int iband=0;iband<m_nband;++iband){ |
| 645 | if(dataPointers[iband]){ |
| 646 | // m_data[iband]=dataPointer+iband*ncol*nrow*getDataTypeSizeBytes(); |
| 647 | m_data[iband]=(uint8_t*)dataPointers[iband]; |
| 648 | // m_data[iband]=(uint8_t*)dataPointers[iband]+iband*ncol*nrow*getDataTypeSizeBytes(); |
| 649 | m_begin[iband]=0; |
| 650 | m_end[iband]=m_begin[iband]+m_blockSize; |
| 651 | } |
| 652 | } |
| 653 | // m_externalData=true; |
| 654 | return(CE_None); |
| 655 | } |
| 656 | else |
| 657 | return(CE_Failure); |
| 658 | } |
| 659 | |
| 660 | ///Open an image for writing, copying data from dataPointer |
| 661 | CPLErr Jim::open(void* dataPointer, int ncol, int nrow, int nplane, const GDALDataType& dataType, bool copyData){ |
| 662 | m_ncol=ncol; |
| 663 | m_nrow=nrow; |
| 664 | m_nplane=nplane; |
| 665 | m_nband=1; |
| 666 | m_dataType=dataType; |
| 667 | m_data.resize(m_nband); |
| 668 | m_begin.resize(m_nband); |
| 669 | m_end.resize(m_nband); |
| 670 | m_blockSize=nrow;//memory contains entire image and has been read already |
| 671 | if(dataPointer){ |
| 672 | if(copyData){ |
| 673 | initMem(0); |
| 674 | memcpy((uint_least8_t*)(m_data[0]),(uint_least8_t*)(dataPointer),getDataTypeSizeBytes()*nrOfCol()*m_blockSize*nrOfPlane()); |
| 675 | } |
| 676 | else{ |
| 677 | m_data[0]=(uint8_t*)dataPointer; |
| 678 | m_begin[0]=0; |
| 679 | m_end[0]=m_begin[0]+m_blockSize; |
| 680 | } |
| 681 | /* |
| 682 | for(int iband=0;iband<m_nband;++iband){ |
| 683 | m_data[iband]=(char *)dataPointer+iband*ncol*nrow*nplane*getDataTypeSizeBytes(); |
| 684 | m_begin[iband]=0; |
| 685 | m_end[iband]=m_begin[iband]+m_blockSize; |
| 686 | } |
| 687 | */ |
| 688 | // m_externalData=true; |
| 689 | return(CE_None); |
| 690 | } |
| 691 | else |
| 692 | return(CE_Failure); |
| 693 | } |
| 694 | |
| 695 | ///Open a multiband image for writing, based on an existing image object |
| 696 | CPLErr Jim::open(std::vector<void*> dataPointers, int ncol, int nrow, int nplane, const GDALDataType& dataType, bool copyData){ |
| 697 | m_ncol=ncol; |
| 698 | m_nrow=nrow; |
| 699 | m_nplane=nplane; |
| 700 | m_nband=dataPointers.size(); |
| 701 | m_dataType=dataType; |
| 702 | m_data.resize(m_nband); |
| 703 | m_begin.resize(m_nband); |
| 704 | m_end.resize(m_nband); |
| 705 | m_blockSize=nrow;//memory contains entire image and has been read already |
| 706 | if(dataPointers.size()){ |
| 707 | if(copyData){ |
| 708 | initMem(0); |
| 709 | for(int iband=0;iband<m_nband;++iband){ |
| 710 | if(dataPointers[iband]){ |
| 711 | memcpy((uint_least8_t*)(m_data[iband]),(uint_least8_t*)(dataPointers[iband]),getDataTypeSizeBytes()*nrOfCol()*m_blockSize*nrOfPlane()); |
| 712 | m_begin[iband]=0; |
| 713 | m_end[iband]=m_begin[iband]+m_blockSize; |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | else{ |
| 718 | for(int iband=0;iband<m_nband;++iband){ |
| 719 | if(dataPointers[iband]){ |
| 720 | m_data[iband]=(uint8_t*)dataPointers[iband]; |
| 721 | m_begin[iband]=0; |
| 722 | m_end[iband]=m_begin[iband]+m_blockSize; |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | // m_externalData=true; |
| 727 | return(CE_None); |
| 728 | } |
| 729 | else |
| 730 | return(CE_Failure); |
| 731 | } |
| 732 | CPLErr Jim::close() |
| 733 | { |
| 734 | if(m_gds){ |
| 735 | GDALClose(m_gds); |
| 736 | } |
| 737 | reset(); |
| 738 | return(CE_None); |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * @return the projection of this data set in string format |
| 743 | **/ |
| 744 | std::string Jim::getProjection() const |
| 745 | { |
| 746 | // if(m_gds) |
| 747 | // return(m_gds->GetProjectionRef()); |
| 748 | // else |
| 749 | return(m_projection); |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * @return the projection of this data set in string format |
| 754 | **/ |
| 755 | std::string Jim::getProjectionRef() const |
| 756 | { |
| 757 | // if(m_gds) |
| 758 | // return(m_gds->GetProjectionRef()); |
| 759 | // else |
| 760 | return(m_projection); |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * @param projection projection string to be used for this dataset |
| 765 | * @return the projection of this data set in string format |
| 766 | **/ |
| 767 | void Jim::setProjectionProj4(const std::string& projection) |
| 768 | { |
| 769 | if(projection.size()){ |
| 770 | OGRSpatialReference theRef; |
| 771 | theRef.SetFromUserInput(projection.c_str()); |
| 772 | char *wktString; |
| 773 | theRef.exportToWkt(&wktString); |
| 774 | m_projection=wktString; |
| 775 | //todo: must not be limited to access==WRITE? |
| 776 | if(m_gds&&m_access==WRITE) |
| 777 | m_gds->SetProjection(wktString); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | /** |
| 782 | * @param projection projection string to be used for this dataset |
| 783 | **/ |
| 784 | //deprecated? |
| 785 | // CPLErr Jim::setProjection(const std::string& projection) |
| 786 | // { |
| 787 | // m_projection=projection; |
| 788 | // if(m_gds){ |
| 789 | // return(m_gds->SetProjection(projection.c_str())); |
| 790 | // } |
| 791 | // else{ |
| 792 | // return(CE_Warning); |
| 793 | // } |
| 794 | // } |
| 795 | |
| 796 | /** |
| 797 | * @param band get data type for this band (start counting from 0) |
| 798 | * @return the GDAL data type of this data set for the selected band |
| 799 | **/ |
| 800 | GDALDataType Jim::getDataTypeDS(int band) const |
| 801 | { |
| 802 | if(!nrOfBand()) |
| 803 | return(GDT_Unknown); |
| 804 | if(nrOfBand()<=band){ |
| 805 | std::string errorString="Error: band number exceeds available bands in getDataType"; |
| 806 | throw(errorString); |
| 807 | } |
| 808 | if(getRasterBand(band)) |
| 809 | return((getRasterBand(band)->GetRasterDataType())); |
| 810 | else |
| 811 | return(GDT_Unknown); |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * @param band get data type for this band (start counting from 0) |
| 816 | * @return the data type of this data set for the selected band |
| 817 | **/ |
| 818 | int Jim::getDataType(int band) const |
| 819 | //GDALDataType Jim::getDataType(int band) const |
| 820 | { |
| 821 | if(!nrOfBand()) |
| 822 | return(0); |
| 823 | if(nrOfBand()<=band){ |
| 824 | std::string errorString="Error: band number exceeds available bands in getDataType"; |
| 825 | throw(errorString); |
| 826 | } |
| 827 | // if(getRasterBand(band)) |
| 828 | // return((getRasterBand(band)->GetRasterDataType())); |
| 829 | // else |
| 830 | return(m_dataType); |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * @param band get data type for this band (start counting from 0) |
| 835 | * @return the GDAL data type of this data set for the selected band |
| 836 | **/ |
| 837 | GDALDataType Jim::getGDALDataType(int band) const |
| 838 | { |
| 839 | if(!nrOfBand()) |
| 840 | return(GDT_Unknown); |
| 841 | if(nrOfBand()<=band){ |
| 842 | std::string errorString="Error: band number exceeds available bands in getDataType"; |
| 843 | throw(errorString); |
| 844 | } |
| 845 | // if(getRasterBand(band)) |
| 846 | // return((getRasterBand(band)->GetRasterDataType())); |
| 847 | // else |
| 848 | switch(getDataType()){ |
| 849 | case(GDT_Byte): |
| 850 | return(GDT_Byte); |
| 851 | case(GDT_Int16): |
| 852 | return(GDT_Int16); |
| 853 | case(GDT_UInt16): |
| 854 | return(GDT_UInt16); |
| 855 | case(GDT_Int32): |
| 856 | return(GDT_Int32); |
| 857 | case(GDT_UInt32): |
| 858 | return(GDT_UInt32); |
| 859 | case(GDT_Float32): |
| 860 | return(GDT_Float32); |
| 861 | case(GDT_Float64): |
| 862 | return(GDT_Float64); |
| 863 | default: |
| 864 | return(GDT_Unknown); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * @param band get GDAL raster band for this band (start counting from 0) |
| 870 | * @return the GDAL raster band of this data set for the selected band |
| 871 | **/ |
| 872 | GDALRasterBand* Jim::getRasterBand(int band) const |
| 873 | { |
| 874 | if(nrOfBand()<=band){ |
| 875 | std::string errorString="Error: band number exceeds available bands in getRasterBand"; |
| 876 | throw(errorString); |
| 877 | } |
| 878 | if(m_gds) |
| 879 | return((m_gds->GetRasterBand(band+1))); |
| 880 | else |
| 881 | return(0); |
| 882 | } |
| 883 | |
| 884 | /** |
| 885 | * @param band get GDAL color table for this band (start counting from 0) |
| 886 | * @return the GDAL color table of this data set for the selected band |
| 887 | **/ |
| 888 | GDALColorTable* Jim::getColorTable(int band) const |
| 889 | { |
| 890 | if(nrOfBand()<=band){ |
| 891 | std::string errorString="Error: band number exceeds available bands in getColorTable"; |
| 892 | throw(errorString); |
| 893 | } |
| 894 | GDALRasterBand* theRasterBand=getRasterBand(band); |
| 895 | if(theRasterBand) |
| 896 | return(theRasterBand->GetColorTable()); |
| 897 | else |
| 898 | return(0); |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * @return the driver description of this data set in string format |
| 903 | **/ |
| 904 | std::string Jim::getDriverDescription() const |
| 905 | { |
| 906 | std::string driverDescription; |
| 907 | if(m_gds) |
| 908 | driverDescription=m_gds->GetDriver()->GetDescription(); |
| 909 | return(driverDescription); |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * @param gt pointer to the six geotransform parameters: |
| 914 | * @param adfGeoTransform[0] top left x |
| 915 | * @param GeoTransform[1] w-e pixel resolution |
| 916 | * @param GeoTransform[2] rotation, 0 if image is "north up" |
| 917 | * @param GeoTransform[3] top left y |
| 918 | * @param GeoTransform[4] rotation, 0 if image is "north up" |
| 919 | * @param GeoTransform[5] n-s pixel resolution |
| 920 | * return CE_None if geotransform could be set for GDAL dataset |
| 921 | **/ |
| 922 | CPLErr Jim::setGeoTransform(double* gt){ |
| 923 | m_gt.resize(6); |
| 924 | // m_isGeoRef=true; |
| 925 | m_gt[0]=gt[0]; |
| 926 | m_gt[1]=gt[1]; |
| 927 | m_gt[2]=gt[2]; |
| 928 | m_gt[3]=gt[3]; |
| 929 | m_gt[4]=gt[4]; |
| 930 | m_gt[5]=gt[5]; |
| 931 | //todo: must not be limited to access==WRITE? |
| 932 | if(m_gds&&m_access==WRITE) |
| 933 | return(m_gds->SetGeoTransform(&m_gt[0])); |
| 934 | else |
| 935 | return(CE_Warning); |
| 936 | } |
| 937 | |
| 938 | CPLErr Jim::setGeoTransform(const std::vector<double>& gt){ |
| 939 | if(gt.size()==6){ |
| 940 | m_gt.resize(6); |
| 941 | // m_isGeoRef=true; |
| 942 | m_gt[0]=gt[0]; |
| 943 | m_gt[1]=gt[1]; |
| 944 | m_gt[2]=gt[2]; |
| 945 | m_gt[3]=gt[3]; |
| 946 | m_gt[4]=gt[4]; |
| 947 | m_gt[5]=gt[5]; |
| 948 | //todo: must not be limited to access==WRITE? |
| 949 | if(m_gds&&m_access==WRITE) |
| 950 | return(m_gds->SetGeoTransform(&m_gt[0])); |
| 951 | } |
| 952 | // else |
| 953 | // return(CE_Warning); |
| 954 | return(CE_Warning); |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * @param imgSrc Use this source image as a template to copy geotranform information |
| 959 | * return CE_None if geotransform could be copied for GDAL dataset |
| 960 | **/ |
| 961 | CPLErr Jim::copyGeoTransform(const Jim& imgSrc) |
| 962 | { |
| 963 | vector<double> gt; |
| 964 | imgSrc.getGeoTransform(gt); |
| 965 | return(setGeoTransform(gt)); |
| 966 | } |
| 967 | |
| 968 | CPLErr Jim::copyGeoReference(const Jim& imgSrc) |
| 969 | { |
| 970 | vector<double> gt; |
| 971 | imgSrc.getGeoTransform(gt); |
| 972 | setGeoTransform(gt); |
| 973 | setProjection(imgSrc.getProjection()); |
| 974 | return(CE_None); |
| 975 | } |
| 976 | |
| 977 | /** |
| 978 | * @param imgSrc Use this pointer to source image as a template to copy geotranform information |
| 979 | **/ |
| 980 | // void Jim::copyGeoTransform(const std::shared_ptr<Jim>& imgSrc) |
| 981 | // { |
| 982 | // double gt[6]; |
| 983 | // imgSrc->getGeoTransform(gt); |
| 984 | // setGeoTransform(gt); |
| 985 | // } |
| 986 | |
| 987 | /** |
| 988 | * @param gt pointer to the six geotransform parameters: |
| 989 | * @param adfGeoTransform[0] top left x |
| 990 | * @param GeoTransform[1] w-e pixel resolution |
| 991 | * @param GeoTransform[2] rotation, 0 if image is "north up" |
| 992 | * @param GeoTransform[3] top left y |
| 993 | * @param GeoTransform[4] rotation, 0 if image is "north up" |
| 994 | * @param GeoTransform[5] n-s pixel resolution |
| 995 | **/ |
| 996 | void Jim::getGeoTransform(double* gt) const{ |
| 997 | if(m_gt.size()==6){ |
| 998 | gt[0]=m_gt[0]; |
| 999 | gt[1]=m_gt[1]; |
| 1000 | gt[2]=m_gt[2]; |
| 1001 | gt[3]=m_gt[3]; |
| 1002 | gt[4]=m_gt[4]; |
| 1003 | gt[5]=m_gt[5]; |
| 1004 | } |
| 1005 | else |
| 1006 | gt=0; |
| 1007 | } |
| 1008 | |
| 1009 | void Jim::getGeoTransform(vector<double>& gt) const{ |
| 1010 | gt=m_gt; |
| 1011 | } |
| 1012 | |
| 1013 | /** |
| 1014 | * @return the geotransform of this data set in string format |
| 1015 | **/ |
| 1016 | // std::string Jim::getGeoTransform() const |
| 1017 | // { |
| 1018 | // std::string gtString; |
| 1019 | // double gt[6];// { 444720, 30, 0, 3751320, 0, -30 }; |
| 1020 | // getGeoTransform(gt); |
| 1021 | // std::ostringstream s; |
| 1022 | // s << "[" << gt[0] << "," << gt[1] << "," << gt[2] << "," << gt[3] << "," << gt[4] << "," << gt[5] << "]"; |
| 1023 | // gtString=s.str(); |
| 1024 | // return(s.str()); |
| 1025 | // } |
| 1026 | |
| 1027 | /** |
| 1028 | * @return the metadata of this data set in C style string format (const version) |
| 1029 | **/ |
| 1030 | #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 13, 0) |
| 1031 | CSLConstList Jim::getMetadata() const |
| 1032 | #else |
| 1033 | char** Jim::getMetadata() const |
| 1034 | #endif |
| 1035 | { |
| 1036 | if(m_gds){ |
| 1037 | if(m_gds->GetMetadata()!=NULL) |
| 1038 | return(m_gds->GetMetadata()); |
| 1039 | else |
| 1040 | return(0); |
| 1041 | } |
| 1042 | else |
| 1043 | return(0); |
| 1044 | // return (char**)""; |
| 1045 | } |
| 1046 | |
| 1047 | /** |
| 1048 | * @return the metadata of this data set in standard template library (stl) string format |
| 1049 | **/ |
| 1050 | void Jim::getMetadata(std::list<std::string>& metadata) const |
| 1051 | { |
| 1052 | if(m_gds){ |
| 1053 | #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 13, 0) |
| 1054 | CSLConstList cmetadata=m_gds->GetMetadata(); |
| 1055 | #else |
| 1056 | char** cmetadata=m_gds->GetMetadata(); |
| 1057 | #endif |
| 1058 | while(*cmetadata!=NULL){ |
| 1059 | metadata.push_back(*(cmetadata)); |
| 1060 | ++cmetadata; |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | /** |
| 1066 | * @return the description of this data set in string format |
| 1067 | **/ |
| 1068 | std::string Jim::getDescription() const |
| 1069 | { |
| 1070 | if(m_gds){ |
| 1071 | if(m_gds->GetDriver()->GetDescription()!=NULL) |
| 1072 | return m_gds->GetDriver()->GetDescription(); |
| 1073 | else |
| 1074 | return(std::string()); |
| 1075 | } |
| 1076 | else |
| 1077 | return(std::string()); |
| 1078 | } |
| 1079 | |
| 1080 | /** |
| 1081 | * @return the meta data item of this data set in string format |
| 1082 | **/ |
| 1083 | std::string Jim::getMetadataItem() const |
| 1084 | { |
| 1085 | if(m_gds){ |
| 1086 | if(m_gds->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME )!=NULL) |
| 1087 | return m_gds->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ); |
| 1088 | return(std::string()); |
| 1089 | } |
| 1090 | else |
| 1091 | return(std::string()); |
| 1092 | } |
| 1093 | |
| 1094 | /** |
| 1095 | * @return the image description (TIFFTAG) of this data set in string format |
| 1096 | **/ |
| 1097 | std::string Jim::getImageDescription() const |
| 1098 | { |
| 1099 | if(m_gds){ |
| 1100 | if(m_gds->GetDriver()->GetMetadataItem("TIFFTAG_IMAGEDESCRIPTION")!=NULL) |
| 1101 | return m_gds->GetDriver()->GetMetadataItem("TIFFTAG_IMAGEDESCRIPTION"); |
| 1102 | return(std::string()); |
| 1103 | } |
| 1104 | else |
| 1105 | return(std::string()); |
| 1106 | } |
| 1107 | |
| 1108 | /** |
| 1109 | * @return the band coding interleave of this data set in string format |
| 1110 | **/ |
| 1111 | std::string Jim::getInterleave() const |
| 1112 | { |
| 1113 | if(m_gds){ |
| 1114 | if(m_gds->GetMetadataItem( "INTERLEAVE", "IMAGE_STRUCTURE")) |
| 1115 | return m_gds->GetMetadataItem( "INTERLEAVE", "IMAGE_STRUCTURE"); |
| 1116 | else |
| 1117 | return("BAND"); |
| 1118 | } |
| 1119 | else |
| 1120 | return(std::string()); |
| 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * @return the compression meta data of this data set in string format |
| 1125 | **/ |
| 1126 | std::string Jim::getCompression() const |
| 1127 | { |
| 1128 | if(m_gds){ |
| 1129 | if(m_gds->GetMetadataItem( "COMPRESSION", "IMAGE_STRUCTURE")) |
| 1130 | return m_gds->GetMetadataItem( "COMPRESSION", "IMAGE_STRUCTURE"); |
| 1131 | return("NONE"); |
| 1132 | } |
| 1133 | else |
| 1134 | return("NONE"); |
| 1135 | } |
| 1136 | |
| 1137 | /** |
| 1138 | * assuming |
| 1139 | * adfGeotransform[0]: ULX (upper left X coordinate) |
| 1140 | * adfGeotransform[1]: $cos(\alpha)\cdot\textrm{Xres}$ |
| 1141 | * adfGeotransform[2]: $-sin(\alpha)\cdot\textrm{Xres}$ |
| 1142 | * adfGeotransform[3]: ULY (upper left Y coordinate) |
| 1143 | * adfGeotransform[4]: $-sin(\alpha)\cdot\textrm{Yres}$ |
| 1144 | * adfGeotransform[5]: $-cos(\alpha)\cdot\textrm{Yres}$ |
| 1145 | |
| 1146 | * @param ulx upper left coordinate in x |
| 1147 | * @param uly upper left coordinate in y |
| 1148 | * @param lrx lower left coordinate in x |
| 1149 | * @param lry lower left coordinate in y |
| 1150 | * @return true if successful and if image is georeferenced |
| 1151 | **/ |
| 1152 | void Jim::getBoundingBox(double& ulx, double& uly, double& lrx, double& lry, OGRCoordinateTransformation *poCT) const |
| 1153 | { |
| 1154 | try{ |
| 1155 | vector<double> gt(6); |
| 1156 | getGeoTransform(gt); |
| 1157 | |
| 1158 | ulx=gt[0]; |
| 1159 | uly=gt[3]; |
| 1160 | lrx=gt[0]+nrOfCol()*gt[1]+nrOfRow()*gt[2]; |
| 1161 | lry=gt[3]+nrOfCol()*gt[4]+nrOfRow()*gt[5]; |
| 1162 | if(poCT){ |
| 1163 | std::vector<double> xvector(4);//ulx,urx,llx,lrx |
| 1164 | std::vector<double> yvector(4);//uly,ury,lly,lry |
| 1165 | xvector[0]=ulx; |
| 1166 | xvector[1]=lrx; |
| 1167 | xvector[2]=lrx; |
| 1168 | xvector[3]=ulx; |
| 1169 | yvector[0]=uly; |
| 1170 | yvector[1]=uly; |
| 1171 | yvector[2]=lry; |
| 1172 | yvector[3]=lry; |
| 1173 | if(!poCT->Transform(xvector.size(),&xvector[0],&yvector[0])){ |
| 1174 | std::ostringstream errorStream; |
| 1175 | errorStream << "Error: cannot apply OGRCoordinateTransformation in Jim::getBoundingBox" << std::endl; |
| 1176 | throw(errorStream.str()); |
| 1177 | } |
| 1178 | ulx=std::min(xvector[0],xvector[3]); |
| 1179 | lrx=std::max(xvector[1],xvector[2]); |
| 1180 | uly=std::max(yvector[0],yvector[1]); |
| 1181 | lry=std::min(yvector[2],yvector[3]); |
| 1182 | } |
| 1183 | } |
| 1184 | catch(std::string errorString){ |
| 1185 | std::cerr << errorString << std::endl; |
| 1186 | throw; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | /** |
| 1191 | * assuming |
| 1192 | * adfGeotransform[0]: ULX (upper left X coordinate) |
| 1193 | * adfGeotransform[1]: $cos(\alpha)\cdot\textrm{Xres}$ |
| 1194 | * adfGeotransform[2]: $-sin(\alpha)\cdot\textrm{Xres}$ |
| 1195 | * adfGeotransform[3]: ULY (upper left Y coordinate) |
| 1196 | * adfGeotransform[4]: $-sin(\alpha)\cdot\textrm{Yres}$ |
| 1197 | * adfGeotransform[5]: $-cos(\alpha)\cdot\textrm{Yres}$ |
| 1198 | |
| 1199 | * @param bbvector[0] upper left coordinate in x |
| 1200 | * @param bbvector[1] upper left coordinate in y |
| 1201 | * @param bbvector[2] lower left coordinate in x |
| 1202 | * @param bbvector[3] lower left coordinate in y |
| 1203 | * @return true if successful and if image is georeferenced |
| 1204 | **/ |
| 1205 | void Jim::getBoundingBox(std::vector<double> &bbvector, OGRCoordinateTransformation *poCT) const |
| 1206 | { |
| 1207 | bbvector.resize(4); |
| 1208 | getBoundingBox(bbvector[0],bbvector[1],bbvector[2],bbvector[3],poCT); |
| 1209 | } |
| 1210 | |
| 1211 | void Jim::getBoundingBox(OGRPolygon *bbPolygon, OGRCoordinateTransformation *poCT) const{ |
| 1212 | OGRLinearRing bbRing; |
| 1213 | std::vector<double> bbvector; |
| 1214 | getBoundingBox(bbvector,poCT); |
| 1215 | OGRPoint ul; |
| 1216 | OGRPoint ur; |
| 1217 | OGRPoint lr; |
| 1218 | OGRPoint ll; |
| 1219 | ul.setX(bbvector[0]); |
| 1220 | ul.setY(bbvector[1]); |
| 1221 | ur.setX(bbvector[2]); |
| 1222 | ur.setY(bbvector[1]); |
| 1223 | lr.setX(bbvector[2]); |
| 1224 | lr.setY(bbvector[3]); |
| 1225 | ll.setX(bbvector[0]); |
| 1226 | ll.setY(bbvector[3]); |
| 1227 | bbRing.addPoint(&ul); |
| 1228 | bbRing.addPoint(&ur); |
| 1229 | bbRing.addPoint(&lr); |
| 1230 | bbRing.addPoint(&ll); |
| 1231 | bbRing.addPoint(&ul); |
| 1232 | bbPolygon->addRing(&bbRing); |
| 1233 | bbPolygon->closeRings(); |
| 1234 | } |
| 1235 | |
| 1236 | // ///get bounding box with coordinate transform based on EPSG code |
| 1237 | // bool Jim::getBoundingBox(std::vector<double> &bbvector, int targetEPSG) const |
| 1238 | // { |
| 1239 | // bbvector.resize(4); |
| 1240 | // OGRSpatialReference targetSRS; |
| 1241 | // if( targetSRS.importFromEPSG(targetEPSG) != OGRERR_NONE ){ |
| 1242 | // std::ostringstream errorStream; |
| 1243 | // errorStream << "Error: cannot import SRS from EPSG code: " << targetEPSG << std::endl; |
| 1244 | // throw(errorStream.str()); |
| 1245 | // } |
| 1246 | // return getBoundingBox(bbvector,&targetSRS); |
| 1247 | // } |
| 1248 | |
| 1249 | /** |
| 1250 | * assuming |
| 1251 | * adfGeotransform[0]: ULX (upper left X coordinate) |
| 1252 | * adfGeotransform[1]: $cos(\alpha)\cdot\textrm{Xres}$ |
| 1253 | * adfGeotransform[2]: $-sin(\alpha)\cdot\textrm{Xres}$ |
| 1254 | * adfGeotransform[3]: ULY (upper left Y coordinate) |
| 1255 | * adfGeotransform[4]: $-sin(\alpha)\cdot\textrm{Yres}$ |
| 1256 | * adfGeotransform[5]: $-cos(\alpha)\cdot\textrm{Yres}$ |
| 1257 | * @param x, y centre coordinates in x and y |
| 1258 | * @return true if image is georeferenced |
| 1259 | **/ |
| 1260 | void Jim::getCenterPos(double& centerX, double& centerY) const |
| 1261 | { |
| 1262 | vector<double> gt(6); |
| 1263 | getGeoTransform(gt); |
| 1264 | |
| 1265 | centerX=gt[0]+(nrOfCol()/2.0)*gt[1]+(nrOfRow()/2.0)*gt[2]; |
| 1266 | centerY=gt[3]+(nrOfCol()/2.0)*gt[4]+(nrOfRow()/2.0)*gt[5]; |
| 1267 | // if(isGeoRef()) |
| 1268 | // return(CE_None); |
| 1269 | // else |
| 1270 | // return(CE_Warning); |
| 1271 | } |
| 1272 | |
| 1273 | /** |
| 1274 | * assuming |
| 1275 | * adfGeotransform[0]: ULX (upper left X coordinate) |
| 1276 | * adfGeotransform[1]: $cos(\alpha)\cdot\textrm{Xres}$ |
| 1277 | * adfGeotransform[2]: $-sin(\alpha)\cdot\textrm{Xres}$ |
| 1278 | * adfGeotransform[3]: ULY (upper left Y coordinate) |
| 1279 | * adfGeotransform[4]: $-sin(\alpha)\cdot\textrm{Yres}$ |
| 1280 | * adfGeotransform[5]: $-cos(\alpha)\cdot\textrm{Yres}$ |
| 1281 | * @param x,y georeferenced coordinates in x and y |
| 1282 | * @param i,j image coordinates (can be fraction of pixels) |
| 1283 | * @return true if image is georeferenced |
| 1284 | **/ |
| 1285 | bool Jim::geo2image(double x, double y, double& i, double& j, OGRCoordinateTransformation *poCT) const |
| 1286 | { |
| 1287 | vector<double> gt(6); |
| 1288 | getGeoTransform(gt); |
| 1289 | |
| 1290 | double thisX=x; |
| 1291 | double thisY=y; |
| 1292 | try{ |
| 1293 | if(poCT){ |
| 1294 | if(!poCT->Transform(1,&thisX,&thisY)){ |
| 1295 | std::ostringstream errorStream; |
| 1296 | errorStream << "Error: cannot apply OGRCoordinateTransformation in Jim::geo2image" << std::endl; |
| 1297 | throw(errorStream.str()); |
| 1298 | } |
| 1299 | } |
| 1300 | double denom=(gt[1]-gt[2]*gt[4]/gt[5]); |
| 1301 | double eps=0.00001; |
| 1302 | if(fabs(denom)>eps){ |
| 1303 | i=(thisX-gt[0]-gt[2]/gt[5]*(thisY-gt[3]))/denom; |
| 1304 | j=(thisY-gt[3]-gt[4]*(thisX-gt[0]-gt[2]/gt[5]*(thisY-gt[3]))/denom)/gt[5]; |
| 1305 | } |
| 1306 | if(isGeoRef()) |
| 1307 | return true; |
| 1308 | else |
| 1309 | return false; |
| 1310 | } |
| 1311 | catch(std::string errorstring){ |
| 1312 | std::cerr << errorstring << std::endl; |
| 1313 | throw; |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | /** |
| 1318 | * assuming |
| 1319 | * adfGeotransform[0]: ULX (upper left X coordinate) |
| 1320 | * adfGeotransform[1]: $cos(\alpha)\cdot\textrm{Xres}$ |
| 1321 | * adfGeotransform[2]: $-sin(\alpha)\cdot\textrm{Xres}$ |
| 1322 | * adfGeotransform[3]: ULY (upper left Y coordinate) |
| 1323 | * adfGeotransform[4]: $-sin(\alpha)\cdot\textrm{Yres}$ |
| 1324 | * adfGeotransform[5]: $-cos(\alpha)\cdot\textrm{Yres}$ |
| 1325 | * @param i,j image coordinates (can be fraction of pixels) |
| 1326 | * @param x,y georeferenced coordinates in x and y (can be fraction of pixels) |
| 1327 | * @return true if image is georeferenced |
| 1328 | **/ |
| 1329 | bool Jim::image2geo(double i, double j, double& x, double& y, OGRCoordinateTransformation *poCT) const |
| 1330 | { |
| 1331 | try{ |
| 1332 | vector<double> gt(6); |
| 1333 | getGeoTransform(gt); |
| 1334 | |
| 1335 | x=gt[0]+(0.5+i)*gt[1]+(0.5+j)*gt[2]; |
| 1336 | y=gt[3]+(0.5+i)*gt[4]+(0.5+j)*gt[5]; |
| 1337 | if(poCT){ |
| 1338 | if(!poCT->Transform(1,&x,&y)){ |
| 1339 | std::ostringstream errorStream; |
| 1340 | errorStream << "Error: cannot apply OGRCoordinateTransformation in Jim::image2geo" << std::endl; |
| 1341 | throw(errorStream.str()); |
| 1342 | } |
| 1343 | } |
| 1344 | if(isGeoRef()) |
| 1345 | return true; |
| 1346 | else |
| 1347 | return false; |
| 1348 | } |
| 1349 | catch(std::string errorstring){ |
| 1350 | std::cerr << errorstring << std::endl; |
| 1351 | throw; |
| 1352 | } |
| 1353 | |
| 1354 | } |
| 1355 | |
| 1356 | /** |
| 1357 | * assuming |
| 1358 | * adfGeotransform[0]: ULX (upper left X coordinate) |
| 1359 | * adfGeotransform[1]: $cos(\alpha)\cdot\textrm{Xres}$ |
| 1360 | * adfGeotransform[2]: $-sin(\alpha)\cdot\textrm{Xres}$ |
| 1361 | * adfGeotransform[3]: ULY (upper left Y coordinate) |
| 1362 | * adfGeotransform[4]: $-sin(\alpha)\cdot\textrm{Yres}$ |
| 1363 | * adfGeotransform[5]: $-cos(\alpha)\cdot\textrm{Yres}$ |
| 1364 | * @param x,y georeferenced coordinates in x and y |
| 1365 | * @return true if image covers the georeferenced location |
| 1366 | **/ |
| 1367 | bool Jim::covers(double x, double y, OGRCoordinateTransformation *poCT) const |
| 1368 | { |
| 1369 | double theULX, theULY, theLRX, theLRY; |
| 1370 | getBoundingBox(theULX,theULY,theLRX,theLRY); |
| 1371 | double ximg=x; |
| 1372 | double yimg=y; |
| 1373 | if(poCT){ |
| 1374 | if(!poCT->Transform(1,&ximg,&yimg)){ |
| 1375 | std::ostringstream errorStream; |
| 1376 | errorStream << "Error: cannot apply OGRCoordinateTransformation in Jim::covers (1)" << std::endl; |
| 1377 | throw(errorStream.str()); |
| 1378 | } |
| 1379 | } |
| 1380 | return((ximg > theULX)&& |
| 1381 | (ximg < theLRX)&& |
| 1382 | (yimg < theULY)&& |
| 1383 | (yimg >theLRY)); |
| 1384 | } |
| 1385 | |
| 1386 | /** |
| 1387 | * assuming |
| 1388 | * adfGeotransform[0]: ULX (upper left X coordinate) |
| 1389 | * adfGeotransform[1]: $cos(\alpha)\cdot\textrm{Xres}$ |
| 1390 | * adfGeotransform[2]: $-sin(\alpha)\cdot\textrm{Xres}$ |
| 1391 | * adfGeotransform[3]: ULY (upper left Y coordinate) |
| 1392 | * adfGeotransform[4]: $-sin(\alpha)\cdot\textrm{Yres}$ |
| 1393 | * adfGeotransform[5]: $-cos(\alpha)\cdot\textrm{Yres}$ |
| 1394 | * @param ulx upper left coordinate in x |
| 1395 | * @param uly upper left coordinate in y |
| 1396 | * @param lrx lower left coordinate in x |
| 1397 | * @param lry lower left coordinate in y |
| 1398 | * @return true if image (partially or all if all is set) covers the bounding box |
| 1399 | **/ |
| 1400 | bool Jim::covers(double ulx, double uly, double lrx, double lry, std::string coverType, OGRCoordinateTransformation *poCT) const |
| 1401 | { |
| 1402 | //initialize coverMap |
| 1403 | std::map<std::string, cover::COVER_TYPE> coverMap; |
| 1404 | coverMap["ALL_TOUCHED"]=cover::ALL_TOUCHED; |
| 1405 | coverMap["ALL_COVERED"]=cover::ALL_COVERED; |
| 1406 | coverMap["ALL_CENTER"]=cover::ALL_CENTER; |
| 1407 | |
| 1408 | double theULX, theULY, theLRX, theLRY; |
| 1409 | getBoundingBox(theULX,theULY,theLRX,theLRY); |
| 1410 | double ulximg=ulx; |
| 1411 | double ulyimg=uly; |
| 1412 | double lrximg=lrx; |
| 1413 | double lryimg=lry; |
| 1414 | if(poCT){ |
| 1415 | std::vector<double> xvector(4);//ulx,urx,llx,lrx |
| 1416 | std::vector<double> yvector(4);//uly,ury,lly,lry |
| 1417 | xvector[0]=ulximg; |
| 1418 | xvector[1]=lrximg; |
| 1419 | xvector[2]=ulximg; |
| 1420 | xvector[3]=lrximg; |
| 1421 | yvector[0]=ulyimg; |
| 1422 | yvector[1]=ulyimg; |
| 1423 | yvector[2]=lryimg; |
| 1424 | yvector[3]=lryimg; |
| 1425 | if(!poCT->Transform(xvector.size(),&xvector[0],&yvector[0])){ |
| 1426 | std::ostringstream errorStream; |
| 1427 | errorStream << "Error: cannot apply OGRCoordinateTransformation in Jim::covers (2)" << std::endl; |
| 1428 | throw(errorStream.str()); |
| 1429 | } |
| 1430 | ulximg=std::min(xvector[0],xvector[2]); |
| 1431 | lrximg=std::max(xvector[1],xvector[3]); |
| 1432 | ulyimg=std::max(yvector[0],yvector[1]); |
| 1433 | lryimg=std::min(yvector[2],yvector[3]); |
| 1434 | } |
| 1435 | |
| 1436 | switch(coverMap[coverType]){ |
| 1437 | case(cover::ALL_COVERED): |
| 1438 | return((theULX<ulximg)&&(theULY>ulyimg)&&(theLRX>lrximg)&&(theLRY<lryimg)); |
| 1439 | // case(cover::ALL_CENTER): |
| 1440 | // break; |
| 1441 | case(cover::ALL_TOUCHED): |
| 1442 | default: |
| 1443 | return((ulximg < theLRX)&&(lrximg > theULX)&&(lryimg < theULY)&&(ulyimg > theLRY)); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | /** |
| 1448 | * @param noDataValues standard template library (stl) vector containing no data values |
| 1449 | * @return number of no data values in this dataset |
| 1450 | **/ |
| 1451 | void Jim::getNoDataValues(std::vector<double>& noDataValues) const |
| 1452 | { |
| 1453 | if(m_noDataValues.size()){ |
| 1454 | noDataValues=m_noDataValues; |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | /** |
| 1459 | * @param noDataValue no data value to be pushed for this dataset |
| 1460 | * @return CE_None if successful |
| 1461 | **/ |
| 1462 | CPLErr Jim::pushNoDataValue(double noDataValue) |
| 1463 | { |
| 1464 | if(find(m_noDataValues.begin(),m_noDataValues.end(),noDataValue)==m_noDataValues.end()) |
| 1465 | m_noDataValues.push_back(noDataValue); |
| 1466 | if(m_noDataValues.size()) |
| 1467 | return(CE_None); |
| 1468 | else |
| 1469 | return(CE_Warning); |
| 1470 | } |
| 1471 | |
| 1472 | CPLErr Jim::open(const std::string& filename, bool readData, unsigned int memory){ |
| 1473 | // reset(); |
| 1474 | // m_nplane=1; |
| 1475 | m_access=READ_ONLY; |
| 1476 | m_filename = filename; |
| 1477 | registerDriver(); |
| 1478 | initMem(memory); |
| 1479 | for(int iband=0;iband<m_nband;++iband){ |
| 1480 | m_begin[iband]=0; |
| 1481 | m_end[iband]=0; |
| 1482 | } |
| 1483 | if(readData){ |
| 1484 | for(int iband=0;iband<nrOfBand();++iband) |
| 1485 | readDataDS(iband,iband); |
| 1486 | } |
| 1487 | return(CE_None); |
| 1488 | } |
| 1489 | |
| 1490 | CPLErr Jim::registerDriver() |
| 1491 | { |
| 1492 | //close if gdaldataset was already set |
| 1493 | if(m_gds) |
| 1494 | GDALClose(m_gds); |
| 1495 | m_gds=0; |
| 1496 | |
| 1497 | GDALAllRegister(); |
| 1498 | if(writeMode()){ |
| 1499 | GDALDriver *poDriver; |
| 1500 | poDriver = GetGDALDriverManager()->GetDriverByName(m_imageType.c_str()); |
| 1501 | if( poDriver == NULL ){ |
| 1502 | std::ostringstream s; |
| 1503 | s << "FileOpenError (" << m_imageType << ")"; |
| 1504 | throw(s.str()); |
| 1505 | } |
| 1506 | #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 13, 0) |
| 1507 | CSLConstList papszMetadata; |
| 1508 | #else |
| 1509 | char **papszMetadata; |
| 1510 | #endif |
| 1511 | papszMetadata = poDriver->GetMetadata(); |
| 1512 | //todo: try and catch if CREATE is not supported (as in PNG) |
| 1513 | if( ! CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE )){ |
| 1514 | std::ostringstream s; |
| 1515 | s << "Error: image type " << m_imageType << " not supported"; |
| 1516 | throw(s.str()); |
| 1517 | } |
| 1518 | //create options |
| 1519 | char **papszOptions=NULL; |
| 1520 | for(std::vector<std::string>::const_iterator optionIt=m_coptions.begin();optionIt!=m_coptions.end();++optionIt) |
| 1521 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 1522 | |
| 1523 | m_gds=poDriver->Create(m_filename.c_str(),nrOfCol(),nrOfRow(),nrOfBand(),getGDALDataType(),papszOptions); |
| 1524 | if(!m_gds){ |
| 1525 | std::ostringstream s; |
| 1526 | s << "Error: could not create driver for " << m_filename; |
| 1527 | throw(s.str()); |
| 1528 | } |
| 1529 | // m_gds=poDriver->Create(m_filename.c_str(),m_ncol,m_nrow,m_nband,m_dataType,papszOptions); |
| 1530 | vector<double> gt(6); |
| 1531 | getGeoTransform(gt); |
| 1532 | if(setGeoTransform(gt)!=CE_None) |
| 1533 | std::cerr << "Warning: could not write geotransform information in " << m_filename << std::endl; |
| 1534 | if(m_projection.size()){ |
| 1535 | setProjection(m_projection); |
| 1536 | } |
| 1537 | |
| 1538 | if(m_noDataValues.size()){ |
| 1539 | for(int iband=0;iband<nrOfBand();++iband) |
| 1540 | GDALSetNoDataValue(m_noDataValues[0],iband); |
| 1541 | } |
| 1542 | |
| 1543 | // m_gds->SetMetadataItem( "TIFFTAG_DOCUMENTNAME", m_filename.c_str()); |
| 1544 | // std::string versionString="jiplib "; |
| 1545 | // versionString+=JIPLIB_VERSION; |
| 1546 | // versionString+=" by Pieter Kempeneers"; |
| 1547 | // m_gds->SetMetadataItem( "TIFFTAG_SOFTWARE", versionString.c_str()); |
| 1548 | time_t rawtime; |
| 1549 | time ( &rawtime ); |
| 1550 | |
| 1551 | time_t tim=time(NULL); |
| 1552 | tm *now=localtime(&tim); |
| 1553 | std::ostringstream datestream; |
| 1554 | //date std::string must be 20 characters long... |
| 1555 | datestream << now->tm_year+1900; |
| 1556 | if(now->tm_mon+1<10) |
| 1557 | datestream << ":0" << now->tm_mon+1; |
| 1558 | else |
| 1559 | datestream << ":" << now->tm_mon+1; |
| 1560 | if(now->tm_mday<10) |
| 1561 | datestream << ":0" << now->tm_mday; |
| 1562 | else |
| 1563 | datestream << ":" << now->tm_mday; |
| 1564 | if(now->tm_hour<10) |
| 1565 | datestream << " 0" << now->tm_hour; |
| 1566 | else |
| 1567 | datestream << " " << now->tm_hour; |
| 1568 | if(now->tm_min<10) |
| 1569 | datestream << ":0" << now->tm_min; |
| 1570 | else |
| 1571 | datestream << ":" << now->tm_min; |
| 1572 | if(now->tm_sec<10) |
| 1573 | datestream << ":0" << now->tm_sec; |
| 1574 | else |
| 1575 | datestream << ":" << now->tm_sec; |
| 1576 | m_gds->SetMetadataItem( "TIFFTAG_DATETIME", datestream.str().c_str()); |
| 1577 | } |
| 1578 | else{ |
| 1579 | #if GDAL_VERSION_MAJOR < 2 |
| 1580 | if(m_access==UPDATE) |
| 1581 | m_gds = (GDALDataset *) GDALOpen(m_filename.c_str(), GA_Update); |
| 1582 | else |
| 1583 | m_gds = (GDALDataset *) GDALOpen(m_filename.c_str(), GA_ReadOnly ); |
| 1584 | #else |
| 1585 | //open options |
| 1586 | char **papszOptions=NULL; |
| 1587 | for(std::vector<std::string>::const_iterator optionIt=m_ooptions.begin();optionIt!=m_ooptions.end();++optionIt) |
| 1588 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 1589 | papszOptions=CSLAddString(papszOptions,NULL); |
| 1590 | if(m_access==UPDATE) |
| 1591 | m_gds = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_UPDATE|GDAL_OF_RASTER, NULL, papszOptions, NULL); |
| 1592 | else{ |
| 1593 | m_gds = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_READONLY|GDAL_OF_RASTER, NULL, papszOptions, NULL); |
| 1594 | } |
| 1595 | #endif |
| 1596 | |
| 1597 | if(m_gds == NULL){ |
| 1598 | std::string errorString="FileOpenError 0"; |
| 1599 | throw(errorString); |
| 1600 | } |
| 1601 | m_ncol=m_gds->GetRasterXSize(); |
| 1602 | m_nrow=m_gds->GetRasterYSize(); |
| 1603 | m_nband=m_gds->GetRasterCount(); |
| 1604 | m_dataType=getDataTypeDS(); |
| 1605 | m_imageType=getImageType(); |
| 1606 | vector<double> adfGeoTransform(6); |
| 1607 | m_gds->GetGeoTransform( &adfGeoTransform[0] ); |
| 1608 | m_gt[0]=adfGeoTransform[0]; |
| 1609 | m_gt[1]=adfGeoTransform[1]; |
| 1610 | m_gt[2]=adfGeoTransform[2]; |
| 1611 | m_gt[3]=adfGeoTransform[3]; |
| 1612 | m_gt[4]=adfGeoTransform[4]; |
| 1613 | m_gt[5]=adfGeoTransform[5]; |
| 1614 | m_projection=m_gds->GetProjectionRef(); |
| 1615 | } |
| 1616 | return(CE_None); |
| 1617 | } |
| 1618 | |
| 1619 | //Open raster dataset for reading or writing |
| 1620 | CPLErr Jim::open(app::AppFactory &app){ |
| 1621 | //input |
| 1622 | Optionjl<std::string> input_opt("fn", "filename", "filename"); |
| 1623 | 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 https://gdal.org/doxygen/gdal_8h.html#a640ada511cbddeefac67c548e009d5ac)","GRIORA_NearestNeighbour"); |
| 1624 | // Optionjl<std::string> extra_opt("extra", "extra", "RGDALRasterIOExtraArg (check http://www.gdal.org/structGDALRasterIOExtraArg.html)"); |
| 1625 | // Optionjl<std::string> targetSRS_opt("t_srs", "t_srs", "Target spatial reference system in EPSG format (e.g., epsg:3035)");//todo |
| 1626 | //output |
| 1627 | Optionjl<double> nodata_opt("nodata", "nodata", "Nodata value to put in image."); |
| 1628 | Optionjl<int> nsample_opt("ns", "ncol", "Number of columns"); |
| 1629 | Optionjl<int> nline_opt("nl", "nrow", "Number of rows"); |
| 1630 | Optionjl<int> nband_opt("nb", "nband", "Number of bands",1); |
| 1631 | Optionjl<int> nplane_opt("nplane", "nplane", "Number of planes",1); |
| 1632 | Optionjl<std::string> otype_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64/Int64/UInt64})","Byte"); |
| 1633 | Optionjl<std::string> oformat_opt("of", "oformat", "Output image format (see also gdal_translate).","GTiff"); |
| 1634 | Optionjl<std::string> coption_opt("co", "co", "Creation option(s) for output file. Multiple options can be specified."); |
| 1635 | Optionjl<std::string> ooption_opt("oo", "oo", "Open option(s) for output file. Multiple options can be specified."); |
| 1636 | Optionjl<unsigned long int> seed_opt("seed", "seed", "seed value for random generator",0); |
| 1637 | Optionjl<double> mean_opt("mean", "mean", "Mean value for random generator",0); |
| 1638 | Optionjl<double> stdev_opt("stdev", "stdev", "Standard deviation for Gaussian random generator",0); |
| 1639 | Optionjl<double> uniform_opt("uniform", "uniform", "start and end values for random value with uniform distribution",0); |
| 1640 | Optionjl<std::string> assignSRS_opt("a_srs", "a_srs", "Assign the spatial reference for the output file, e.g., epsg:3035 to use European projection and force to European grid"); |
| 1641 | 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"); |
| 1642 | 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"); |
| 1643 | // Optionjl<std::string> description_opt("d", "description", "Set image description"); |
| 1644 | //input and output |
| 1645 | Optionjl<int> band_opt("b", "band", "Bands to open, index starts from 0"); |
| 1646 | Optionjl<std::string> extent_opt("e", "extent", "get boundary from extent from polygons in vector file"); |
| 1647 | Optionjl<double> ulx_opt("ulx", "ulx", "Upper left x value bounding box"); |
| 1648 | Optionjl<double> uly_opt("uly", "uly", "Upper left y value bounding box"); |
| 1649 | Optionjl<double> lrx_opt("lrx", "lrx", "Lower right x value bounding box"); |
| 1650 | Optionjl<double> lry_opt("lry", "lry", "Lower right y value bounding box"); |
| 1651 | Optionjl<double> dx_opt("dx", "dx", "Resolution in x"); |
| 1652 | Optionjl<double> dy_opt("dy", "dy", "Resolution in y"); |
| 1653 | Optionjl<bool> align_opt("align", "align", "Align output bounding box to input image",false); |
| 1654 | Optionjl<std::string> access_opt("access", "access", "access (READ_ONLY, UPDATE)","READ_ONLY",2); |
| 1655 | Optionjl<bool> noread_opt("noread", "noread", "do not read data when opening",false); |
| 1656 | Optionjl<bool> band2plane_opt("band2plane", "band2plane", "read bands as planes",false); |
| 1657 | Optionjl<unsigned long int> memory_opt("mem", "mem", "Buffer size (in MB) to read image data blocks in memory",0,1); |
| 1658 | Optionjl<bool> nogeo_opt("nogeo", "nogeo", "use image coordinates instead of spatial reference system",false); |
| 1659 | Optionjl<short> verbose_opt("verbose", "verbose", "verbose output",0,2); |
| 1660 | |
| 1661 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 1662 | try{ |
| 1663 | doProcess=input_opt.retrieveOption(app); |
| 1664 | nodata_opt.retrieveOption(app); |
| 1665 | band_opt.retrieveOption(app); |
| 1666 | ulx_opt.retrieveOption(app); |
| 1667 | uly_opt.retrieveOption(app); |
| 1668 | lrx_opt.retrieveOption(app); |
| 1669 | lry_opt.retrieveOption(app); |
| 1670 | dx_opt.retrieveOption(app); |
| 1671 | dy_opt.retrieveOption(app); |
| 1672 | align_opt.retrieveOption(app); |
| 1673 | resample_opt.retrieveOption(app); |
| 1674 | extent_opt.retrieveOption(app); |
| 1675 | // extra_opt.retrieveOption(app); |
| 1676 | // targetSRS_opt.retrieveOption(app); |
| 1677 | nsample_opt.retrieveOption(app); |
| 1678 | nline_opt.retrieveOption(app); |
| 1679 | nband_opt.retrieveOption(app); |
| 1680 | nplane_opt.retrieveOption(app); |
| 1681 | otype_opt.retrieveOption(app); |
| 1682 | oformat_opt.retrieveOption(app); |
| 1683 | coption_opt.retrieveOption(app); |
| 1684 | ooption_opt.retrieveOption(app); |
| 1685 | seed_opt.retrieveOption(app); |
| 1686 | mean_opt.retrieveOption(app); |
| 1687 | stdev_opt.retrieveOption(app); |
| 1688 | uniform_opt.retrieveOption(app); |
| 1689 | assignSRS_opt.retrieveOption(app); |
| 1690 | sourceSRS_opt.retrieveOption(app); |
| 1691 | targetSRS_opt.retrieveOption(app); |
| 1692 | access_opt.retrieveOption(app); |
| 1693 | noread_opt.retrieveOption(app); |
| 1694 | band2plane_opt.retrieveOption(app); |
| 1695 | memory_opt.retrieveOption(app); |
| 1696 | nogeo_opt.retrieveOption(app); |
| 1697 | verbose_opt.retrieveOption(app); |
| 1698 | } |
| 1699 | catch(std::string predefinedString){ |
| 1700 | std::cout << predefinedString << std::endl; |
| 1701 | } |
| 1702 | if(!doProcess){ |
| 1703 | std::cout << std::endl; |
| 1704 | std::ostringstream helpStream; |
| 1705 | helpStream << "exception thrown due to help info"; |
| 1706 | throw(helpStream.str());//help was invoked, stop processing |
| 1707 | } |
| 1708 | |
| 1709 | // std::vector<std::string> badKeys; |
| 1710 | // app.badKeys(badKeys); |
| 1711 | // if(badKeys.size()){ |
| 1712 | // std::ostringstream errorStream; |
| 1713 | // if(badKeys.size()>1) |
| 1714 | // errorStream << "Error: unknown keys: "; |
| 1715 | // else |
| 1716 | // errorStream << "Error: unknown key: "; |
| 1717 | // for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 1718 | // errorStream << badKeys[ikey] << " "; |
| 1719 | // } |
| 1720 | // errorStream << std::endl; |
| 1721 | // throw(errorStream.str()); |
| 1722 | // } |
| 1723 | statfactory::StatFactory stat; |
| 1724 | |
| 1725 | OGRCoordinateTransformation *target2gds=0; |
| 1726 | OGRCoordinateTransformation *gds2target=0; |
| 1727 | |
| 1728 | //get bounding box from extentReader if defined |
| 1729 | VectorOgr extentReader; |
| 1730 | |
| 1731 | if(input_opt.empty()){ |
| 1732 | OGRSpatialReference assignSpatialRef; |
| 1733 | if(assignSRS_opt.size()){ |
| 1734 | assignSpatialRef.SetFromUserInput(assignSRS_opt[0].c_str()); |
| 1735 | #if GDAL_VERSION_MAJOR > 2 |
| 1736 | assignSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 1737 | #endif |
| 1738 | } |
| 1739 | //get bounding box from extentReader if defined |
| 1740 | if(extent_opt.size()){ |
| 1741 | double e_ulx; |
| 1742 | double e_uly; |
| 1743 | double e_lrx; |
| 1744 | double e_lry; |
| 1745 | for(int iextent=0;iextent<extent_opt.size();++iextent){ |
| 1746 | std::vector<std::string> layernames; |
| 1747 | layernames.clear(); |
| 1748 | extentReader.open(extent_opt[iextent],layernames,true); |
| 1749 | |
| 1750 | // OGRSpatialReference *vectorSpatialRef=extentReader.getLayer(0)->GetSpatialRef(); |
| 1751 | OGRSpatialReference vectorSpatialRef=extentReader.getSpatialRef(); |
| 1752 | #if GDAL_VERSION_MAJOR > 2 |
| 1753 | vectorSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 1754 | #endif |
| 1755 | OGRCoordinateTransformation *vector2assign=0; |
| 1756 | vector2assign = OGRCreateCoordinateTransformation(&vectorSpatialRef, &assignSpatialRef); |
| 1757 | if(assignSpatialRef.IsSame(&vectorSpatialRef)){ |
| 1758 | vector2assign=0; |
| 1759 | } |
| 1760 | else{ |
| 1761 | if(!vector2assign){ |
| 1762 | std::ostringstream errorStream; |
| 1763 | errorStream << "Error: cannot create OGRCoordinateTransformation vector to assignSRS" << std::endl; |
| 1764 | throw(errorStream.str()); |
| 1765 | } |
| 1766 | } |
| 1767 | extentReader.getExtent(e_ulx,e_uly,e_lrx,e_lry,vector2assign); |
| 1768 | ulx_opt.push_back(e_ulx); |
| 1769 | uly_opt.push_back(e_uly); |
| 1770 | lrx_opt.push_back(e_lrx); |
| 1771 | lry_opt.push_back(e_lry); |
| 1772 | extentReader.close(); |
| 1773 | } |
| 1774 | e_ulx=stat.mymin(ulx_opt); |
| 1775 | e_uly=stat.mymax(uly_opt); |
| 1776 | e_lrx=stat.mymax(lrx_opt); |
| 1777 | e_lry=stat.mymin(lry_opt); |
| 1778 | ulx_opt.clear(); |
| 1779 | uly_opt.clear(); |
| 1780 | lrx_opt.clear(); |
| 1781 | lrx_opt.clear(); |
| 1782 | ulx_opt.push_back(e_ulx); |
| 1783 | uly_opt.push_back(e_uly); |
| 1784 | lrx_opt.push_back(e_lrx); |
| 1785 | lry_opt.push_back(e_lry); |
| 1786 | } |
| 1787 | |
| 1788 | if(dx_opt.size()||dy_opt.size()){ |
| 1789 | if(dx_opt.empty()){ |
| 1790 | std::ostringstream errorStream; |
| 1791 | errorStream << "Warning: cell size in x not defined (use option --dx)." << std::endl; |
| 1792 | // Jim(); |
| 1793 | throw(errorStream.str()); |
| 1794 | } |
| 1795 | if(dy_opt.empty()){ |
| 1796 | std::ostringstream errorStream; |
| 1797 | errorStream << "Warning: cell size in y not defined (use option --dy)." << std::endl; |
| 1798 | // Jim(); |
| 1799 | throw(errorStream.str()); |
| 1800 | } |
| 1801 | if(ulx_opt.empty()||uly_opt.empty()||lrx_opt.empty()||lry_opt.empty()){ |
| 1802 | std::ostringstream errorStream; |
| 1803 | errorStream << "Warning: bounding box not defined (use options --ulx --uly --lrx --lry)." << std::endl; |
| 1804 | // Jim(); |
| 1805 | throw(errorStream.str()); |
| 1806 | } |
| 1807 | nsample_opt.clear(); |
| 1808 | nsample_opt.push_back((lrx_opt[0]-ulx_opt[0])/dx_opt[0]); |
| 1809 | nline_opt.clear(); |
| 1810 | nline_opt.push_back((uly_opt[0]-lry_opt[0])/dy_opt[0]); |
| 1811 | } |
| 1812 | else if(nsample_opt.size()||nline_opt.size()){ |
| 1813 | if(nsample_opt.empty()){ |
| 1814 | std::ostringstream errorStream; |
| 1815 | errorStream << "Warning: no number of columns (use option --ncol)." << std::endl; |
| 1816 | // Jim(); |
| 1817 | throw(errorStream.str()); |
| 1818 | } |
| 1819 | if(nline_opt.empty()){ |
| 1820 | std::ostringstream errorStream; |
| 1821 | errorStream << "Warning: no number of rows (use option --nrow)." << std::endl; |
| 1822 | // Jim(); |
| 1823 | throw(errorStream.str()); |
| 1824 | } |
| 1825 | } |
| 1826 | // GDALDataType theType=string2GDAL(otype_opt[0]); |
| 1827 | int theType=0; |
| 1828 | theType=string2GDAL(otype_opt[0]); |
| 1829 | if(theType==GDT_Unknown) |
| 1830 | theType=string2JDT(otype_opt[0]); |
| 1831 | // open(nsample_opt[0],nline_opt[0],nband_opt[0],theType); |
| 1832 | m_ncol = nsample_opt[0]; |
| 1833 | m_nrow = nline_opt[0]; |
| 1834 | m_nband = nband_opt[0]; |
| 1835 | m_nplane = nplane_opt[0]; |
| 1836 | m_dataType = theType; |
| 1837 | m_coptions=coption_opt; |
| 1838 | initMem(0); |
| 1839 | for(int iband=0;iband<m_nband;++iband){ |
| 1840 | m_begin[iband]=0; |
| 1841 | m_end[iband]=m_begin[iband]+m_blockSize; |
| 1842 | } |
| 1843 | if(m_filename!=""){ |
| 1844 | // m_writeMode=true; |
| 1845 | m_access=WRITE; |
| 1846 | registerDriver(); |
| 1847 | } |
| 1848 | if(ulx_opt.size()&&uly_opt.size()&&lrx_opt.size()&&lry_opt.size()){ |
| 1849 | double gt[6]; |
| 1850 | if(ulx_opt[0]<lrx_opt[0]) |
| 1851 | gt[0]=ulx_opt[0]; |
| 1852 | else |
| 1853 | gt[0]=0; |
| 1854 | if(dx_opt.size()) |
| 1855 | gt[1]=dx_opt[0]; |
| 1856 | else if(lrx_opt[0]-ulx_opt[0]>0){ |
| 1857 | gt[1]=lrx_opt[0]-ulx_opt[0]; |
| 1858 | gt[1]/=nrOfCol(); |
| 1859 | } |
| 1860 | else |
| 1861 | gt[1]=1; |
| 1862 | gt[2]=0; |
| 1863 | if(uly_opt[0]>lry_opt[0]) |
| 1864 | gt[3]=uly_opt[0]; |
| 1865 | else |
| 1866 | gt[3]=0; |
| 1867 | gt[4]=0; |
| 1868 | if(dy_opt.size()) |
| 1869 | gt[5]=-dy_opt[0]; |
| 1870 | else if(uly_opt[0]-lry_opt[0]>0){ |
| 1871 | gt[5]=lry_opt[0]-uly_opt[0]; |
| 1872 | gt[5]/=nrOfRow(); |
| 1873 | } |
| 1874 | else |
| 1875 | gt[5]=-1; |
| 1876 | setGeoTransform(gt); |
| 1877 | if(assignSRS_opt.size()) |
| 1878 | setProjectionProj4(assignSRS_opt[0]); |
| 1879 | } |
| 1880 | else{ |
| 1881 | double gt[6]; |
| 1882 | gt[0]=0; |
| 1883 | gt[1]=1; |
| 1884 | gt[2]=0; |
| 1885 | gt[3]=0; |
| 1886 | gt[4]=0; |
| 1887 | gt[5]=-1; |
| 1888 | setGeoTransform(gt); |
| 1889 | } |
| 1890 | gsl_rng* rndgen=stat.getRandomGenerator(seed_opt[0]); |
| 1891 | double value=mean_opt[0]; |
| 1892 | std::vector<double> lineBuffer(nrOfCol(),value); |
| 1893 | double a=0; |
| 1894 | double b=1; |
| 1895 | std::string distribution="none"; |
| 1896 | if(uniform_opt.size()>1){ |
| 1897 | distribution="uniform"; |
| 1898 | a=uniform_opt[0]; |
| 1899 | b=uniform_opt[1]; |
| 1900 | } |
| 1901 | else if(stdev_opt[0]>0){ |
| 1902 | distribution="gaussian"; |
| 1903 | a=mean_opt[0]; |
| 1904 | b=stdev_opt[0]; |
| 1905 | } |
| 1906 | else |
| 1907 | distribution="none"; |
| 1908 | |
| 1909 | if(stat.getDistributionType(distribution)==statfactory::StatFactory::none){ |
| 1910 | for(size_t iband=0;iband<nrOfBand();++iband){ |
| 1911 | if(getDataType() < GDT_TypeCount) |
| 1912 | setData(mean_opt[0],iband); |
| 1913 | } |
| 1914 | } |
| 1915 | else{ |
| 1916 | if(nrOfPlane()>1){ |
| 1917 | std::string errorString; |
| 1918 | std::cerr << "Error: operation not supported for multi-plane objects" << std::endl; |
| 1919 | throw(errorString); |
| 1920 | } |
| 1921 | for(unsigned int iband=0;iband<nrOfBand();++iband){ |
| 1922 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 1923 | #pragma omp parallel for |
| 1924 | #else |
| 1925 | #endif |
| 1926 | for(unsigned int irow=0;irow<nrOfRow();++irow){ |
| 1927 | std::vector<double> lineBuffer(nrOfCol(),mean_opt[0]); |
| 1928 | for(unsigned int icol=0;icol<nrOfCol();++icol){ |
| 1929 | if(stat.getDistributionType(distribution)==statfactory::StatFactory::none) |
| 1930 | break; |
| 1931 | else |
| 1932 | value=stat.getRandomValue(rndgen,distribution,a,b); |
| 1933 | lineBuffer[icol]=value; |
| 1934 | } |
| 1935 | writeData(lineBuffer,irow,iband); |
| 1936 | } |
| 1937 | } |
| 1938 | stat.freeRandomGenerator(rndgen); |
| 1939 | } |
| 1940 | } |
| 1941 | else if(input_opt.size()){ |
| 1942 | setAccess(access_opt[0]); |
| 1943 | m_filename=input_opt[0]; |
| 1944 | m_coptions=coption_opt; |
| 1945 | m_ooptions=ooption_opt; |
| 1946 | //set class member variables based on GDAL dataset |
| 1947 | registerDriver(); |
| 1948 | if(assignSRS_opt.size()) |
| 1949 | setProjectionProj4(assignSRS_opt[0]); |
| 1950 | if(sourceSRS_opt.size()) |
| 1951 | setProjectionProj4(sourceSRS_opt[0]); |
| 1952 | // OGRSpatialReference gdsSpatialRef(m_gds->GetProjectionRef()); |
| 1953 | OGRSpatialReference gdsSpatialRef=getSpatialRef(); |
| 1954 | #if GDAL_VERSION_MAJOR > 2 |
| 1955 | gdsSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 1956 | #endif |
| 1957 | if(extent_opt.size()){ |
| 1958 | double e_ulx; |
| 1959 | double e_uly; |
| 1960 | double e_lrx; |
| 1961 | double e_lry; |
| 1962 | for(int iextent=0;iextent<extent_opt.size();++iextent){ |
| 1963 | std::vector<std::string> layernames; |
| 1964 | layernames.clear(); |
| 1965 | extentReader.open(extent_opt[iextent],layernames,true); |
| 1966 | |
| 1967 | // OGRSpatialReference *vectorSpatialRef=extentReader.getLayer(0)->GetSpatialRef(); |
| 1968 | OGRSpatialReference vectorSpatialRef=extentReader.getSpatialRef(); |
| 1969 | #if GDAL_VERSION_MAJOR > 2 |
| 1970 | vectorSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 1971 | #endif |
| 1972 | OGRCoordinateTransformation *vector2raster=0; |
| 1973 | vector2raster = OGRCreateCoordinateTransformation(&vectorSpatialRef, &gdsSpatialRef); |
| 1974 | if(gdsSpatialRef.IsSame(&vectorSpatialRef)){ |
| 1975 | vector2raster=0; |
| 1976 | } |
| 1977 | else{ |
| 1978 | if(!vector2raster){ |
| 1979 | std::ostringstream errorStream; |
| 1980 | errorStream << "Error: cannot create OGRCoordinateTransformation vector to GDAL raster dataset" << std::endl; |
| 1981 | throw(errorStream.str()); |
| 1982 | } |
| 1983 | } |
| 1984 | extentReader.getExtent(e_ulx,e_uly,e_lrx,e_lry,vector2raster); |
| 1985 | ulx_opt.push_back(e_ulx); |
| 1986 | uly_opt.push_back(e_uly); |
| 1987 | lrx_opt.push_back(e_lrx); |
| 1988 | lry_opt.push_back(e_lry); |
| 1989 | extentReader.close(); |
| 1990 | } |
| 1991 | e_ulx=stat.mymin(ulx_opt); |
| 1992 | e_uly=stat.mymax(uly_opt); |
| 1993 | e_lrx=stat.mymax(lrx_opt); |
| 1994 | e_lry=stat.mymin(lry_opt); |
| 1995 | ulx_opt.clear(); |
| 1996 | uly_opt.clear(); |
| 1997 | lrx_opt.clear(); |
| 1998 | lrx_opt.clear(); |
| 1999 | ulx_opt.push_back(e_ulx); |
| 2000 | uly_opt.push_back(e_uly); |
| 2001 | lrx_opt.push_back(e_lrx); |
| 2002 | lry_opt.push_back(e_lry); |
| 2003 | } |
| 2004 | else{ |
| 2005 | if(targetSRS_opt.size()){ |
| 2006 | OGRSpatialReference targetSpatialRef; |
| 2007 | targetSpatialRef.SetFromUserInput(targetSRS_opt[0].c_str()); |
| 2008 | #if GDAL_VERSION_MAJOR > 2 |
| 2009 | targetSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 2010 | #endif |
| 2011 | |
| 2012 | char *wktString; |
| 2013 | targetSpatialRef.exportToWkt(&wktString); |
| 2014 | // m_projection=wktString; |
| 2015 | gds2target = OGRCreateCoordinateTransformation(&gdsSpatialRef, &targetSpatialRef); |
| 2016 | target2gds = OGRCreateCoordinateTransformation(&targetSpatialRef, &gdsSpatialRef); |
| 2017 | if(targetSpatialRef.IsSame(&gdsSpatialRef)){ |
| 2018 | target2gds=0; |
| 2019 | gds2target=0; |
| 2020 | } |
| 2021 | else{ |
| 2022 | if(!target2gds){ |
| 2023 | std::ostringstream errorStream; |
| 2024 | errorStream << "Error: cannot create OGRCoordinateTransformation target to GDAL dataset" << std::endl; |
| 2025 | throw(errorStream.str()); |
| 2026 | } |
| 2027 | if(!gds2target){ |
| 2028 | std::ostringstream errorStream; |
| 2029 | errorStream << "Error: cannot create OGRCoordinateTransformation GDAL dataset to target" << std::endl; |
| 2030 | throw(errorStream.str()); |
| 2031 | } |
| 2032 | } |
| 2033 | //both ulx and uly need to be set |
| 2034 | if(ulx_opt.size() && uly_opt.size()){ |
| 2035 | if(target2gds){ |
| 2036 | if(!target2gds->Transform(ulx_opt.size(),&ulx_opt[0],&uly_opt[0])){ |
| 2037 | std::ostringstream errorStream; |
| 2038 | std::cerr << "Error: cannot apply OGRCoordinateTransformation" << std::endl; |
| 2039 | errorStream << "Error: cannot apply OGRCoordinateTransformation" << std::endl; |
| 2040 | throw(errorStream.str()); |
| 2041 | } |
| 2042 | } |
| 2043 | } |
| 2044 | else{ |
| 2045 | ulx_opt.clear(); |
| 2046 | uly_opt.clear(); |
| 2047 | } |
| 2048 | if(lrx_opt.size() && lry_opt.size()){ |
| 2049 | if(target2gds){ |
| 2050 | if(!target2gds->Transform(lrx_opt.size(),&lrx_opt[0],&lry_opt[0])){ |
| 2051 | std::ostringstream errorStream; |
| 2052 | errorStream << "Error: cannot apply OGRCoordinateTransformation" << std::endl; |
| 2053 | throw(errorStream.str()); |
| 2054 | } |
| 2055 | } |
| 2056 | } |
| 2057 | else{ |
| 2058 | lrx_opt.clear(); |
| 2059 | lry_opt.clear(); |
| 2060 | } |
| 2061 | } |
| 2062 | else if(nogeo_opt[0]){ |
| 2063 | if(nogeo_opt.size()&&dx_opt.size()) |
| 2064 | dx_opt[0]*=getDeltaX(); |
| 2065 | if(nogeo_opt.size()&&dy_opt.size()) |
| 2066 | dy_opt[0]*=getDeltaY(); |
| 2067 | //here comes nogeo |
| 2068 | //ulx_opt and uly_opt in image coordinates: convert to georeferenced coordinates |
| 2069 | if(ulx_opt.size() && uly_opt.size()){ |
| 2070 | this->image2geo(ulx_opt[0],uly_opt[0],ulx_opt[0],uly_opt[0]); |
| 2071 | ulx_opt[0]-=getDeltaX()/2; |
| 2072 | uly_opt[0]+=getDeltaY()/2; |
| 2073 | } |
| 2074 | if(lrx_opt.size() && lry_opt.size()){ |
| 2075 | //lrx_opt and lry_opt in image coordinates: convert to georeferenced coordinates |
| 2076 | this->image2geo(lrx_opt[0],lry_opt[0],lrx_opt[0],lry_opt[0]); |
| 2077 | lrx_opt[0]+=getDeltaX()/2; |
| 2078 | lry_opt[0]-=getDeltaY()/2; |
| 2079 | } |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | std::vector<double> gds_gt(6); |
| 2084 | m_gds->GetGeoTransform(&gds_gt[0]); |
| 2085 | int gds_ncol=m_gds->GetRasterXSize(); |
| 2086 | int gds_nrow=m_gds->GetRasterYSize(); |
| 2087 | double gds_ulx=gds_gt[0]; |
| 2088 | double gds_uly=gds_gt[3]; |
| 2089 | double gds_lrx=gds_gt[0]+gds_ncol*gds_gt[1]+gds_nrow*gds_gt[2]; |
| 2090 | double gds_lry=gds_gt[3]+gds_ncol*gds_gt[4]+gds_nrow*gds_gt[5]; |
| 2091 | double gds_dx=gds_gt[1]; |
| 2092 | double gds_dy=-gds_gt[5]; |
| 2093 | |
| 2094 | if(ulx_opt.empty()) |
| 2095 | ulx_opt.push_back(gds_ulx); |
| 2096 | else if(align_opt[0]){ |
| 2097 | if(ulx_opt[0]>this->getUlx()) |
| 2098 | ulx_opt[0]-=fmod(ulx_opt[0]-this->getUlx(),getDeltaX()); |
| 2099 | else if(ulx_opt[0]<this->getUlx()) |
| 2100 | ulx_opt[0]+=fmod(this->getUlx()-ulx_opt[0],getDeltaX())-getDeltaX(); |
| 2101 | } |
| 2102 | if(uly_opt.empty()) |
| 2103 | uly_opt.push_back(gds_uly); |
| 2104 | else if(align_opt[0]){ |
| 2105 | if(uly_opt[0]<this->getUly()) |
| 2106 | uly_opt[0]+=fmod(this->getUly()-uly_opt[0],getDeltaY()); |
| 2107 | else if(uly_opt[0]>this->getUly()) |
| 2108 | uly_opt[0]-=fmod(uly_opt[0]-this->getUly(),getDeltaY())+getDeltaY(); |
| 2109 | } |
| 2110 | if(lrx_opt.empty()) |
| 2111 | lrx_opt.push_back(gds_lrx); |
| 2112 | else if(align_opt[0]){ |
| 2113 | if(lrx_opt[0]<this->getLrx()) |
| 2114 | lrx_opt[0]+=fmod(this->getLrx()-lrx_opt[0],getDeltaX()); |
| 2115 | else if(lrx_opt[0]>this->getLrx()) |
| 2116 | lrx_opt[0]-=fmod(lrx_opt[0]-this->getLrx(),getDeltaX())+getDeltaX(); |
| 2117 | } |
| 2118 | if(lry_opt.empty()) |
| 2119 | lry_opt.push_back(gds_lry); |
| 2120 | else if(align_opt[0]){ |
| 2121 | if(lry_opt[0]>this->getLry()) |
| 2122 | lry_opt[0]-=fmod(lry_opt[0]-this->getLry(),getDeltaY()); |
| 2123 | else if(lry_opt[0]<this->getLry()) |
| 2124 | lry_opt[0]+=fmod(this->getLry()-lry_opt[0],getDeltaY())-getDeltaY(); |
| 2125 | } |
| 2126 | //now ulx_opt, uly_opt, lrx_opt and lry_opt are in GDS SRS coordinates |
| 2127 | if(band_opt.empty()){ |
| 2128 | while(band_opt.size()<nrOfBand()) |
| 2129 | band_opt.push_back(band_opt.size()); |
| 2130 | } |
| 2131 | m_nband=band_opt.size(); |
| 2132 | |
| 2133 | GDALRasterBand *poBand;//we will fetch the first band to obtain the gds metadata |
| 2134 | |
| 2135 | if(m_gds){ |
| 2136 | #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 13, 0) |
| 2137 | CSLConstList papszMetadata; |
| 2138 | #else |
| 2139 | char **papszMetadata; |
| 2140 | #endif |
| 2141 | papszMetadata = m_gds->GetMetadata("SUBDATASETS"); |
| 2142 | vector<string> LayersAll; |
| 2143 | if( papszMetadata ){ |
| 2144 | std::ostringstream errorStream; |
| 2145 | errorStream << "Warning: dataset has subdatasets, please select a subdataset" << std::endl; |
| 2146 | throw(errorStream.str()); |
| 2147 | } |
| 2148 | else |
| 2149 | poBand = m_gds->GetRasterBand(band_opt[0]+1);//GDAL uses 1 based index |
| 2150 | } |
| 2151 | else{ |
| 2152 | std::ostringstream errorStream; |
| 2153 | errorStream << "Error: no GDAL dataset" << std::endl; |
| 2154 | throw(errorStream.str()); |
| 2155 | } |
| 2156 | std::vector<double> gds_bb; |
| 2157 | getBoundingBox(gds_bb); |
| 2158 | if(dx_opt.empty()){ |
| 2159 | if(nsample_opt.size()){ |
| 2160 | if(verbose_opt[0]) |
| 2161 | std::cout << "nsample_opt[0]: " << nsample_opt[0] << std::endl; |
| 2162 | dx_opt.push_back((lrx_opt[0]-ulx_opt[0])/nsample_opt[0]); |
| 2163 | // dx_opt.push_back((gds_lrx-gds_ulx)/nsample_opt[0]); |
| 2164 | } |
| 2165 | else |
| 2166 | dx_opt.push_back(gds_dx); |
| 2167 | } |
| 2168 | if(dy_opt.empty()){ |
| 2169 | if(nline_opt.size()){ |
| 2170 | if(verbose_opt[0]) |
| 2171 | std::cout << "nline_opt[0]: " << nline_opt[0] << std::endl; |
| 2172 | dy_opt.push_back((uly_opt[0]-lry_opt[0])/nline_opt[0]); |
| 2173 | // dy_opt.push_back((gds_bb[1]-gds_bb[3])/nline_opt[0]); |
| 2174 | } |
| 2175 | else |
| 2176 | dy_opt.push_back(getDeltaY()); |
| 2177 | } |
| 2178 | if(verbose_opt[0]){ |
| 2179 | std::cout << "dx_opt[0]: " << dx_opt[0] << std::endl; |
| 2180 | std::cout << "dy_opt[0]: " << dy_opt[0] << std::endl; |
| 2181 | } |
| 2182 | //force bounding box to be within dataset |
| 2183 | if(ulx_opt[0]<gds_ulx) |
| 2184 | ulx_opt[0]=gds_ulx; |
| 2185 | if(uly_opt[0]>gds_uly) |
| 2186 | uly_opt[0]=gds_uly; |
| 2187 | if(lrx_opt[0]>gds_lrx) |
| 2188 | lrx_opt[0]=gds_lrx; |
| 2189 | if(lry_opt[0]<gds_lry) |
| 2190 | lry_opt[0]=gds_lry; |
| 2191 | |
| 2192 | if(verbose_opt[0]){ |
| 2193 | std::cout << "forced boundingbox:" << std::endl; |
| 2194 | std::cout << "ulx=" << ulx_opt[0] << std::endl; |
| 2195 | std::cout << "uly=" << uly_opt[0] << std::endl; |
| 2196 | std::cout << "lrx=" << lrx_opt[0] << std::endl; |
| 2197 | std::cout << "lry=" << lry_opt[0] << std::endl; |
| 2198 | } |
| 2199 | std::vector<double> gt(6); |
| 2200 | gt[0]=ulx_opt[0]; |
| 2201 | gt[3]=uly_opt[0]; |
| 2202 | gt[1]=dx_opt[0];//todo: adfGeotransform[1]: $cos(\alpha)\cdot\textrm{Xres}$ |
| 2203 | gt[2]=0;//todo: $-sin(\alpha)\cdot\textrm{Xres}$ |
| 2204 | gt[4]=0;//todo: $-sin(\alpha)\cdot\textrm{Yres}$ |
| 2205 | gt[5]=-dy_opt[0];//todo: a$-cos(\alpha)\cdot\textrm{Yres} |
| 2206 | setGeoTransform(gt); |
| 2207 | m_resample=getGDALResampleIO(resample_opt[0]); |
| 2208 | int nBufXSize=static_cast<unsigned int>(ceil((lrx_opt[0]-ulx_opt[0])/dx_opt[0]-FLT_EPSILON)); |
| 2209 | int nBufYSize=static_cast<unsigned int>(ceil((uly_opt[0]-lry_opt[0])/dy_opt[0]-FLT_EPSILON)); |
| 2210 | m_ncol=nBufXSize; |
| 2211 | m_nrow=nBufYSize; |
| 2212 | m_blockSize=m_nrow; |
| 2213 | |
| 2214 | if(verbose_opt[0]){ |
| 2215 | std::cout << "m_ncol: " << m_ncol << std::endl; |
| 2216 | std::cout << "m_nrow: " << m_nrow << std::endl; |
| 2217 | } |
| 2218 | //we initialize memory using class member variables instead of those read from GDAL dataset |
| 2219 | if(band2plane_opt[0]){ |
| 2220 | #if MIALLIB == 1 |
| 2221 | m_nplane=m_nband; |
| 2222 | m_nband=1; |
| 2223 | initMem(memory_opt[0]); |
| 2224 | m_begin[0]=0; |
| 2225 | m_end[0]=m_begin[0]+m_blockSize; |
| 2226 | if(!noread_opt[0]){ |
| 2227 | readDataPlanes(band_opt); |
| 2228 | } |
| 2229 | #else |
| 2230 | std::ostringstream errorStream; |
| 2231 | errorStream << "Warning: planes not supported, please compile with MIALLIB" << std::endl; |
| 2232 | throw(errorStream.str()); |
| 2233 | #endif |
| 2234 | } |
| 2235 | else{ |
| 2236 | if(!noread_opt[0]){ |
| 2237 | if(getDeltaY() > 0){ |
| 2238 | if(!covers(ulx_opt[0],uly_opt[0],lrx_opt[0],lry_opt[0])){ |
| 2239 | std::ostringstream errorStream; |
| 2240 | errorStream << "Warning: raster dataset does not cover required bounding box" << std::endl; |
| 2241 | throw(errorStream.str()); |
| 2242 | } |
| 2243 | } |
| 2244 | else{ |
| 2245 | //test |
| 2246 | std::cout << "skipping test on bounding box for non-georeferenced image" << std::endl; |
| 2247 | } |
| 2248 | |
| 2249 | //we initialize memory using class member variables instead of those read from GDAL dataset |
| 2250 | initMem(memory_opt[0]); |
| 2251 | for(int iband=0;iband<nrOfBand();++iband){ |
| 2252 | m_begin[iband]=0; |
| 2253 | m_end[iband]=m_begin[iband]+m_blockSize; |
| 2254 | if(!noread_opt[0]){ |
| 2255 | //we can not use readData(iband) because sequence of band_opt might not correspond bands in GDAL dataset |
| 2256 | if(verbose_opt[0]){ |
| 2257 | std::cout << "reading dataset" << std::endl; |
| 2258 | } |
| 2259 | readDataDS(iband,band_opt[iband]); |
| 2260 | if(verbose_opt[0]){ |
| 2261 | std::cout << "dataset has been read" << std::endl; |
| 2262 | } |
| 2263 | } |
| 2264 | } |
| 2265 | } |
| 2266 | else{ |
| 2267 | if(memory_opt[0]<=0) |
| 2268 | m_blockSize=nrOfRow(); |
| 2269 | else{ |
| 2270 | m_blockSize=static_cast<unsigned int>(memory_opt[0]*1000000/nrOfBand()/nrOfCol()/getDataTypeSizeBytes()); |
| 2271 | if(getBlockSizeY(0)) |
| 2272 | m_blockSize-=m_blockSize%getBlockSizeY(0); |
| 2273 | if(m_blockSize<1) |
| 2274 | m_blockSize=1; |
| 2275 | if(m_blockSize>nrOfRow()) |
| 2276 | m_blockSize=nrOfRow(); |
| 2277 | } |
| 2278 | } |
| 2279 | } |
| 2280 | } |
| 2281 | else{ |
| 2282 | std::ostringstream errorStream; |
| 2283 | errorStream << "Warning: no number of rows or columns provided, nor input filename." << std::endl; |
| 2284 | // Jim(); |
| 2285 | throw(errorStream.str()); |
| 2286 | } |
| 2287 | setNoData(nodata_opt); |
| 2288 | return(CE_None); |
| 2289 | } |
| 2290 | |
| 2291 | /** |
| 2292 | * @param band Band that must be read to cache |
| 2293 | * @return true if block was read |
| 2294 | **/ |
| 2295 | CPLErr Jim::readDataDS(int band, int ds_band) |
| 2296 | { |
| 2297 | CPLErr returnValue=CE_None; |
| 2298 | if(m_blockSize<nrOfRow()){ |
| 2299 | std::ostringstream s; |
| 2300 | s << "Error: increase memory to read all pixels in memory (now at " << 100.0*m_blockSize/nrOfRow() << "%)"; |
| 2301 | throw(s.str()); |
| 2302 | } |
| 2303 | if(m_gds == NULL){ |
| 2304 | std::string errorString="Error in readData"; |
| 2305 | throw(errorString); |
| 2306 | } |
| 2307 | //should have been set already... |
| 2308 | m_begin[band]=0; |
| 2309 | m_end[band]=m_begin[band]+m_blockSize; |
| 2310 | return(readNewBlockDS(0,band,ds_band)); |
| 2311 | } |
| 2312 | |
| 2313 | // CPLErr Jim::readData(int band) |
| 2314 | // { |
| 2315 | // CPLErr returnValue=CE_None; |
| 2316 | // if(m_blockSize<nrOfRow()){ |
| 2317 | // std::ostringstream s; |
| 2318 | // s << "Error: increase memory to read all pixels in memory (now at " << 100.0*m_blockSize/nrOfRow() << "%)"; |
| 2319 | // throw(s.str()); |
| 2320 | // } |
| 2321 | // if(m_gds == NULL){ |
| 2322 | // std::string errorString="Error in readData"; |
| 2323 | // throw(errorString); |
| 2324 | // } |
| 2325 | // m_begin[band]=0; |
| 2326 | // m_end[band]=nrOfRow(); |
| 2327 | // GDALRasterBand *poBand; |
| 2328 | // if(nrOfBand()<=iband){ |
| 2329 | // std::string errorString="Error: band number exceeds available bands in readData"; |
| 2330 | // throw(errorString); |
| 2331 | // } |
| 2332 | // poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index |
| 2333 | // returnValue=poBand->RasterIO(GF_Read,0,m_begin[band],nrOfCol(),m_end[band]-m_begin[band],m_data[band],nrOfCol(),m_end[band]-m_begin[band],getDataType(),0,0); |
| 2334 | // return(returnValue);//new block was read |
| 2335 | // } |
| 2336 | |
| 2337 | /** |
| 2338 | * @return true if block was read |
| 2339 | **/ |
| 2340 | CPLErr Jim::readData() |
| 2341 | { |
| 2342 | CPLErr returnValue=CE_None; |
| 2343 | for(int iband=0;iband<nrOfBand();++iband){ |
| 2344 | if(readData(iband)!=CE_None) |
| 2345 | returnValue=CE_Failure; |
| 2346 | } |
| 2347 | return(returnValue);//new block was read |
| 2348 | } |
| 2349 | |
| 2350 | /** |
| 2351 | * @param row Read a new block for caching this row (if needed) |
| 2352 | * @param band Band that must be read to cache |
| 2353 | * @return true if block was read |
| 2354 | **/ |
| 2355 | CPLErr Jim::readNewBlockDS(int row, int iband, int ds_band){ |
| 2356 | CPLErr returnValue=CE_None; |
| 2357 | if(m_gds == NULL){ |
| 2358 | std::string errorString="Error in readNewBlock"; |
| 2359 | throw(errorString); |
| 2360 | } |
| 2361 | if(m_end[iband]<m_blockSize)//first time |
| 2362 | m_end[iband]=m_blockSize; |
| 2363 | while(row>=m_end[iband]&&m_begin[iband]<nrOfRow()){ |
| 2364 | m_begin[iband]+=m_blockSize; |
| 2365 | m_end[iband]=m_begin[iband]+m_blockSize; |
| 2366 | } |
| 2367 | if(m_end[iband]>nrOfRow()) |
| 2368 | m_end[iband]=nrOfRow(); |
| 2369 | |
| 2370 | int gds_ncol=m_gds->GetRasterXSize(); |
| 2371 | int gds_nrow=m_gds->GetRasterYSize(); |
| 2372 | int gds_nband=m_gds->GetRasterCount(); |
| 2373 | vector<double> gds_gt(6); |
| 2374 | m_gds->GetGeoTransform(&gds_gt[0]); |
| 2375 | double gds_ulx=gds_gt[0]; |
| 2376 | double gds_uly=gds_gt[3]; |
| 2377 | double gds_lrx=gds_gt[0]+gds_ncol*gds_gt[1]+gds_nrow*gds_gt[2]; |
| 2378 | double gds_lry=gds_gt[3]+gds_ncol*gds_gt[4]+gds_nrow*gds_gt[5]; |
| 2379 | double gds_dx=gds_gt[1]; |
| 2380 | double gds_dy=-gds_gt[5]; |
| 2381 | double diffXm=getUlx()-gds_ulx; |
| 2382 | // double diffYm=gds_uly-getUly(); |
| 2383 | |
| 2384 | // double dfXSize=diffXm/gds_dx; |
| 2385 | double dfXSize=(getLrx()-getUlx())/gds_dx;//x-size in pixels of region to read in original image |
| 2386 | double dfXOff=diffXm/gds_dx; |
| 2387 | // double dfYSize=diffYm/gds_dy; |
| 2388 | // double dfYSize=(getUly()-getLry())/gds_dy;//y-size in pixels of region to read in original image |
| 2389 | // double dfYOff=diffYm/gds_dy; |
| 2390 | // int nYOff=static_cast<int>(dfYOff); |
| 2391 | int nXSize=static_cast<unsigned int>(ceil((getLrx()-getUlx())/gds_dx));//x-size in pixels of region to read in original image |
| 2392 | int nXOff=static_cast<int>(dfXOff); |
| 2393 | if(nXSize>gds_ncol-nXOff) |
| 2394 | nXSize=gds_ncol-nXOff; |
| 2395 | |
| 2396 | double dfYSize=0; |
| 2397 | double dfYOff=0; |
| 2398 | int nYSize=0; |
| 2399 | int nYOff=0; |
| 2400 | |
| 2401 | GDALRasterIOExtraArg sExtraArg; |
| 2402 | INIT_RASTERIO_EXTRA_ARG(sExtraArg); |
| 2403 | sExtraArg.eResampleAlg = m_resample; |
| 2404 | // for(int iband=0;iband<m_nband;++iband){ |
| 2405 | //fetch raster band |
| 2406 | GDALRasterBand *poBand; |
| 2407 | if(nrOfBand()<=iband){ |
| 2408 | std::string errorString="Error: band number exceeds available bands in readNewBlock"; |
| 2409 | throw(errorString); |
| 2410 | } |
| 2411 | poBand = m_gds->GetRasterBand(ds_band+1);//GDAL uses 1 based index |
| 2412 | |
| 2413 | dfYSize=(m_end[iband]-m_begin[iband])*getDeltaY()/gds_dy;//y-size in pixels of region to read in original image |
| 2414 | nYSize=static_cast<unsigned int>(ceil((m_end[iband]-m_begin[iband])*getDeltaY()/gds_dy));//y-size in pixels of region to read in original image |
| 2415 | dfYOff=(gds_uly-getUly())/gds_dy+m_begin[iband]*getDeltaY()/gds_dy; |
| 2416 | nYOff=static_cast<int>(dfYOff); |
| 2417 | if(nYSize>gds_nrow-nYOff) |
| 2418 | nYSize=gds_nrow-nYOff; |
| 2419 | bool useOverview=false; |
| 2420 | if(poBand->GetOverviewCount()){ |
| 2421 | // useOverview=true; |
| 2422 | // for(auto ovit=m_ooptions.begin();ovit!=m_ooptions.end();++ovit) |
| 2423 | // std::cout << *ovit << std::endl; |
| 2424 | // useOverview=true; |
| 2425 | // if(std::find_if(m_ooptions.begin(), m_ooptions.end(), [](const std::string& str) { return str.find("OVERVIEW_LEVEL") != std::string::npos; }) != m_ooptions.end()) { |
| 2426 | // useOverview=false; |
| 2427 | // } |
| 2428 | // } |
| 2429 | // if(useOverview){ |
| 2430 | //calculate number of desired samples in overview |
| 2431 | int nDesiredSamples=static_cast<unsigned int>(ceil((gds_lrx-gds_ulx)/getDeltaX()))*static_cast<unsigned int>(ceil((gds_uly-gds_lry)/getDeltaY())); |
| 2432 | poBand=poBand->GetRasterSampleOverview(nDesiredSamples); |
| 2433 | if(poBand->GetXSize()*poBand->GetYSize()<nDesiredSamples){ |
| 2434 | //should never be entered as GetRasterSampleOverview must return best overview or original band in worst case... |
| 2435 | // std::cout << "Warning: not enough samples in best overview, falling back to original band" << std::endl; |
| 2436 | poBand = m_gds->GetRasterBand(iband+1);//GDAL uses 1 based index |
| 2437 | } |
| 2438 | int ods_ncol=poBand->GetXSize(); |
| 2439 | int ods_nrow=poBand->GetYSize(); |
| 2440 | double ods_dx=gds_dx*gds_ncol/ods_ncol; |
| 2441 | double ods_dy=gds_dy*gds_nrow/ods_nrow; |
| 2442 | |
| 2443 | // dfXSize=diffXm/ods_dx; |
| 2444 | dfXSize=(getLrx()-getUlx())/ods_dx; |
| 2445 | nXSize=static_cast<unsigned int>(ceil((getLrx()-getUlx())/ods_dx));//x-size in pixels of region to read in overview image |
| 2446 | dfXOff=diffXm/ods_dx; |
| 2447 | nXOff=static_cast<int>(dfXOff); |
| 2448 | if(nXSize>ods_ncol-nXOff) |
| 2449 | nXSize=ods_ncol-nXOff; |
| 2450 | |
| 2451 | dfYSize=(m_end[iband]-m_begin[iband])*getDeltaY()/ods_dy;//y-size in pixels of region to read in overview image |
| 2452 | nYSize=static_cast<unsigned int>(ceil((m_end[iband]-m_begin[iband])*getDeltaY()/ods_dy));//y-size in pixels of region to read in overview image |
| 2453 | dfYOff=(gds_uly-getUly())/ods_dy+m_begin[iband]*getDeltaY()/ods_dy; |
| 2454 | nYOff=static_cast<int>(dfYOff); |
| 2455 | if(nYSize>ods_nrow-nYOff) |
| 2456 | nYSize=ods_nrow-nYOff; |
| 2457 | } |
| 2458 | if(dfXOff-nXOff>0||dfYOff-nYOff>0||getDeltaX()<gds_dx||getDeltaX()>gds_dx||getDeltaY()<gds_dy||getDeltaY()>gds_dy){ |
| 2459 | sExtraArg.bFloatingPointWindowValidity = TRUE; |
| 2460 | sExtraArg.dfXOff = dfXOff; |
| 2461 | sExtraArg.dfYOff = dfYOff; |
| 2462 | sExtraArg.dfXSize = dfXSize; |
| 2463 | sExtraArg.dfYSize = dfYSize; |
| 2464 | } |
| 2465 | else{ |
| 2466 | sExtraArg.bFloatingPointWindowValidity = FALSE; |
| 2467 | sExtraArg.dfXOff = 0; |
| 2468 | sExtraArg.dfYOff = 0; |
| 2469 | sExtraArg.dfXSize = dfXSize; |
| 2470 | sExtraArg.dfYSize = dfYSize; |
| 2471 | } |
| 2472 | //test |
| 2473 | // std::cout << "nYOff: " << nYOff << std::endl; |
| 2474 | // std::cout << "dfXOff: " << dfXOff << std::endl; |
| 2475 | // std::cout << "dfYOff: " << dfYOff << std::endl; |
| 2476 | // std::cout << "nXSize: " << nXSize << std::endl; |
| 2477 | // std::cout << "nYSize: " << nYSize << std::endl; |
| 2478 | // std::cout << "nrOfCol(): " << nrOfCol() << std::endl; |
| 2479 | // std::cout << "nrOfRow(): " << nrOfRow() << std::endl; |
| 2480 | // std::cout << "getDeltaX(): " << getDeltaX() << std::endl; |
| 2481 | // std::cout << "getDeltaY(): " << getDeltaY() << std::endl; |
| 2482 | // std::cout << "gds_dx: " << gds_dx << std::endl; |
| 2483 | // std::cout << "gds_dy: " << gds_dy << std::endl; |
| 2484 | // std::cout << "getUlx(): " << getUlx() << std::endl; |
| 2485 | // std::cout << "getUly(): " << getUly() << std::endl; |
| 2486 | // std::cout << "gds_ulx: " << gds_ulx << std::endl; |
| 2487 | // std::cout << "gds_uly: " << gds_uly << std::endl; |
| 2488 | // eRWFlag Either GF_Read to read a region of data, or GF_Write to write a region of data. |
| 2489 | // nXOff The pixel offset to the top left corner of the region of the band to be accessed. This would be zero to start from the left side. |
| 2490 | // nYOff The line offset to the top left corner of the region of the band to be accessed. This would be zero to start from the top. |
| 2491 | // nXSize The width of the region of the band to be accessed in pixels. |
| 2492 | // nYSize The height of the region of the band to be accessed in lines. |
| 2493 | // pData The buffer into which the data should be read, or from which it should be written. This buffer must contain at least nBufXSize * nBufYSize words of type eBufType. It is organized in left to right, top to bottom pixel order. Spacing is controlled by the nPixelSpace, and nLineSpace parameters. |
| 2494 | // nBufXSize the width of the buffer image into which the desired region is to be read, or from which it is to be written. |
| 2495 | // nBufYSize the height of the buffer image into which the desired region is to be read, or from which it is to be written. |
| 2496 | // eBufType the type of the pixel values in the pData data buffer. The pixel values will automatically be translated to/from the GDALRasterBand data type as needed. |
| 2497 | // nPixelSpace The byte offset from the start of one pixel value in pData to the start of the next pixel value within a scanline. If defaulted (0) the size of the datatype eBufType is used. |
| 2498 | // nLineSpace The byte offset from the start of one scanline in pData to the start of the next. If defaulted (0) the size of the datatype eBufType * nBufXSize is used. |
| 2499 | // psExtraArg (new in GDAL 2.0) pointer to a GDALRasterIOExtraArg structure with additional arguments to specify resampling and progress callback, or NULL for default behaviour. The GDAL_RASTERIO_RESAMPLING configuration option can also be defined to override the default resampling to one of BILINEAR, CUBIC, CUBICSPLINE, LANCZOS, AVERAGE or MODE. |
| 2500 | |
| 2501 | returnValue=poBand->RasterIO(GF_Read,nXOff,nYOff+m_begin[iband],nXSize,nYSize,m_data[iband],nrOfCol(),m_end[iband]-m_begin[iband],getGDALDataType(),0,0,&sExtraArg); |
| 2502 | // returnValue=poBand->RasterIO(GF_Read,0,m_begin[iband],nrOfCol(),m_end[iband]-m_begin[iband],m_data[iband],nrOfCol(),m_end[iband]-m_begin[iband],getDataType(),0,0); |
| 2503 | // } |
| 2504 | return(returnValue);//new block was read |
| 2505 | } |
| 2506 | |
| 2507 | // CPLErr Jim::readNewBlock(int row, int band) |
| 2508 | // { |
| 2509 | // CPLErr returnValue=CE_None; |
| 2510 | // if(m_gds == NULL){ |
| 2511 | // std::string errorString="Error in readNewBlock"; |
| 2512 | // throw(errorString); |
| 2513 | // } |
| 2514 | // if(m_end[band]<m_blockSize)//first time |
| 2515 | // m_end[band]=m_blockSize; |
| 2516 | // while(row>=m_end[band]&&m_begin[band]<nrOfRow()){ |
| 2517 | // m_begin[band]+=m_blockSize; |
| 2518 | // m_end[band]=m_begin[band]+m_blockSize; |
| 2519 | // } |
| 2520 | // if(m_end[band]>nrOfRow()) |
| 2521 | // m_end[band]=nrOfRow(); |
| 2522 | // for(int iband=0;iband<m_nband;++iband){ |
| 2523 | // //fetch raster band |
| 2524 | // GDALRasterBand *poBand; |
| 2525 | // //todo: replace assert with exception |
| 2526 | // assert(iband<nrOfBand()+1); |
| 2527 | // poBand = m_gds->GetRasterBand(iband+1);//GDAL uses 1 based index |
| 2528 | // returnValue=poBand->RasterIO(GF_Read,0,m_begin[iband],nrOfCol(),m_end[iband]-m_begin[iband],m_data[iband],nrOfCol(),m_end[iband]-m_begin[iband],getDataType(),0,0); |
| 2529 | // } |
| 2530 | // return(returnValue);//new block was read |
| 2531 | // } |
| 2532 | |
| 2533 | #if MIALLIB == 1 |
| 2534 | CPLErr Jim::readDataPlanes(std::vector<int> bands){ |
| 2535 | CPLErr returnValue=CE_None; |
| 2536 | if(m_gds == NULL){ |
| 2537 | std::string errorString="Error in readNewBlock"; |
| 2538 | throw(errorString); |
| 2539 | } |
| 2540 | if(m_end[0]<m_blockSize)//first time |
| 2541 | m_end[0]=m_blockSize; |
| 2542 | if(m_end[0]>nrOfRow()) |
| 2543 | m_end[0]=nrOfRow(); |
| 2544 | |
| 2545 | int gds_ncol=m_gds->GetRasterXSize(); |
| 2546 | int gds_nrow=m_gds->GetRasterYSize(); |
| 2547 | int gds_nband=m_gds->GetRasterCount(); |
| 2548 | double gds_gt[6]; |
| 2549 | m_gds->GetGeoTransform(gds_gt); |
| 2550 | double gds_ulx=gds_gt[0]; |
| 2551 | double gds_uly=gds_gt[3]; |
| 2552 | double gds_lrx=gds_gt[0]+gds_ncol*gds_gt[1]+gds_nrow*gds_gt[2]; |
| 2553 | double gds_lry=gds_gt[3]+gds_ncol*gds_gt[4]+gds_nrow*gds_gt[5]; |
| 2554 | double gds_dx=gds_gt[1]; |
| 2555 | double gds_dy=-gds_gt[5]; |
| 2556 | double diffXm=getUlx()-gds_ulx; |
| 2557 | // double diffYm=gds_uly-getUly(); |
| 2558 | |
| 2559 | // double dfXSize=diffXm/gds_dx; |
| 2560 | double dfXSize=(getLrx()-getUlx())/gds_dx;//x-size in pixels of region to read in original image |
| 2561 | double dfXOff=diffXm/gds_dx; |
| 2562 | // double dfYSize=diffYm/gds_dy; |
| 2563 | // double dfYSize=(getUly()-getLry())/gds_dy;//y-size in piyels of region to read in original image |
| 2564 | // double dfYOff=diffYm/gds_dy; |
| 2565 | // int nYOff=static_cast<int>(dfYOff); |
| 2566 | int nXSize=static_cast<unsigned int>(ceil((getLrx()-getUlx())/gds_dx));//x-size in pixels of region to read in original image |
| 2567 | int nXOff=static_cast<int>(dfXOff); |
| 2568 | if(nXSize>gds_ncol) |
| 2569 | nXSize=gds_ncol; |
| 2570 | |
| 2571 | double dfYSize=0; |
| 2572 | double dfYOff=0; |
| 2573 | int nYSize=0; |
| 2574 | int nYOff=0; |
| 2575 | |
| 2576 | GDALRasterIOExtraArg sExtraArg; |
| 2577 | INIT_RASTERIO_EXTRA_ARG(sExtraArg); |
| 2578 | sExtraArg.eResampleAlg = m_resample; |
| 2579 | dfYSize=(m_end[0]-m_begin[0])*getDeltaY()/gds_dy;//y-size in pixels of region to read in original image |
| 2580 | nYSize=static_cast<unsigned int>(ceil((m_end[0]-m_begin[0])*getDeltaY()/gds_dy));//y-size in pixels of region to read in original image |
| 2581 | if(nYSize>gds_nrow) |
| 2582 | nYSize=gds_nrow; |
| 2583 | dfYOff=(gds_uly-getUly())/gds_dy+m_begin[0]*getDeltaY()/gds_dy; |
| 2584 | nYOff=static_cast<int>(dfYOff); |
| 2585 | if(dfXOff-nXOff>0||dfYOff-nYOff>0||getDeltaX()<gds_dx||getDeltaX()>gds_dx||getDeltaY()<gds_dy||getDeltaY()>gds_dy){ |
| 2586 | sExtraArg.bFloatingPointWindowValidity = TRUE; |
| 2587 | sExtraArg.dfXOff = dfXOff; |
| 2588 | sExtraArg.dfYOff = dfYOff; |
| 2589 | sExtraArg.dfXSize = dfXSize; |
| 2590 | sExtraArg.dfYSize = dfYSize; |
| 2591 | } |
| 2592 | else{ |
| 2593 | sExtraArg.bFloatingPointWindowValidity = FALSE; |
| 2594 | sExtraArg.dfXOff = 0; |
| 2595 | sExtraArg.dfYOff = 0; |
| 2596 | sExtraArg.dfXSize = dfXSize; |
| 2597 | sExtraArg.dfYSize = dfYSize; |
| 2598 | } |
| 2599 | std::vector<int> gdalbands=bands; |
| 2600 | for(int iband=0;iband<bands.size();++iband) |
| 2601 | gdalbands[iband]=bands[iband]+1; |
| 2602 | |
| 2603 | // eRWFlag Either GF_Read to read a region of data, or GF_Write to write a region of data. |
| 2604 | // nXOff The pixel offset to the top left corner of the region of the band to be accessed. This would be zero to start from the left side. |
| 2605 | // nYOff The line offset to the top left corner of the region of the band to be accessed. This would be zero to start from the top. |
| 2606 | // nXSize The width of the region of the band to be accessed in pixels. |
| 2607 | // nYSize The height of the region of the band to be accessed in lines. |
| 2608 | // pData The buffer into which the data should be read, or from which it should be written. This buffer must contain at least nBufXSize * nBufYSize * nBandCount words of type eBufType. It is organized in left to right,top to bottom pixel order. Spacing is controlled by the nPixelSpace, and nLineSpace parameters. |
| 2609 | // nBufXSize the width of the buffer image into which the desired region is to be read, or from which it is to be written. |
| 2610 | // nBufYSize the height of the buffer image into which the desired region is to be read, or from which it is to be written. |
| 2611 | // eBufType the type of the pixel values in the pData data buffer. The pixel values will automatically be translated to/from the GDALRasterBand data type as needed. |
| 2612 | // nBandCount the number of bands being read or written. |
| 2613 | // panBandMap the list of nBandCount band numbers being read/written. Note band numbers are 1 based. This may be NULL to select the first nBandCount bands. |
| 2614 | // nPixelSpace The byte offset from the start of one pixel value in pData to the start of the next pixel value within a scanline. If defaulted (0) the size of the datatype eBufType is used. |
| 2615 | // nLineSpace The byte offset from the start of one scanline in pData to the start of the next. If defaulted (0) the size of the datatype eBufType * nBufXSize is used. |
| 2616 | // nBandSpace the byte offset from the start of one bands data to the start of the next. If defaulted (0) the value will be nLineSpace * nBufYSize implying band sequential organization of the data buffer. |
| 2617 | // psExtraArg (new in GDAL 2.0) pointer to a GDALRasterIOExtraArg structure with additional arguments to specify resampling and progress callback, or NULL for default behaviour. The GDAL_RASTERIO_RESAMPLING configuration option can also be defined to override the default resampling to one of BILINEAR, CUBIC, CUBICSPLINE, LANCZOS, AVERAGE or MODE. |
| 2618 | returnValue=getDataset()->RasterIO(GF_Read,nXOff,nYOff+m_begin[0],nXSize,nYSize,m_data[0],nrOfCol(),m_end[0]-m_begin[0],getGDALDataType(),gdalbands.size(),&gdalbands[0],0,0,0,&sExtraArg); |
| 2619 | return(returnValue);//new block was read |
| 2620 | } |
| 2621 | #endif |
| 2622 | /** |
| 2623 | * @param x Reported column where minimum value in image was found (start counting from 0) |
| 2624 | * @param y Reported row where minimum value in image was found (start counting from 0) |
| 2625 | * @param band Search minimum value in image for this band |
| 2626 | * @return minimum value in image for the selected band |
| 2627 | **/ |
| 2628 | double Jim::getMin(int& x, int& y, int band){ |
| 2629 | double minValue=0; |
| 2630 | std::vector<double> lineBuffer(nrOfCol()); |
| 2631 | bool isValid=false; |
| 2632 | for(int irow=0;irow<nrOfRow();++irow){ |
| 2633 | readData(lineBuffer,irow,band); |
| 2634 | for(int icol=0;icol<nrOfCol();++icol){ |
| 2635 | if(isNoData(lineBuffer[icol])) |
| 2636 | continue; |
| 2637 | if(isValid){ |
| 2638 | if(lineBuffer[icol]<minValue){ |
| 2639 | y=irow; |
| 2640 | x=icol; |
| 2641 | minValue=lineBuffer[icol]; |
| 2642 | } |
| 2643 | } |
| 2644 | else{ |
| 2645 | y=irow; |
| 2646 | x=icol; |
| 2647 | minValue=lineBuffer[icol]; |
| 2648 | isValid=true; |
| 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | if(isValid) |
| 2653 | return minValue; |
| 2654 | else |
| 2655 | throw(static_cast<std::string>("Warning: not initialized")); |
| 2656 | } |
| 2657 | |
| 2658 | /** |
| 2659 | * @param x Reported column where maximum value in image was found (start counting from 0) |
| 2660 | * @param y Reported row where maximum value in image was found (start counting from 0) |
| 2661 | * @param band Search minimum value in image for this band |
| 2662 | * @return maximum value in image for the selected band |
| 2663 | **/ |
| 2664 | double Jim::getMax(int& x, int& y, int band){ |
| 2665 | double maxValue=0; |
| 2666 | std::vector<double> lineBuffer(nrOfCol()); |
| 2667 | bool isValid=false; |
| 2668 | for(int irow=0;irow<nrOfRow();++irow){ |
| 2669 | readData(lineBuffer,irow,band); |
| 2670 | for(int icol=0;icol<nrOfCol();++icol){ |
| 2671 | if(isNoData(lineBuffer[icol])) |
| 2672 | continue; |
| 2673 | if(isValid){ |
| 2674 | if(lineBuffer[icol]>maxValue){ |
| 2675 | y=irow; |
| 2676 | x=icol; |
| 2677 | maxValue=lineBuffer[icol]; |
| 2678 | } |
| 2679 | } |
| 2680 | else{ |
| 2681 | y=irow; |
| 2682 | x=icol; |
| 2683 | maxValue=lineBuffer[icol]; |
| 2684 | isValid=true; |
| 2685 | } |
| 2686 | } |
| 2687 | } |
| 2688 | if(isValid) |
| 2689 | return maxValue; |
| 2690 | else |
| 2691 | throw(static_cast<std::string>("Warning: no valid pixels found")); |
| 2692 | } |
| 2693 | |
| 2694 | /** |
| 2695 | * @param startCol, endCol, startRow, endRow Search extreme value in this region of interest (all indices start counting from 0) |
| 2696 | * @param band Search extreme value in image for this band |
| 2697 | * @param minValue Reported minimum value within searched region |
| 2698 | * @param maxValue Reported maximum value within searched region |
| 2699 | **/ |
| 2700 | void Jim::getMinMax(int startCol, int endCol, int startRow, int endRow, int band, double& minValue, double& maxValue) |
| 2701 | { |
| 2702 | bool isConstraint=(maxValue>minValue); |
| 2703 | double minConstraint=minValue; |
| 2704 | double maxConstraint=maxValue; |
| 2705 | std::vector<double> lineBuffer(endCol-startCol+1); |
| 2706 | bool isValid=false; |
| 2707 | //todo: replace assert with exception |
| 2708 | assert(endRow<nrOfRow()); |
| 2709 | for(int irow=startCol;irow<endRow+1;++irow){ |
| 2710 | readData(lineBuffer,startCol,endCol,irow,band); |
| 2711 | for(int icol=0;icol<lineBuffer.size();++icol){ |
| 2712 | if(isNoData(lineBuffer[icol])) |
| 2713 | continue; |
| 2714 | if(isValid){ |
| 2715 | if(isConstraint){ |
| 2716 | if(lineBuffer[icol]<minConstraint) |
| 2717 | continue; |
| 2718 | if(lineBuffer[icol]>maxConstraint) |
| 2719 | continue; |
| 2720 | } |
| 2721 | if(lineBuffer[icol]<minValue) |
| 2722 | minValue=lineBuffer[icol]; |
| 2723 | if(lineBuffer[icol]>maxValue) |
| 2724 | maxValue=lineBuffer[icol]; |
| 2725 | } |
| 2726 | else{ |
| 2727 | if(isConstraint){ |
| 2728 | if(lineBuffer[icol]<minConstraint) |
| 2729 | continue; |
| 2730 | if(lineBuffer[icol]>maxConstraint) |
| 2731 | continue; |
| 2732 | } |
| 2733 | minValue=lineBuffer[icol]; |
| 2734 | maxValue=lineBuffer[icol]; |
| 2735 | isValid=true; |
| 2736 | } |
| 2737 | } |
| 2738 | } |
| 2739 | if(!isValid) |
| 2740 | throw(static_cast<std::string>("Warning: not initialized")); |
| 2741 | } |
| 2742 | |
| 2743 | /** |
| 2744 | * @param minValue Reported minimum value in image |
| 2745 | * @param maxValue Reported maximum value in image |
| 2746 | * @param band Search extreme value in image for this band |
| 2747 | **/ |
| 2748 | void Jim::getMinMax(double& minValue, double& maxValue, int band) |
| 2749 | { |
| 2750 | bool isConstraint=(maxValue>minValue); |
| 2751 | double minConstraint=minValue; |
| 2752 | double maxConstraint=maxValue; |
| 2753 | std::vector<double> lineBuffer(nrOfCol()); |
| 2754 | bool isValid=false; |
| 2755 | |
| 2756 | for(int irow=0;irow<nrOfRow();++irow){ |
| 2757 | readData(lineBuffer,irow,band); |
| 2758 | for(int icol=0;icol<nrOfCol();++icol){ |
| 2759 | if(isNoData(lineBuffer[icol])) |
| 2760 | continue; |
| 2761 | if(isValid){ |
| 2762 | if(isConstraint){ |
| 2763 | if(lineBuffer[icol]<minConstraint) |
| 2764 | continue; |
| 2765 | if(lineBuffer[icol]>maxConstraint) |
| 2766 | continue; |
| 2767 | } |
| 2768 | if(lineBuffer[icol]<minValue) |
| 2769 | minValue=lineBuffer[icol]; |
| 2770 | if(lineBuffer[icol]>maxValue) |
| 2771 | maxValue=lineBuffer[icol]; |
| 2772 | } |
| 2773 | else{ |
| 2774 | if(isConstraint){ |
| 2775 | if(lineBuffer[icol]<minConstraint) |
| 2776 | continue; |
| 2777 | if(lineBuffer[icol]>maxConstraint) |
| 2778 | continue; |
| 2779 | } |
| 2780 | minValue=lineBuffer[icol]; |
| 2781 | maxValue=lineBuffer[icol]; |
| 2782 | isValid=true; |
| 2783 | } |
| 2784 | } |
| 2785 | } |
| 2786 | if(!isValid) |
| 2787 | throw(static_cast<std::string>("Warning: not initialized")); |
| 2788 | } |
| 2789 | |
| 2790 | |
| 2791 | /** |
| 2792 | * @param histvector The reported histogram with counts per bin |
| 2793 | * @param min, max Only calculate histogram for values between min and max. If min>=max, calculate min and max from the image |
| 2794 | * @param nbin Number of bins used for calculating the histogram. If nbin is 0, the number of bins is automatically calculated from min and max |
| 2795 | * @param theBand The band for which to calculate the histogram (start counting from 0) |
| 2796 | * @param kde Apply kernel density function for a Gaussian basis function |
| 2797 | * @return number of valid pixels in this dataset for the the selected band |
| 2798 | **/ |
| 2799 | double Jim::getHistogram(std::vector<double>& histvector, double& min, double& max, int& nbin, int theBand, bool kde){ |
| 2800 | double minValue=0; |
| 2801 | double maxValue=0; |
| 2802 | |
| 2803 | if(min>=max) |
| 2804 | getMinMax(minValue,maxValue,theBand); |
| 2805 | else{ |
| 2806 | minValue=min; |
| 2807 | maxValue=max; |
| 2808 | } |
| 2809 | if(min<max&&min>minValue) |
| 2810 | minValue=min; |
| 2811 | if(min<max&&max<maxValue) |
| 2812 | maxValue=max; |
| 2813 | min=minValue; |
| 2814 | max=maxValue; |
| 2815 | |
| 2816 | double sigma=0; |
| 2817 | if(kde){ |
| 2818 | double meanValue=0; |
| 2819 | double stdDev=0; |
| 2820 | GDALProgressFunc pfnProgress; |
| 2821 | void* pProgressData; |
| 2822 | GDALRasterBand* rasterBand; |
| 2823 | rasterBand=getRasterBand(theBand); |
| 2824 | rasterBand->ComputeStatistics(0,&minValue,&maxValue,&meanValue,&stdDev,pfnProgress,pProgressData); |
| 2825 | //rest minvalue and MaxValue as ComputeStatistics does not account for nodata, scale and offset |
| 2826 | minValue=min; |
| 2827 | maxValue=max; |
| 2828 | |
| 2829 | if(m_scale.size()>theBand){ |
| 2830 | stdDev*=m_scale[theBand]; |
| 2831 | } |
| 2832 | sigma=1.06*stdDev*pow(getNvalid(theBand),-0.2); |
| 2833 | } |
| 2834 | |
| 2835 | double scale=0; |
| 2836 | if(maxValue>minValue){ |
| 2837 | if(nbin==0) |
| 2838 | nbin=maxValue-minValue+1; |
| 2839 | scale=static_cast<double>(nbin-1)/(maxValue-minValue); |
| 2840 | } |
| 2841 | else |
| 2842 | nbin=1; |
| 2843 | //todo: replace assert with exception |
| 2844 | assert(nbin>0); |
| 2845 | if(histvector.size()!=nbin){ |
| 2846 | histvector.resize(nbin); |
| 2847 | for(int i=0;i<nbin;histvector[i++]=0); |
| 2848 | } |
| 2849 | double nvalid=0; |
| 2850 | unsigned long int ninvalid=0; |
| 2851 | std::vector<double> lineBuffer(nrOfCol()); |
| 2852 | for(int irow=0;irow<nrOfRow();++irow){ |
| 2853 | readData(lineBuffer,irow,theBand); |
| 2854 | for(int icol=0;icol<nrOfCol();++icol){ |
| 2855 | if(isNoData(lineBuffer[icol])) |
| 2856 | ++ninvalid; |
| 2857 | else if(lineBuffer[icol]>maxValue) |
| 2858 | ++ninvalid; |
| 2859 | else if(lineBuffer[icol]<minValue) |
| 2860 | ++ninvalid; |
| 2861 | else if(nbin==1) |
| 2862 | ++histvector[0]; |
| 2863 | else{//scale to [0:nbin] |
| 2864 | if(sigma>0){ |
| 2865 | //create kde for Gaussian basis function |
| 2866 | //todo: speed up by calculating first and last bin with non-zero contriubtion... |
| 2867 | //todo: calculate real surface below pdf by using gsl_cdf_gaussian_P(x-mean+binsize,sigma)-gsl_cdf_gaussian_P(x-mean,sigma) |
| 2868 | for(int ibin=0;ibin<nbin;++ibin){ |
| 2869 | double icenter=minValue+static_cast<double>(maxValue-minValue)*(ibin+0.5)/nbin; |
| 2870 | double thePdf=gsl_ran_gaussian_pdf(lineBuffer[icol]-icenter, sigma); |
| 2871 | histvector[ibin]+=thePdf; |
| 2872 | nvalid+=thePdf; |
| 2873 | } |
| 2874 | } |
| 2875 | else{ |
| 2876 | int theBin=static_cast<unsigned long int>(scale*(lineBuffer[icol]-minValue)); |
| 2877 | //todo: replace assert with exception |
| 2878 | assert(theBin>=0); |
| 2879 | assert(theBin<nbin); |
| 2880 | ++histvector[theBin]; |
| 2881 | ++nvalid; |
| 2882 | } |
| 2883 | // else if(lineBuffer[icol]==maxValue) |
| 2884 | // ++histvector[nbin-1]; |
| 2885 | // else |
| 2886 | // ++histvector[static_cast<int>(static_cast<double>(lineBuffer[icol]-minValue)/(maxValue-minValue)*(nbin-1))]; |
| 2887 | } |
| 2888 | } |
| 2889 | } |
| 2890 | // unsigned long int nvalid=nrOfCol()*nrOfRow()-ninvalid; |
| 2891 | return nvalid; |
| 2892 | } |
| 2893 | |
| 2894 | /** |
| 2895 | * @param range Sorted vector containing the range of image values |
| 2896 | * @param band The band for which to calculate the range |
| 2897 | **/ |
| 2898 | void Jim::getRange(std::vector<short>& range, int band) |
| 2899 | { |
| 2900 | std::vector<short> lineBuffer(nrOfCol()); |
| 2901 | range.clear(); |
| 2902 | for(int irow=0;irow<nrOfRow();++irow){ |
| 2903 | readData(lineBuffer,irow,band); |
| 2904 | for(int icol=0;icol<nrOfCol();++icol){ |
| 2905 | if(find(range.begin(),range.end(),lineBuffer[icol])==range.end()) |
| 2906 | range.push_back(lineBuffer[icol]); |
| 2907 | } |
| 2908 | } |
| 2909 | sort(range.begin(),range.end()); |
| 2910 | } |
| 2911 | |
| 2912 | /** |
| 2913 | * @param band The band for which to calculate the number of valid pixels |
| 2914 | * @return number of valid pixels in this dataset for the the selected band |
| 2915 | **/ |
| 2916 | unsigned long int Jim::getNvalid(int band) |
| 2917 | { |
| 2918 | unsigned long int nvalid=0; |
| 2919 | if(m_noDataValues.size()){ |
| 2920 | std::vector<double> lineBuffer(nrOfCol()); |
| 2921 | for(int irow=0;irow<nrOfRow();++irow){ |
| 2922 | readData(lineBuffer,irow,band); |
| 2923 | for(int icol=0;icol<nrOfCol();++icol){ |
| 2924 | if(isNoData(lineBuffer[icol])) |
| 2925 | continue; |
| 2926 | else |
| 2927 | ++nvalid; |
| 2928 | } |
| 2929 | } |
| 2930 | return nvalid; |
| 2931 | } |
| 2932 | else |
| 2933 | return(nrOfCol()*nrOfRow()); |
| 2934 | } |
| 2935 | |
| 2936 | /** |
| 2937 | * @param band The band for which to calculate the number of valid pixels |
| 2938 | * @return number of invalid pixels in this dataset for the the selected band |
| 2939 | **/ |
| 2940 | unsigned long int Jim::getNinvalid(int band) |
| 2941 | { |
| 2942 | unsigned long int nvalid=0; |
| 2943 | if(m_noDataValues.size()){ |
| 2944 | std::vector<double> lineBuffer(nrOfCol()); |
| 2945 | for(int irow=0;irow<nrOfRow();++irow){ |
| 2946 | readData(lineBuffer,irow,band); |
| 2947 | for(int icol=0;icol<nrOfCol();++icol){ |
| 2948 | if(isNoData(lineBuffer[icol])) |
| 2949 | continue; |
| 2950 | else |
| 2951 | ++nvalid; |
| 2952 | } |
| 2953 | } |
| 2954 | return (nrOfCol()*nrOfRow())-nvalid; |
| 2955 | } |
| 2956 | else |
| 2957 | return(0); |
| 2958 | } |
| 2959 | |
| 2960 | /** |
| 2961 | * @param refX, refY Calculated reference pixel position in geo-refererenced coordinates |
| 2962 | * @param band The band for which to calculate the number of valid pixels |
| 2963 | **/ |
| 2964 | |
| 2965 | void Jim::getRefPix(double& refX, double &refY, int band) |
| 2966 | { |
| 2967 | std::vector<double> lineBuffer(nrOfCol()); |
| 2968 | double validCol=0; |
| 2969 | double validRow=0; |
| 2970 | int nvalidCol=0; |
| 2971 | int nvalidRow=0; |
| 2972 | for(int irow=0;irow<nrOfRow();++irow){ |
| 2973 | readData(lineBuffer,irow,band); |
| 2974 | for(int icol=0;icol<nrOfCol();++icol){ |
| 2975 | // bool valid=(find(m_noDataValues.begin(),m_noDataValues.end(),lineBuffer[icol])==m_noDataValues.end()); |
| 2976 | // if(valid){ |
| 2977 | if(!isNoData(lineBuffer[icol])){ |
| 2978 | validCol+=icol+1; |
| 2979 | ++nvalidCol; |
| 2980 | validRow+=irow+1; |
| 2981 | ++nvalidRow; |
| 2982 | } |
| 2983 | } |
| 2984 | } |
| 2985 | if(isGeoRef()){ |
| 2986 | //reference coordinate is lower left corner of pixel in center of gravity |
| 2987 | //we need geo coordinates for exactly this location: validCol(Row)/nvalidCol(Row)-0.5 |
| 2988 | double cgravi=validCol/nvalidCol-0.5; |
| 2989 | double cgravj=validRow/nvalidRow-0.5; |
| 2990 | double refpixeli=floor(cgravi); |
| 2991 | double refpixelj=ceil(cgravj-1); |
| 2992 | //but image2geo provides location at center of pixel (shifted half pixel right down) |
| 2993 | image2geo(refpixeli,refpixelj,refX,refY); |
| 2994 | //refX and refY now refer to center of gravity pixel |
| 2995 | refX-=0.5*getDeltaX();//shift to left corner |
| 2996 | refY-=0.5*getDeltaY();//shift to lower left corner |
| 2997 | } |
| 2998 | else{ |
| 2999 | refX=floor(validCol/nvalidCol-0.5);//left corner |
| 3000 | refY=floor(validRow/nvalidRow-0.5);//upper corner |
| 3001 | //shift to lower left corner of pixel |
| 3002 | refY+=1; |
| 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | //from Writer |
| 3007 | /** |
| 3008 | * @param row Write a new block for caching this row (if needed) |
| 3009 | * @param band Band that must be written in cache |
| 3010 | * @return true if write was successful |
| 3011 | **/ |
| 3012 | CPLErr Jim::writeNewBlock(int row, int band) |
| 3013 | { |
| 3014 | CPLErr returnValue=CE_None; |
| 3015 | if(m_gds == NULL){ |
| 3016 | std::string errorString="Error in writeNewBlock"; |
| 3017 | throw(errorString); |
| 3018 | } |
| 3019 | //todo: replace assert with exception |
| 3020 | //assert(row==m_end) |
| 3021 | if(m_end[band]>nrOfRow()) |
| 3022 | m_end[band]=nrOfRow(); |
| 3023 | //fetch raster band |
| 3024 | GDALRasterBand *poBand; |
| 3025 | if(nrOfBand()<=band){ |
| 3026 | std::string errorString="Error: band number exceeds available bands in writeNewBlock"; |
| 3027 | throw(errorString); |
| 3028 | } |
| 3029 | poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index |
| 3030 | try{ |
| 3031 | returnValue=poBand->RasterIO(GF_Write,0,m_begin[band],nrOfCol(),m_end[band]-m_begin[band],m_data[band],nrOfCol(),m_end[band]-m_begin[band],getGDALDataType(),0,0); |
| 3032 | } |
| 3033 | catch(...){ |
| 3034 | std::string errorString="Error: RasterIO failed in writeNewBlock"; |
| 3035 | throw(errorString); |
| 3036 | } |
| 3037 | if(m_begin[band]+m_blockSize<nrOfRow()){ |
| 3038 | m_begin[band]+=m_blockSize;//m_begin points to first line in block that will be written next |
| 3039 | m_end[band]=m_begin[band]+m_blockSize;//m_end points to last line in block that will be written next |
| 3040 | } |
| 3041 | if(m_end[band]>nrOfRow()) |
| 3042 | m_end[band]=nrOfRow(); |
| 3043 | return(returnValue);//new block was written |
| 3044 | } |
| 3045 | |
| 3046 | |
| 3047 | CPLErr Jim::write(){ |
| 3048 | //write, but do not reset |
| 3049 | CPLErr returnValue=CE_None; |
| 3050 | if(m_data.size()&&m_filename.size()){ |
| 3051 | for(int iband=0;iband<nrOfBand();++iband) |
| 3052 | writeNewBlock(nrOfRow(),iband); |
| 3053 | } |
| 3054 | char **papszOptions=NULL; |
| 3055 | for(std::vector<std::string>::const_iterator optionIt=m_coptions.begin();optionIt!=m_coptions.end();++optionIt) |
| 3056 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 3057 | if(papszOptions) |
| 3058 | CSLDestroy(papszOptions); |
| 3059 | if(m_gds) |
| 3060 | GDALClose(m_gds); |
| 3061 | m_gds=0; |
| 3062 | // reset(); |
| 3063 | return(returnValue);//GDAL was closed |
| 3064 | } |
| 3065 | |
| 3066 | CPLErr Jim::writeDataPlanes(){ |
| 3067 | CPLErr returnValue=CE_None; |
| 3068 | if(m_gds == NULL){ |
| 3069 | std::string errorString="Error in writeDataPlanes: object contains no gdal dataset"; |
| 3070 | throw(errorString); |
| 3071 | } |
| 3072 | char **papszOptions=NULL; |
| 3073 | for(std::vector<std::string>::const_iterator optionIt=m_coptions.begin();optionIt!=m_coptions.end();++optionIt) |
| 3074 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 3075 | if(papszOptions) |
| 3076 | CSLDestroy(papszOptions); |
| 3077 | |
| 3078 | std::vector<int> gdalbands(nrOfPlane()); |
| 3079 | for(int iplane=0;iplane<nrOfPlane();++iplane) |
| 3080 | gdalbands[iplane]=iplane+1;//gdal band numbers are 1 based (starting from 1) |
| 3081 | |
| 3082 | GDALRasterIOExtraArg sExtraArg; |
| 3083 | INIT_RASTERIO_EXTRA_ARG(sExtraArg); |
| 3084 | sExtraArg.bFloatingPointWindowValidity = FALSE; |
| 3085 | sExtraArg.dfXOff = 0; |
| 3086 | sExtraArg.dfYOff = 0; |
| 3087 | sExtraArg.dfXSize = 0; |
| 3088 | sExtraArg.dfYSize = 0; |
| 3089 | returnValue=getDataset()->RasterIO(GF_Write,0,0,nrOfCol(),nrOfRow(),m_data[0],nrOfCol(),nrOfRow(),getGDALDataType(),nrOfPlane(),&gdalbands[0],0,0,0,&sExtraArg); |
| 3090 | if(m_gds) |
| 3091 | GDALClose(m_gds); |
| 3092 | m_gds=0; |
| 3093 | return(returnValue); |
| 3094 | // reset(); |
| 3095 | } |
| 3096 | |
| 3097 | ///write to file without reset (keep data in memory) |
| 3098 | CPLErr Jim::write(app::AppFactory &app){ |
| 3099 | Optionjl<std::string> input_opt("fn", "filename", "filename"); |
| 3100 | Optionjl<std::string> oformat_opt("of", "oformat", "Output image format (see also gdal_translate).","GTiff"); |
| 3101 | Optionjl<std::string> coption_opt("co", "co", "Creation option for output file. Multiple options can be specified."); |
| 3102 | Optionjl<double> nodata_opt("nodata", "nodata", "Nodata value to put in image."); |
| 3103 | Optionjl<string> colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)"); |
| 3104 | Optionjl<string> description_opt("d", "description", "Set image description"); |
| 3105 | Optionjl<unsigned long int> memory_opt("mem", "mem", "Buffer size (in MB) to read image data blocks in memory",0,1); |
| 3106 | |
| 3107 | coption_opt.setHide(1); |
| 3108 | memory_opt.setHide(1); |
| 3109 | |
| 3110 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 3111 | doProcess=input_opt.retrieveOption(app); |
| 3112 | oformat_opt.retrieveOption(app); |
| 3113 | coption_opt.retrieveOption(app); |
| 3114 | nodata_opt.retrieveOption(app); |
| 3115 | colorTable_opt.retrieveOption(app); |
| 3116 | description_opt.retrieveOption(app); |
| 3117 | memory_opt.retrieveOption(app); |
| 3118 | if(!doProcess){ |
| 3119 | std::cout << std::endl; |
| 3120 | std::ostringstream helpStream; |
| 3121 | helpStream << "help info: "; |
| 3122 | throw(helpStream.str());//help was invoked, stop processing |
| 3123 | } |
| 3124 | std::vector<std::string> badKeys; |
| 3125 | app.badKeys(badKeys); |
| 3126 | if(badKeys.size()){ |
| 3127 | std::ostringstream errorStream; |
| 3128 | if(badKeys.size()>1) |
| 3129 | errorStream << "Error: unknown keys: "; |
| 3130 | else |
| 3131 | errorStream << "Error: unknown key: "; |
| 3132 | for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 3133 | errorStream << badKeys[ikey] << " "; |
| 3134 | } |
| 3135 | errorStream << std::endl; |
| 3136 | throw(errorStream.str()); |
| 3137 | } |
| 3138 | if(nodata_opt.size()) |
| 3139 | setNoData(nodata_opt); |
| 3140 | if(input_opt.size()){ |
| 3141 | CPLErr result=setFile(input_opt[0],oformat_opt[0],memory_opt[0],coption_opt); |
| 3142 | if(colorTable_opt.size()){ |
| 3143 | if(colorTable_opt[0]!="none"||colorTable_opt[0]!="None") |
| 3144 | setColorTable(colorTable_opt[0]); |
| 3145 | } |
| 3146 | if(description_opt.size()){ |
| 3147 | if(description_opt[0]!="none"||description_opt[0]!="None") |
| 3148 | setImageDescription(description_opt[0]); |
| 3149 | } |
| 3150 | write(); |
| 3151 | return(result); |
| 3152 | } |
| 3153 | else{ |
| 3154 | std::ostringstream helpStream; |
| 3155 | helpStream << "Warning: filename not set"; |
| 3156 | std::cerr << helpStream.str() << std::endl; |
| 3157 | throw(helpStream.str());//help was invoked, stop processing |
| 3158 | } |
| 3159 | } |
| 3160 | |
| 3161 | void Jim::setData(double value){ |
| 3162 | setData(value,0); |
| 3163 | for(size_t iband=1;iband<nrOfBand();++iband) |
| 3164 | memcpy(static_cast<uint_least8_t*>(m_data[iband]),static_cast<uint_least8_t*>(m_data[0]),getDataTypeSizeBytes()*nrOfCol()*m_blockSize*nrOfPlane()); |
| 3165 | } |
| 3166 | |
| 3167 | void Jim::setData(double value, int band){ |
| 3168 | if(m_data.empty()){ |
| 3169 | std::ostringstream s; |
| 3170 | s << "Error: Jim not initialized, m_data is empty"; |
| 3171 | std::cerr << s.str() << std::endl; |
| 3172 | throw(s.str()); |
| 3173 | } |
| 3174 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 3175 | #pragma omp parallel for |
| 3176 | #else |
| 3177 | #endif |
| 3178 | for(int irow=0;irow<nrOfRow();++irow){ |
| 3179 | int index=irow*nrOfCol(); |
| 3180 | int minindex=index; |
| 3181 | int maxindex=index+nrOfCol(); |
| 3182 | for(index=minindex;index<maxindex;++index){ |
| 3183 | double dvalue=value; |
| 3184 | switch(getDataType()){ |
| 3185 | case(GDT_Byte): |
| 3186 | static_cast<unsigned char*>(m_data[band])[index]=static_cast<unsigned char>(dvalue); |
| 3187 | break; |
| 3188 | case(GDT_Int16): |
| 3189 | static_cast<short*>(m_data[band])[index]=static_cast<short>(dvalue); |
| 3190 | break; |
| 3191 | case(GDT_UInt16): |
| 3192 | static_cast<unsigned short*>(m_data[band])[index]=static_cast<unsigned short>(dvalue); |
| 3193 | break; |
| 3194 | case(GDT_Int32): |
| 3195 | static_cast<int*>(m_data[band])[index]=static_cast<int>(dvalue); |
| 3196 | break; |
| 3197 | case(GDT_UInt32): |
| 3198 | static_cast<unsigned int*>(m_data[band])[index]=static_cast<unsigned int>(dvalue); |
| 3199 | break; |
| 3200 | case(GDT_Float32): |
| 3201 | static_cast<float*>(m_data[band])[index]=static_cast<float>(dvalue); |
| 3202 | break; |
| 3203 | case(GDT_Float64): |
| 3204 | static_cast<double*>(m_data[band])[index]=static_cast<double>(dvalue); |
| 3205 | break; |
| 3206 | default: |
| 3207 | std::string errorString="Error: data type not supported"; |
| 3208 | throw(errorString); |
| 3209 | break; |
| 3210 | } |
| 3211 | } |
| 3212 | } |
| 3213 | for(size_t iplane=1;iplane<nrOfPlane();++iplane) |
| 3214 | memcpy(static_cast<uint_least8_t*>(m_data[band])+iplane*getDataTypeSizeBytes()*nrOfCol()*nrOfRow(),static_cast<uint_least8_t*>(m_data[band]),getDataTypeSizeBytes()*nrOfCol()*nrOfRow()); |
| 3215 | } |
| 3216 | |
| 3217 | void Jim::setData(double value, double ulx, double uly, double lrx, double lry, int band, double dx, double dy, bool nogeo){ |
| 3218 | if(m_data.empty()){ |
| 3219 | std::ostringstream s; |
| 3220 | s << "Error: Jim not initialized, m_data is empty"; |
| 3221 | std::cerr << s.str() << std::endl; |
| 3222 | throw(s.str()); |
| 3223 | } |
| 3224 | size_t stride=1; |
| 3225 | |
| 3226 | double uli=0; |
| 3227 | double ulj=0; |
| 3228 | double lri=0; |
| 3229 | double lrj=0; |
| 3230 | double stridei=1; |
| 3231 | double stridej=1; |
| 3232 | if(nogeo){ |
| 3233 | uli=ulx; |
| 3234 | ulj=uly; |
| 3235 | lri=lrx; |
| 3236 | lrj=lry; |
| 3237 | if(dx>0) |
| 3238 | stridei=dx; |
| 3239 | else |
| 3240 | stridei=1; |
| 3241 | if(dy>0) |
| 3242 | stridej=dy; |
| 3243 | else |
| 3244 | stridej=1; |
| 3245 | } |
| 3246 | else{ |
| 3247 | if(dx<=0) |
| 3248 | dx=getDeltaX(); |
| 3249 | if(dy<=0) |
| 3250 | dy=getDeltaY(); |
| 3251 | //do align |
| 3252 | if(ulx>this->getUlx()) |
| 3253 | ulx-=fmod(ulx-this->getUlx(),dx); |
| 3254 | else if(ulx<this->getUlx()) |
| 3255 | ulx+=fmod(this->getUlx()-ulx,dx)-dx; |
| 3256 | if(lrx<this->getLrx()) |
| 3257 | lrx+=fmod(this->getLrx()-lrx,dx); |
| 3258 | else if(lrx>this->getLrx()) |
| 3259 | lrx-=fmod(lrx-this->getLrx(),dx)+dx; |
| 3260 | if(lry>this->getLry()) |
| 3261 | lry-=fmod(lry-this->getLry(),dy); |
| 3262 | else if(lry<this->getLry()) |
| 3263 | lry+=fmod(this->getLry()-lry,dy)-dy; |
| 3264 | if(uly<this->getUly()) |
| 3265 | uly+=fmod(this->getUly()-uly,dy); |
| 3266 | else if(uly>this->getUly()) |
| 3267 | uly-=fmod(uly-this->getUly(),dy)+dy; |
| 3268 | // this->geo2image(ulx-0.5*this->getDeltaX(),uly+0.5*this->getDeltaY(),uli,ulj); |
| 3269 | // this->geo2image(lrx-1.5*this->getDeltaX(),lry+1.5*this->getDeltaY(),lri,lrj); |
| 3270 | |
| 3271 | // uli=floor(uli); |
| 3272 | // ulj=floor(ulj); |
| 3273 | // lri=floor(lri); |
| 3274 | // lrj=floor(lrj); |
| 3275 | |
| 3276 | this->geo2image(ulx,uly,uli,ulj); |
| 3277 | this->geo2image(lrx-this->getDeltaX(),lry+this->getDeltaY(),lri,lrj); |
| 3278 | |
| 3279 | uli=floor(uli); |
| 3280 | ulj=floor(ulj); |
| 3281 | lri=floor(lri)+1; |
| 3282 | lrj=floor(lrj)+1; |
| 3283 | |
| 3284 | if(dx>0) |
| 3285 | stridei=dx/getDeltaX(); |
| 3286 | else |
| 3287 | stridei=1; |
| 3288 | if(dy>0) |
| 3289 | stridej=dy/getDeltaY(); |
| 3290 | else |
| 3291 | stridej=1; |
| 3292 | } |
| 3293 | //todo: we are working with lower right corner of pixels for lri lrj -> adapt code below |
| 3294 | std::ostringstream errorStream; |
| 3295 | if(uli<0||uli>nrOfCol()){ |
| 3296 | errorStream << "Error: columns requested out of bounding box" << std::endl; |
| 3297 | std::cerr << errorStream.str() << std::endl; |
| 3298 | throw(errorStream.str()); |
| 3299 | } |
| 3300 | if(lri<0||lri>nrOfCol()){ |
| 3301 | errorStream << "Error: columns requested out of bounding box" << std::endl; |
| 3302 | std::cerr << errorStream.str() << std::endl; |
| 3303 | throw(errorStream.str()); |
| 3304 | } |
| 3305 | if(ulj<0||ulj>nrOfRow()){ |
| 3306 | errorStream << "Error: rows requested out of bounding box" << std::endl; |
| 3307 | std::cerr << errorStream.str() << std::endl; |
| 3308 | throw(errorStream.str()); |
| 3309 | } |
| 3310 | if(lrj<0||lrj>nrOfRow()){ |
| 3311 | errorStream << "Error: rows requested out of bounding box" << std::endl; |
| 3312 | std::cerr << errorStream.str() << std::endl; |
| 3313 | throw(errorStream.str()); |
| 3314 | } |
| 3315 | for(int irow=ulj;irow<lrj;irow+=stridej){ |
| 3316 | int index=irow*nrOfCol(); |
| 3317 | int minindex=index+uli; |
| 3318 | int maxindex=index+lri; |
| 3319 | for(index=minindex;index<maxindex;index+=stridei){ |
| 3320 | double dvalue=value; |
| 3321 | switch(getDataType()){ |
| 3322 | case(GDT_Byte): |
| 3323 | static_cast<unsigned char*>(m_data[band])[index]=static_cast<unsigned char>(dvalue); |
| 3324 | break; |
| 3325 | case(GDT_Int16): |
| 3326 | static_cast<short*>(m_data[band])[index]=static_cast<short>(dvalue); |
| 3327 | break; |
| 3328 | case(GDT_UInt16): |
| 3329 | static_cast<unsigned short*>(m_data[band])[index]=static_cast<unsigned short>(dvalue); |
| 3330 | break; |
| 3331 | case(GDT_Int32): |
| 3332 | static_cast<int*>(m_data[band])[index]=static_cast<int>(dvalue); |
| 3333 | break; |
| 3334 | case(GDT_UInt32): |
| 3335 | static_cast<unsigned int*>(m_data[band])[index]=static_cast<unsigned int>(dvalue); |
| 3336 | break; |
| 3337 | case(GDT_Float32): |
| 3338 | static_cast<float*>(m_data[band])[index]=static_cast<float>(dvalue); |
| 3339 | break; |
| 3340 | case(GDT_Float64): |
| 3341 | static_cast<double*>(m_data[band])[index]=static_cast<double>(dvalue); |
| 3342 | break; |
| 3343 | default: |
| 3344 | std::string errorString="Error: data type not supported"; |
| 3345 | throw(errorString); |
| 3346 | break; |
| 3347 | } |
| 3348 | } |
| 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | ///Create a JSON string from a Jim image |
| 3353 | std::string Jim::jim2json() { |
| 3354 | Json::Value custom; |
| 3355 | |
| 3356 | // 1. Handle Json::Value using our centralized helper |
| 3357 | json_util::get_member(custom, "size") = static_cast<int>(1); |
| 3358 | |
| 3359 | int iimg = 0; |
| 3360 | Json::Value image; |
| 3361 | json_util::get_member(image, "path") = getFileName(); |
| 3362 | |
| 3363 | std::string wktString = getProjectionRef(); |
| 3364 | std::string key("EPSG"); |
| 3365 | std::size_t foundEPSG = wktString.rfind(key); |
| 3366 | |
| 3367 | if (foundEPSG != std::string::npos) { |
| 3368 | std::string fromEPSG = wktString.substr(foundEPSG); |
| 3369 | std::size_t foundFirstDigit = fromEPSG.find_first_of("0123456789"); |
| 3370 | std::size_t foundLastDigit = fromEPSG.find_last_of("0123456789"); |
| 3371 | |
| 3372 | if (foundFirstDigit != std::string::npos) { |
| 3373 | std::string epsgString = fromEPSG.substr(foundFirstDigit, foundLastDigit - foundFirstDigit + 1); |
| 3374 | try { |
| 3375 | json_util::get_member(image, "epsg") = std::stoi(epsgString); |
| 3376 | } catch (...) { |
| 3377 | // Fallback if stoi fails on malformed strings |
| 3378 | json_util::get_member(image, "epsg") = 0; |
| 3379 | } |
| 3380 | } |
| 3381 | } |
| 3382 | |
| 3383 | // Use the helper for the index key "0" |
| 3384 | json_util::get_member(custom, "0") = image; |
| 3385 | |
| 3386 | // 2. Handle StreamWriterBuilder |
| 3387 | // This is the specific source of your StreamWriterBuilderixERKSs error. |
| 3388 | Json::StreamWriterBuilder builder; |
| 3389 | builder[Json::String("indentation")] = ""; |
| 3390 | return Json::writeString(builder, custom); |
| 3391 | } |
| 3392 | |
| 3393 | std::shared_ptr<Jim> Jim::clone(bool copyData) { |
| 3394 | std::shared_ptr<Jim> pJim=std::dynamic_pointer_cast<Jim>(cloneImpl(copyData)); |
| 3395 | if(pJim){ |
| 3396 | return(pJim); |
| 3397 | } |
| 3398 | else{ |
| 3399 | std::cerr << "Warning: static pointer cast may slice object" << std::endl; |
| 3400 | return(std::static_pointer_cast<Jim>(cloneImpl(copyData))); |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | // /** |
| 3405 | // * @param filename Open a raster dataset with this filename |
| 3406 | // * @param imgSrc Use this source image as a template to copy image attributes |
| 3407 | // * @param options Creation options |
| 3408 | // **/ |
| 3409 | // void Jim::open(const std::string& filename, const ImgReaderGdal& imgSrc, const std::vector<std::string>& options) |
| 3410 | // { |
| 3411 | // m_ncol=imgSrc.nrOfCol(); |
| 3412 | // m_nrow=imgSrc.nrOfRow(); |
| 3413 | // m_nband=imgSrc.nrOfBand(); |
| 3414 | // m_dataType=imgSrc.getDataType(); |
| 3415 | // setFile(filename,imgSrc,options); |
| 3416 | // // m_filename=filename; |
| 3417 | // // m_options=options; |
| 3418 | // // setDriver(imgSrc); |
| 3419 | // } |
| 3420 | |
| 3421 | /** |
| 3422 | * @param filename Open a raster dataset with this filename |
| 3423 | * @param imgSrc Use this source image as a template to copy image attributes |
| 3424 | * @param options Creation options |
| 3425 | **/ |
| 3426 | CPLErr Jim::open(const std::string& filename, const Jim& imgSrc, const std::vector<std::string>& options) |
| 3427 | { |
| 3428 | return(open(filename,imgSrc,0,options)); |
| 3429 | } |
| 3430 | |
| 3431 | /** |
| 3432 | * @param filename Open a raster dataset with this filename |
| 3433 | * @param imgSrc Use this source image as a template to copy image attributes |
| 3434 | * @param memory Available memory to cache image raster data (in MB) |
| 3435 | * @param options Creation options |
| 3436 | **/ |
| 3437 | CPLErr Jim::open(const std::string& filename, const Jim& imgSrc, unsigned int memory, const std::vector<std::string>& options) |
| 3438 | { |
| 3439 | m_ncol=imgSrc.nrOfCol(); |
| 3440 | m_nrow=imgSrc.nrOfRow(); |
| 3441 | m_nband=imgSrc.nrOfBand(); |
| 3442 | m_nplane=imgSrc.nrOfPlane(); |
| 3443 | m_dataType=imgSrc.getDataType(); |
| 3444 | setProjection(imgSrc.getProjection()); |
| 3445 | copyGeoTransform(imgSrc); |
| 3446 | if(setFile(filename,imgSrc.getImageType(),memory,options)!=CE_None) |
| 3447 | return(CE_Failure); |
| 3448 | m_gds->SetMetadata(imgSrc.getMetadata()); |
| 3449 | if(imgSrc.getColorTable()!=NULL) |
| 3450 | setColorTable(imgSrc.getColorTable()); |
| 3451 | return(CE_None); |
| 3452 | } |
| 3453 | |
| 3454 | CPLErr Jim::open(Jim& imgSrc, bool copyData) |
| 3455 | { |
| 3456 | m_ncol=imgSrc.nrOfCol(); |
| 3457 | m_nrow=imgSrc.nrOfRow(); |
| 3458 | m_nband=imgSrc.nrOfBand(); |
| 3459 | m_nplane=imgSrc.nrOfPlane(); |
| 3460 | m_dataType=imgSrc.getDataType(); |
| 3461 | std::string srcProjection=imgSrc.getProjection(); |
| 3462 | if(srcProjection.size()) |
| 3463 | setProjection(srcProjection); |
| 3464 | copyGeoTransform(imgSrc); |
| 3465 | imgSrc.getNoDataValues(m_noDataValues); |
| 3466 | imgSrc.getScale(m_scale); |
| 3467 | imgSrc.getOffset(m_offset); |
| 3468 | initMem(0); |
| 3469 | for(int iband=0;iband<m_nband;++iband){ |
| 3470 | m_begin[iband]=0; |
| 3471 | m_end[iband]=m_begin[iband]+m_blockSize; |
| 3472 | if(copyData) |
| 3473 | imgSrc.copyData(m_data[iband],iband); |
| 3474 | } |
| 3475 | //todo: check if filename needs to be set, but as is it is used for writing, I don't think so. |
| 3476 | // if(imgSrc.getFileName()!=""){ |
| 3477 | // m_filename=imgSrc.getFileName(); |
| 3478 | // std::cerr << "Warning: filename not set, dataset not defined yet" << std::endl; |
| 3479 | // } |
| 3480 | return(CE_None); |
| 3481 | } |
| 3482 | |
| 3483 | /** |
| 3484 | * @param imgSrc Use this source image as a template to copy image attributes |
| 3485 | * @param copyData Copy data from source image when true |
| 3486 | **/ |
| 3487 | // CPLErr Jim::open(std::shared_ptr<Jim> imgSrc, bool copyData) |
| 3488 | // { |
| 3489 | // m_ncol=imgSrc->nrOfCol(); |
| 3490 | // m_nrow=imgSrc->nrOfRow(); |
| 3491 | // m_nband=imgSrc->nrOfBand(); |
| 3492 | // m_dataType=imgSrc->getDataType(); |
| 3493 | // setProjection(imgSrc->getProjection()); |
| 3494 | // copyGeoTransform(imgSrc); |
| 3495 | // imgSrc->getNoDataValues(m_noDataValues); |
| 3496 | // imgSrc->getScale(m_scale); |
| 3497 | // imgSrc->getOffset(m_offset); |
| 3498 | // if(m_filename!=""){ |
| 3499 | // m_writeMode=true; |
| 3500 | // registerDriver(); |
| 3501 | // } |
| 3502 | // else |
| 3503 | // m_writeMode=false; |
| 3504 | // initMem(0); |
| 3505 | // for(int iband=0;iband<m_nband;++iband){ |
| 3506 | // m_begin[iband]=0; |
| 3507 | // m_end[iband]=m_begin[iband]+m_blockSize; |
| 3508 | // if(copyData){ |
| 3509 | // std::vector<double> lineInput(nrOfCol()); |
| 3510 | // for(int iband=0;iband<nrOfBand();++iband){ |
| 3511 | // for(int irow=0;irow<nrOfRow();++irow){ |
| 3512 | // imgSrc->readData(lineInput,irow,iband,NEAR); |
| 3513 | // writeData(lineInput,irow,iband); |
| 3514 | // } |
| 3515 | // } |
| 3516 | // // imgSrc->copyData(m_data[iband],iband); |
| 3517 | // } |
| 3518 | // } |
| 3519 | // //todo: check if filename needs to be set, but as is it is used for writing, I don't think so. |
| 3520 | // // if(imgSrc->getFileName()!=""){ |
| 3521 | // // m_filename=imgSrc->getFileName(); |
| 3522 | // // std::cerr << "Warning: filename not set, dataset not defined yet" << std::endl; |
| 3523 | // // } |
| 3524 | // return(CE_None); |
| 3525 | // } |
| 3526 | |
| 3527 | // /** |
| 3528 | // * @param filename Open a raster dataset with this filename |
| 3529 | // * @param ncol Number of columns in image |
| 3530 | // * @param nrow Number of rows in image |
| 3531 | // * @param nband Number of bands in image |
| 3532 | // * @param dataType The data type of the image (one of the GDAL supported datatypes: GDT_Byte, GDT_[U]Int[16|32], GDT_Float[32|64]) |
| 3533 | // * @param imageType Image type. Currently only those formats where the drivers support the Create method can be written |
| 3534 | // * @param options Creation options |
| 3535 | // **/ |
| 3536 | // void Jim::open(const std::string& filename, int ncol, int nrow, unsigned int nband, const GDALDataType& dataType, const std::string& imageType, const std::vector<std::string>& options) |
| 3537 | // { |
| 3538 | // m_ncol = ncol; |
| 3539 | // m_nrow = nrow; |
| 3540 | // m_nband = nband; |
| 3541 | // m_dataType = dataType; |
| 3542 | // setFile(filename,imageType,options); |
| 3543 | // // m_filename = filename; |
| 3544 | // // m_options=options; |
| 3545 | // // setDriver(imageType); |
| 3546 | // } |
| 3547 | |
| 3548 | /** |
| 3549 | * @param filename Open a raster dataset with this filename |
| 3550 | * @param ncol Number of columns in image |
| 3551 | * @param nrow Number of rows in image |
| 3552 | * @param nband Number of bands in image |
| 3553 | * @param dataType The data type of the image (one of the GDAL supported datatypes: GDT_Byte, GDT_[U]Int[16|32], GDT_Float[32|64]) |
| 3554 | * @param imageType Image type. Currently only those formats where the drivers support the Create method can be written |
| 3555 | * @param memory Available memory to cache image raster data (in MB) |
| 3556 | * @param options Creation options |
| 3557 | **/ |
| 3558 | CPLErr Jim::open(const std::string& filename, int ncol, int nrow, int nband, const GDALDataType& dataType, const std::string& imageType, unsigned int memory, const std::vector<std::string>& options) |
| 3559 | { |
| 3560 | m_ncol = ncol; |
| 3561 | m_nrow = nrow; |
| 3562 | m_nband = nband; |
| 3563 | m_nplane = 1; |
| 3564 | m_dataType = dataType; |
| 3565 | return(setFile(filename,imageType,memory,options)); |
| 3566 | } |
| 3567 | |
| 3568 | /** |
| 3569 | * @param ncol Number of columns in image |
| 3570 | * @param nrow Number of rows in image |
| 3571 | * @param nband Number of bands in image |
| 3572 | * @param dataType The data type of the image (one of the GDAL supported datatypes: GDT_Byte, GDT_[U]Int[16|32], GDT_Float[32|64]) |
| 3573 | **/ |
| 3574 | CPLErr Jim::open(int ncol, int nrow, int nband, const GDALDataType& dataType) |
| 3575 | { |
| 3576 | m_ncol = ncol; |
| 3577 | m_nrow = nrow; |
| 3578 | m_nplane=1; |
| 3579 | m_nband = nband; |
| 3580 | m_dataType = dataType; |
| 3581 | initMem(0); |
| 3582 | for(int iband=0;iband<m_nband;++iband){ |
| 3583 | m_begin[iband]=0; |
| 3584 | m_end[iband]=m_begin[iband]+m_blockSize; |
| 3585 | } |
| 3586 | if(m_filename!=""){ |
| 3587 | // m_writeMode=true; |
| 3588 | m_access=WRITE; |
| 3589 | registerDriver(); |
| 3590 | } |
| 3591 | return(CE_None); |
| 3592 | } |
| 3593 | |
| 3594 | ///Open an image for writing |
| 3595 | CPLErr Jim::open(int ncol, int nrow, int nband, int nplane, const GDALDataType& dataType){ |
| 3596 | m_ncol=ncol; |
| 3597 | m_nrow=nrow; |
| 3598 | m_nplane=nplane; |
| 3599 | m_nband=nband; |
| 3600 | m_dataType=dataType; |
| 3601 | m_data.resize(m_nband); |
| 3602 | m_begin.resize(m_nband); |
| 3603 | m_end.resize(m_nband); |
| 3604 | m_blockSize=nrow;//memory contains entire image and has been read already |
| 3605 | initMem(0); |
| 3606 | for(int iband=0;iband<m_nband;++iband){ |
| 3607 | m_begin[iband]=0; |
| 3608 | m_end[iband]=m_begin[iband]+m_blockSize; |
| 3609 | } |
| 3610 | if(m_filename!=""){ |
| 3611 | // m_writeMode=true; |
| 3612 | m_access=WRITE; |
| 3613 | registerDriver(); |
| 3614 | } |
| 3615 | return(CE_None); |
| 3616 | } |
| 3617 | |
| 3618 | ///set file attributes for writing |
| 3619 | /** |
| 3620 | * @param filename Open a raster dataset with this filename |
| 3621 | * @param imageType Image type. Currently only those formats where the drivers support the Create method can be written |
| 3622 | **/ |
| 3623 | CPLErr Jim::setFile(const std::string& filename, const std::string& imageType, unsigned int memory, const std::vector<std::string>& options) |
| 3624 | { |
| 3625 | CPLErr returnValue=CE_None; |
| 3626 | m_access=WRITE; |
| 3627 | // m_writeMode=true; |
| 3628 | m_filename=filename; |
| 3629 | m_coptions=options; |
| 3630 | m_imageType=imageType; |
| 3631 | if(nrOfCol()&&nrOfRow()&&nrOfBand()){ |
| 3632 | registerDriver(); |
| 3633 | if(m_data.empty()) |
| 3634 | initMem(memory); |
| 3635 | for(int iband=0;iband<nrOfBand();++iband){ |
| 3636 | m_begin[iband]=0; |
| 3637 | m_end[iband]=m_begin[iband]+m_blockSize; |
| 3638 | } |
| 3639 | if(m_noDataValues.size()){ |
| 3640 | for(int iband=0;iband<nrOfBand();++iband){ |
| 3641 | returnValue=GDALSetNoDataValue(m_noDataValues[0],iband); |
| 3642 | if(returnValue!=CE_None) |
| 3643 | break; |
| 3644 | } |
| 3645 | } |
| 3646 | } |
| 3647 | return(returnValue); |
| 3648 | } |
| 3649 | |
| 3650 | ///Copy data |
| 3651 | CPLErr Jim::copyData(void* data, int band){ |
| 3652 | memcpy(static_cast<uint_least8_t*>(data),static_cast<uint_least8_t*>(m_data[band]),getDataTypeSizeBytes()*nrOfCol()*m_blockSize*nrOfPlane()); |
| 3653 | m_begin[band]=0; |
| 3654 | m_end[band]=nrOfRow(); |
| 3655 | return(CE_None); |
| 3656 | }; |
| 3657 | |
| 3658 | std::shared_ptr<Jim> Jim::createImg() { |
| 3659 | return(std::make_shared<Jim>()); |
| 3660 | }; |
| 3661 | |
| 3662 | std::shared_ptr<Jim> Jim::createImg(app::AppFactory &theApp){ |
| 3663 | std::shared_ptr<Jim> pJim=std::make_shared<Jim>(theApp); |
| 3664 | return(pJim); |
| 3665 | } |
| 3666 | |
| 3667 | std::shared_ptr<Jim> Jim::createImg(const std::shared_ptr<Jim> pSrc, bool copyData){ |
| 3668 | std::shared_ptr<Jim> pJim=std::make_shared<Jim>(*pSrc,copyData); |
| 3669 | return(pJim); |
| 3670 | } |
| 3671 | |
| 3672 | /* ///Create new shared pointer to Jim object */ |
| 3673 | /** |
| 3674 | * @param input (type: std::string) input filename |
| 3675 | * @return shared pointer to new Jim object |
| 3676 | **/ |
| 3677 | std::shared_ptr<Jim> Jim::createImg(const std::string filename, bool readData, unsigned int memory){ |
| 3678 | std::shared_ptr<Jim> pJim=std::make_shared<Jim>(filename,readData,memory); |
| 3679 | // std::shared_ptr<Jim> pJim=std::make_shared<Jim>(filename,memory); |
| 3680 | return(pJim); |
| 3681 | } |
| 3682 | |
| 3683 | /** |
| 3684 | * @param metadata Set this metadata when writing the image (if supported byt the driver) |
| 3685 | **/ |
| 3686 | CPLErr Jim::setMetadata(char** metadata) |
| 3687 | { |
| 3688 | if(m_gds){ |
| 3689 | m_gds->SetMetadata(metadata); |
| 3690 | return(CE_None); |
| 3691 | } |
| 3692 | else |
| 3693 | return(CE_Warning); |
| 3694 | } |
| 3695 | |
| 3696 | //default projection: ETSR-LAEA |
| 3697 | // std::string Jim::setProjection(void) |
| 3698 | // { |
| 3699 | // std::string theProjection; |
| 3700 | // OGRSpatialReference oSRS; |
| 3701 | // char *pszSRS_WKT = NULL; |
| 3702 | // //// oSRS.importFromEPSG(3035); |
| 3703 | // oSRS.SetGeogCS("ETRS89","European_Terrestrial_Reference_System_1989","GRS 1980",6378137,298.2572221010042,"Greenwich",0,"degree",0.0174532925199433); |
| 3704 | // // cout << setprecision(16) << "major axis: " << oSRS.GetSemiMajor(NULL) << endl;//notice that major axis can be set to a different value than the default to the well known standard corresponding to the name (European_Terrestrial_Reference_System_1989), but that new value, while recognized by GetSemiMajor, will not be written in the geotiff tag! |
| 3705 | // oSRS.SetProjCS( "ETRS89 / ETRS-LAEA" ); |
| 3706 | // oSRS.SetLAEA(52,10,4321000,3210000); |
| 3707 | // oSRS.exportToWkt( &pszSRS_WKT ); |
| 3708 | // theProjection=pszSRS_WKT; |
| 3709 | // CPLFree( pszSRS_WKT ); |
| 3710 | // assert(m_gds); |
| 3711 | // m_gds->SetProjection(theProjection.c_str()); |
| 3712 | // return(theProjection); |
| 3713 | // } |
| 3714 | |
| 3715 | |
| 3716 | /** |
| 3717 | * @param filename ASCII file containing 5 columns: index R G B ALFA (0:transparent, 255:solid) |
| 3718 | * @param band band number to set color table (starting counting from 0) |
| 3719 | **/ |
| 3720 | CPLErr Jim::setColorTable(const std::string& filename, int band) |
| 3721 | { |
| 3722 | //todo: fool proof table in file (no checking currently done...) |
| 3723 | std::ifstream ftable(filename.c_str(),std::ios::in); |
| 3724 | std::string line; |
| 3725 | // poCT=new GDALColorTable(); |
| 3726 | GDALColorTable colorTable; |
| 3727 | short nline=0; |
| 3728 | while(getline(ftable,line)){ |
| 3729 | ++nline; |
| 3730 | std::istringstream ist(line); |
| 3731 | GDALColorEntry sEntry; |
| 3732 | short id; |
| 3733 | ist >> id >> sEntry.c1 >> sEntry.c2 >> sEntry.c3 >> sEntry.c4; |
| 3734 | colorTable.SetColorEntry(id,&sEntry); |
| 3735 | } |
| 3736 | if(m_gds){ |
| 3737 | (m_gds->GetRasterBand(band+1))->SetColorTable(&colorTable); |
| 3738 | return(CE_None); |
| 3739 | } |
| 3740 | else |
| 3741 | return(CE_Warning); |
| 3742 | } |
| 3743 | |
| 3744 | /** |
| 3745 | * @param colorTable Instance of the GDAL class GDALColorTable |
| 3746 | * @param band band number to set color table (starting counting from 0) |
| 3747 | **/ |
| 3748 | CPLErr Jim::setColorTable(GDALColorTable* colorTable, int band) |
| 3749 | { |
| 3750 | if(m_gds){ |
| 3751 | (m_gds->GetRasterBand(band+1))->SetColorTable(colorTable); |
| 3752 | return(CE_None); |
| 3753 | } |
| 3754 | else |
| 3755 | return(CE_Warning); |
| 3756 | } |
| 3757 | |
| 3758 | // //write an entire image from memory to file |
| 3759 | // bool Jim::writeData(void* pdata, const GDALDataType& dataType, int band){ |
| 3760 | // //fetch raster band |
| 3761 | // GDALRasterBand *poBand; |
| 3762 | // if(band>=nrOfBand()+1){ |
| 3763 | // std::ostringstream s; |
| 3764 | // s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")"; |
| 3765 | // throw(s.str()); |
| 3766 | // } |
| 3767 | // poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index |
| 3768 | // poBand->RasterIO(GF_Write,0,0,nrOfCol(),nrOfRow(),pdata,nrOfCol(),nrOfRow(),dataType,0,0); |
| 3769 | // return true; |
| 3770 | // } |
| 3771 | |
| 3772 | /** |
| 3773 | * @param ogrReader Vector dataset as an instance of the ImgReaderOgr that must be rasterized |
| 3774 | * @param burnValues Values to burn into raster cells (one value for each band) |
| 3775 | * @param eoption special options controlling rasterization (ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG) |
| 3776 | * "ATTRIBUTE": |
| 3777 | * Identifies an attribute field on the features to be used for a burn in value. The value will be burned into all output bands. If specified, padfLayerBurnValues will not be used and can be a NULL pointer. |
| 3778 | * "CHUNKYSIZE": |
| 3779 | * The height in lines of the chunk to operate on. The larger the chunk size the less times we need to make a pass through all the shapes. If it is not set or set to zero the default chunk size will be used. Default size will be estimated based on the GDAL cache buffer size using formula: cache_size_bytes/scanline_size_bytes, so the chunk will not exceed the cache. |
| 3780 | * "ALL_TOUCHED": |
| 3781 | * May be set to TRUE to set all pixels touched by the line or polygons, not just those whose center is within the polygon or that are selected by brezenhams line algorithm. Defaults to FALSE. |
| 3782 | "BURN_VALUE_ |
| 3783 | * May be set to "Z" to use the Z values of the geometries. The value from padfLayerBurnValues or the attribute field value is added to this before burning. In default case dfBurnValue is burned as it is. This is implemented properly only for points and lines for now. Polygons will be burned using the Z value from the first point. The M value may be supported in the future. |
| 3784 | * "MERGE_ALG": |
| 3785 | * May be REPLACE (the default) or ADD. REPLACE results in overwriting of value, while ADD adds the new value to the existing raster, suitable for heatmaps for instance. |
| 3786 | * @param layernames Names of the vector dataset layers to process. Leave empty to process all layers |
| 3787 | **/ |
| 3788 | // CPLErr Jim::rasterizeOgr(ImgReaderOgr& ogrReader, const std::vector<double>& burnValues, const std::vector<std::string>& eoption, const std::vector<std::string>& layernames ){ |
| 3789 | // std::vector<int> bands; |
| 3790 | // if(burnValues.empty()&&eoption.empty()){ |
| 3791 | // std::string errorString="Error: either burn values or control options must be provided"; |
| 3792 | // throw(errorString); |
| 3793 | // } |
| 3794 | // for(int iband=0;iband<nrOfBand();++iband) |
| 3795 | // bands.push_back(iband+1); |
| 3796 | // std::vector<OGRLayerH> layers; |
| 3797 | // int nlayer=0; |
| 3798 | |
| 3799 | // std::vector<double> burnBands;//burn values for all bands in a single layer |
| 3800 | // std::vector<double> burnLayers;//burn values for all bands and all layers |
| 3801 | // if(burnValues.size()){ |
| 3802 | // burnBands=burnValues; |
| 3803 | // while(burnBands.size()<nrOfBand()) |
| 3804 | // burnBands.push_back(burnValues[0]); |
| 3805 | // } |
| 3806 | // for(int ilayer=0;ilayer<ogrReader.getLayerCount();++ilayer){ |
| 3807 | // std::string currentLayername=ogrReader.getLayer(ilayer)->GetName(); |
| 3808 | // if(layernames.size()) |
| 3809 | // if(find(layernames.begin(),layernames.end(),currentLayername)==layernames.end()) |
| 3810 | // continue; |
| 3811 | // std::cout << "processing layer " << currentLayername << std::endl; |
| 3812 | // layers.push_back((OGRLayerH)ogrReader.getLayer(ilayer)); |
| 3813 | // ++nlayer; |
| 3814 | // if(burnValues.size()){ |
| 3815 | // for(int iband=0;iband<nrOfBand();++iband) |
| 3816 | // burnLayers.insert(burnLayers.end(),burnBands.begin(),burnBands.end()); |
| 3817 | // } |
| 3818 | // } |
| 3819 | // void* pTransformArg=NULL; |
| 3820 | // GDALProgressFunc pfnProgress=NULL; |
| 3821 | // void* pProgressArg=NULL; |
| 3822 | |
| 3823 | // char **coptions=NULL; |
| 3824 | // for(std::vector<std::string>::const_iterator optionIt=eoption.begin();optionIt!=eoption.end();++optionIt) |
| 3825 | // coptions=CSLAddString(coptions,optionIt->c_str()); |
| 3826 | |
| 3827 | // if(eoption.size()){ |
| 3828 | // if(GDALRasterizeLayers( (GDALDatasetH)m_gds,nrOfBand(),&(bands[0]),layers.size(),&(layers[0]),NULL,pTransformArg,NULL,coptions,pfnProgress,pProgressArg)!=CE_None){ |
| 3829 | // std::string errorString(CPLGetLastErrorMsg()); |
| 3830 | // throw(errorString); |
| 3831 | // } |
| 3832 | // } |
| 3833 | // else if(burnValues.size()){ |
| 3834 | // if(GDALRasterizeLayers( (GDALDatasetH)m_gds,nrOfBand(),&(bands[0]),layers.size(),&(layers[0]),NULL,pTransformArg,&(burnLayers[0]),NULL,pfnProgress,pProgressArg)!=CE_None){ |
| 3835 | // std::string errorString(CPLGetLastErrorMsg()); |
| 3836 | // throw(errorString); |
| 3837 | // } |
| 3838 | // } |
| 3839 | // else{ |
| 3840 | // std::string errorString="Error: either attribute fieldname or burn values must be set to rasterize vector dataset"; |
| 3841 | // throw(errorString); |
| 3842 | // } |
| 3843 | // //do not overwrite m_gds with what is in m_data |
| 3844 | // m_access=READ_ONLY; |
| 3845 | // // m_writeMode=false; |
| 3846 | // return(CE_None); |
| 3847 | // } |
| 3848 | |
| 3849 | // /** |
| 3850 | // * @param ogrReader Vector dataset as an instance of the ImgReaderOgr that must be rasterized |
| 3851 | // * @param burnValues Values to burn into raster cells (one value for each band) |
| 3852 | // * @param layernames Names of the vector dataset layers to process. Leave empty to process all layers |
| 3853 | // **/ |
| 3854 | // CPLErr Jim::rasterizeBuf(ImgReaderOgr& ogrReader, double burnValue, const std::vector<std::string>& layernames ){ |
| 3855 | // if(m_blockSize<nrOfRow()){ |
| 3856 | // std::ostringstream s; |
| 3857 | // s << "Error: increase memory to perform rasterize in entirely in buffer (now at " << 100.0*m_blockSize/nrOfRow() << "%)"; |
| 3858 | // throw(s.str()); |
| 3859 | // } |
| 3860 | // std::vector<OGRLayerH> layers; |
| 3861 | // int nlayer=0; |
| 3862 | |
| 3863 | // std::vector<double> burnBands;//burn values for all bands in a single layer |
| 3864 | // while(burnBands.size()<nrOfBand()) |
| 3865 | // burnBands.push_back(burnValue); |
| 3866 | // for(int ilayer=0;ilayer<ogrReader.getLayerCount();++ilayer){ |
| 3867 | // std::string currentLayername=ogrReader.getLayer(ilayer)->GetName(); |
| 3868 | // if(layernames.size()) |
| 3869 | // if(find(layernames.begin(),layernames.end(),currentLayername)==layernames.end()) |
| 3870 | // continue; |
| 3871 | // std::cout << "processing layer " << currentLayername << std::endl; |
| 3872 | // layers.push_back((OGRLayerH)ogrReader.getLayer(ilayer)); |
| 3873 | // ++nlayer; |
| 3874 | // } |
| 3875 | // void* pTransformArg=NULL; |
| 3876 | // GDALProgressFunc pfnProgress=NULL; |
| 3877 | // void* pProgressArg=NULL; |
| 3878 | |
| 3879 | // for(int iband=0;iband<nrOfBand();++iband){ |
| 3880 | // Vector2d<double> initBlock(nrOfRow(),nrOfCol()); |
| 3881 | // writeDataBlock(initBlock,0,nrOfCol()-1,0,nrOfRow()-1,iband); |
| 3882 | // double gt[6]; |
| 3883 | // getGeoTransform(gt); |
| 3884 | // if(GDALRasterizeLayersBuf(m_data[iband],nrOfCol(),nrOfRow(),getGDALDataType(),getDataTypeSizeBytes(),0,layers.size(),&(layers[0]), getProjectionRef().c_str(),gt,NULL, pTransformArg, burnBands[iband],NULL,pfnProgress,pProgressArg)!=CE_None){ |
| 3885 | // std::string errorString(CPLGetLastErrorMsg()); |
| 3886 | // throw(errorString); |
| 3887 | // } |
| 3888 | // } |
| 3889 | // return(CE_None); |
| 3890 | // } |
| 3891 | |
| 3892 | // CPLErr Jim::rasterizeBuf(const std::string& ogrFilename){ |
| 3893 | // VectorOgr ogrReader; |
| 3894 | // std::vector<std::string> layernames; |
| 3895 | // layernames.clear(); |
| 3896 | // ogrReader.open(ogrFilename,layernames,true); |
| 3897 | // CPLErr retValue=CE_None; |
| 3898 | // retValue=rasterizeBuf(ogrReader,1); |
| 3899 | // ogrReader.close(); |
| 3900 | // return(retValue); |
| 3901 | // } |
| 3902 | |
| 3903 | |
| 3904 | //todo: support transform |
| 3905 | ///Warning: this function cannot be exported via SWIG to Python as such, as it is destructive |
| 3906 | |
| 3907 | // CPLErr Jim::rasterizeBuf(VectorOgr& ogrReader, app::AppFactory &app){ |
| 3908 | // // CPLErr Jim::rasterizeBuf(VectorOgr& ogrReader, double burnValue, const std::vector<std::string>& eoption, const std::vector<std::string>& layernames ){ |
| 3909 | // Optionjl<double> burn_opt("burn", "burn", "burn value", 1); |
| 3910 | // Optionjl<string> eoption_opt("eo","eo", "special extent options controlling rasterization: ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG, e.g., -eo ALL_TOUCHED=TRUE"); |
| 3911 | // Optionjl<string> layernames_opt("ln", "ln", "Layer names"); |
| 3912 | // bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 3913 | // try{ |
| 3914 | // doProcess=burn_opt.retrieveOption(app); |
| 3915 | // eoption_opt.retrieveOption(app); |
| 3916 | // layernames_opt.retrieveOption(app); |
| 3917 | // } |
| 3918 | // catch(std::string predefinedString){ |
| 3919 | // std::cout << predefinedString << std::endl; |
| 3920 | // } |
| 3921 | // if(!doProcess){ |
| 3922 | // std::cout << std::endl; |
| 3923 | // std::ostringstream helpStream; |
| 3924 | // helpStream << "exception thrown due to help info"; |
| 3925 | // throw(helpStream.str());//help was invoked, stop processing |
| 3926 | // } |
| 3927 | // return rasterizeBuf(ogrReader, burn_opt[0], eoption_opt, layernames_opt); |
| 3928 | // } |
| 3929 | |
| 3930 | // CPLErr Jim::rasterizeBuf(VectorOgr& ogrReader, double burnValue, const std::vector<std::string>& eoption, const std::vector<std::string>& layernames ){ |
| 3931 | // if(m_blockSize<nrOfRow()){ |
| 3932 | // std::ostringstream s; |
| 3933 | // s << "Error: increase memory to perform rasterize in entirely in buffer (now at " << 100.0*m_blockSize/nrOfRow() << "%)"; |
| 3934 | // throw(s.str()); |
| 3935 | // } |
| 3936 | // std::vector<OGRLayerH> layers; |
| 3937 | // if(layernames.size()){ |
| 3938 | // for(std::vector<std::string>::const_iterator nit=layernames.begin();nit!=layernames.end();++nit) |
| 3939 | // layers.push_back((OGRLayerH)ogrReader.getLayer(*nit)); |
| 3940 | // } |
| 3941 | // else{ |
| 3942 | // for(size_t ilayer=0;ilayer<ogrReader.getLayerCount();++ilayer) |
| 3943 | // layers.push_back((OGRLayerH)ogrReader.getLayer(ilayer)); |
| 3944 | // } |
| 3945 | // void* pTransformArg=NULL; |
| 3946 | // GDALProgressFunc pfnProgress=NULL; |
| 3947 | // void* pProgressArg=NULL; |
| 3948 | |
| 3949 | // char **coptions=NULL; |
| 3950 | // for(std::vector<std::string>::const_iterator optionIt=eoption.begin();optionIt!=eoption.end();++optionIt) |
| 3951 | // coptions=CSLAddString(coptions,optionIt->c_str()); |
| 3952 | |
| 3953 | // // #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 3954 | // // #pragma omp parallel for |
| 3955 | // // #else |
| 3956 | // // #endif |
| 3957 | // for(int iband=0;iband<nrOfBand();++iband){ |
| 3958 | // Vector2d<double> initBlock(nrOfRow(),nrOfCol()); |
| 3959 | // writeDataBlock(initBlock,0,nrOfCol()-1,0,nrOfRow()-1,iband); |
| 3960 | // vector<double> gt(6); |
| 3961 | // getGeoTransform(gt); |
| 3962 | // if(GDALRasterizeLayersBuf(m_data[iband],nrOfCol(),nrOfRow(),getGDALDataType(),getDataTypeSizeBytes(),0,layers.size(),&(layers[0]), getProjectionRef().c_str(),>[0],NULL, pTransformArg, burnValue, coptions,pfnProgress,pProgressArg)!=CE_None){ |
| 3963 | // std::string errorString(CPLGetLastErrorMsg()); |
| 3964 | // throw(errorString); |
| 3965 | // } |
| 3966 | // } |
| 3967 | // return(CE_None); |
| 3968 | // } |
| 3969 | |
| 3970 | void Jim::d_rasterizeBuf(VectorOgr& ogrReader, app::AppFactory &app){ |
| 3971 | Optionjl<double> burn_opt("burn", "burn", "burn value", 1); |
| 3972 | Optionjl<string> eoption_opt("eo","eo", "special extent options controlling rasterization: ATTRIBUTE|CHUNKYSIZE|ALL_TOUCHED|BURN_VALUE_FROM|MERGE_ALG, e.g., -eo ALL_TOUCHED=TRUE"); |
| 3973 | Optionjl<string> layernames_opt("ln", "ln", "Layer names"); |
| 3974 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 3975 | try{ |
| 3976 | doProcess=burn_opt.retrieveOption(app); |
| 3977 | eoption_opt.retrieveOption(app); |
| 3978 | layernames_opt.retrieveOption(app); |
| 3979 | } |
| 3980 | catch(std::string predefinedString){ |
| 3981 | std::cout << predefinedString << std::endl; |
| 3982 | } |
| 3983 | if(!doProcess){ |
| 3984 | std::cout << std::endl; |
| 3985 | std::ostringstream helpStream; |
| 3986 | helpStream << "exception thrown due to help info"; |
| 3987 | throw(helpStream.str());//help was invoked, stop processing |
| 3988 | } |
| 3989 | try{ |
| 3990 | doProcess=burn_opt.retrieveOption(app); |
| 3991 | eoption_opt.retrieveOption(app); |
| 3992 | layernames_opt.retrieveOption(app); |
| 3993 | } |
| 3994 | catch(std::string predefinedString){ |
| 3995 | std::cout << predefinedString << std::endl; |
| 3996 | } |
| 3997 | if(!doProcess){ |
| 3998 | std::cout << std::endl; |
| 3999 | std::ostringstream helpStream; |
| 4000 | helpStream << "exception thrown due to help info"; |
| 4001 | throw(helpStream.str());//help was invoked, stop processing |
| 4002 | } |
| 4003 | d_rasterizeBuf(ogrReader, burn_opt[0], eoption_opt, layernames_opt); |
| 4004 | } |
| 4005 | |
| 4006 | void Jim::d_rasterizeBuf(VectorOgr& ogrReader, double burnValue, const std::vector<std::string>& eoption, const std::vector<std::string>& layernames ){ |
| 4007 | if(m_blockSize<nrOfRow()){ |
| 4008 | std::ostringstream s; |
| 4009 | s << "Error: increase memory to perform rasterize in entirely in buffer (now at " << 100.0*m_blockSize/nrOfRow() << "%)"; |
| 4010 | throw(s.str()); |
| 4011 | } |
| 4012 | std::vector<OGRLayerH> layers; |
| 4013 | if(layernames.size()){ |
| 4014 | for(std::vector<std::string>::const_iterator nit=layernames.begin();nit!=layernames.end();++nit) |
| 4015 | layers.push_back((OGRLayerH)ogrReader.getLayer(*nit)); |
| 4016 | } |
| 4017 | else{ |
| 4018 | for(size_t ilayer=0;ilayer<ogrReader.getLayerCount();++ilayer) |
| 4019 | layers.push_back((OGRLayerH)ogrReader.getLayer(ilayer)); |
| 4020 | } |
| 4021 | void* pTransformArg=NULL; |
| 4022 | GDALProgressFunc pfnProgress=NULL; |
| 4023 | void* pProgressArg=NULL; |
| 4024 | |
| 4025 | char **coptions=NULL; |
| 4026 | for(std::vector<std::string>::const_iterator optionIt=eoption.begin();optionIt!=eoption.end();++optionIt) |
| 4027 | coptions=CSLAddString(coptions,optionIt->c_str()); |
| 4028 | |
| 4029 | // #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4030 | // #pragma omp parallel for |
| 4031 | // #else |
| 4032 | // #endif |
| 4033 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4034 | Vector2d<double> initBlock(nrOfRow(),nrOfCol()); |
| 4035 | writeDataBlock(initBlock,0,nrOfCol()-1,0,nrOfRow()-1,iband); |
| 4036 | vector<double> gt(6); |
| 4037 | getGeoTransform(gt); |
| 4038 | if(GDALRasterizeLayersBuf(m_data[iband],nrOfCol(),nrOfRow(),getGDALDataType(),getDataTypeSizeBytes(),0,layers.size(),&(layers[0]), getProjectionRef().c_str(),>[0],NULL, pTransformArg, burnValue, coptions,pfnProgress,pProgressArg)!=CE_None){ |
| 4039 | std::string errorString(CPLGetLastErrorMsg()); |
| 4040 | throw(errorString); |
| 4041 | } |
| 4042 | } |
| 4043 | } |
| 4044 | |
| 4045 | /** |
| 4046 | * |
| 4047 | * |
| 4048 | * @param t1 minimum threshold |
| 4049 | * @param t2 maximum threshold |
| 4050 | * |
| 4051 | * @return output image |
| 4052 | */ |
| 4053 | std::shared_ptr<Jim> Jim::setThreshold(double t1, double t2){ |
| 4054 | std::shared_ptr<Jim> imgWriter=Jim::createImg(); |
| 4055 | setThreshold(*imgWriter, t1, t2); |
| 4056 | return(imgWriter); |
| 4057 | } |
| 4058 | |
| 4059 | /** |
| 4060 | * |
| 4061 | * |
| 4062 | * @param t1 minimum threshold |
| 4063 | * @param t2 maximum threshold |
| 4064 | * |
| 4065 | * @return output image |
| 4066 | */ |
| 4067 | std::shared_ptr<Jim> Jim::setAbsThreshold(double t1, double t2){ |
| 4068 | std::shared_ptr<Jim> imgWriter=Jim::createImg(); |
| 4069 | setAbsThreshold(*imgWriter, t1, t2); |
| 4070 | return(imgWriter); |
| 4071 | } |
| 4072 | |
| 4073 | void Jim::setThreshold(Jim& imgWriter, double t1, double t2){ |
| 4074 | try{ |
| 4075 | imgWriter.open(*this,false); |
| 4076 | if(m_noDataValues.empty()){ |
| 4077 | std::string errorString="Error: no data value not set"; |
| 4078 | throw(errorString); |
| 4079 | } |
| 4080 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4081 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4082 | #pragma omp parallel for |
| 4083 | #else |
| 4084 | #endif |
| 4085 | for(int irow=0;irow<nrOfRow();++irow){ |
| 4086 | std::vector<double> lineInput(nrOfCol()); |
| 4087 | readData(lineInput,irow,iband); |
| 4088 | for(int icol=0;icol<nrOfCol();++icol){ |
| 4089 | if(lineInput[icol]>=t1&&lineInput[icol]<=t2) |
| 4090 | continue; |
| 4091 | else |
| 4092 | lineInput[icol]=m_noDataValues[0]; |
| 4093 | } |
| 4094 | imgWriter.writeData(lineInput,irow,iband); |
| 4095 | } |
| 4096 | } |
| 4097 | } |
| 4098 | catch(std::string errorstring){ |
| 4099 | std::cerr << errorstring << std::endl; |
| 4100 | throw; |
| 4101 | } |
| 4102 | catch(...){ |
| 4103 | throw; |
| 4104 | } |
| 4105 | } |
| 4106 | |
| 4107 | void Jim::setThresholdMin(Jim& imgWriter, double minThreshold){ |
| 4108 | try{ |
| 4109 | imgWriter.open(*this,false); |
| 4110 | if(m_noDataValues.empty()){ |
| 4111 | std::string errorString="Error: no data value not set"; |
| 4112 | throw(errorString); |
| 4113 | } |
| 4114 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4115 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4116 | #pragma omp parallel for |
| 4117 | #else |
| 4118 | #endif |
| 4119 | for(int irow=0;irow<nrOfRow();++irow){ |
| 4120 | std::vector<double> lineInput(nrOfCol()); |
| 4121 | readData(lineInput,irow,iband); |
| 4122 | for(int icol=0;icol<nrOfCol();++icol){ |
| 4123 | if(lineInput[icol]>=minThreshold) |
| 4124 | continue; |
| 4125 | else |
| 4126 | lineInput[icol]=m_noDataValues[0]; |
| 4127 | } |
| 4128 | imgWriter.writeData(lineInput,irow,iband); |
| 4129 | } |
| 4130 | } |
| 4131 | } |
| 4132 | catch(std::string errorstring){ |
| 4133 | std::cerr << errorstring << std::endl; |
| 4134 | throw; |
| 4135 | } |
| 4136 | catch(...){ |
| 4137 | throw; |
| 4138 | } |
| 4139 | } |
| 4140 | |
| 4141 | void Jim::setThresholdMax(Jim& imgWriter, double maxThreshold){ |
| 4142 | try{ |
| 4143 | imgWriter.open(*this,false); |
| 4144 | if(m_noDataValues.empty()){ |
| 4145 | std::string errorString="Error: no data value not set"; |
| 4146 | throw(errorString); |
| 4147 | } |
| 4148 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4149 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4150 | #pragma omp parallel for |
| 4151 | #else |
| 4152 | #endif |
| 4153 | for(int irow=0;irow<nrOfRow();++irow){ |
| 4154 | std::vector<double> lineInput(nrOfCol()); |
| 4155 | readData(lineInput,irow,iband); |
| 4156 | for(int icol=0;icol<nrOfCol();++icol){ |
| 4157 | if(lineInput[icol]<=maxThreshold) |
| 4158 | continue; |
| 4159 | else |
| 4160 | lineInput[icol]=m_noDataValues[0]; |
| 4161 | } |
| 4162 | imgWriter.writeData(lineInput,irow,iband); |
| 4163 | } |
| 4164 | } |
| 4165 | } |
| 4166 | catch(std::string errorstring){ |
| 4167 | std::cerr << errorstring << std::endl; |
| 4168 | throw; |
| 4169 | } |
| 4170 | catch(...){ |
| 4171 | throw; |
| 4172 | } |
| 4173 | } |
| 4174 | |
| 4175 | void Jim::setAbsThreshold(Jim& imgWriter, double t1, double t2){ |
| 4176 | try{ |
| 4177 | imgWriter.open(*this,false); |
| 4178 | if(m_noDataValues.empty()){ |
| 4179 | std::string errorString="Error: no data value not set"; |
| 4180 | throw(errorString); |
| 4181 | } |
| 4182 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4183 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4184 | #pragma omp parallel for |
| 4185 | #else |
| 4186 | #endif |
| 4187 | for(int irow=0;irow<nrOfRow();++irow){ |
| 4188 | std::vector<double> lineInput(nrOfCol()); |
| 4189 | readData(lineInput,irow,iband); |
| 4190 | for(int icol=0;icol<nrOfCol();++icol){ |
| 4191 | if(fabs(lineInput[icol])>=t1&&fabs(lineInput[icol])<=t2) |
| 4192 | continue; |
| 4193 | else |
| 4194 | lineInput[icol]=m_noDataValues[0]; |
| 4195 | } |
| 4196 | imgWriter.writeData(lineInput,irow,iband); |
| 4197 | } |
| 4198 | } |
| 4199 | } |
| 4200 | catch(std::string errorstring){ |
| 4201 | std::cerr << errorstring << std::endl; |
| 4202 | throw; |
| 4203 | } |
| 4204 | catch(...){ |
| 4205 | throw; |
| 4206 | } |
| 4207 | } |
| 4208 | |
| 4209 | void Jim::setAbsThresholdMin(Jim& imgWriter, double minThreshold){ |
| 4210 | try{ |
| 4211 | imgWriter.open(*this,false); |
| 4212 | if(m_noDataValues.empty()){ |
| 4213 | std::string errorString="Error: no data value not set"; |
| 4214 | throw(errorString); |
| 4215 | } |
| 4216 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4217 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4218 | #pragma omp parallel for |
| 4219 | #else |
| 4220 | #endif |
| 4221 | for(int irow=0;irow<nrOfRow();++irow){ |
| 4222 | std::vector<double> lineInput(nrOfCol()); |
| 4223 | readData(lineInput,irow,iband); |
| 4224 | for(int icol=0;icol<nrOfCol();++icol){ |
| 4225 | if(fabs(lineInput[icol])>=minThreshold) |
| 4226 | continue; |
| 4227 | else |
| 4228 | lineInput[icol]=m_noDataValues[0]; |
| 4229 | } |
| 4230 | imgWriter.writeData(lineInput,irow,iband); |
| 4231 | } |
| 4232 | } |
| 4233 | } |
| 4234 | catch(std::string errorstring){ |
| 4235 | std::cerr << errorstring << std::endl; |
| 4236 | throw; |
| 4237 | } |
| 4238 | catch(...){ |
| 4239 | throw; |
| 4240 | } |
| 4241 | } |
| 4242 | |
| 4243 | void Jim::setAbsThresholdMax(Jim& imgWriter, double maxThreshold){ |
| 4244 | try{ |
| 4245 | imgWriter.open(*this,false); |
| 4246 | if(m_noDataValues.empty()){ |
| 4247 | std::string errorString="Error: no data value not set"; |
| 4248 | throw(errorString); |
| 4249 | } |
| 4250 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4251 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4252 | #pragma omp parallel for |
| 4253 | #else |
| 4254 | #endif |
| 4255 | for(int irow=0;irow<nrOfRow();++irow){ |
| 4256 | std::vector<double> lineInput(nrOfCol()); |
| 4257 | readData(lineInput,irow,iband); |
| 4258 | for(int icol=0;icol<nrOfCol();++icol){ |
| 4259 | if(fabs(lineInput[icol])<=maxThreshold) |
| 4260 | continue; |
| 4261 | else |
| 4262 | lineInput[icol]=m_noDataValues[0]; |
| 4263 | } |
| 4264 | imgWriter.writeData(lineInput,irow,iband); |
| 4265 | } |
| 4266 | } |
| 4267 | } |
| 4268 | catch(std::string errorstring){ |
| 4269 | std::cerr << errorstring << std::endl; |
| 4270 | throw; |
| 4271 | } |
| 4272 | catch(...){ |
| 4273 | throw; |
| 4274 | } |
| 4275 | } |
| 4276 | |
| 4277 | std::shared_ptr<Jim> Jim::setThreshold(double t1, double t2, double value){ |
| 4278 | std::shared_ptr<Jim> imgWriter=Jim::createImg(); |
| 4279 | setThreshold(*imgWriter, t1, t2, value); |
| 4280 | return(imgWriter); |
| 4281 | } |
| 4282 | |
| 4283 | std::shared_ptr<Jim> Jim::setAbsThreshold(double t1, double t2, double value){ |
| 4284 | std::shared_ptr<Jim> imgWriter=Jim::createImg(); |
| 4285 | setAbsThreshold(*imgWriter, t1, t2, value); |
| 4286 | return(imgWriter); |
| 4287 | } |
| 4288 | |
| 4289 | std::shared_ptr<Jim> Jim::setThreshold(app::AppFactory &theApp){ |
| 4290 | std::shared_ptr<Jim> imgWriter=Jim::createImg(); |
| 4291 | setThreshold(*imgWriter, theApp); |
| 4292 | return(imgWriter); |
| 4293 | } |
| 4294 | |
| 4295 | void Jim::setThresholdMin(Jim& imgWriter, double minThreshold, double value){ |
| 4296 | try{ |
| 4297 | imgWriter.open(*this,false); |
| 4298 | if(m_noDataValues.empty()){ |
| 4299 | std::string errorString="Error: no data value not set"; |
| 4300 | throw(errorString); |
| 4301 | } |
| 4302 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4303 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4304 | #pragma omp parallel for |
| 4305 | #else |
| 4306 | #endif |
| 4307 | for(int irow=0;irow<nrOfRow();++irow){ |
| 4308 | std::vector<double> lineInput(nrOfCol()); |
| 4309 | readData(lineInput,irow,iband); |
| 4310 | for(int icol=0;icol<nrOfCol();++icol){ |
| 4311 | if((lineInput[icol]>=minThreshold)) |
| 4312 | lineInput[icol]=value; |
| 4313 | else |
| 4314 | lineInput[icol]=m_noDataValues[0]; |
| 4315 | } |
| 4316 | imgWriter.writeData(lineInput,irow,iband); |
| 4317 | } |
| 4318 | } |
| 4319 | } |
| 4320 | catch(std::string errorstring){ |
| 4321 | std::cerr << errorstring << std::endl; |
| 4322 | throw; |
| 4323 | } |
| 4324 | catch(...){ |
| 4325 | throw; |
| 4326 | } |
| 4327 | } |
| 4328 | |
| 4329 | void Jim::setThresholdMax(Jim& imgWriter, double maxThreshold, double value){ |
| 4330 | try{ |
| 4331 | imgWriter.open(*this,false); |
| 4332 | if(m_noDataValues.empty()){ |
| 4333 | std::string errorString="Error: no data value not set"; |
| 4334 | throw(errorString); |
| 4335 | } |
| 4336 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4337 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4338 | #pragma omp parallel for |
| 4339 | #else |
| 4340 | #endif |
| 4341 | for(int irow=0;irow<nrOfRow();++irow){ |
| 4342 | std::vector<double> lineInput(nrOfCol()); |
| 4343 | readData(lineInput,irow,iband); |
| 4344 | for(int icol=0;icol<nrOfCol();++icol){ |
| 4345 | if((lineInput[icol]>=maxThreshold)) |
| 4346 | lineInput[icol]=value; |
| 4347 | else |
| 4348 | lineInput[icol]=m_noDataValues[0]; |
| 4349 | } |
| 4350 | imgWriter.writeData(lineInput,irow,iband); |
| 4351 | } |
| 4352 | } |
| 4353 | } |
| 4354 | catch(std::string errorstring){ |
| 4355 | std::cerr << errorstring << std::endl; |
| 4356 | throw; |
| 4357 | } |
| 4358 | catch(...){ |
| 4359 | throw; |
| 4360 | } |
| 4361 | } |
| 4362 | |
| 4363 | void Jim::setThreshold(Jim& imgWriter, double t1, double t2, double value){ |
| 4364 | try{ |
| 4365 | imgWriter.open(*this,false); |
| 4366 | if(m_noDataValues.empty()){ |
| 4367 | std::string errorString="Error: no data value not set"; |
| 4368 | throw(errorString); |
| 4369 | } |
| 4370 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4371 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4372 | #pragma omp parallel for |
| 4373 | #else |
| 4374 | #endif |
| 4375 | for(int irow=0;irow<nrOfRow();++irow){ |
| 4376 | std::vector<double> lineInput(nrOfCol()); |
| 4377 | readData(lineInput,irow,iband); |
| 4378 | for(int icol=0;icol<nrOfCol();++icol){ |
| 4379 | if((lineInput[icol]>=t1)&&(lineInput[icol]<=t2)) |
| 4380 | lineInput[icol]=value; |
| 4381 | else |
| 4382 | lineInput[icol]=m_noDataValues[0]; |
| 4383 | } |
| 4384 | imgWriter.writeData(lineInput,irow,iband); |
| 4385 | } |
| 4386 | } |
| 4387 | } |
| 4388 | catch(std::string errorstring){ |
| 4389 | std::cerr << errorstring << std::endl; |
| 4390 | throw; |
| 4391 | } |
| 4392 | catch(...){ |
| 4393 | throw; |
| 4394 | } |
| 4395 | } |
| 4396 | |
| 4397 | void Jim::setAbsThresholdMin(Jim& imgWriter, double minThreshold, double value){ |
| 4398 | try{ |
| 4399 | imgWriter.open(*this,false); |
| 4400 | if(m_noDataValues.empty()){ |
| 4401 | std::string errorString="Error: no data value not set"; |
| 4402 | throw(errorString); |
| 4403 | } |
| 4404 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4405 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4406 | #pragma omp parallel for |
| 4407 | #else |
| 4408 | #endif |
| 4409 | for(int irow=0;irow<nrOfRow();++irow){ |
| 4410 | std::vector<double> lineInput(nrOfCol()); |
| 4411 | readData(lineInput,irow,iband); |
| 4412 | for(int icol=0;icol<nrOfCol();++icol){ |
| 4413 | if(fabs(lineInput[icol])>=minThreshold) |
| 4414 | lineInput[icol]=value; |
| 4415 | else |
| 4416 | lineInput[icol]=m_noDataValues[0]; |
| 4417 | } |
| 4418 | imgWriter.writeData(lineInput,irow,iband); |
| 4419 | } |
| 4420 | } |
| 4421 | } |
| 4422 | catch(std::string errorstring){ |
| 4423 | std::cerr << errorstring << std::endl; |
| 4424 | throw; |
| 4425 | } |
| 4426 | catch(...){ |
| 4427 | throw; |
| 4428 | } |
| 4429 | } |
| 4430 | |
| 4431 | void Jim::setAbsThresholdMax(Jim& imgWriter, double maxThreshold, double value){ |
| 4432 | try{ |
| 4433 | imgWriter.open(*this,false); |
| 4434 | if(m_noDataValues.empty()){ |
| 4435 | std::string errorString="Error: no data value not set"; |
| 4436 | throw(errorString); |
| 4437 | } |
| 4438 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4439 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4440 | #pragma omp parallel for |
| 4441 | #else |
| 4442 | #endif |
| 4443 | for(int irow=0;irow<nrOfRow();++irow){ |
| 4444 | std::vector<double> lineInput(nrOfCol()); |
| 4445 | readData(lineInput,irow,iband); |
| 4446 | for(int icol=0;icol<nrOfCol();++icol){ |
| 4447 | if(fabs(lineInput[icol])<=maxThreshold) |
| 4448 | lineInput[icol]=value; |
| 4449 | else |
| 4450 | lineInput[icol]=m_noDataValues[0]; |
| 4451 | } |
| 4452 | imgWriter.writeData(lineInput,irow,iband); |
| 4453 | } |
| 4454 | } |
| 4455 | } |
| 4456 | catch(std::string errorstring){ |
| 4457 | std::cerr << errorstring << std::endl; |
| 4458 | throw; |
| 4459 | } |
| 4460 | catch(...){ |
| 4461 | throw; |
| 4462 | } |
| 4463 | } |
| 4464 | |
| 4465 | void Jim::setAbsThreshold(Jim& imgWriter, double t1, double t2, double value){ |
| 4466 | try{ |
| 4467 | imgWriter.open(*this,false); |
| 4468 | if(m_noDataValues.empty()){ |
| 4469 | std::string errorString="Error: no data value not set"; |
| 4470 | throw(errorString); |
| 4471 | } |
| 4472 | for(int iband=0;iband<nrOfBand();++iband){ |
| 4473 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 4474 | #pragma omp parallel for |
| 4475 | #else |
| 4476 | #endif |
| 4477 | for(int irow=0;irow<nrOfRow();++irow){ |
| 4478 | std::vector<double> lineInput(nrOfCol()); |
| 4479 | readData(lineInput,irow,iband); |
| 4480 | for(int icol=0;icol<nrOfCol();++icol){ |
| 4481 | if((fabs(lineInput[icol])>=t1)&&(fabs(lineInput[icol])<=t2)) |
| 4482 | lineInput[icol]=value; |
| 4483 | else |
| 4484 | lineInput[icol]=m_noDataValues[0]; |
| 4485 | } |
| 4486 | imgWriter.writeData(lineInput,irow,iband); |
| 4487 | } |
| 4488 | } |
| 4489 | } |
| 4490 | catch(std::string errorstring){ |
| 4491 | std::cerr << errorstring << std::endl; |
| 4492 | throw; |
| 4493 | } |
| 4494 | catch(...){ |
| 4495 | throw; |
| 4496 | } |
| 4497 | } |
| 4498 | |
| 4499 | void Jim::setThreshold(Jim& imgWriter, app::AppFactory &theApp){ |
| 4500 | Optionjl<double> min_opt("min", "min", "minimum value to be valid"); |
| 4501 | Optionjl<double> max_opt("max", "max", "maximum value to be valid"); |
| 4502 | Optionjl<double> value_opt("value", "value", "value to be set if within min and max (if not set, valid pixels will remain their input value)"); |
| 4503 | Optionjl<bool> abs_opt("abs", "abs", "check for absolute values",false); |
| 4504 | Optionjl<double> nodata_opt("nodata", "nodata", "Nodata value to put in image."); |
| 4505 | |
| 4506 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 4507 | doProcess=min_opt.retrieveOption(theApp); |
| 4508 | max_opt.retrieveOption(theApp); |
| 4509 | value_opt.retrieveOption(theApp); |
| 4510 | abs_opt.retrieveOption(theApp); |
| 4511 | nodata_opt.retrieveOption(theApp); |
| 4512 | if(!doProcess){ |
| 4513 | std::cout << std::endl; |
| 4514 | std::ostringstream helpStream; |
| 4515 | helpStream << "help info: "; |
| 4516 | throw(helpStream.str());//help was invoked, stop processing |
| 4517 | } |
| 4518 | std::vector<std::string> badKeys; |
| 4519 | theApp.badKeys(badKeys); |
| 4520 | if(badKeys.size()){ |
| 4521 | std::ostringstream errorStream; |
| 4522 | if(badKeys.size()>1) |
| 4523 | errorStream << "Error: unknown keys: "; |
| 4524 | else |
| 4525 | errorStream << "Error: unknown key: "; |
| 4526 | for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 4527 | errorStream << badKeys[ikey] << " "; |
| 4528 | } |
| 4529 | errorStream << std::endl; |
| 4530 | throw(errorStream.str()); |
| 4531 | } |
| 4532 | if(nodata_opt.size()) |
| 4533 | setNoData(nodata_opt); |
| 4534 | if(abs_opt[0]){ |
| 4535 | if(value_opt.size()){ |
| 4536 | if(min_opt.size()&&max_opt.size()) |
| 4537 | setAbsThreshold(imgWriter,min_opt[0],max_opt[0],value_opt[0]); |
| 4538 | else if(min_opt.size()) |
| 4539 | setAbsThresholdMin(imgWriter,min_opt[0],value_opt[0]); |
| 4540 | else if(max_opt.size()) |
| 4541 | setAbsThresholdMax(imgWriter,max_opt[0],value_opt[0]); |
| 4542 | else{ |
| 4543 | std::ostringstream errorStream; |
| 4544 | errorStream << "Error: missing min or max threshold"; |
| 4545 | throw(errorStream.str()); |
| 4546 | } |
| 4547 | } |
| 4548 | else{ |
| 4549 | if(min_opt.size()&&max_opt.size()) |
| 4550 | setAbsThreshold(imgWriter,min_opt[0],max_opt[0]); |
| 4551 | else if(min_opt.size()) |
| 4552 | setAbsThresholdMin(imgWriter,min_opt[0]); |
| 4553 | else if(max_opt.size()) |
| 4554 | setAbsThresholdMax(imgWriter,max_opt[0]); |
| 4555 | else{ |
| 4556 | std::ostringstream errorStream; |
| 4557 | errorStream << "Error: missing min or max threshold"; |
| 4558 | throw(errorStream.str()); |
| 4559 | } |
| 4560 | } |
| 4561 | } |
| 4562 | else{ |
| 4563 | if(value_opt.size()){ |
| 4564 | if(min_opt.size()&&max_opt.size()) |
| 4565 | setThreshold(imgWriter,min_opt[0],max_opt[0],value_opt[0]); |
| 4566 | else if(min_opt.size()) |
| 4567 | setThresholdMin(imgWriter,min_opt[0],value_opt[0]); |
| 4568 | else if(max_opt.size()) |
| 4569 | setThresholdMax(imgWriter,max_opt[0],value_opt[0]); |
| 4570 | else{ |
| 4571 | std::ostringstream errorStream; |
| 4572 | errorStream << "Error: missing min or max threshold"; |
| 4573 | throw(errorStream.str()); |
| 4574 | } |
| 4575 | } |
| 4576 | else{ |
| 4577 | if(min_opt.size()&&max_opt.size()) |
| 4578 | setThreshold(imgWriter,min_opt[0],max_opt[0]); |
| 4579 | else if(min_opt.size()) |
| 4580 | setThresholdMin(imgWriter,min_opt[0]); |
| 4581 | else if(max_opt.size()) |
| 4582 | setThresholdMax(imgWriter,max_opt[0]); |
| 4583 | else{ |
| 4584 | std::ostringstream errorStream; |
| 4585 | errorStream << "Error: missing min or max threshold"; |
| 4586 | throw(errorStream.str()); |
| 4587 | } |
| 4588 | } |
| 4589 | } |
| 4590 | } |