GDAL:创建矢量线、矢量面数据

分享给有需要的人,代码质量勿喷。

C/C++——常规——附加包含目录——添加include目录

链接器——输入——附加依赖项——添加...\gdal_i.lib

一、创建矢量线数据 单个要素

//GDAL
#include "ogrsf_frmts.h"

void xjCreateVectorLineByGDAL(QList xjListNode, const QString &xjSavePath)
 {
	 GDALAllRegister();
	 OGRRegisterAll();
	 const char *xjDriverName = "ESRI Shapefile";
	 GDALDriver *xjDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(xjDriverName);
	 if (xjDriver == NULL)
	 {
		 QMessageBox::warning(0, ("prompt"), QString(xjDriverName) + "driver not available!!!");
		 return;
	 }

	 GDALDataset *xjDataset = xjDriver->Create(xjSavePath.toStdString().c_str(), 0, 0, 0, GDT_Unknown, NULL);
	 if (xjDataset == NULL)
	 {
		 QMessageBox::warning(0, ("prompt"), "Creation of output file failed!!!");
		 return;
	 }

	 OGRLayer *xjLayer = xjDataset->CreateLayer("point_out", NULL, wkbLineStringZM, NULL);
	 if (xjLayer == NULL)
	 {
		 QMessageBox::warning(0, ("prompt"), "Layer creation failed!!!");
		 return;
	 }

/* GDALDataset* xjDataset = (GDALDataset*)GDALOpenEx(xjShpPath.toStdString().c_str(), GDAL_OF_UPDATE, NULL, NULL, NULL);
	OGRLayer *xjLayer = xjDataset->GetLayer(0); */

	 //创建属性字段
	 OGRFieldDefn fieldID("ID", OFTInteger);
	 fieldID.SetWidth(32);
	 xjLayer->CreateField(&fieldID);
	 OGRFieldDefn fieldNAME("NAME", OFTString);
	 fieldNAME.SetWidth(32);
	 xjLayer->CreateField(&fieldNAME);
	 OGRFieldDefn fieldLENGTH("LENGTH", OFTReal);
	 fieldLENGTH.SetPrecision(16);
	 xjLayer->CreateField(&fieldLENGTH);

	 //创建要素
	 OGRFeature *xjFeature = OGRFeature::CreateFeature(xjLayer->GetLayerDefn());
	 OGRLineString xjLine;
	 int pointCount = xjListNode.size();
	 for (int i = 0; i < pointCount; i++)
	 {
		 xjPoint Pd = xjListNode.at(i);
		 OGRPoint pt;
		 pt.setX(Pd.x);
		 pt.setY(Pd.y);
		 pt.setZ(Pd.z);
		 xjLine.addPoint(&pt);
	 }
	 xjFeature->SetGeometry(&xjLine);

	 //属性
	 xjFeature->SetField("ID", "id");
	 xjFeature->SetField("NAME", "name");
	 xjFeature->SetField("LENGTH", 2.3);

	 //判断
	 if (xjLayer->CreateFeature(xjFeature) != OGRERR_NONE)
	 {
		 QMessageBox::warning(0, ("prompt"), "Failed to create feature in shapefile!!!");
		 return;
	 }
	 OGRFeature::DestroyFeature(xjFeature);

	 GDALClose(xjDataset);
 }

二、创建矢量面数据 多个要素

 void xjCreateVectorPolygonByGDAL(const QList> &xjListPoint, const QString &xjSavePath)
 {
	 GDALAllRegister();
	 OGRRegisterAll();
	 const char *xjDriverName = "ESRI Shapefile";
	 GDALDriver *xjDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(xjDriverName);
	 if (xjDriver == NULL)
	 {
		 QMessageBox::warning(0, "prompt", "error of GDAL: Driver not available!!!");
		 return;
	 }

	 GDALDataset *xjDataset = xjDriver->Create(xjSavePath.toStdString().c_str(), 0, 0, 0, GDT_Unknown, NULL);
	 if (xjDataset == NULL)
	 {
		 QMessageBox::warning(0, "prompt", "error of GDAL: Creation of output file failed!!!");
		 return;
	 }

	 OGRLayer *xjLayer = xjDataset->CreateLayer("polygon1", NULL, wkbPolygonZM, NULL);
	 if (xjLayer == NULL)
	 {
		 QMessageBox::warning(0, "prompt", "error of GDAL: Layer creation failed!!!");
		 return;
	 }

	 //创建属性字段
	 OGRFieldDefn fieldID("ID", OFTInteger);
	 fieldID.SetWidth(32);
	 xjLayer->CreateField(&fieldID);
	 OGRFieldDefn fieldNAME("NAME", OFTString);
	 fieldNAME.SetWidth(32);
	 xjLayer->CreateField(&fieldNAME);
	 OGRFieldDefn fieldAREA("AREA", OFTReal);
	 fieldAREA.SetPrecision(16);
	 xjLayer->CreateField(&fieldAREA);

	 //创建要素
	 for (int i = 0; i < xjListPoint.size(); i++)
	 {
		 //创建要素
		 OGRFeature *xjFeature = OGRFeature::CreateFeature(xjLayer->GetLayerDefn());
		 //矢量面要素的边界是闭合环
		 OGRLinearRing xjRing;
		 QList list = xjListPoint.at(i);
		 for (int j = 0; j < list.size(); j++)
		 {
			 xjRing.addPoint(list.at(j).x, list.at(j).y, list.at(j).z);
		 }
		 xjRing.closeRings();//首尾点重合形成闭合环

		 //图层添加要素
		 OGRPolygon xjPolygon;
		 xjPolygon.addRing(&xjRing);
		 xjFeature->SetGeometry(&xjPolygon);

		 //设置属性
		 xjFeature->SetFID(i);
		 xjFeature->SetField(0, i);
		 std::string pname = "name_" + to_string(i);
		 xjFeature->SetField(1, pname.c_str());
		 xjFeature->SetField("AREA", 2.2);

		 //判断
		 if (xjLayer->CreateFeature(xjFeature) != OGRERR_NONE)
		 {
			 QMessageBox::warning(0, "prompt", "error of GDAL: Failed to create feature in shapefile!!!");
			 continue;
		 }
		 OGRFeature::DestroyFeature(xjFeature);
	 }
	 GDALClose(xjDataset);
 }

 

你可能感兴趣的:(GDAL,C++)