GDAL显示线性shp文件-LMLPHP

http://pan.baidu.com/s/1qWIDphU  (工程文件在vs2008中编写)

1、使用到的技术

GDAL:读取矢量数据

GDI:    绘制矢量数据

2、详细解释

GDI绘图:

void CDisplayShpDialogDlg::initialCDC(void)
{
m_slider_r.SetRange(,,FALSE); //对slider范围的设定
m_slider_g.SetRange(,,FALSE);
m_slider_b.SetRange(,,FALSE);
m_pDC = m_picDraw.GetDC();      //获取图形控件的DC
m_picDraw.GetClientRect(m_rectPicture); //获取图形控件的大小
}

GDAL数据读取:

void CDisplayShpDialogDlg::DrawShp()
{
//定义画笔的颜色
newPen.CreatePen(PS_SOLID,,RGB(m_slider_r.GetPos(),m_slider_g.GetPos(),m_slider_b.GetPos())); //给CDC设置会画时的画笔
pOldPen = m_pDC->SelectObject(&newPen);                                    //将CDC与画笔关联起来 CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO" );                         //使GDAL支持中文
OGRRegisterAll();
//打开数据
GDALDataset *poDS ; //定义一个图形数据集
poDS = (GDALDataset *)GDALOpenEx(m_filePathName.GetBuffer(),GDAL_OF_VECTOR,NULL,NULL,NULL); //打开矢量数据并返回指针给数据集
if (poDS==NULL)
{
MessageBox("Open Failed");
exit();
}
OGRLayer *poLayer = poDS->GetLayer(); //从数据集中获取图层
poLayer->GetExtent(envelope);        //获取图层的坐标范围到envelop指向的内存中 poLayer->ResetReading();
OGRFeature *poFeature;            //定义要素指针
while((poFeature = poLayer->GetNextFeature())!=NULL) //从图层中获取要素
{
OGRGeometry *poGeometry=poFeature->GetGeometryRef(); //从要素中获取几何图形
if (poGeometry!=NULL)
{
switch (wkbFlatten(poGeometry->getGeometryType())) //判别几何图形的类型
{case wkbLineString:                   //如果几何图形的类型为线状执行下面程序
OGRLineString *poLine = (OGRLineString*)poGeometry;
for(int i = ;i<poLine->getNumPoints();i++)
{
double staX = poLine->getX(i);
double staY = poLine->getY(i);
TransformCoordinate(&staX,&staY);    //坐标转换
if(i==)
{
m_pDC->MoveTo(staX,staY); //使用CDC类进行绘图
}
else
{
m_pDC->LineTo(staX,staY);
}
}
OGRFeature::DestroyFeature( poFeature );
break;
}
} }
m_pDC->SelectObject(pOldPen);
newPen.DeleteObject();
}
05-11 15:11