用于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. js用for循环模拟数组翻转

    文章地址 https://www.cnblogs.com/sandraryan/ js本身为数组提供了reverse()方法,可以翻转数组,返回一个新的数组,不影响原数组. 本例中用for循环简单模拟 ...

  2. Codeforces Round #181 (Div. 2)

    A. Array 模拟. B. Coach 模拟. C. Beautiful Numbers good number的位和最大不超过\(10^7\),那么只要枚举a或b的个数,然后最多循环7次判断位和 ...

  3. 常用mime.types

    以下是从nginx配置文件mime.types中提取出的最常用的文件格式, 整理了下, 方便查看 类型 文件格式 default_type application/octet-stream - tex ...

  4. There is no PasswordEncoder mapped for the id "null"的解决办法

    今日在SpringBoot项目中使用 Spring Security ,登录时发现报500错,报错信息如下: There is no PasswordEncoder mapped for the id ...

  5. P1009 字符三角形

    题目描述 输入一个字符c,按照阳历输出的格式输出由该字符组成的一个字符三角形. 输入格式 输入包含一个字符c. 输出格式 输出由该字符c组成的字符三角形. 样例输入 A 样例输出 A AAA AAAA ...

  6. JDK自带的native2ascii工具介绍

    背景:在做Java开发的时候,常常会出现一些乱码,或者无法正确识别或读取的文件,比如常见的validator验证用的消息资源(properties)文件就需要进行Unicode重新编码.原因是java ...

  7. ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(2)

    创建公共分页参数类Common/GridPager.cs using System; using System.Collections.Generic; using System.Linq; usin ...

  8. 【41.34%】【BZOJ 1003】[ZJOI2006]物流运输

    Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 6420  Solved: 2654 [Submit][Status][Discuss] Descri ...

  9. location对象相关

    JS是由DOM(文档对象模型).BOM(浏览器对象模型).以及ECMA组成,而location对象是BOM中的一个非常重要的对象,所有关于地址栏信息的内容都在这里.了解location对象之前让我们先 ...

  10. python数据分析经常使用的库

    这个列表包含数据分析经常使用的Python库,供大家使用.1. 网络通用urllib -网络库(stdlib).requests -网络库.grab – 网络库(基于pycurl).pycurl – ...