pktools 2.6.7
Processing Kernel for geospatial data
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ImgWriterOgr Class Reference
Collaboration diagram for ImgWriterOgr:
Collaboration graph
[legend]

Public Member Functions

 ImgWriterOgr (const std::string &filename, const std::string &imageType="ESRI Shapefile")
 
 ImgWriterOgr (const std::string &filename, ImgReaderOgr &imgReaderOgr)
 
 ImgWriterOgr (const std::string &filename, ImgReaderOgr &imgReaderOgr, bool copyData)
 
void open (const std::string &filename, ImgReaderOgr &imgReaderOgr)
 
void open (const std::string &filename, const std::string &imageType="ESRI Shapefile")
 
void close (void)
 
int ascii2ogr (const std::string &filename, const std::string &layername, const std::vector< std::string > &fieldName, const std::vector< OGRFieldType > &fieldType, short colX=1, short colY=2, const std::string &theProjection="", const OGRwkbGeometryType &eGType=wkbPoint, const char fs=' ')
 
OGRLayer * createLayer (const std::string &layername="New layer", const std::string &theProjection="", const OGRwkbGeometryType &eGType=wkbUnknown, char **papszOptions=NULL)
 
OGRLayer * copyLayer (OGRLayer *poSrcLayer, const std::string &layername, char **papszOptions=NULL)
 
void createField (const std::string &fieldname, const OGRFieldType &fieldType, int theLayer=0)
 
OGRLayer * getLayer (int layer=0) const
 
std::string getLayerName (int layer=0)
 
int getFields (std::vector< std::string > &fields, int layer=0) const
 
int getFields (std::vector< OGRFieldDefn * > &fields, int layer=0) const
 
void copyFields (const ImgReaderOgr &imgReaderOgr, int srcLayer=0, int targetLayer=0)
 
void addLineString (std::vector< OGRPoint * > &points, const std::string &fieldName, const std::string &theId, int layer=0)
 
void addRing (std::vector< OGRPoint * > &points, const std::string &fieldName, int theId, int layer=0)
 
void addLineString (std::vector< OGRPoint * > &points, const std::string &fieldName, int theId, int layer=0)
 
void addPoint (double x, double y, const std::map< std::string, double > &pointAttributes, std::string fieldName, const std::string &theId, int layer=0)
 
void addPoint (double x, double y, const std::map< std::string, double > &pointAttributes, std::string fieldName, int theId, int layer=0)
 
int addData (ImgReaderGdal &imgReader, int layer=0, bool verbose=false)
 
OGRFeature * createFeature (int layer=0)
 
OGRErr createFeature (OGRFeature *theFeature, int layer=0)
 
int getFieldCount (int layer=0) const
 
int getFeatureCount (int layer=0) const
 
OGRDataSource * getDataSource (void)
 
OGRSFDriver * getDriver (void) const
 

Protected Member Functions

void setCodec (const std::string &imageType)
 
void setCodec (OGRSFDriver *poDriver)
 

Protected Attributes

std::string m_filename
 
OGRDataSource * m_datasource
 

Detailed Description

Definition at line 35 of file ImgWriterOgr.h.

Constructor & Destructor Documentation

◆ ImgWriterOgr() [1/4]

ImgWriterOgr::ImgWriterOgr ( void  )

Definition at line 28 of file ImgWriterOgr.cc.

29{}

◆ ImgWriterOgr() [2/4]

ImgWriterOgr::ImgWriterOgr ( const std::string &  filename,
const std::string &  imageType = "ESRI Shapefile" 
)

Definition at line 35 of file ImgWriterOgr.cc.

36{
37 open(filename,imageType);
38}

◆ ImgWriterOgr() [3/4]

ImgWriterOgr::ImgWriterOgr ( const std::string &  filename,
ImgReaderOgr imgReaderOgr 
)

Definition at line 40 of file ImgWriterOgr.cc.

