C++、GDAL创建shapefile文件

源代码网址:http://download.csdn.net/detail/ivanljf/5834823

一、先贴出第一段代码:

 

#include "ogrsf_frmts.h"
#include <iostream>
using namespace std;
int main()
{
	const char *pszDriverName = "ESRI Shapefile";
	OGRSFDriver *poDriver;

	OGRRegisterAll();

	poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
		pszDriverName );
	if( poDriver == NULL )
	{
		printf( "%s driver not available.\n", pszDriverName );
		exit( 1 );
	}
	OGRDataSource *poDS;
	poDS = poDriver->CreateDataSource( "point_out.shp", NULL );//但文件夹中以前存在point_out.shp文件时会报错;
	if( poDS == NULL )
	{
		printf( "Creation of output file failed.\n" );
		exit( 1 );
	}

	OGRLayer *poLayer;
	poDS->CreateLayer( "point_out_layer", NULL, wkbPoint, NULL );
	poLayer=NULL;
	poLayer = poDS->GetLayer(0);//shp文件只有一个图层;
	if( poLayer == NULL )
	{
		printf( "Layer creation failed.\n" );
		exit( 1 );
	}

	OGRFieldDefn oField( "Name", OFTString );

	oField.SetWidth(32);

	if( poLayer->CreateField( &oField ) != OGRERR_NONE )
	{
		printf( "Creating Name field failed.\n" );
		exit( 1 );
	}

	double x, y;
	char szName[33];
	cout<<"输入:x,y,name"<<"(Example:30,30,lu)"<<"逗号为英文下的逗号,否则按回车便结束程序"<<endl;
	cout<<"要想结束输入,按ctrl+d,再按回车键"<<endl;
	while( !feof(stdin) // stdin文件流的结束符按ctrl+d,这样便结束while循环;
		&& fscanf( stdin, "%lf,%lf,%32s", &x, &y, szName ) == 3 )
	{
		OGRFeature *poFeature;
		poFeature=OGRFeature::CreateFeature(poLayer->GetLayerDefn());//必须用CreateFeature来生成对象,用new生成对象出错
		//poFeature = new OGRFeature( poLayer->GetLayerDefn() );//必须用CreateFeature来生成对象,用new生成对象出错
		poFeature->SetField( "Name", szName );

		OGRPoint pt;

		pt.setX( x );
		pt.setY( y );

		poFeature->SetGeometry( &pt ); 

		if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
		{
			printf( "Failed to create feature in shapefile.\n" );
			exit( 1 );
		}

		OGRFeature::DestroyFeature( poFeature );
	}
	
	OGRDataSource::DestroyDataSource( poDS );
}


该代码的注意事项:

1、在实例化要素时,原来用的是:poFeature = new OGRFeature( poLayer->GetLayerDefn() ); 但在OGRFeature::DestroyFeature( poFeature );报错,后来改为poFeature=OGRFeature::CreateFeature(poLayer->GetLayerDefn());来实例化要素。

2、在运行程序时,要求输入x,y坐标,以及Name的属性值,三个值之间用逗号隔开,注意:逗号的形式应是英文下的逗号,否则出错,之间退出程序;

3、要想结束输入,退出程序,需按ctrl+d,再按回车。

 

C++、GDAL创建shapefile文件_第1张图片

此实例,是在shp文件中输入了5个点:

C++、GDAL创建shapefile文件_第2张图片

 

二、再贴出第二段代码:

#include "gdal_priv.h"
#include "ogrsf_frmts.h"
#include <iostream>
using namespace std;
int main(int argc,char* argv[])
{
	OGRRegisterAll();
	const char *pszDriverName="ESRI Shapefile";
	OGRSFDriver *poDriver;
	poDriver=OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
	if (poDriver==NULL)
	{
		cout<<pszDriverName<<" driver not available."<<endl;
		//exit(1);
	}
	OGRDataSource *poDS;
	poDS=poDriver->CreateDataSource("F:\\point_out1.shp",NULL);
	if (poDS==NULL)
	{
		cout<<"Creation of point_out.shp file failed."<<endl;
		//exit(1);
	}
	OGRLayer *poLayer;
	poLayer=poDS->CreateLayer("point_out",NULL,wkbPoint,NULL);

	if (poLayer==NULL)
	{
		cout<<"Layer creation failed."<<endl;
		//exit(1);
	}
	OGRFieldDefn firstField("faci_code",OFTInteger);
	OGRFieldDefn secondField("X",OFTReal);
	OGRFieldDefn thirdField("Y",OFTReal);
	firstField.SetWidth(32);
	secondField.SetWidth(32);
	thirdField.SetWidth(32);
	poLayer->CreateField(&firstField);
	poLayer->CreateField(&secondField);
	poLayer->CreateField(&thirdField);

	double x,y;
	int code;
	for(int i=0;i!=100;++i)
	{
		code=i+1;
		x=i;
		y=100+i;
		OGRFeature *poFeature;
		poFeature=OGRFeature::CreateFeature(poLayer->GetLayerDefn());
		poFeature->SetField("faci_code",code);
		poFeature->SetField("X",x);
		poFeature->SetField("Y",y);
		OGRPoint pt;
		pt.setX(x);
		pt.setY(y);
		poFeature->SetGeometry(&pt);
		if (poLayer->CreateFeature(poFeature)!=OGRERR_NONE)
		{
			cout<<"Failed to create feature in shapefile."<<endl;
			//exit(1);
		}
		OGRFeature::DestroyFeature(poFeature);
	}
	OGRDataSource::DestroyDataSource(poDS);
	//system("pause");
	return 0;
}


注意事项:

1、同一段代码一样,在创建要素时,用CreateFeature函数,而不是new.

2、用ENVI打开的效果:

C++、GDAL创建shapefile文件_第3张图片

 

源代码网址:http://download.csdn.net/detail/ivanljf/5834823

你可能感兴趣的:(C++、GDAL创建shapefile文件)