读取XML中的信息GDAL生成shp文件
发布时间:2020-12-16 02:19:43 所属栏目:百科 来源:网络整理
导读:在上一篇介绍了通过影像获取边界范围,这一篇介绍通过XML获取影像边界范围,不过下面是生成的点图层,需要的可以改为面图层。 在国产卫星影像(如GF1,GF2)压缩包中,通常会存在一个XML文件,里面存有一些影像的基本信息,先需要通过XML中的四角点的坐标生
在上一篇介绍了通过影像获取边界范围,这一篇介绍通过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 转为 double temPoint.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( "%sn",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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ruby-on-rails – 有没有办法为rails scaffold generator添
- 通过Ajax Laravel DataTable返回html内容(使用yajrabox包)
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 18 Automat
- ruby-on-rails – 如何使用simple_form在datetime字段中仅设
- <com原理>与<com内幕>的聚合实现部分。
- MP4 FLASH等播放代码
- webpack+es6+node+react初实践及总结
- cocos2dx: win32工程Release 和 Debug版本使用的标准库
- oracle数据库中为已经存在表的主键ID设置自增
- PostgreSQL:使用pgcrypto加密列