41{
42 m_filename=filename;
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);
49 }
50}

◆ ImgWriterOgr() [4/4]

ImgWriterOgr::ImgWriterOgr ( const std::string &  filename,
ImgReaderOgr imgReaderOgr,
bool  copyData 
)

Definition at line 52 of file ImgWriterOgr.cc.

53{
54 CPLErrorReset();
55 m_filename=filename;
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);
62 if(copyData){
63 OGRFeature *poFeature;
64 while(true){// (poFeature = imgReaderOgr.getLayer()->GetNextFeature()) != NULL ){
65 poFeature = imgReaderOgr.getLayer(ilayer)->GetNextFeature();
66 if( poFeature == NULL )
67 break;
68 OGRFeature *poDstFeature = NULL;
69
70 poDstFeature=createFeature(ilayer);
71 //todo: check here if SetFrom works (experienced segmentation fault)
72 if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE ){
73 const char* fmt;
74 std::string errorString="Unable to translate feature %d from layer %s.\n";
75 fmt=errorString.c_str();
76 CPLError( CE_Failure, CPLE_AppDefined,
77 fmt,
78 poFeature->GetFID(), getLayerName().c_str() );
79 // CPLError( CE_Failure, CPLE_AppDefined,
80 // "Unable to translate feature %d from layer %s.\n",
81 // poFeature->GetFID(), getLayerName().c_str() );
82
83 OGRFeature::DestroyFeature( poFeature );
84 OGRFeature::DestroyFeature( poDstFeature );
85 }
86 poDstFeature->SetFID( poFeature->GetFID() );
87 OGRFeature::DestroyFeature( poFeature );
88
89 CPLErrorReset();
90 if(createFeature( poDstFeature,ilayer ) != OGRERR_NONE){
91 const char* fmt;
92 std::string errorString="Unable to translate feature %d from layer %s.\n";
93 fmt=errorString.c_str();
94 CPLError( CE_Failure, CPLE_AppDefined,
95 fmt,
96 poFeature->GetFID(), getLayerName().c_str() );
97 OGRFeature::DestroyFeature( poDstFeature );
98 }
99 OGRFeature::DestroyFeature( poDstFeature );
100 }
101 }
102 }
103}

◆ ~ImgWriterOgr()

ImgWriterOgr::~ImgWriterOgr ( void  )

Definition at line 31 of file ImgWriterOgr.cc.

32{
33}

Member Function Documentation

◆ addData()

int ImgWriterOgr::addData ( ImgReaderGdal imgReader,
int  layer = 0,
bool  verbose = false 
)

Definition at line 674 of file ImgWriterOgr.cc.

675{
676 OGRLayer *poLayer;
677 assert(m_datasource->GetLayerCount()>theLayer);
678 if(verbose)
679 std::cout << "number of layers: " << m_datasource->GetLayerCount() << std::endl;
680 if(verbose)
681 std::cout << "get layer " << theLayer << std::endl;
682 poLayer = m_datasource->GetLayer(theLayer);
683 //start reading features from the layer
684 OGRFeature *poFeature;
685 if(verbose)
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);
692 }
693 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
694 int nfield=poFDefn->GetFieldCount();
695 if(verbose)
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){
706 double imgData;
707 imgReader.readData(imgData,x,y,iband);
708 //todo: put imgdata in field
709 std::ostringstream fs;
710 fs << "band" << iband;
711 poFeature->SetField(fs.str().c_str(),imgData);
712 }
713 }
714 return(nfield);
715}
int nrOfBand(void) const
Get the number of bands of this dataset.
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...
Definition: ImgReaderGdal.h:95

◆ addLineString() [1/2]

void ImgWriterOgr::addLineString ( std::vector< OGRPoint * > &  points,
const std::string &  fieldName,
const std::string &  theId,
int  layer = 0 
)

Definition at line 463 of file ImgWriterOgr.cc.

