508{
509 char **papszOptions=NULL;
510 createLayer(layername, theProjection, eGType, papszOptions);
511
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
520
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<='~'){
532 std::string csvRecord;
533 while(getline(fpoints,csvRecord)){
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)){
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{
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);
662 thePolygon.addRing(&theRing);
663
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}