读取XML中的信息GDAL生成shp文件

来源:互联网 发布:网络用语及其意思 编辑:程序博客网 时间:2024/05/17 23:04
在上一篇介绍了通过影像获取边界范围,这一篇介绍通过XML获取影像边界范围,不过下面是生成的点图层,需要的可以改为面图层。
在国产卫星影像(如GF1,GF2)压缩包中,通常会存在一个XML文件,里面存有一些影像的基本信息,先需要通过XML中的四角点的坐标生成一个shp文件,即影像对应的有效范围边框。
在此也非常感谢提供Markup.h和Markup.cpp(需要的可以去我的资源中下载)的那个朋友<img alt="微笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif" />,帮我解决了一个大问题。也希望这能帮助需要的朋友。
<ImgFour_points(const char *pszXMLFileName, const char *pszDSTShpFileName){CMarkup xml;bool load=false;load=xml.Load(pszXMLFileName);xml.ResetMainPos();if (load){cout<<"从XML提取影像四角点并生成点shp图层..."<<endl;//获取四个点坐标vector<Point_xy> BoundaryPoint;Point_xy temPoint;   //点结构体对象
if (xml.FindChildElem("SatelliteID"))cout<<"卫星:"<<xml.GetChildData()<<endl;if(xml.FindChildElem("SceneID"))cout<<"景序列号:"<<xml.GetChildData()<<endl;
<span style="white-space:pre"></span>//XML中的四个点坐标if(xml.FindChildElem("TopLeftLatitude")){CString str = (xml.GetChildData().GetBuffer(0));  //为了CString 转为 doubletemPoint.y =atof(str.GetBuffer(str.GetLength()));
<span style="white-space:pre"></span>}if(xml.FindChildElem("TopLeftLongitude")){CString str = (xml.GetChildData().GetBuffer(0));temPoint.x =atof(str.GetBuffer(str.GetLength()));}BoundaryPoint.push_back(temPoint);    //加入Vector为了后面shp的生成if(xml.FindChildElem("TopRightLatitude")){CString str = (xml.GetChildData().GetBuffer(0));temPoint.y =atof(str.GetBuffer(str.GetLength()));}if(xml.FindChildElem("TopRightLongitude")){CString str = (xml.GetChildData().GetBuffer(0));temPoint.x =atof(str.GetBuffer(str.GetLength()));//temPoint.x = xml.GetChildData();//cout<<"TopRightLongitude"<<xml.GetChildData()<<endl;}BoundaryPoint.push_back(temPoint);if(xml.FindChildElem("BottomRightLatitude")){CString str = (xml.GetChildData().GetBuffer(0));temPoint.y =atof(str.GetBuffer(str.GetLength()));}if(xml.FindChildElem("BottomRightLongitude")){CString str = (xml.GetChildData().GetBuffer(0));temPoint.x =atof(str.GetBuffer(str.GetLength()));}BoundaryPoint.push_back(temPoint);if(xml.FindChildElem("BottomLeftLatitude")){CString str = (xml.GetChildData().GetBuffer(0));temPoint.y =atof(str.GetBuffer(str.GetLength()));}if(xml.FindChildElem("BottomLeftLongitude")){CString str = (xml.GetChildData().GetBuffer(0));temPoint.x =atof(str.GetBuffer(str.GetLength()));}BoundaryPoint.push_back(temPoint);
//为了支持中文路径CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");//注册shape文件驱动const char* pszDriverName="ESRI Shapefile";OGRSFDriver *poDriver;OGRRegisterAll();poDriver=OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);if (poDriver==NULL){printf("%s driver is not available!",pszDriverName);exit(1);}//创建shape文件;OGRDataSource *poShpDS;//如果名字有.shp后缀,则直接在当前目录下生成文件;poShpDS=poDriver->CreateDataSource(pszDSTShpFileName,NULL); if (poShpDS==NULL){printf("Create my shape file failed!");exit(1);}//创建输出图层;OGRLayer *poLayer;;//为shp设置地理坐标OGRSpatialReference oSRS;  char    *pszWKT = NULL;oSRS.SetWellKnownGeogCS( "WGS84" );oSRS.exportToWkt( &pszWKT );//printf( "%s\n", pszWKT );poLayer=poShpDS->CreateLayer(pszDSTShpFileName,&oSRS, wkbPoint, NULL);if (poLayer==NULL){printf("Creat layer failed!");exit(1);}//添加属性字段OGRFieldDefn oFieldfilename("ImgName",OFTString);oFieldfilename.SetWidth(100);if (poLayer->CreateField(&oFieldfilename,1)!=OGRERR_NONE){printf("Create Point Field Failed!");exit(1);}OGRFieldDefn oFieldX("X",OFTReal);if (poLayer->CreateField(&oFieldX,1)!=OGRERR_NONE){printf("Create Point Field Failed!");exit(1);}OGRFieldDefn oFieldY("Y",OFTReal);if (poLayer->CreateField(&oFieldY,1)!=OGRERR_NONE){printf("Create Point Field Failed!");exit(1);}//创建features,写入feature到磁盘;OGRFeature *poFeature;poFeature=OGRFeature::CreateFeature(poLayer->GetLayerDefn());//添加属性信息string path = pszXMLFileName;int pos = path.find_last_of('\\');string tifName(path.substr(pos + 1) );poFeature->SetField("ImgName",tifName.c_str());//绘制外边框OGRLineString Line;OGRLinearRing ob_LinearRing;for (int n_point = 0; n_point < BoundaryPoint.size(); n_point++ ){OGRPoint Point(BoundaryPoint[n_point].x , BoundaryPoint[n_point].y );poFeature->SetField("X",BoundaryPoint[n_point].x);poFeature->SetField("Y",BoundaryPoint[n_point].y);poFeature->SetGeometry(&Point);if (poLayer->CreateFeature(poFeature)!=OGRERR_NONE){printf("Failed create feature in shapefile!");exit(1);}}OGRFeature::DestroyFeature(poFeature);OGRDataSource::DestroyDataSource(poShpDS);printf("创建矢量数据成功!\n");cout<<"BoundaryPoint.capacity :"<<BoundaryPoint.capacity()<<endl;vector<Point_xy>().swap(BoundaryPoint);cout<<"BoundaryPoint.capacity :"<<BoundaryPoint.capacity()<<endl;}
return true;
}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 脸部五官太小怎么办 小班教案我该怎么办 鼠标不能拖拽怎么办 病了怎么办教学反思 想开水果店没经验怎么办 婴儿听力筛查未通过怎么办 怀孕吐到胃出血怎么办 hp打印机颜色浅怎么办 爱普生打印机没有红色怎么办 孕吐吐到胃出血怎么办 孩子不会写2怎么办 口渴了怎么办大班教案 小班健康跌倒了怎么办 刷油漆有纹路怎么办 纯白门会发黄怎么办 发现小孩子偷钱怎么办 青春期孩子偷钱怎么办 孩子偷钱屡教不改怎么办 孩子经常偷钱怎么办 小孩子总是偷钱怎么办 小孩喜欢偷钱怎么办 小孩老是偷钱怎么办 如果孩子偷钱怎么办 小孩在家偷钱怎么办 买了西晒房怎么办 乳胶漆有打磨痕迹怎么办 墙上乳胶漆用水擦完太亮了怎么办 外墙腻子不好打磨怎么办 腻子打磨的灰尘怎么办 家里的壁纸脏了怎么办 家装用冷风管冬天取暖怎么办? 房子装修冷色系怎么办 高血压突然变低血压怎么办 玩单机游戏卡顿怎么办 老人血压太低怎么办 老年人血压太低怎么办 觉总是不够睡怎么办 老年人气不够用怎么办 卧室晒不到太阳怎么办 农村房子墙角受潮怎么办 房间里面有虫子怎么办