463 {
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";
473 throw(errorString);
474 }
475 if(createFeature(poFeature,layer)!=OGRERR_NONE){
476 std::string errorString="Failed to create feature";
477 throw(errorString);
478 }
479 OGRFeature::DestroyFeature( poFeature );
480}

◆ addLineString() [2/2]

void ImgWriterOgr::addLineString ( std::vector< OGRPoint * > &  points,
const std::string &  fieldName,
int  theId,
int  layer = 0 
)

Definition at line 413 of file ImgWriterOgr.cc.

413 {
414 OGRFeature *poFeature;
415 poFeature = createFeature(layer);
416 poFeature->SetStyleString("PEN(c:#FF0000,w:5px)");//see also http://www.gdal.org/ogr/ogr_feature_style.html
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";
424 throw(errorString);
425 }
426 if(createFeature(poFeature,layer)!=OGRERR_NONE){
427 std::string errorString="Failed to create feature";
428 throw(errorString);
429 }
430 OGRFeature::DestroyFeature( poFeature );
431}

◆ addPoint() [1/2]

void ImgWriterOgr::addPoint ( double  x,
double  y,
const std::map< std::string, double > &  pointAttributes,
std::string  fieldName,
const std::string &  theId,
int  layer = 0 
)

Definition at line 369 of file ImgWriterOgr.cc.

369 {
370 OGRFeature *poFeature;
371 poFeature=createFeature(layer);
372 OGRPoint pt;
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);
376 }
377 pt.setX(x);
378 pt.setY(y);
379 poFeature->SetGeometry( &pt );
380 if(createFeature(poFeature,layer)!=OGRERR_NONE){
381 std::string errorString="Failed to create feature";
382 throw(errorString);
383 }
384 OGRFeature::DestroyFeature( poFeature );
385}

◆ addPoint() [2/2]

void ImgWriterOgr::addPoint ( double  x,
double  y,
const std::map< std::string, double > &  pointAttributes,
std::string  fieldName,
int  theId,
int  layer = 0 
)

Definition at line 387 of file ImgWriterOgr.cc.

387 {
388 OGRFeature *poFeature;
389 poFeature = createFeature(layer);
390 OGRPoint pt;
391 if(pointAttributes.size()+1!=poFeature->GetFieldCount()){
392 std::ostringstream ess;
393 ess << "Failed to add feature: " << pointAttributes.size() << " !=" << poFeature->GetFieldCount() << std::endl;
394 throw(ess.str());
395 }
396 assert(pointAttributes.size()+1==poFeature->GetFieldCount());
397 poFeature->SetField( fieldName.c_str(), theId);
398 int fid=0;
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);
401 }
402 pt.setX(x);
403 pt.setY(y);
404 poFeature->SetGeometry( &pt );
405 if(createFeature(poFeature,layer)!=OGRERR_NONE){
406 std::string errorString="Failed to create feature";
407 throw(errorString);
408 }
409 OGRFeature::DestroyFeature( poFeature );
410}

◆ addRing()

void ImgWriterOgr::addRing ( std::vector< OGRPoint * > &  points,
const std::string &  fieldName,
int  theId,
int  layer = 0 
)

Definition at line 434 of file ImgWriterOgr.cc.

434 {
435 OGRFeature *poFeature;
436 poFeature = createFeature(layer);
437 poFeature->SetStyleString("PEN(c:#FF0000,w:5px)");//see also http://www.gdal.org/ogr/ogr_feature_style.html
438 poFeature->SetField( fieldName.c_str(), theId);
439 // OGRLineString theLineString;
440 // theLineString.setNumPoints(points.size());
441 OGRPolygon thePolygon;
442 OGRLinearRing theRing;
443 for(int ip=0;ip<points.size();++ip)
444 theRing.addPoint(points[ip]);
445 // theRing.addPoint(points[0]);//close the ring
446 theRing.closeRings();//redundent with previous line?
447 thePolygon.addRing(&theRing);
448 // SetSpatialFilter(&thePolygon)
449 poFeature->SetGeometry( &thePolygon );
450 if(createFeature(poFeature,layer)!=OGRERR_NONE){
451 std::string errorString="Failed to create feature";
452 throw(errorString);
453 OGRFeature::DestroyFeature( poFeature );
454 }
455 if(poFeature->SetGeometry( &thePolygon )!=OGRERR_NONE){
456 std::string errorString="Failed to set polygon as feature geometry";
457 throw(errorString);
458 }
459 OGRFeature::DestroyFeature( poFeature );
460}

