源代码网址: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. 新浪微博顶部新评论提示层效果——position:fixed

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. PHP截取中文字符串

    这里的输出的长度是6,那么一个汉字的字符长度就是3咯,可是老师演示的一个字符的长度却是2,百思不得其解. 查了一下资料发现,这个问题的答案与系统所采用的字符编码方式有关: 1. utf-8 如果系统采 ...

  3. cocos2dx lua中继承与覆盖C++方法

    cocos2dx的extern.lua中的class方法为lua扩展了面向对象的功能,这使我们在开发中可以方便的继承原生类 但是用function返回对象的方法来继承C++类是没有super字段的,这 ...

  4. Effective java笔记6--异常

    充分发挥异常的优点,可以提高一个程序的可读性.可靠性和可维护性.如果使用不当的话,它们也会带来负面影响. 一.只针对不正常的条件才使用异常 先看一段代码: //Horrible abuse of ex ...

  5. MySQL定期分析检查与优化表

    定期分析表   ANALYZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name]   本语句用于分析和存储表的关键字分布.在分析期间,使 ...

  6. SPF详解

    什么是SPF? 这里的SPF不是防晒指数,而是指Sender Policy Framework.翻译过来就是发信者策略架构,比较拗口,通常都直接称为SPF. SPF是跟DNS相关的一项技术,它的内容写 ...

  7. 9、NFC技术:NDEF文本格式解析

    NDEF文本格式规范     不管什么格式的数据本质上都是由一些字节组成的.对于NDEF文本格式来说.这些数据的第1个字节描述了数据的状态,然后若干个字节描述文本的语言编码,最后剩余字节表示文本数据. ...

  8. Unity 5 引擎收费版和免费版的区别(转)

    最新Unity 5的Professional Edition(收费版)具备全新而强大的功能,除了全局动态光照或是最新的基于物理的着色器之外,也把原本分开销售的Team License放入,并含有12个 ...

  9. 转来的 cuda makefile 写法学习

    原文作者:FreeAquar 原文出处:http://www.cnblogs.com/FreeAquar/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给 ...

  10. anible包模块管理

    ansible使用包管理模块 一般使用的包管理模块的RPM,和YUM 参数 必填 默认 选择 说明 Conf_file No YUM的配置文件 Disable_dbg_check No No Yes/ ...