源代码网址: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

C++、GDAL创建shapefile文件的更多相关文章

  1. C++、GDAL创建shapefile,并向矢量文件中添加网格

    //总体来说这个过程就是构建数据源->构建层->构建要素->构建形状->关闭数据源. //要包含的GDAL头文件 #include <gdal_priv.h> #i ...

  2. 在C#中使用GDAL创建Shape文件

    这几天在项目中考虑使用GDAL,由于10年没有用过VC了,就在网上搜了下怎么样在C# 中使用GDAL,看到了http://blog.csdn.net/liminlu0314/article/detai ...

  3. ENVI显示GDAL创建GeoTiff文件的一个问题及其思考

    作者:朱金灿 来源:http://blog.csdn.net/clever101 使用gdal创建一个100*100的红色的geotiff图像,代码如下: #include <assert.h& ...

  4. python-geopandas读取、创建shapefile文件

    作者:fungis 描述:一个热带生活.乐于分享.努力搬砖的giser 交流邮箱:fungis@163.com shapefile是GIS中非常重要的一种数据类型,在ArcGIS中被称为要素类(Fea ...

  5. 结合C++和GDAL实现shapefile(shp)文件的创建和写入

    工具:vs2012+GDAL 2.0 包含头文件: #include "ogrsf_frmts.h" int main() { const char *pszDriverName ...

  6. 使用Python Shapefile Library创建和编辑Shapefile文件

    介绍 shapefile是GIS中非常重要的一种数据类型,在ArcGIS中被称为要素类(Feature Classes),主要包括点(point).线(polyline)和多边形(polygon).P ...

  7. Java 使用GDAL 读写 shapefile

    读取shp文件,并把它转化为json import org.gdal.ogr.*; import org.gdal.ogr.Driver; import org.gdal.gdal.*; public ...

  8. GDAL 生成shp文件

    附件:http://pan.baidu.com/s/1i3GPwrV(C#版GDAL接口.dll) 示例程序: http://pan.baidu.com/s/1jpIKQ  (程序是在vs2008 x ...

  9. C#、C++用GDAL读shp文件(转载)

    C#.C++用GDAL读shp文件 C#用GDAL读shp文件 (2012-08-14 17:09:45) 标签: 杂谈 分类: c#方面的总结 1.目前使用开发环境为VS2008+GDAL1.81 ...

随机推荐

  1. http server 下载地址

    windows 64为位:https://www.apachelounge.com/download/

  2. linux 命令行字符终端terminal下强制清空回收站

    回收站其实就是一个文件夹,存放被删掉的文件. ubuntu 回收站的路径: $HOME/.local/share/Trash/ 强制清空回收站: rm -fr $HOME/.local/share/T ...

  3. 戴维·卡梅伦(David William Donald Cameron)经典语录

    戴维·威廉·唐纳德·卡梅伦(英语:David William Donald Cameron,1966年10月9日-),汉化译名为甘民乐.现任英国首相.第一财政大臣.公务员事务部部长和保守党党魁,也是英 ...

  4. MyEclipse8.5注册码 到2015年

    Subscriber: onechenProduct ID: E3MP (MyEclipse Professional Subscription)License version: 3.0Full Ma ...

  5. Android SDK更新 Connection to http://dl-ssl.google.com refused 解决方法

    问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository ...

  6. [转]linux之date命令

    转自:http://www.cnblogs.com/peida/archive/2012/12/13/2815687.html 在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用 ...

  7. eclipse quick diff功能

    Eclipse文本编辑器和Java编辑器都提供了quick diff功能.这就使得你可以快速地识别出当前所编辑文件版本和该文件的参考版本之间的不同. 如果编辑器的quick diff功能没有启用,可以 ...

  8. 提供给开发者 10 款最好的 Python IDE

    Python 非常易学,强大的编程语言.Python 包括高效高级的数据结构,提供简单且高效的面向对象编程. Python 的学习过程少不了 IDE 或者代码编辑器,或者集成的开发编辑器(IDE).这 ...

  9. PermGen space Eclipse 终极解决方案

    1.选中项目右键 run or debug configurations... 2.在 VM arguments 加入  -Xms128m -Xmx512m -XX:PermSize=64M -XX: ...

  10. PASCAL相关图书推荐

    PASCAL程序设计(第2版) 作      者 郑启华 著 出 版 社 清华大学出版社 出版时间 2013-01-01 版      次 2 页      数 286 印刷时间 2013-01-01 ...