◆ ascii2ogr()

int ImgWriterOgr::ascii2ogr ( const std::string &  filename,
const std::string &  layername,
const std::vector< std::string > &  fieldName,
const std::vector< OGRFieldType > &  fieldType,
short  colX = 1,
short  colY = 2,
const std::string &  theProjection = "",
const OGRwkbGeometryType &  eGType = wkbPoint,
const char  fs = ' ' 
)

Definition at line 507 of file ImgWriterOgr.cc.

508{
509 char **papszOptions=NULL;
510 createLayer(layername, theProjection, eGType, papszOptions);
511 //create attribute fields that should appear on the layer. Fields must be added to the layer before any features are written. To create a field we initialize an OGRField object with the information about the field. In the case of Shapefiles, the field width and precision is significant in the creation of the output .dbf file, so we set it specifically, though generally the defaults are OK
512 int ncol=fieldName.size();
513 assert(colX>=0);
514 assert(colY>=0);
515 assert(colX<ncol+2);
516 assert(colY<ncol+2);
517 for(int ifield=0;ifield<ncol;++ifield)
518 createField(fieldName[ifield],fieldType[ifield]);
519 //create a local OGRFeature, set attributes and attach geometry before trying to write it to the layer. It is imperative that this feature be instantiated from the OGRFeatureDefn associated with the layer it will be written to.
520 //todo: try to open and catch if failure...
521 std::ifstream fpoints(filename.c_str(),std::ios::in);
522 std::string line;
523 OGRPolygon thePolygon;
524 OGRLinearRing theRing;
525 OGRPoint firstPoint;
526 OGRFeature *polyFeature;
527 if(eGType!=wkbPoint)
528 polyFeature=createFeature();
529
530
531 if(fs>' '&&fs<='~'){//field separator is a regular character (minimum ASCII code is space, maximum ASCII code is tilde)
532 std::string csvRecord;
533 while(getline(fpoints,csvRecord)){//read a line
534 OGRFeature *pointFeature;
535 if(eGType==wkbPoint)
536 pointFeature=createFeature();
537 OGRPoint thePoint;
538 bool skip=false;
539 std::istringstream csvstream(csvRecord);
540 std::string value;
541 int colId=0;
542 int fieldId=0;
543 while(getline(csvstream,value,fs)){//read a column
544 if(colId==colX)
545 thePoint.setX(atof(value.c_str()));
546 else if(colId==colY)
547 thePoint.setY(atof(value.c_str()));
548 else{
549 switch(fieldType[fieldId]){
550 case(OFTReal):
551 if(eGType==wkbPoint)
552 pointFeature->SetField(fieldId,atof(value.c_str()));
553 else if(firstPoint.IsEmpty())
554 polyFeature->SetField(fieldId,atof(value.c_str()));
555 break;
556 case(OFTInteger):
557 if(eGType==wkbPoint)
558 pointFeature->SetField(fieldId,atoi(value.c_str()));
559 else if(firstPoint.IsEmpty())
560 polyFeature->SetField(fieldId,atoi(value.c_str()));
561 break;
562 case(OFTString):
563 if(eGType==wkbPoint)
564 pointFeature->SetField(fieldId,value.c_str());
565 else if(firstPoint.IsEmpty())
566 polyFeature->SetField(fieldId,value.c_str());
567 break;
568 default:
569 break;
570 }
571 ++fieldId;
572 }
573 ++colId;
574 }
575 if(colId!=fieldId+2){
576 std::ostringstream ess;
577 ess << "Error: colId = " << colId << " is different from fieldId+2 = " << fieldId;
578 throw(ess.str());
579 }
580 if(eGType==wkbPoint){
581 pointFeature->SetGeometry( &thePoint );
582 if(createFeature(pointFeature)!=OGRERR_NONE){
583 std::string errorString="Failed to create feature";
584 throw(errorString);
585 OGRFeature::DestroyFeature( pointFeature );
586 }
587 }
588 else{
589 if(firstPoint.IsEmpty()){
590 firstPoint=thePoint;
591 }
592 theRing.addPoint(&thePoint);
593 }
594 }
595 }
596 else{//space or tab delimited fields
597 while(getline(fpoints,line)){
598 OGRFeature *pointFeature;
599 if(eGType==wkbPoint)
600 pointFeature=createFeature();
601 OGRPoint thePoint;
602 bool skip=false;
603 std::istringstream ist(line);
604 std::string value;
605 int colId=0;
606 int fieldId=0;
607 while(ist >> value){
608 if(colId==colX)
609 thePoint.setX(atof(value.c_str()));
610 else if(colId==colY)
611 thePoint.setY(atof(value.c_str()));
612 else{
613 switch(fieldType[fieldId]){
614 case(OFTReal):
615 if(eGType==wkbPoint)
616 pointFeature->SetField(fieldId,atof(value.c_str()));
617 else if(firstPoint.IsEmpty())
618 polyFeature->SetField(fieldId,atof(value.c_str()));
619 break;
620 case(OFTInteger):
621 if(eGType==wkbPoint)
622 pointFeature->SetField(fieldId,atoi(value.c_str()));
623 else if(firstPoint.IsEmpty())
624 polyFeature->SetField(fieldId,atoi(value.c_str()));
625 break;
626 case(OFTString):
627 if(eGType==wkbPoint)
628 pointFeature->SetField(fieldId,value.c_str());
629 else if(firstPoint.IsEmpty())
630 polyFeature->SetField(fieldId,value.c_str());
631 break;
632 default:
633 break;
634 }
635 ++fieldId;
636 }
637 ++colId;
638 }
639 if(colId!=fieldId+2){
640 std::ostringstream ess;
641 ess << "Error: colId = " << colId << " is different from fieldId+2 = " << fieldId;
642 throw(ess.str());
643 }
644 if(eGType==wkbPoint){
645 pointFeature->SetGeometry( &thePoint );
646 if(createFeature(pointFeature)!=OGRERR_NONE){
647 std::string errorString="Failed to create feature";
648 throw(errorString);
649 OGRFeature::DestroyFeature( pointFeature );
650 }
651 }
652 else{
653 if(firstPoint.IsEmpty()){
654 firstPoint=thePoint;
655 }
656 theRing.addPoint(&thePoint);
657 }
658 }
659 }
660 if(eGType!=wkbPoint){
661 theRing.addPoint(&firstPoint);//close the ring
662 thePolygon.addRing(&theRing);
663 // SetSpatialFilter(&thePolygon)
664 polyFeature->SetGeometry( &thePolygon );
665 if(createFeature(polyFeature)!=OGRERR_NONE){
666 std::string errorString="Failed to create feature";
667 throw(errorString);
668 OGRFeature::DestroyFeature( polyFeature );
669 }
670 }
671 return getFeatureCount();
672}

