用于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. JavaScript中判断整数的方法

    一.使用取余运算符判断 任何整数都会被1整除,即余数是0.利用这个规则来判断是否是整数. 1 2 3 4 5 function isInteger(obj) {     return obj%1 == ...

  2. 4-3 调试代码命令 scrapy shell http://blog.jobbole.com/114496/(入口url)

    调试代码命令 scrapy shell http://blog.jobbole.com/114496/(入口url)

  3. 高可用之keepalived的配置文件详解

    ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover ...

  4. C# 从 short 转 byte 方法

    本文告诉大家多个方法转换 short 和 byte 有简单的也有快的 快速简单的方法 static short ToShort(short byte1, short byte2) { return ( ...

  5. 2019-9-24-dotnet-remoting-使用事件

    title author date CreateTime categories dotnet remoting 使用事件 lindexi 2019-09-24 15:39:26 +0800 2018- ...

  6. linux 老式 PCI 探测

    在老的内核版本中, 函数 pci_register_driver, 不是一直被 PCI 驱动使用. 相反, 它 们要么手工浏览系统中的 PCI 设备列表, 要么它们将调用一个能够搜索一个特定 PCI ...

  7. Linux 内核同步 urb

    不幸的是, 同步 urb 没有一个象中断, 控制, 和块 urb 的初始化函数. 因此它们必须在 驱动中"手动"初始化, 在它们可被提交给 USB 核心之前. 下面是一个如何正确初 ...

  8. 使用git命令修改commit提交信息

    很多时候我们在提交代码时可能会把commit提交信息写错了,这个时候我们就可以用到下面的git命令来修改commit提交信息 git commit --amend 输入"i"之后进 ...

  9. Laravel -- Excel 导入(import) (v2.1.0)

    原博客地址 https://www.jianshu.com/p/7287ebdc77bb Install (安装) //> composer.json 中 require中添加如下: " ...

  10. gradle 打包后第三方登录不上

    使用 gradlew clean assembleReleaseChannels 生成不用的渠道包后 第三方登录不上 原因:打包未设置好APP的 .keystore