源代码网址: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. Web Api 在线参考文档

    参考文档: https://developer.mozilla.org/zh-CN/docs/Web/API

  2. js二级下拉被flash档住的解决办法

    在<object></object>及以内的代码加入到<script>标签对内 <script language="javascript" ...

  3. 利用ICSharpCode.SharpZipLib.Zip进行文件压缩

    官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...

  4. Word2003使用VBA教程

    [正文] 注:本文中所有vba代码都是储存在doc中,而非normal.dot 1.打开一个.doc文档 2.按ALT+F11 3.左侧 Project-插入-模块 4.输入自己的代码,一定要是函数的 ...

  5. XAMPP 的安装配置

    --转载时请保留下面,以供大家加我MSN,增强交流,共同学习.--姜庭华  msn: jaimejth@live.cn--博客:http://blog.csdn.net/jaimejth 软件下载在以 ...

  6. 对于REST中无状态(stateless)的一点认识

    今天早上在Yahoo的邮件列表里看到一篇颇有意思的讨论,标题为RESTful vs. unRESTful: Session IDs and Authentication(51CTO编者注:意为REST ...

  7. Android通过代码获取View

    View view = LayoutInflater.from(mContext).inflate(R.layout.song_item_adapter, null); LayoutInflater ...

  8. rtree

    https://zh.wikipedia.org/wiki/R%E6%A0%91 http://blog.csdn.net/jiqiren007/article/details/5377750 htt ...

  9. C# GDI+学习笔记1

    —前言 本文是学习C# GDI+系列的第一篇文章,简单的介绍了GDI+的一些基本绘图内容,比较粗糙.但本文主要是让大家简单的回顾一下GDI+的基本概念.本篇文章的参考代码请在此下载 . GDIPTes ...

  10. 经典排序算法(Java版)

    1.冒泡排序 Bubble Sort 最简单的排序方法是冒泡排序方法.这种方法的基本思想是,将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮.在冒泡排序算法中我们要对这个“气泡” ...