源代码网址: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,再按回车。

此实例,是在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

最新文章

  1. unity gizmo绘制圆形帮助调试
  2. logback使用总结
  3. Java NIO原理分析
  4. Linux环境下vsftpd参数配置
  5. PLSQL_基础系列07_插入方式Pivoting / Unconditional / Conditional ALL / Conditional FIRST INSERT(案例)
  6. FZU2234 牧场物语 DP
  7. can&#39;t find -lsocket的解决办法
  8. Robotium API -- click/clickLong操作
  9. NET分布式缓存Memcached测试体验
  10. 8、手把手教你Extjs5(八)自定义菜单2
  11. workflow--相关笔记
  12. 源码包安装php7.2
  13. 洛谷 4115 Qtree4——链分治
  14. 初探react(一)
  15. Shell函数和数组
  16. 阅读代码工具:Visual Studio Code
  17. css实现固定行
  18. Delphi笔记-自定义提示窗口
  19. column count of mysql.proc is wrong. expected 20,found 16. the table is probably corruptd.
  20. [Xamarin.iOS] 如何引用Objective-c寫的Class Library (转帖)

热门文章

  1. modeler与activiti进行整合
  2. Struts2实现Preparable接口和【struts2】继承ActionSupport类
  3. Java 中无参无返回值方法的使用
  4. Nodepad++ tab改成4个空格
  5. web医疗影像浏览demo及地址
  6. 字符串中符号的替换---replace的用法
  7. eclipse 在Navigator视图中查看资源
  8. ansible官方文档翻译之变量
  9. 多线程的Python 教程--“贪吃蛇”
  10. Linux中的.emacs文件