• 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.
    ", pszDriverName );
    		exit( 1 );
    	}
    	OGRDataSource *poDS;
    	poDS = poDriver->CreateDataSource( "point_out.shp", NULL );//但文件夹中以前存在point_out.shp文件时会报错;
    	if( poDS == NULL )
    	{
    		printf( "Creation of output file failed.
    " );
    		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.
    " );
    		exit( 1 );
    	}
    
    	OGRFieldDefn oField( "Name", OFTString );
    
    	oField.SetWidth(32);
    
    	if( poLayer->CreateField( &oField ) != OGRERR_NONE )
    	{
    		printf( "Creating Name field failed.
    " );
    		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.
    " );
    			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,再按回车。

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

    二、再贴出第二段代码:

    #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打开的效果:

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

  • 相关阅读:
    各种小例子
    作业 5/20
    课程总结
    构建之法 读书笔记一
    Android实现本地音频播放(可拖动条)
    自我介绍
    上周总结
    《梦断代码》读书笔记三
    《梦断代码》读书笔记二
    《梦断代码》读书笔记一
  • 原文地址:https://www.cnblogs.com/aukle/p/3226228.html
Copyright © 2020-2023  润新知