◆ close()

void ImgWriterOgr::close ( void  )

Definition at line 126 of file ImgWriterOgr.cc.

127{
128#if GDAL_VERSION_MAJOR < 2
129 OGRDataSource::DestroyDataSource(m_datasource);
130#else
131 GDALClose(m_datasource);
132#endif
133}

◆ copyFields()

void ImgWriterOgr::copyFields ( const ImgReaderOgr imgReaderOgr,
int  srcLayer = 0,
int  targetLayer = 0 
)

Definition at line 352 of file ImgWriterOgr.cc.

352 {
353 if(targetLayer<0)
354 targetLayer=m_datasource->GetLayerCount()-1;//get back layer
355 //get fields from imgReaderOgr
356 std::vector<OGRFieldDefn*> fields;
357
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();
364 throw(errorString);
365 }
366 }
367}

◆ createFeature() [1/2]

OGRFeature * ImgWriterOgr::createFeature ( int  layer = 0)

Definition at line 482 of file ImgWriterOgr.cc.

482 {
483 return(OGRFeature::CreateFeature(m_datasource->GetLayer(layer)->GetLayerDefn()));
484}

◆ createFeature() [2/2]

OGRErr ImgWriterOgr::createFeature ( OGRFeature *  theFeature,
int  layer = 0 
)

