使用GDAL/OGR读取多边形数据

这里的多边形考虑了wkbPolygon和wkbMultiPolygon两种类型。首先是wkbPolygon类型:

    poPolygon=(OGRPolygon*) poGeometry;
    poPolygon->closeRings();                      //闭合环,使起点与终点重合
    poEnvelope=new OGREnvelope;           //获取该要素的四至范围
    poGeometry->getEnvelope(poEnvelope);
    if(0==GNum)
    {
     minX=poEnvelope->MinX;
     minY=poEnvelope->MinY;
     maxX=poEnvelope->MaxX;
     maxY=poEnvelope->MaxY;
    }
    else
    {
     if((poEnvelope->MinX)<minX) minX=poEnvelope->MinX;
     if((poEnvelope->MinY)<minY) minY=poEnvelope->MinY;
     if((poEnvelope->MaxX)>maxX) maxX=poEnvelope->MaxX;
     if((poEnvelope->MaxY)>maxY) maxY=poEnvelope->MaxY;
    }
    int   NumInteriorRings = poPolygon->getNumInteriorRings();   //内环个数    
    //while (NumInteriorRings!=0)                                                             //如果内环个数为零,即是简单多边形
     poOGRLinearRing = poPolygon->getExteriorRing(); //内环为零时,简单多变形的外环即是其多边形本身
     GPointNum.push_back(poOGRLinearRing->getNumPoints());
     OGRRawPoint *Gpoints=new OGRRawPoint[GPointNum.at(GNum)];
     poOGRLinearRing->getPoints(Gpoints,NULL);
     for (int k=0;k<GPointNum.at(GNum);k++)
     {
      PointX.push_back(Gpoints[k].x);
      PointY.push_back(Gpoints[k].y);
     }
     GNum++;
     delete []Gpoints;

而当有的多边形对象包含有好几个多边形时,则为wkbMultiPolygon。其获取的方法类似,只不过要判断该多边形几何体中几何体的个数,得到个数之后再依次循环每个几何体:

int NumMPolygon=MultiPolygon->getNumGeometries();//集合几何体中几何体的个数
   for (int i=0; i<NumMPolygon;i++)                                                               //循环每个几何体
   {
             OGRGeometry * FirstGeometry = MultiPolygon->getGeometryRef(i);
     OGRPolygon*poMultiPolygon = (OGRPolygon *)FirstGeometry; 
     OGRLinearRing *poMultiLinearRing = poMultiPolygon->getExteriorRing();
     GPointNum.push_back(poMultiLinearRing->getNumPoints());
     OGRRawPoint *Gpoints=new OGRRawPoint[GPointNum.at(GNum)];
     poMultiLinearRing->getPoints(Gpoints,NULL);
     for (int k=0;k<GPointNum.at(GNum);k++)
     {
      PointX.push_back(Gpoints[k].x);
      PointY.push_back(Gpoints[k].y);
     }
     GNum++;
     delete []Gpoints;    
   }



转载自:https://blog.csdn.net/guoqiong07/article/details/38300079

You may also like...