用于GDAL,C++开发环境测试。

#include <iostream>
#include "gdal_priv.h"
#include "ogrsf_frmts.h"
#include "ogr_geometry.h"
#pragma comment (lib,"C:\\warmerda\\bld\\lib\\gdal_i.lib")
using namespace std; void CreateShapeFile()
{
const char *pszDriverName = "ESRI Shapefile";
OGRSFDriver *poDriver; OGRRegisterAll(); poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
pszDriverName );
if( poDriver == NULL )
{
printf( "%s driver not available.\n", pszDriverName );
exit( );
} OGRDataSource *poDS; poDS = poDriver->CreateDataSource( "point_out.shp", NULL );
if( poDS == NULL )
{
printf( "Creation of output file failed.\n" );
exit( );
} OGRLayer *poLayer; poLayer = poDS->CreateLayer( "point_out", NULL, wkbPoint, NULL );
if( poLayer == NULL )
{
printf( "Layer creation failed.\n" );
exit( );
} OGRFieldDefn oField( "Name", OFTString ); oField.SetWidth(); if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
printf( "Creating Name field failed.\n" );
exit( );
} double x, y;
char szName[]; while( !feof(stdin)
&& fscanf( stdin, "%lf,%lf,%32s", &x, &y, szName ) == )
{
OGRFeature *poFeature; poFeature = new OGRFeature( poLayer->GetLayerDefn() );
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( );
}
OGRFeature::DestroyFeature( poFeature );
}
OGRDataSource::DestroyDataSource( poDS ); }
int main()
{
//CreateShapeFile();
//cout<<"创建成功"<<endl;
const char *pszDriverName="ESRI Shapefile";
OGRSFDriver *poDriver;
OGRRegisterAll();
poDriver=OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
if(poDriver==NULL)
{
cout<<"注册失败"<<endl;
}
OGRDataSource *poDS;
poDS=poDriver->CreateDataSource("point_out.shp",NULL);
if(poDS==NULL)
{
cout<<"创建驱动失败!"<<endl;
}
OGRLayer *poLayer;
poLayer=poDS->CreateLayer("point_out",NULL,wkbPoint,NULL);
if(poLayer==NULL)
{
cout<<"创建图层失败"<<endl;
}
OGRFieldDefn oField("Name",OFTString);
oField.SetWidth();
if(poLayer->CreateField(&oField)!=OGRERR_NONE)
{
cout<<"添加字段失败!"<<endl;
}
//下面从stdin中循环读取 “x,y,name”格式的点信息。
double x=,y=;
char szName[];
for(int a=;a!=;++a)
{
x+=a;
y+=a;
OGRFeature *poFeature;
poFeature=new OGRFeature(poLayer->GetLayerDefn());
poFeature->SetField("Name",szName);
OGRPoint pt;
pt.setX(x);
pt.setY(y);
poFeature->SetGeometry(&pt);
if(poLayer->CreateFeature(poFeature)!=OGRERR_NONE)
{
cout<<"创建要素失败"<<endl;
}
//OGRFeature::DestroyFeature(*poFeature);//创建一个feature.OGRLayer::CreateFeature() 只是重新复制了一个feature,因此操作完成后需要清除feature对象
}
//最后释放datasoure资源,刷新所有写的操作
OGRDataSource::DestroyDataSource(poDS);
cout<<"文件创建成功!"<<endl;
return ;
}

GDAl C++ 创建Shp的更多相关文章

  1. [转] arcgis Engine创建shp图层

    小生 原文 arcgis Engine创建shp图层 以创建点图层为例.首先要得到保存文件的地址. SaveFileDialog saveFileDialog = new SaveFileDialog ...

  2. AE CreateFeatureClass 创建shp. 删除shp. 向shp中添加要素

    /// <summary> /// 创建多边形shp /// </summary> /// <param name="pPolygon">< ...

  3. GDAL C#读取shp中文属性值乱码问题

    GDAL的C#版本读取shp中,如果属性值中含有中文,读出来有可能是乱码的问题,根据SWIG生成的C#代码调试发现问题所在,在Ogr.cs文件中有这么一个函数,代码如下: internal stati ...

  4. arcEngine开发之根据点坐标创建Shp图层

    思路 根据点坐标创建Shapefile文件大致思路是这样的: (1)创建表的工作空间,通过 IField.IFieldsEdit.IField 等接口创建属性字段,添加到要素集中. (2)根据获取点的 ...

  5. GDAL显示线性shp文件

    http://pan.baidu.com/s/1qWIDphU  (工程文件在vs2008中编写) 1.使用到的技术 GDAL:读取矢量数据 GDI:    绘制矢量数据 2.详细解释 GDI绘图: ...

  6. 【gdal】创建GeoTiff栅格数据

    1 //定义转换参数 2 private readonly double[] d_transform = { 69.999999999999972, 0.01, 0.0, 44.99999999999 ...

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

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

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

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

  9. Android GIS开发系列-- 入门季(13)Gdal简单写个shp文件

    Gdal是用来读写栅格与矢量数据的,在Gdal官网,可以下载相关的资源进行平台的编译.其实Arcgis底层也是用Gdal来读取shp文件的,那在Android中可以直接读写shp文件吗,是可以的.这里 ...

随机推荐

  1. IE下form表单密码输入框可以输入中文问题

    今天遇到了一个问题: 在IE浏览器登录界面,密码输入框,切换到中文输入法,竟然可以输入中文,已经设置过了input的type="password". 解决方法: 可以给input设 ...

  2. 彻底解决tensorflow:ImportError: Could not find 'cudart64_90.dll' tensorflow安装

    今天装tensorflow-gpu出现了很多问题 1.pip install tensorflow-gpu下载过慢 解决办法可查看 Python机器学习常用模块 2.安装完tensorflow以后,运 ...

  3. JQuery操作select下拉框

    JQuery操作select下拉框 获取Select选择的Text和Value $("#select_id").change(function(){//code...}); //为 ...

  4. Nuget 通过 dotnet 命令行发布

    在开发完成一个好用的轮子就想将这个轮子发布到 nuget 让其他小伙伴可以来使用,但是 nuget.org 的登陆速度太慢,本文介绍一个命令行发布的方法,通过命令行发布的方法可以配合 Jenkins ...

  5. H3C FTP主动数据传输方式

  6. linux 使用 gdb

    gdb 对于看系统内部是非常有用. 在这个级别精通调试器的使用要求对 gdb 命令有信心, 需要理解目标平台的汇编代码, 以及对应源码和优化的汇编码的能力. 调试器必须把内核作为一个应用程序来调用. ...

  7. 喵喵电影git操作

    1.git remote 2.git remote add origin '项目地址'   (origin为远程仓库名字) 3.git remote 4.git push origin master ...

  8. 51nod 1282 时钟

    1282 时钟  题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N个时钟,每个时钟有M个指针,P个刻度.时钟是圆形的,P个刻度均分 ...

  9. CF1209

    CF1209 A B 水题不管 C 因为要求最终整个序列是要单调的 所以我们就考虑枚举断点$x$ 之后把$<x$的数放到第一个集合 把$> x$的数放到第二个集合 至于$=x$的数 他能放 ...

  10. Spring+dubbo错误(一)

    10-21 20:51:39.593 ERROR [ContextLoader.java:351] Context initialization failed org.springframework. ...