Definition at line 486 of file ImgWriterOgr.cc.

486 {
487 return m_datasource->GetLayer(layer)->CreateFeature(theFeature);
488}

◆ createField()

void ImgWriterOgr::createField ( const std::string &  fieldname,
const OGRFieldType &  fieldType,
int  theLayer = 0 
)

Definition at line 296 of file ImgWriterOgr.cc.

297{
298 OGRFieldDefn oField( fieldname.c_str(), fieldType );
299 if(fieldType==OFTString)
300 oField.SetWidth(32);
301 if(theLayer<0)
302 theLayer=m_datasource->GetLayerCount()-1;//get back layer
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();
307 throw(errorString);
308 }
309}

◆ createLayer()

OGRLayer * ImgWriterOgr::createLayer ( const std::string &  layername = "New layer",
const std::string &  theProjection = "",
const OGRwkbGeometryType &  eGType = wkbUnknown,
char **  papszOptions = NULL 
)

Definition at line 255 of file ImgWriterOgr.cc.

256{
257 if( !m_datasource->TestCapability( ODsCCreateLayer ) ){
258 std::string errorString="Test capability to create layer failed";
259 throw(errorString);
260 }
261 //papszOptions = CSLSetNameValue( papszOptions, "DIM", "1" );
262 //if points: use wkbPoint
263 //if no constraints on the types geometry to be written: use wkbUnknown
264 OGRLayer* poLayer;
265
266 OGRSpatialReference oSRS;
267
268 if(theProjection!=""){
269 oSRS.SetFromUserInput(theProjection.c_str());
270 poLayer=m_datasource->CreateLayer( layername.c_str(), &oSRS, eGType,papszOptions );
271 // if(theProjection.find("EPSPG:")!=std::string::npos){
272 // int epsg_code=atoi(theProjection.substr(theProjection.find_first_not_of("EPSG:")).c_str());
273 // OGRSpatialReference oSRS;
274 // oSRS.importFromEPSG(epsg_code);
275 // poLayer=m_datasource->CreateLayer( layername.c_str(), &oSRS, eGType,papszOptions );
276 // }
277 // else{
278 // OGRSpatialReference oSRS(theProjection.c_str());
279 // poLayer=m_datasource->CreateLayer( layername.c_str(), &oSRS, eGType,papszOptions );
280 // }
281 // }
282 // oSRS.importFromProj4(theProjection);
283 }
284 else
285 poLayer=m_datasource->CreateLayer( layername.c_str(), NULL, eGType,papszOptions );
286 //check if destroy is needed?!
287 CSLDestroy( papszOptions );
288 if( poLayer == NULL ){
289 std::string errorstring="Layer creation failed";
290 throw(errorstring);
291 }
292 // OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
293 return poLayer;
294}

◆ getDataSource()

OGRDataSource * ImgWriterOgr::getDataSource ( void  )
inline

