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

在上一篇介绍了通过影像获取边界范围,这一篇介绍通过XML获取影像边界范围,不过下面是生成的点图层,需要的可以改为面图层。
在国产卫星影像(如GF1,GF2)压缩包中,通常会存在一个XML文件,里面存有一些影像的基本信息,先需要通过XML中的四角点的坐标生成一个shp文件,即影像对应的有效范围边框。
在此也非常感谢提供Markup.h和Markup.cpp(需要的可以去我的资源中下载)的那个朋友微笑,帮我解决了一个大问题。也希望这能帮助需要的朋友。
 BoundaryPoint;
		Point_xy temPoint;   //点结构体对象
		if (xml.FindChildElem("SatelliteID"))
			cout<<"卫星:"<
		//XML中的四个点坐标
		if(xml.FindChildElem("TopLeftLatitude"))
			{
				CString str = (xml.GetChildData().GetBuffer(0));  //为了CString 转为 double
				temPoint.y =atof(str.GetBuffer(str.GetLength()));			
			}
		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"<
		//为了支持中文路径
	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 :"<().swap(BoundaryPoint);
	cout<<"BoundaryPoint.capacity :"<
return true;
 
  
}

你可能感兴趣的:(RS,&,GIS,经验)