GDAL写点shp(多个点、带属性)

 附上gdal书籍的源码及数据:GDAL书籍源码GDAL书籍数据

void CPowerTower_SlopeDoc::writevectorpoint(std::string PathName, CString type_name, osg::Vec3dArray * m_pts)
{
	ref_ptr<orsISpatialReference> utm = getSRService()->CreateSpatialReference();
	utm->importFromEPSG(32649);

	CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
	CPLSetConfigOption("SHAPE_ENCODING", "");

	GDALAllRegister();
	const char *pszDriverName = _T("ESRI Shapefile");
	GDALDriver *poDriver;
	poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName);

	if (poDriver == NULL)

	{
		AfxMessageBox(_T("驱动器不可用"));
		return;
	}

	OGRSpatialReference InputOGRS;
	InputOGRS.SetWellKnownGeogCS("WGS84");

	GDALDataset *poDS = poDriver->Create(PathName.c_str(), 0, 0, 0, GDT_Unknown, NULL);
	OGRLayer* poLayer = poDS->CreateLayer(type_name, &InputOGRS, wkbPoint25D, NULL);
	if (poLayer == NULL)

	{
		AfxMessageBox(_T("创建输出文件失败"));
		return;
	}

	//属性表创建
	OGRFieldDefn oFieldId("ID", OFTInteger);
	OGRFieldDefn oFieldx("x", OFTReal);
	OGRFieldDefn oFieldy("y", OFTReal);
	OGRFieldDefn oFieldz("z", OFTReal);
	oFieldId.SetWidth(5);
	oFieldx.SetWidth(32);
	oFieldy.SetWidth(32);
	oFieldz.SetWidth(32);
	poLayer->CreateField(&oFieldId);
	poLayer->CreateField(&oFieldx);
	poLayer->CreateField(&oFieldy);
	poLayer->CreateField(&oFieldz);

	CPowerTower_SlopeView *pView;
	POSITION pos = GetFirstViewPosition();
	pView = (CPowerTower_SlopeView *)GetNextView(pos);

	OGRPoint point;
	OGRPolygon poly;
	OGRLinearRing ring;

	int nSize = m_pts->size();
	for (int i = 0; i < nSize; i++)
	{
		orsPOINT3D pt((*m_pts)[i].x(), (*m_pts)[i].y(), (*m_pts)[i].z());
		orsPOINT3D gc;
		utm->ToGeocentric(pt, &gc);

		point.setX(gc.X);
		point.setY(gc.Y);
		point.setZ(gc.Z);
	
		//属性表赋值
		OGRFeature *poFeature;
		poFeature = OGRFeature::CreateFeature(poLayer->GetLayerDefn());

		poFeature->SetField("ID", i);
		poFeature->SetField("x", gc.X);
		poFeature->SetField("y", gc.Y);
		poFeature->SetField("z", gc.Z);

		poFeature->SetGeometry(&point);
		if (poLayer->CreateFeature(poFeature) != OGRERR_NONE)
		{
			printf("Failed to create feature in shapefile.\n");
			return;
		}
		OGRFeature::DestroyFeature(poFeature);
	}

	GDALClose(poDS);
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值