Definition at line 66 of file ImgWriterOgr.h.

66{return m_datasource;};

◆ getDriver()

OGRSFDriver * ImgWriterOgr::getDriver ( void  ) const
inline

Definition at line 67 of file ImgWriterOgr.h.

67{return m_datasource->GetDriver();};

◆ getFeatureCount()

int ImgWriterOgr::getFeatureCount ( int  layer = 0) const

Definition at line 502 of file ImgWriterOgr.cc.

503{
504 return(getLayer(layer)->GetFeatureCount());
505}

◆ getFieldCount()

int ImgWriterOgr::getFieldCount ( int  layer = 0) const

Definition at line 490 of file ImgWriterOgr.cc.

491{
492 assert(m_datasource->GetLayerCount()>layer);
493 OGRLayer *poLayer;
494 if((poLayer = m_datasource->GetLayer(layer))==NULL){
495 std::string errorstring="Could not get layer";
496 throw(errorstring);
497 }
498 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
499 return(poFDefn->GetFieldCount());
500}

◆ getFields() [1/2]

int ImgWriterOgr::getFields ( std::vector< OGRFieldDefn * > &  fields,
int  layer = 0 
) const

Definition at line 331 of file ImgWriterOgr.cc.

332{
333 if(layer<0)
334 layer=m_datasource->GetLayerCount()-1;
335 assert(m_datasource->GetLayerCount()>layer);
336 OGRLayer *poLayer;
337 if((poLayer = m_datasource->GetLayer(layer))==NULL){
338 std::string errorstring="Could not get layer";
339 throw(errorstring);
340 }
341 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
342 fields.clear();
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);
347 }
348 assert(fields.size()==getFieldCount(layer));
349 return(fields.size());
350}

◆ getFields() [2/2]

int ImgWriterOgr::getFields ( std::vector< std::string > &  fields,
int  layer = 0 
) const

Definition at line 311 of file ImgWriterOgr.cc.

312{
313 if(layer<0)
314 layer=m_datasource->GetLayerCount()-1;
315 assert(m_datasource->GetLayerCount()>layer);
316 OGRLayer *poLayer;
317 if((poLayer = m_datasource->GetLayer(layer))==NULL){
318 std::string errorstring="Could not get layer";
319 throw(errorstring);
320 }
321 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
322 fields.clear();
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();
327 }
328 return(fields.size());
329}

◆ getLayer()

OGRLayer * ImgWriterOgr::getLayer ( int  layer = 0) const
inline

Definition at line 50 of file ImgWriterOgr.h.

50{return m_datasource->GetLayer(layer);};

◆ getLayerName()

std::string ImgWriterOgr::getLayerName ( int  layer = 0)
inline

Definition at line 51 of file ImgWriterOgr.h.

51{return m_datasource->GetLayer(layer)->GetLayerDefn()->GetName();};

◆ open() [1/2]

void ImgWriterOgr::open ( const std::string &  filename,
const std::string &  imageType = "ESRI Shapefile" 
)

Definition at line 119 of file ImgWriterOgr.cc.

120{
121 m_filename = filename;
122 setCodec(imageType);
123}

◆ open() [2/2]

void ImgWriterOgr::open ( const std::string &  filename,
ImgReaderOgr imgReaderOgr 
)

Definition at line 107 of file ImgWriterOgr.cc.

108{
109 m_filename=filename;
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);
116 }
117}

◆ setCodec() [1/2]

void ImgWriterOgr::setCodec ( const std::string &  imageType)
protected

Definition at line 136 of file ImgWriterOgr.cc.

