读取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
- 读取XML中的信息GDAL生成shp文件
- C# Gdal 读取SHP文件
- C#中Gdal读取SHP文件
- C#中Gdal读取SHP文件
- GDAL读取shp文件VS2015+GDAL2.1
- GDAL读写shp文件
- 【GDAL/OGR】利用GDAL/OGR读取shp文件并转换为json文件(Java版)
- C#不用ArcEngine,生成Shp文件(二)---------读取.shp文件格式
- 在GDAL拒绝生成.aux.xml文件
- C#、C++用GDAL读shp文件
- GDAL-OGR对shp文件的操作
- node-gdal实践-创建shp文件
- C#、C++用GDAL读shp文件
- GDAL矢量(.shp)文件读写与创建
- shp文件的读取
- shp文件的读取
- 读取XML文件信息
- GDAL读取jpg文件中GPS坐标信息
- HTML-文本格式化
- 开发者和程序员需要关注的42个播客
- hdoj1078【DP·记忆化搜索】
- 将UILongPressGestureRecognizer写成Block
- CSU - 1774 慷慨的奖励(模拟链表)
- 读取XML中的信息GDAL生成shp文件
- Linux下MPI并行编程环境搭建配置
- WebService服务创建
- java动态代理(JDK和cglib)
- 表达式的转换(sdut-2484)
- SICP-Python版本(1.7)
- 嵌入式linux基于netkit-ftp-0.17的ftp客户端移植
- Android编程:UDP客户端和TCP客户端
- in-第一周总结