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 OGRDataSource::DestroyDataSource(m_datasource);
132 void ImgWriterOgr::setCodec(
const std::string& imageType){
136 OGRSFDriver *poDriver;
137 poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(imageType.c_str());
138 if( poDriver == NULL ){
139 std::string errorString=
"FileOpenError";
142 m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), TRUE );
143 if( m_datasource == NULL ){
144 m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), FALSE );
145 if ( m_datasource != NULL){
146 std::string errorString=
"Update mode not supported, delete output dataset first";
148 OGRDataSource::DestroyDataSource(m_datasource);
152 m_datasource=poDriver->CreateDataSource(m_filename.c_str(),NULL);
155 int nLayerCount = m_datasource->GetLayerCount();
156 for(
int iLayer = 0; iLayer < nLayerCount; ++iLayer){
157 if(m_datasource->DeleteLayer(iLayer)!=OGRERR_NONE){
158 std::string errorstring=
"DeleteLayer() failed when overwrite requested";
163 if(m_datasource==NULL){
164 std::string errorString=
"Creation of output file failed";
169 void ImgWriterOgr::setCodec(OGRSFDriver *poDriver){
171 if( poDriver == NULL ){
172 std::string errorString=
"FileOpenError";
175 m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), TRUE );
176 if( m_datasource == NULL ){
177 m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), FALSE );
178 if ( m_datasource != NULL){
179 std::string errorString=
"Update mode not supported, delete output dataset first";
181 OGRDataSource::DestroyDataSource(m_datasource);
185 m_datasource=poDriver->CreateDataSource(m_filename.c_str(),NULL);
188 int nLayerCount = m_datasource->GetLayerCount();
189 for(
int iLayer = 0; iLayer < nLayerCount; ++iLayer){
190 if(m_datasource->DeleteLayer(iLayer)!=OGRERR_NONE){
191 std::string errorstring=
"DeleteLayer() failed when overwrite requested";
196 if(m_datasource==NULL){
197 std::string errorString=
"Creation of output file failed";
207 OGRLayer* ImgWriterOgr::createLayer(
const std::string& layername,
const std::string& theProjection,
const OGRwkbGeometryType& eGType,
char** papszOptions)
209 if( !m_datasource->TestCapability( ODsCCreateLayer ) ){
210 std::string errorString=
"Test capability to create layer failed";
218 OGRSpatialReference oSRS;
220 if(theProjection!=
""){
221 oSRS.SetFromUserInput(theProjection.c_str());
222 poLayer=m_datasource->CreateLayer( layername.c_str(), &oSRS, eGType,papszOptions );
237 poLayer=m_datasource->CreateLayer( layername.c_str(), NULL, eGType,papszOptions );
239 CSLDestroy( papszOptions );
240 if( poLayer == NULL ){
241 std::string errorstring=
"Layer creation failed";
248 void ImgWriterOgr::createField(
const std::string& fieldname,
const OGRFieldType& fieldType,
int theLayer)
250 OGRFieldDefn oField( fieldname.c_str(), fieldType );
251 if(fieldType==OFTString)
254 theLayer=m_datasource->GetLayerCount()-1;
255 if(m_datasource->GetLayer(theLayer)->CreateField( &oField ) != OGRERR_NONE ){
256 std::ostringstream es;
257 es <<
"Creating field " << fieldname <<
" failed";
258 std::string errorString=es.str();
263 int ImgWriterOgr::getFields(std::vector<std::string>& fields,
int layer)
const
266 layer=m_datasource->GetLayerCount()-1;
267 assert(m_datasource->GetLayerCount()>layer);
269 if((poLayer = m_datasource->GetLayer(layer))==NULL){
270 std::string errorstring=
"Could not get layer";
273 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
275 fields.resize(poFDefn->GetFieldCount());
276 for(
int iField=0;iField<poFDefn->GetFieldCount();++iField){
277 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
278 fields[iField]=poFieldDefn->GetNameRef();
280 return(fields.size());
283 int ImgWriterOgr::getFields(std::vector<OGRFieldDefn*>& fields,
int layer)
const
286 layer=m_datasource->GetLayerCount()-1;
287 assert(m_datasource->GetLayerCount()>layer);
289 if((poLayer = m_datasource->GetLayer(layer))==NULL){
290 std::string errorstring=
"Could not get layer";
293 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
295 fields.resize(poFDefn->GetFieldCount());
296 for(
int iField=0;iField<poFDefn->GetFieldCount();++iField){
297 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
298 fields[iField]=poFDefn->GetFieldDefn(iField);
300 assert(fields.size()==getFieldCount(layer));
301 return(fields.size());
304 void ImgWriterOgr::copyFields(
const ImgReaderOgr& imgReaderOgr,
int srcLayer,
int targetLayer){
306 targetLayer=m_datasource->GetLayerCount()-1;
308 std::vector<OGRFieldDefn*> fields;
310 imgReaderOgr.getFields(fields,srcLayer);
311 for(
int iField=0;iField<fields.size();++iField){
312 if(m_datasource->GetLayer(targetLayer)->CreateField(fields[iField]) != OGRERR_NONE ){
313 std::ostringstream es;
314 es <<
"Creating field " << fields[iField]->GetNameRef() <<
" failed";
315 std::string errorString=es.str();
321 void ImgWriterOgr::addPoint(
double x,
double y,
const std::map<std::string,double>& pointAttributes, std::string fieldName,
const std::string& theId,
int layer){
322 OGRFeature *poFeature;
323 poFeature=createFeature(layer);
325 poFeature->SetField( fieldName.c_str(), theId.c_str());
326 for(std::map<std::string,double>::const_iterator mit=pointAttributes.begin();mit!=pointAttributes.end();++mit){
327 poFeature->SetField((mit->first).c_str(), mit->second);
331 poFeature->SetGeometry( &pt );
332 if(createFeature(poFeature,layer)!=OGRERR_NONE){
333 std::string errorString=
"Failed to create feature";
336 OGRFeature::DestroyFeature( poFeature );
339 void ImgWriterOgr::addPoint(
double x,
double y,
const std::map<std::string,double>& pointAttributes, std::string fieldName,
int theId,
int layer){
340 OGRFeature *poFeature;
341 poFeature = createFeature(layer);
343 if(pointAttributes.size()+1!=poFeature->GetFieldCount()){
344 std::ostringstream ess;
345 ess <<
"Failed to add feature: " << pointAttributes.size() <<
" !=" << poFeature->GetFieldCount() << std::endl;
348 assert(pointAttributes.size()+1==poFeature->GetFieldCount());
349 poFeature->SetField( fieldName.c_str(), theId);
351 for(std::map<std::string,double>::const_iterator mit=pointAttributes.begin();mit!=pointAttributes.end();++mit){
352 poFeature->SetField((mit->first).c_str(),mit->second);
356 poFeature->SetGeometry( &pt );
357 if(createFeature(poFeature,layer)!=OGRERR_NONE){
358 std::string errorString=
"Failed to create feature";
361 OGRFeature::DestroyFeature( poFeature );
365 void ImgWriterOgr::addLineString(std::vector<OGRPoint*>& points,
const std::string& fieldName,
int theId,
int layer){
366 OGRFeature *poFeature;
367 poFeature = createFeature(layer);
368 poFeature->SetStyleString(
"PEN(c:#FF0000,w:5px)");
369 poFeature->SetField( fieldName.c_str(), theId);
370 OGRLineString theLineString;
371 theLineString.setNumPoints(points.size());
372 for(
int ip=0;ip<points.size();++ip)
373 theLineString.setPoint(ip,points[ip]);
374 if(poFeature->SetGeometry( &theLineString )!=OGRERR_NONE){
375 std::string errorString=
"Failed to set line OGRLineString as feature geometry";
378 if(createFeature(poFeature,layer)!=OGRERR_NONE){
379 std::string errorString=
"Failed to create feature";
382 OGRFeature::DestroyFeature( poFeature );
386 void ImgWriterOgr::addRing(std::vector<OGRPoint*>& points,
const std::string& fieldName,
int theId,
int layer){
387 OGRFeature *poFeature;
388 poFeature = createFeature(layer);
389 poFeature->SetStyleString(
"PEN(c:#FF0000,w:5px)");
390 poFeature->SetField( fieldName.c_str(), theId);
393 OGRPolygon thePolygon;
394 OGRLinearRing theRing;
395 for(
int ip=0;ip<points.size();++ip)
396 theRing.addPoint(points[ip]);
398 theRing.closeRings();
399 thePolygon.addRing(&theRing);
401 poFeature->SetGeometry( &thePolygon );
402 if(createFeature(poFeature,layer)!=OGRERR_NONE){
403 std::string errorString=
"Failed to create feature";
405 OGRFeature::DestroyFeature( poFeature );
407 if(poFeature->SetGeometry( &thePolygon )!=OGRERR_NONE){
408 std::string errorString=
"Failed to set polygon as feature geometry";
411 OGRFeature::DestroyFeature( poFeature );
415 void ImgWriterOgr::addLineString(std::vector<OGRPoint*>& points,
const std::string& fieldName,
const std::string& theId,
int layer){
416 OGRFeature *poFeature;
417 poFeature = createFeature(layer);
418 poFeature->SetField( fieldName.c_str(), theId.c_str());
419 OGRLineString theLineString;
420 theLineString.setNumPoints(points.size());
421 for(
int ip=0;ip<points.size();++ip)
422 theLineString.setPoint(ip,points[ip]);
423 if(poFeature->SetGeometry( &theLineString )!=OGRERR_NONE){
424 std::string errorString=
"Failed to set line OGRLineString as feature geometry";
427 if(createFeature(poFeature,layer)!=OGRERR_NONE){
428 std::string errorString=
"Failed to create feature";
431 OGRFeature::DestroyFeature( poFeature );
434 OGRFeature* ImgWriterOgr::createFeature(
int layer){
435 return(OGRFeature::CreateFeature(m_datasource->GetLayer(layer)->GetLayerDefn()));
438 OGRErr ImgWriterOgr::createFeature(OGRFeature *theFeature,
int layer){
439 return m_datasource->GetLayer(layer)->CreateFeature(theFeature);
442 int ImgWriterOgr::getFieldCount(
int layer)
const
444 assert(m_datasource->GetLayerCount()>layer);
446 if((poLayer = m_datasource->GetLayer(layer))==NULL){
447 std::string errorstring=
"Could not get layer";
450 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
451 return(poFDefn->GetFieldCount());
454 int ImgWriterOgr::getFeatureCount(
int layer)
const
456 return(getLayer(layer)->GetFeatureCount());
459 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)
461 char **papszOptions=NULL;
462 createLayer(layername, theProjection, eGType, papszOptions);
464 int ncol=fieldName.size();
469 for(
int ifield=0;ifield<ncol;++ifield)
470 createField(fieldName[ifield],fieldType[ifield]);
473 std::ifstream fpoints(filename.c_str(),std::ios::in);
475 OGRPolygon thePolygon;
476 OGRLinearRing theRing;
478 OGRFeature *polyFeature;
480 polyFeature=createFeature();
484 std::string csvRecord;
485 while(getline(fpoints,csvRecord)){
486 OGRFeature *pointFeature;
488 pointFeature=createFeature();
491 std::istringstream csvstream(csvRecord);
495 while(getline(csvstream,value,fs)){
497 thePoint.setX(atof(value.c_str()));
499 thePoint.setY(atof(value.c_str()));
501 switch(fieldType[fieldId]){
504 pointFeature->SetField(fieldId,atof(value.c_str()));
505 else if(firstPoint.IsEmpty())
506 polyFeature->SetField(fieldId,atof(value.c_str()));
510 pointFeature->SetField(fieldId,atoi(value.c_str()));
511 else if(firstPoint.IsEmpty())
512 polyFeature->SetField(fieldId,atof(value.c_str()));
516 pointFeature->SetField(fieldId,value.c_str());
517 else if(firstPoint.IsEmpty())
518 polyFeature->SetField(fieldId,atof(value.c_str()));
527 if(colId!=fieldId+2){
528 std::ostringstream ess;
529 ess <<
"Error: colId = " << colId <<
" is different from fieldId+2 = " << fieldId;
532 if(eGType==wkbPoint){
533 pointFeature->SetGeometry( &thePoint );
534 if(createFeature(pointFeature)!=OGRERR_NONE){
535 std::string errorString=
"Failed to create feature";
537 OGRFeature::DestroyFeature( pointFeature );
541 if(firstPoint.IsEmpty()){
544 theRing.addPoint(&thePoint);
549 while(getline(fpoints,line)){
550 OGRFeature *pointFeature;
552 pointFeature=createFeature();
555 std::istringstream ist(line);
561 thePoint.setX(atof(value.c_str()));
563 thePoint.setY(atof(value.c_str()));
565 switch(fieldType[fieldId]){
568 pointFeature->SetField(fieldId,atof(value.c_str()));
569 else if(firstPoint.IsEmpty())
570 polyFeature->SetField(fieldId,atof(value.c_str()));
574 pointFeature->SetField(fieldId,atoi(value.c_str()));
575 else if(firstPoint.IsEmpty())
576 polyFeature->SetField(fieldId,atof(value.c_str()));
580 pointFeature->SetField(fieldId,value.c_str());
581 else if(firstPoint.IsEmpty())
582 polyFeature->SetField(fieldId,atof(value.c_str()));
591 if(colId!=fieldId+2){
592 std::ostringstream ess;
593 ess <<
"Error: colId = " << colId <<
" is different from fieldId+2 = " << fieldId;
596 if(eGType==wkbPoint){
597 pointFeature->SetGeometry( &thePoint );
598 if(createFeature(pointFeature)!=OGRERR_NONE){
599 std::string errorString=
"Failed to create feature";
601 OGRFeature::DestroyFeature( pointFeature );
605 if(firstPoint.IsEmpty()){
608 theRing.addPoint(&thePoint);
612 if(eGType!=wkbPoint){
613 theRing.addPoint(&firstPoint);
614 thePolygon.addRing(&theRing);
616 polyFeature->SetGeometry( &thePolygon );
617 if(createFeature(polyFeature)!=OGRERR_NONE){
618 std::string errorString=
"Failed to create feature";
620 OGRFeature::DestroyFeature( polyFeature );
623 return getFeatureCount();
626 int ImgWriterOgr::addData(
const ImgReaderGdal& imgReader,
int theLayer,
bool verbose)
629 assert(m_datasource->GetLayerCount()>theLayer);
631 std::cout <<
"number of layers: " << m_datasource->GetLayerCount() << std::endl;
633 std::cout <<
"get layer " << theLayer << std::endl;
634 poLayer = m_datasource->GetLayer(theLayer);
636 OGRFeature *poFeature;
638 std::cout <<
"reset reading" << std::endl;
639 poLayer->ResetReading();
640 for(
int iband=0;iband<imgReader.nrOfBand();++iband){
641 std::ostringstream fs;
642 fs <<
"band" << iband;
643 createField(fs.str(),OFTReal,theLayer);
645 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
646 int nfield=poFDefn->GetFieldCount();
648 std::cout <<
"new number of fields: " << nfield << std::endl;
649 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
650 OGRGeometry *poGeometry;
651 poGeometry = poFeature->GetGeometryRef();
652 assert(poGeometry != NULL
653 && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
654 OGRPoint *poPoint = (OGRPoint *) poGeometry;
655 double x=poPoint->getX();
656 double y=poPoint->getY();
657 for(
int iband=0;iband<imgReader.nrOfBand();++iband){
659 imgReader.readData(imgData,GDT_Float64,x,y,iband);
661 std::ostringstream fs;
662 fs <<
"band" << iband;
663 poFeature->SetField(fs.str().c_str(),imgData);