文章源代码如下:
Shapefile.h
class ContourLine { public: ContourLine(); //ShapeFile(char *); ~ContourLine(void); void ReadContour(char*); void SavaContour(char*); vector<Line> mContours;//等高线 double MinElevation; double MaxElevation; int lineNumber;//shpfile数据中线多个数 int pointNumber;//shpfile数据中点多个数 private: char *FilePath; //数据路径 public: void SetPath(char *path); double ContourLine::GetLengthBetweenPoint(Point P1,Point P2); void ContourLine::GetLength(Line& pContour); public: double minx,miny,maxx,maxy; };
Shapefile.cpp
//构造函数 ContourLine::ContourLine() { OGRRegisterAll(); pointNumber = 0; } ContourLine::~ContourLine(void) { } void ContourLine::ReadContour(char *heightName) { mContours.clear(); OGRDataSource* poDS = OGRSFDriverRegistrar::Open( this->FilePath, FALSE ); if( poDS == NULL ) { return; } OGRLayer* poLayer = poDS->GetLayer(0); if (poLayer == NULL) { return; } poLayer->ResetReading(); OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn(); OGRFeature* poFeature = poLayer->GetNextFeature(); int number=0; Line tempContour; while( poFeature != NULL ) { OGRGeometry* poGeometry = poFeature->GetGeometryRef(); OGRLineString *poPolyline = (OGRLineString *) poGeometry; tempContour.Elevation= poFeature->GetFieldAsDouble (heightName); if (tempContour.Elevation < 0 || poPolyline->getNumPoints() < 3) { poFeature = poLayer->GetNextFeature(); continue; } int ptNumber = poPolyline->getNumPoints(); pointNumber +=ptNumber; for(int i = 0; i < ptNumber; i++) { Point Point; Point.x=poPolyline->getX(i); Point.y=poPolyline->getY(i); tempContour.Points.push_back(Point); } //计算tempContour的长度 this->GetLength(tempContour); if(number==0) { this->MaxElevation=this->MinElevation=tempContour.Elevation; } else { if(tempContour.Elevation<this->MinElevation) this->MinElevation=tempContour.Elevation; if(tempContour.Elevation>this->MaxElevation) this->MaxElevation=tempContour.Elevation; } number++; mContours.push_back(tempContour); tempContour.Points.clear(); poFeature = poLayer->GetNextFeature(); } this->lineNumber=number; } void ContourLine::SavaContour(char *FilePath) { char FileName[MaxFileNameSize]; char *p=strrchr(FilePath,'\\')+1; strcpy(FileName,p); int i=0,j=0; while(i<MaxFileNameSize &&FileName[i]!='\0' &&FileName[i]!='.') i++; if(i !=MaxFileNameSize) FileName[i]='\0'; const char *pszDriverName = "ESRI Shapefile"; OGRSFDriver *poDriver; poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName ); if( poDriver == NULL ) { printf( "%s driver not available.\n", pszDriverName ); return; } OGRDataSource *poDS; poDS = poDriver->CreateDataSource( FilePath, NULL ); if( poDS == NULL ) { printf( "Creation of output file failed.\n" ); return; } OGRLayer *poLayer; poLayer = poDS->CreateLayer(FileName, NULL, wkbLineString, NULL ); if( poLayer == NULL ) { printf( "Layer creation failed.\n" ); return; } OGRFieldDefn oField( "contour", OFTReal ); if( poLayer->CreateField( &oField ) != OGRERR_NONE ) { printf( "Creating field failed.\n" ); return; } int LineNumber=static_cast<int>(mContours.size()); for(i=0;i<LineNumber;i++) { OGRFeature *poFeature=new OGRFeature( poLayer->GetLayerDefn() ); poFeature->SetField( "contour", mContours[i].Elevation ); OGRLineString *poPolyline = new OGRLineString(); int num=static_cast<int>(mContours[i].Points.size()); poPolyline->setNumPoints(num); for(j=0;j<num;j++) { poPolyline->setPoint(j,mContours[i].Points[j].x,mContours[i].Points[j].y); } poFeature->SetGeometryDirectly(poPolyline); if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE ) { printf( "Failed to create feature in shapefile.\n" ); return; } } OGRDataSource::DestroyDataSource( poDS ); } void ContourLine::SetPath(char *path) { FilePath = path; } //计算2点之间的距离 double ContourLine::GetLengthBetweenPoint(Point P1,Point P2) { double length; length = sqrt((double)((P2.x - P1.x)*(P2.x - P1.x) + (P2.y - P1.y)*(P2.y - P1.y))); return length; } //计算线pContour的长度,并把长度结果存入length void ContourLine::GetLength(Line& pContour) { double tempLength;//两点之间的距离 double totalLength;//一条线的距离 totalLength = 0; //pContour中点的数目 int lineNumber = pContour.Points.size(); for(int i = 0; i < lineNumber-1;i++) { tempLength = GetLengthBetweenPoint(pContour.Points[i],pContour.Points[i+1]); totalLength = totalLength + tempLength; } //返回线段长度 pContour.Length = totalLength; }
会提示如下错误:
Shapefile.h:40:9: error: extra qualification ‘ContourLine::’ on member ‘GetLengthBetweenPoint’ [-fpermissive]
Shapefile.h:41:7: error: extra qualification ‘ContourLine::’ on member ‘GetLength’ [-fpermissive]
错误的原因是:类的定义中
double ContourLine::GetLengthBetweenPoint(Point P1,Point P2); void ContourLine::GetLength(Line& pContour);
不需要用
double ContourLine::,或者类的函数声明和类的定义是放在一个文件里面,也不许要用***::
上述错误代码改为
double GetLengthBetweenPoint(Point P1,Point P2); void GetLength(Line& pContour);