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

前端之家收集整理的这篇文章主要介绍了读取XML中的信息GDAL生成shp文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
上一篇介绍了通过影像获取边界范围,这一篇介绍通过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( "%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;
 
}
原文链接:https://www.f2er.com/xml/295017.html

猜你在找的XML相关文章