23 #include "ogr_spatialref.h"
24 #include "ImgWriterGdal.h"
31 ImgWriterGdal::ImgWriterGdal(
void)
32 : m_gds(NULL), m_ncol(0), m_nrow(0), m_nband(0)
39 ImgWriterGdal::~ImgWriterGdal(
void)
47 void ImgWriterGdal::open(
const std::string& filename,
const ImgReaderGdal& imgSrc,
const std::vector<std::string>& options)
51 m_ncol=imgSrc.nrOfCol();
52 m_nrow=imgSrc.nrOfRow();
53 m_nband=imgSrc.nrOfBand();
54 m_type=imgSrc.getDataType();
77 void ImgWriterGdal::open(
const std::string& filename,
int ncol,
int nrow,
int nband,
const GDALDataType& dataType,
const std::string& imageType,
const std::vector<std::string>& options)
80 m_filename = filename;
94 void ImgWriterGdal::close(
void)
97 char **papszOptions=NULL;
98 for(std::vector<std::string>::const_iterator optionIt=m_options.begin();optionIt!=m_options.end();++optionIt)
99 papszOptions=CSLAddString(papszOptions,optionIt->c_str());
100 CSLDestroy(papszOptions);
106 GDALDriver *poDriver;
107 poDriver = GetGDALDriverManager()->GetDriverByName(imgSrc.getDriverDescription().c_str());
108 if( poDriver == NULL ){
109 std::string errorString=
"FileOpenError";
112 char **papszMetadata;
113 papszMetadata = poDriver->GetMetadata();
115 assert( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ));
116 char **papszOptions=NULL;
117 for(std::vector<std::string>::const_iterator optionIt=m_options.begin();optionIt!=m_options.end();++optionIt)
118 papszOptions=CSLAddString(papszOptions,optionIt->c_str());
126 m_gds=poDriver->Create(m_filename.c_str(),m_ncol,m_nrow,m_nband,m_type,papszOptions);
128 setProjection(imgSrc.getProjection());
130 imgSrc.getGeoTransform(gt);
133 m_gds->SetMetadata(imgSrc.getMetadata() );
134 m_gds->SetMetadataItem(
"TIFFTAG_DOCUMENTNAME", m_filename.c_str());
135 std::string versionString=
"pktools ";
136 versionString+=VERSION;
137 versionString+=
" by Pieter Kempeneers";
138 m_gds->SetMetadataItem(
"TIFFTAG_SOFTWARE", versionString.c_str());
142 time_t tim=time(NULL);
143 tm *now=localtime(&tim);
144 std::ostringstream datestream;
146 datestream << now->tm_year+1900;
148 datestream <<
":0" << now->tm_mon+1;
150 datestream <<
":" << now->tm_mon+1;
152 datestream <<
":0" << now->tm_mday;
154 datestream <<
":" << now->tm_mday;
156 datestream <<
" 0" << now->tm_hour;
158 datestream <<
" " << now->tm_hour;
160 datestream <<
":0" << now->tm_min;
162 datestream <<
":" << now->tm_min;
164 datestream <<
":0" << now->tm_sec;
166 datestream <<
":" << now->tm_sec;
167 m_gds->SetMetadataItem(
"TIFFTAG_DATETIME", datestream.str().c_str());
177 if(imgSrc.getColorTable()!=NULL)
178 setColorTable(imgSrc.getColorTable());
181 void ImgWriterGdal::setCodec(
const std::string& imageType)
184 GDALDriver *poDriver;
185 poDriver = GetGDALDriverManager()->GetDriverByName(imageType.c_str());
186 if( poDriver == NULL ){
187 std::ostringstream s;
188 s <<
"FileOpenError (" << imageType <<
")";
191 char **papszMetadata;
192 papszMetadata = poDriver->GetMetadata();
194 assert( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ));
195 char **papszOptions=NULL;
196 for(std::vector<std::string>::const_iterator optionIt=m_options.begin();optionIt!=m_options.end();++optionIt)
197 papszOptions=CSLAddString(papszOptions,optionIt->c_str());
204 m_gds=poDriver->Create(m_filename.c_str(),m_ncol,m_nrow,m_nband,m_type,papszOptions);
208 m_gds->SetMetadataItem(
"TIFFTAG_DOCUMENTNAME", m_filename.c_str());
209 std::string versionString=
"pktools ";
210 versionString+=VERSION;
211 versionString+=
" by Pieter Kempeneers";
212 m_gds->SetMetadataItem(
"TIFFTAG_SOFTWARE", versionString.c_str());
216 time_t tim=time(NULL);
217 tm *now=localtime(&tim);
218 std::ostringstream datestream;
220 datestream << now->tm_year+1900;
222 datestream <<
":0" << now->tm_mon+1;
224 datestream <<
":" << now->tm_mon+1;
226 datestream <<
":0" << now->tm_mday;
228 datestream <<
":" << now->tm_mday;
230 datestream <<
" 0" << now->tm_hour;
232 datestream <<
" " << now->tm_hour;
234 datestream <<
":0" << now->tm_min;
236 datestream <<
":" << now->tm_min;
238 datestream <<
":0" << now->tm_sec;
240 datestream <<
":" << now->tm_sec;
241 m_gds->SetMetadataItem(
"TIFFTAG_DATETIME", datestream.str().c_str());
245 void ImgWriterGdal::setMetadata(
char** metadata)
248 m_gds->SetMetadata(metadata);
251 std::string ImgWriterGdal::getProjection(
void)
const
254 std::string theProjection=m_gds->GetProjectionRef();
261 return theProjection;
265 void ImgWriterGdal::setGeoTransform(
double* gt){
274 m_gds->SetGeoTransform(m_gt);
295 void ImgWriterGdal::copyGeoTransform(
const ImgReaderGdal& imgSrc)
297 setProjection(imgSrc.getProjection());
299 imgSrc.getGeoTransform(gt);
305 std::string ImgWriterGdal::setProjectionProj4(
const std::string& projection)
310 OGRSpatialReference theRef;
311 theRef.SetFromUserInput(projection.c_str());
313 theRef.exportToWkt(&wktString);
315 m_gds->SetProjection(wktString);
340 void ImgWriterGdal::setProjection(
const std::string& projection)
344 OGRSpatialReference oSRS;
345 char *pszSRS_WKT = NULL;
347 m_gds->SetProjection(projection.c_str());
352 std::string ImgWriterGdal::setProjection(
void)
354 std::string theProjection;
355 OGRSpatialReference oSRS;
356 char *pszSRS_WKT = NULL;
358 oSRS.SetGeogCS(
"ETRS89",
"European_Terrestrial_Reference_System_1989",
"GRS 1980",6378137,298.2572221010042,
"Greenwich",0,
"degree",0.0174532925199433);
360 oSRS.SetProjCS(
"ETRS89 / ETRS-LAEA" );
361 oSRS.SetLAEA(52,10,4321000,3210000);
362 oSRS.exportToWkt( &pszSRS_WKT );
363 theProjection=pszSRS_WKT;
364 CPLFree( pszSRS_WKT );
366 m_gds->SetProjection(theProjection.c_str());
367 return(theProjection);
370 bool ImgWriterGdal::getBoundingBox(
double& ulx,
double& uly,
double& lrx,
double& lry)
const
373 m_gds->GetGeoTransform(gt);
385 lrx=gt[0]+nrOfCol()*gt[1]+nrOfRow()*gt[2];
386 lry=gt[3]+nrOfCol()*gt[4]+nrOfRow()*gt[5];
403 bool ImgWriterGdal::getCentrePos(
double& x,
double& y)
const
406 m_gds->GetGeoTransform(gt);
415 x=gt[0]+(nrOfCol()/2.0)*gt[1]+(nrOfRow()/2.0)*gt[2];
416 y=gt[3]+(nrOfCol()/2.0)*gt[4]+(nrOfRow()/2.0)*gt[5];
426 bool ImgWriterGdal::geo2image(
double x,
double y,
double& i,
double& j)
const
431 m_gds->GetGeoTransform(gt);
439 double denom=(gt[1]-gt[2]*gt[4]/gt[5]);
442 i=(x-gt[0]-gt[2]/gt[5]*(y-gt[3]))/denom;
443 j=(y-gt[3]-gt[4]*(x-gt[0]-gt[2]/gt[5]*(y-gt[3]))/denom)/gt[5];
460 bool ImgWriterGdal::image2geo(
double i,
double j,
double& x,
double& y)
const
463 m_gds->GetGeoTransform(gt);
473 x=gt[0]+(0.5+i)*gt[1]+(0.5+j)*gt[2];
474 y=gt[3]+(0.5+i)*gt[4]+(0.5+j)*gt[5];
485 bool ImgWriterGdal::covers(
double x,
double y)
const
487 double theULX, theULY, theLRX, theLRY;
488 getBoundingBox(theULX,theULY,theLRX,theLRY);
489 return((x > theULX)&&
495 bool ImgWriterGdal::covers(
double ulx,
double uly,
double lrx,
double lry)
const
497 double theULX, theULY, theLRX, theLRY;
498 getBoundingBox(theULX,theULY,theLRX,theLRY);
499 return((ulx < theLRX)&&(lrx > theULX)&&(lry < theULY)&&(uly > theLRY));
502 std::string ImgWriterGdal::getGeoTransform()
const
506 m_gds->GetGeoTransform(gt);
515 std::ostringstream s;
516 s <<
"[" << gt[0] <<
"," << gt[1] <<
"," << gt[2] <<
"," << gt[3] <<
"," << gt[4] <<
"," << gt[5] <<
"]";
520 void ImgWriterGdal::getGeoTransform(
double* gt)
const{
522 m_gds->GetGeoTransform(gt);
555 GDALDataType ImgWriterGdal::getDataType(
int band)
const
557 assert(band<m_nband+1);
558 return (m_gds->GetRasterBand(band+1))->GetRasterDataType();
561 GDALRasterBand* ImgWriterGdal::getRasterBand(
int band)
563 assert(band<m_nband+1);
564 return (m_gds->GetRasterBand(band+1));
568 void ImgWriterGdal::setColorTable(
const std::string& filename,
int band)
571 std::ifstream ftable(filename.c_str(),std::ios::in);
574 GDALColorTable colorTable;
576 while(getline(ftable,line)){
578 std::istringstream ist(line);
579 GDALColorEntry sEntry;
581 ist >>
id >> sEntry.c1 >> sEntry.c2 >> sEntry.c3 >> sEntry.c4;
583 colorTable.SetColorEntry(
id,&sEntry);
587 (m_gds->GetRasterBand(band+1))->SetColorTable(&colorTable);
590 void ImgWriterGdal::setColorTable(GDALColorTable* colorTable,
int band)
592 (m_gds->GetRasterBand(band+1))->SetColorTable(colorTable);
596 bool ImgWriterGdal::writeData(
void* pdata,
const GDALDataType& dataType,
int band)
const{
598 GDALRasterBand *poBand;
599 if(band>=nrOfBand()+1){
600 std::ostringstream s;
601 s <<
"band (" << band <<
") exceeds nrOfBand (" << nrOfBand() <<
")";
604 poBand = m_gds->GetRasterBand(band+1);
605 poBand->RasterIO(GF_Write,0,0,nrOfCol(),nrOfRow(),pdata,nrOfCol(),nrOfRow(),dataType,0,0);