136 {
137#if GDAL_VERSION_MAJOR < 2
138 //register the drivers
139 OGRRegisterAll();
140 //fetch the OGR file driver
141 OGRSFDriver *poDriver;
142 poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(imageType.c_str());
143#else
144 //register the drivers
145 GDALAllRegister();
146 GDALDriver *poDriver;
147 poDriver = GetGDALDriverManager()->GetDriverByName(imageType.c_str());
148#endif
149 if( poDriver == NULL ){
150 std::string errorString="FileOpenError";
151 throw(errorString);
152 }
153#if GDAL_VERSION_MAJOR < 2
154 m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), TRUE );
155#else
156 m_datasource = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_UPDATE||GDAL_OF_VECTOR, NULL, NULL, NULL);
157#endif
158 if( m_datasource == NULL ){
159#if GDAL_VERSION_MAJOR < 2
160 m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), FALSE );
161#else
162 m_datasource = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_READONLY||GDAL_OF_VECTOR, NULL, NULL, NULL);
163#endif
164 if ( m_datasource != NULL){// we can only open in not update mode
165 std::string errorString="Update mode not supported, delete output dataset first";
166 throw(errorString);
167#if GDAL_VERSION_MAJOR < 2
168 OGRDataSource::DestroyDataSource(m_datasource);
169#else
170 GDALClose(m_datasource);
171#endif
172 m_datasource = NULL;
173 }
174 else //create the data source
175#if GDAL_VERSION_MAJOR < 2
176 m_datasource=poDriver->CreateDataSource(m_filename.c_str(),NULL);
177#else
178 m_datasource=poDriver->Create(m_filename.c_str(),0,0,0,GDT_Unknown,NULL);
179#endif
180 }
181 else{//datasets exists, always overwrite all layers (no update append for now)
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";
186 throw(errorstring);
187 }
188 }
189 }
190 if(m_datasource==NULL){
191 std::string errorString="Creation of output file failed";
192 throw(errorString);
193 }
194}

◆ setCodec() [2/2]

void ImgWriterOgr::setCodec ( OGRSFDriver *  poDriver)
protected

Definition at line 197 of file ImgWriterOgr.cc.

197 {
198 OGRRegisterAll();
199#else
200void ImgWriterOgr::setCodec(GDALDriver *poDriver){
201 GDALAllRegister();
202#endif
203 if( poDriver == NULL ){
204 std::string errorString="FileOpenError";
205 throw(errorString);
206 }
207#if GDAL_VERSION_MAJOR < 2
208 m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), TRUE );
209#else
210 m_datasource = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_UPDATE||GDAL_OF_VECTOR, NULL, NULL, NULL);
211#endif
212 if( m_datasource == NULL ){
213#if GDAL_VERSION_MAJOR < 2
214 m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), FALSE );
215#else
216 m_datasource = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_READONLY||GDAL_OF_VECTOR, NULL, NULL, NULL);
217#endif
218 if ( m_datasource != NULL){// we can only open in not update mode
219 std::string errorString="Update mode not supported, delete output dataset first";
220 throw(errorString);
221#if GDAL_VERSION_MAJOR < 2
222 OGRDataSource::DestroyDataSource(m_datasource);
223#else
224 GDALClose(m_datasource);
225#endif
226 m_datasource = NULL;
227 }
228 else //create the data source
229#if GDAL_VERSION_MAJOR < 2
230 m_datasource=poDriver->CreateDataSource(m_filename.c_str(),NULL);
231#else
232 m_datasource=poDriver->Create(m_filename.c_str(),0,0,0,GDT_Unknown,NULL);
233#endif
234 }
235 else{//datasets exists, always overwrite all layers (no update append for now)
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";
240 throw(errorstring);
241 }
242 }
243 }
244 if(m_datasource==NULL){
245 std::string errorString="Creation of output file failed";
246 throw(errorString);
247 }
248}

Member Data Documentation

◆ m_datasource

OGRDataSource* ImgWriterOgr::m_datasource
protected

Definition at line 85 of file ImgWriterOgr.h.

◆ m_filename

std::string ImgWriterOgr::m_filename
protected

Definition at line 83 of file ImgWriterOgr.h.


The documentation for this class was generated from the following files: