23 #include "ImgReaderOgr.h" 24 #include "ImgWriterOgr.h" 25 #include "ImgReaderGdal.h" 26 #include "cpl_string.h" 28 ImgWriterOgr::ImgWriterOgr(
void)
31 ImgWriterOgr::~ImgWriterOgr(
void)
35 ImgWriterOgr::ImgWriterOgr(
const std::string& filename,
const std::string& imageType)
37 open(filename,imageType);
40 ImgWriterOgr::ImgWriterOgr(
const std::string& filename,
ImgReaderOgr& imgReaderOgr)
43 setCodec(imgReaderOgr.getDriver());
44 int nlayer=imgReaderOgr.getDataSource()->GetLayerCount();
45 for(
int ilayer=0;ilayer<nlayer;++ilayer){
46 std::string layername = imgReaderOgr.getLayer(ilayer)->GetName();
47 createLayer(layername,imgReaderOgr.getProjection(),imgReaderOgr.getGeometryType(),NULL);
48 copyFields(imgReaderOgr,ilayer,ilayer);
52 ImgWriterOgr::ImgWriterOgr(
const std::string& filename,
ImgReaderOgr& imgReaderOgr,
bool copyData)
56 setCodec(imgReaderOgr.getDriver());
57 int nlayer=imgReaderOgr.getDataSource()->GetLayerCount();
58 for(
int ilayer=0;ilayer<nlayer;++ilayer){
59 std::string layername = imgReaderOgr.getLayer(ilayer)->GetName();
60 createLayer(layername,imgReaderOgr.getProjection(),imgReaderOgr.getGeometryType(),NULL);
61 copyFields(imgReaderOgr,ilayer,ilayer);
63 OGRFeature *poFeature;
65 poFeature = imgReaderOgr.getLayer(ilayer)->GetNextFeature();
66 if( poFeature == NULL )
68 OGRFeature *poDstFeature = NULL;
70 poDstFeature=createFeature(ilayer);
72 if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE ){
74 std::string errorString=
"Unable to translate feature %d from layer %s.\n";
75 fmt=errorString.c_str();
76 CPLError( CE_Failure, CPLE_AppDefined,
78 poFeature->GetFID(), getLayerName().c_str() );
83 OGRFeature::DestroyFeature( poFeature );
84 OGRFeature::DestroyFeature( poDstFeature );
86 poDstFeature->SetFID( poFeature->GetFID() );
87 OGRFeature::DestroyFeature( poFeature );
90 if(createFeature( poDstFeature,ilayer ) != OGRERR_NONE){
92 std::string errorString=
"Unable to translate feature %d from layer %s.\n";
93 fmt=errorString.c_str();
94 CPLError( CE_Failure, CPLE_AppDefined,
96 poFeature->GetFID(), getLayerName().c_str() );
97 OGRFeature::DestroyFeature( poDstFeature );
99 OGRFeature::DestroyFeature( poDstFeature );
107 void ImgWriterOgr::open(
const std::string& filename,
ImgReaderOgr& imgReaderOgr)
110 setCodec(imgReaderOgr.getDriver());
111 int nlayer=imgReaderOgr.getDataSource()->GetLayerCount();
112 for(
int ilayer=0;ilayer<nlayer;++ilayer){
113 std::string layername = imgReaderOgr.getLayer(ilayer)->GetName();
114 createLayer(layername,imgReaderOgr.getProjection(),imgReaderOgr.getGeometryType(),NULL);
115 copyFields(imgReaderOgr,ilayer,ilayer);
119 void ImgWriterOgr::open(
const std::string& filename,
const std::string& imageType)
121 m_filename = filename;
126 void ImgWriterOgr::close(
void)
128 #if GDAL_VERSION_MAJOR < 2 129 OGRDataSource::DestroyDataSource(m_datasource);
131 GDALClose(m_datasource);
136 void ImgWriterOgr::setCodec(
const std::string& imageType){
137 #if GDAL_VERSION_MAJOR < 2 141 OGRSFDriver *poDriver;
142 poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(imageType.c_str());
146 GDALDriver *poDriver;
147 poDriver = GetGDALDriverManager()->GetDriverByName(imageType.c_str());
149 if( poDriver == NULL ){
150 std::string errorString=
"FileOpenError";
153 #if GDAL_VERSION_MAJOR < 2 154 m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), TRUE );
156 m_datasource = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_UPDATE||GDAL_OF_VECTOR, NULL, NULL, NULL);
158 if( m_datasource == NULL ){
159 #if GDAL_VERSION_MAJOR < 2 160 m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), FALSE );
162 m_datasource = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_READONLY||GDAL_OF_VECTOR, NULL, NULL, NULL);
164 if ( m_datasource != NULL){
165 std::string errorString=
"Update mode not supported, delete output dataset first";
167 #if GDAL_VERSION_MAJOR < 2 168 OGRDataSource::DestroyDataSource(m_datasource);
170 GDALClose(m_datasource);
175 #if GDAL_VERSION_MAJOR < 2 176 m_datasource=poDriver->CreateDataSource(m_filename.c_str(),NULL);
178 m_datasource=poDriver->Create(m_filename.c_str(),0,0,0,GDT_Unknown,NULL);
182 int nLayerCount = m_datasource->GetLayerCount();
183 for(
int iLayer = 0; iLayer < nLayerCount; ++iLayer){
184 if(m_datasource->DeleteLayer(iLayer)!=OGRERR_NONE){
185 std::string errorstring=
"DeleteLayer() failed when overwrite requested";
190 if(m_datasource==NULL){
191 std::string errorString=
"Creation of output file failed";
196 #if GDAL_VERSION_MAJOR < 2 197 void ImgWriterOgr::setCodec(OGRSFDriver *poDriver){
200 void ImgWriterOgr::setCodec(GDALDriver *poDriver){
203 if( poDriver == NULL ){
204 std::string errorString=
"FileOpenError";
207 #if GDAL_VERSION_MAJOR < 2 208 m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), TRUE );
210 m_datasource = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_UPDATE||GDAL_OF_VECTOR, NULL, NULL, NULL);
212 if( m_datasource == NULL ){
213 #if GDAL_VERSION_MAJOR < 2 214 m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), FALSE );
216 m_datasource = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_READONLY||GDAL_OF_VECTOR, NULL, NULL, NULL);
218 if ( m_datasource != NULL){
219 std::string errorString=
"Update mode not supported, delete output dataset first";
221 #if GDAL_VERSION_MAJOR < 2 222 OGRDataSource::DestroyDataSource(m_datasource);
224 GDALClose(m_datasource);
229 #if GDAL_VERSION_MAJOR < 2 230 m_datasource=poDriver->CreateDataSource(m_filename.c_str(),NULL);
232 m_datasource=poDriver->Create(m_filename.c_str(),0,0,0,GDT_Unknown,NULL);
236 int nLayerCount = m_datasource->GetLayerCount();
237 for(
int iLayer = 0; iLayer < nLayerCount; ++iLayer){
238 if(m_datasource->DeleteLayer(iLayer)!=OGRERR_NONE){
239 std::string errorstring=
"DeleteLayer() failed when overwrite requested";
244 if(m_datasource==NULL){
245 std::string errorString=
"Creation of output file failed";
255 OGRLayer* ImgWriterOgr::createLayer(
const std::string& layername,
const std::string& theProjection,
const OGRwkbGeometryType& eGType,
char** papszOptions)
257 if( !m_datasource->TestCapability( ODsCCreateLayer ) ){
258 std::string errorString=
"Test capability to create layer failed";
266 OGRSpatialReference oSRS;
268 if(theProjection!=
""){
269 oSRS.SetFromUserInput(theProjection.c_str());
270 poLayer=m_datasource->CreateLayer( layername.c_str(), &oSRS, eGType,papszOptions );
285 poLayer=m_datasource->CreateLayer( layername.c_str(), NULL, eGType,papszOptions );
287 CSLDestroy( papszOptions );
288 if( poLayer == NULL ){
289 std::string errorstring=
"Layer creation failed";
296 void ImgWriterOgr::createField(
const std::string& fieldname,
const OGRFieldType& fieldType,
int theLayer)
298 OGRFieldDefn oField( fieldname.c_str(), fieldType );
299 if(fieldType==OFTString)
302 theLayer=m_datasource->GetLayerCount()-1;
303 if(m_datasource->GetLayer(theLayer)->CreateField( &oField ) != OGRERR_NONE ){
304 std::ostringstream es;
305 es <<
"Creating field " << fieldname <<
" failed";
306 std::string errorString=es.str();
311 int ImgWriterOgr::getFields(std::vector<std::string>& fields,
int layer)
const 314 layer=m_datasource->GetLayerCount()-1;
315 assert(m_datasource->GetLayerCount()>layer);
317 if((poLayer = m_datasource->GetLayer(layer))==NULL){
318 std::string errorstring=
"Could not get layer";
321 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
323 fields.resize(poFDefn->GetFieldCount());
324 for(
int iField=0;iField<poFDefn->GetFieldCount();++iField){
325 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
326 fields[iField]=poFieldDefn->GetNameRef();
328 return(fields.size());
331 int ImgWriterOgr::getFields(std::vector<OGRFieldDefn*>& fields,
int layer)
const 334 layer=m_datasource->GetLayerCount()-1;
335 assert(m_datasource->GetLayerCount()>layer);
337 if((poLayer = m_datasource->GetLayer(layer))==NULL){
338 std::string errorstring=
"Could not get layer";
341 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
343 fields.resize(poFDefn->GetFieldCount());
344 for(
int iField=0;iField<poFDefn->GetFieldCount();++iField){
345 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
346 fields[iField]=poFDefn->GetFieldDefn(iField);
348 assert(fields.size()==getFieldCount(layer));
349 return(fields.size());
352 void ImgWriterOgr::copyFields(
const ImgReaderOgr& imgReaderOgr,
int srcLayer,
int targetLayer){
354 targetLayer=m_datasource->GetLayerCount()-1;
356 std::vector<OGRFieldDefn*> fields;
358 imgReaderOgr.getFields(fields,srcLayer);
359 for(
int iField=0;iField<fields.size();++iField){
360 if(m_datasource->GetLayer(targetLayer)->CreateField(fields[iField]) != OGRERR_NONE ){
361 std::ostringstream es;
362 es <<
"Creating field " << fields[iField]->GetNameRef() <<
" failed";
363 std::string errorString=es.str();
369 void ImgWriterOgr::addPoint(
double x,
double y,
const std::map<std::string,double>& pointAttributes, std::string fieldName,
const std::string& theId,
int layer){
370 OGRFeature *poFeature;
371 poFeature=createFeature(layer);
373 poFeature->SetField( fieldName.c_str(), theId.c_str());
374 for(std::map<std::string,double>::const_iterator mit=pointAttributes.begin();mit!=pointAttributes.end();++mit){
375 poFeature->SetField((mit->first).c_str(), mit->second);
379 poFeature->SetGeometry( &pt );
380 if(createFeature(poFeature,layer)!=OGRERR_NONE){
381 std::string errorString=
"Failed to create feature";
384 OGRFeature::DestroyFeature( poFeature );
387 void ImgWriterOgr::addPoint(
double x,
double y,
const std::map<std::string,double>& pointAttributes, std::string fieldName,
int theId,
int layer){
388 OGRFeature *poFeature;
389 poFeature = createFeature(layer);
391 if(pointAttributes.size()+1!=poFeature->GetFieldCount()){
392 std::ostringstream ess;
393 ess <<
"Failed to add feature: " << pointAttributes.size() <<
" !=" << poFeature->GetFieldCount() << std::endl;
396 assert(pointAttributes.size()+1==poFeature->GetFieldCount());
397 poFeature->SetField( fieldName.c_str(), theId);
399 for(std::map<std::string,double>::const_iterator mit=pointAttributes.begin();mit!=pointAttributes.end();++mit){
400 poFeature->SetField((mit->first).c_str(),mit->second);
404 poFeature->SetGeometry( &pt );
405 if(createFeature(poFeature,layer)!=OGRERR_NONE){
406 std::string errorString=
"Failed to create feature";
409 OGRFeature::DestroyFeature( poFeature );
413 void ImgWriterOgr::addLineString(std::vector<OGRPoint*>& points,
const std::string& fieldName,
int theId,
int layer){
414 OGRFeature *poFeature;
415 poFeature = createFeature(layer);
416 poFeature->SetStyleString(
"PEN(c:#FF0000,w:5px)");
417 poFeature->SetField( fieldName.c_str(), theId);
418 OGRLineString theLineString;
419 theLineString.setNumPoints(points.size());
420 for(
int ip=0;ip<points.size();++ip)
421 theLineString.setPoint(ip,points[ip]);
422 if(poFeature->SetGeometry( &theLineString )!=OGRERR_NONE){
423 std::string errorString=
"Failed to set line OGRLineString as feature geometry";
426 if(createFeature(poFeature,layer)!=OGRERR_NONE){
427 std::string errorString=
"Failed to create feature";
430 OGRFeature::DestroyFeature( poFeature );
434 void ImgWriterOgr::addRing(std::vector<OGRPoint*>& points,
const std::string& fieldName,
int theId,
int layer){
435 OGRFeature *poFeature;
436 poFeature = createFeature(layer);
437 poFeature->SetStyleString(
"PEN(c:#FF0000,w:5px)");
438 poFeature->SetField( fieldName.c_str(), theId);
441 OGRPolygon thePolygon;
442 OGRLinearRing theRing;
443 for(
int ip=0;ip<points.size();++ip)
444 theRing.addPoint(points[ip]);
446 theRing.closeRings();
447 thePolygon.addRing(&theRing);
449 poFeature->SetGeometry( &thePolygon );
450 if(createFeature(poFeature,layer)!=OGRERR_NONE){
451 std::string errorString=
"Failed to create feature";
453 OGRFeature::DestroyFeature( poFeature );
455 if(poFeature->SetGeometry( &thePolygon )!=OGRERR_NONE){
456 std::string errorString=
"Failed to set polygon as feature geometry";
459 OGRFeature::DestroyFeature( poFeature );
463 void ImgWriterOgr::addLineString(std::vector<OGRPoint*>& points,
const std::string& fieldName,
const std::string& theId,
int layer){
464 OGRFeature *poFeature;
465 poFeature = createFeature(layer);
466 poFeature->SetField( fieldName.c_str(), theId.c_str());
467 OGRLineString theLineString;
468 theLineString.setNumPoints(points.size());
469 for(
int ip=0;ip<points.size();++ip)
470 theLineString.setPoint(ip,points[ip]);
471 if(poFeature->SetGeometry( &theLineString )!=OGRERR_NONE){
472 std::string errorString=
"Failed to set line OGRLineString as feature geometry";
475 if(createFeature(poFeature,layer)!=OGRERR_NONE){
476 std::string errorString=
"Failed to create feature";
479 OGRFeature::DestroyFeature( poFeature );
482 OGRFeature* ImgWriterOgr::createFeature(
int layer){
483 return(OGRFeature::CreateFeature(m_datasource->GetLayer(layer)->GetLayerDefn()));
486 OGRErr ImgWriterOgr::createFeature(OGRFeature *theFeature,
int layer){
487 return m_datasource->GetLayer(layer)->CreateFeature(theFeature);
490 int ImgWriterOgr::getFieldCount(
int layer)
const 492 assert(m_datasource->GetLayerCount()>layer);
494 if((poLayer = m_datasource->GetLayer(layer))==NULL){
495 std::string errorstring=
"Could not get layer";
498 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
499 return(poFDefn->GetFieldCount());
502 int ImgWriterOgr::getFeatureCount(
int layer)
const 504 return(getLayer(layer)->GetFeatureCount());
507 int ImgWriterOgr::ascii2ogr(
const std::string& filename,
const std::string &layername,
const std::vector<std::string>& fieldName,
const std::vector<OGRFieldType>& fieldType,
short colX,
short colY,
const std::string& theProjection,
const OGRwkbGeometryType& eGType,
const char fs)
509 char **papszOptions=NULL;
510 createLayer(layername, theProjection, eGType, papszOptions);
512 int ncol=fieldName.size();
517 for(
int ifield=0;ifield<ncol;++ifield)
518 createField(fieldName[ifield],fieldType[ifield]);
521 std::ifstream fpoints(filename.c_str(),std::ios::in);
523 OGRPolygon thePolygon;
524 OGRLinearRing theRing;
526 OGRFeature *polyFeature;
528 polyFeature=createFeature();
532 std::string csvRecord;
533 while(getline(fpoints,csvRecord)){
534 OGRFeature *pointFeature;
536 pointFeature=createFeature();
539 std::istringstream csvstream(csvRecord);
543 while(getline(csvstream,value,fs)){
545 thePoint.setX(atof(value.c_str()));
547 thePoint.setY(atof(value.c_str()));
549 switch(fieldType[fieldId]){
552 pointFeature->SetField(fieldId,atof(value.c_str()));
553 else if(firstPoint.IsEmpty())
554 polyFeature->SetField(fieldId,atof(value.c_str()));
558 pointFeature->SetField(fieldId,atoi(value.c_str()));
559 else if(firstPoint.IsEmpty())
560 polyFeature->SetField(fieldId,atoi(value.c_str()));
564 pointFeature->SetField(fieldId,value.c_str());
565 else if(firstPoint.IsEmpty())
566 polyFeature->SetField(fieldId,value.c_str());
575 if(colId!=fieldId+2){
576 std::ostringstream ess;
577 ess <<
"Error: colId = " << colId <<
" is different from fieldId+2 = " << fieldId;
580 if(eGType==wkbPoint){
581 pointFeature->SetGeometry( &thePoint );
582 if(createFeature(pointFeature)!=OGRERR_NONE){
583 std::string errorString=
"Failed to create feature";
585 OGRFeature::DestroyFeature( pointFeature );
589 if(firstPoint.IsEmpty()){
592 theRing.addPoint(&thePoint);
597 while(getline(fpoints,line)){
598 OGRFeature *pointFeature;
600 pointFeature=createFeature();
603 std::istringstream ist(line);
609 thePoint.setX(atof(value.c_str()));
611 thePoint.setY(atof(value.c_str()));
613 switch(fieldType[fieldId]){
616 pointFeature->SetField(fieldId,atof(value.c_str()));
617 else if(firstPoint.IsEmpty())
618 polyFeature->SetField(fieldId,atof(value.c_str()));
622 pointFeature->SetField(fieldId,atoi(value.c_str()));
623 else if(firstPoint.IsEmpty())
624 polyFeature->SetField(fieldId,atoi(value.c_str()));
628 pointFeature->SetField(fieldId,value.c_str());
629 else if(firstPoint.IsEmpty())
630 polyFeature->SetField(fieldId,value.c_str());
639 if(colId!=fieldId+2){
640 std::ostringstream ess;
641 ess <<
"Error: colId = " << colId <<
" is different from fieldId+2 = " << fieldId;
644 if(eGType==wkbPoint){
645 pointFeature->SetGeometry( &thePoint );
646 if(createFeature(pointFeature)!=OGRERR_NONE){
647 std::string errorString=
"Failed to create feature";
649 OGRFeature::DestroyFeature( pointFeature );
653 if(firstPoint.IsEmpty()){
656 theRing.addPoint(&thePoint);
660 if(eGType!=wkbPoint){
661 theRing.addPoint(&firstPoint);
662 thePolygon.addRing(&theRing);
664 polyFeature->SetGeometry( &thePolygon );
665 if(createFeature(polyFeature)!=OGRERR_NONE){
666 std::string errorString=
"Failed to create feature";
668 OGRFeature::DestroyFeature( polyFeature );
671 return getFeatureCount();
674 int ImgWriterOgr::addData(
ImgReaderGdal& imgReader,
int theLayer,
bool verbose)
677 assert(m_datasource->GetLayerCount()>theLayer);
679 std::cout <<
"number of layers: " << m_datasource->GetLayerCount() << std::endl;
681 std::cout <<
"get layer " << theLayer << std::endl;
682 poLayer = m_datasource->GetLayer(theLayer);
684 OGRFeature *poFeature;
686 std::cout <<
"reset reading" << std::endl;
687 poLayer->ResetReading();
688 for(
int iband=0;iband<imgReader.
nrOfBand();++iband){
689 std::ostringstream fs;
690 fs <<
"band" << iband;
691 createField(fs.str(),OFTReal,theLayer);
693 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
694 int nfield=poFDefn->GetFieldCount();
696 std::cout <<
"new number of fields: " << nfield << std::endl;
697 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
698 OGRGeometry *poGeometry;
699 poGeometry = poFeature->GetGeometryRef();
700 assert(poGeometry != NULL
701 && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
702 OGRPoint *poPoint = (OGRPoint *) poGeometry;
703 double x=poPoint->getX();
704 double y=poPoint->getY();
705 for(
int iband=0;iband<imgReader.
nrOfBand();++iband){
707 imgReader.
readData(imgData,x,y,iband);
709 std::ostringstream fs;
710 fs <<
"band" << iband;
711 poFeature->SetField(fs.str().c_str(),imgData);
void readData(T &value, int col, int row, int band=0)
Read a single pixel cell value at a specific column and row for a specific band (all indices start co...
int nrOfBand(void) const
Get the number of bands of this dataset.