C++、GDAL创建shapefile文件
源代码网址: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文件的更多相关文章
- C++、GDAL创建shapefile,并向矢量文件中添加网格
//总体来说这个过程就是构建数据源->构建层->构建要素->构建形状->关闭数据源. //要包含的GDAL头文件 #include <gdal_priv.h> #i ...
- 在C#中使用GDAL创建Shape文件
这几天在项目中考虑使用GDAL,由于10年没有用过VC了,就在网上搜了下怎么样在C# 中使用GDAL,看到了http://blog.csdn.net/liminlu0314/article/detai ...
- ENVI显示GDAL创建GeoTiff文件的一个问题及其思考
作者:朱金灿 来源:http://blog.csdn.net/clever101 使用gdal创建一个100*100的红色的geotiff图像,代码如下: #include <assert.h& ...
- python-geopandas读取、创建shapefile文件
作者:fungis 描述:一个热带生活.乐于分享.努力搬砖的giser 交流邮箱:fungis@163.com shapefile是GIS中非常重要的一种数据类型,在ArcGIS中被称为要素类(Fea ...
- 结合C++和GDAL实现shapefile(shp)文件的创建和写入
工具:vs2012+GDAL 2.0 包含头文件: #include "ogrsf_frmts.h" int main() { const char *pszDriverName ...
- 使用Python Shapefile Library创建和编辑Shapefile文件
介绍 shapefile是GIS中非常重要的一种数据类型,在ArcGIS中被称为要素类(Feature Classes),主要包括点(point).线(polyline)和多边形(polygon).P ...
- Java 使用GDAL 读写 shapefile
读取shp文件,并把它转化为json import org.gdal.ogr.*; import org.gdal.ogr.Driver; import org.gdal.gdal.*; public ...
- GDAL 生成shp文件
附件:http://pan.baidu.com/s/1i3GPwrV(C#版GDAL接口.dll) 示例程序: http://pan.baidu.com/s/1jpIKQ (程序是在vs2008 x ...
- C#、C++用GDAL读shp文件(转载)
C#.C++用GDAL读shp文件 C#用GDAL读shp文件 (2012-08-14 17:09:45) 标签: 杂谈 分类: c#方面的总结 1.目前使用开发环境为VS2008+GDAL1.81 ...
随机推荐
- Oracle安装错误ora-00922(zhuan)
Oracle安装错误ora-00922(缺少或无效选项) (2012-03-19 10:49:27) 转载▼ 标签: 杂谈 安装Oracle 11g R2的过程中,在新建数据库实例时出现了该错误, ...
- BZOJ 1911 特别行动队
另一个版本的斜率优化...这个要好理解一些. #include<iostream> #include<cstdio> #include<cstring> #incl ...
- C# list installed softwares
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Product&q ...
- setTimeout/setInterval
//使用 setTimeout 时需注意,当该代码执行时,JS 会立即编译函数第一个参数“code” //所以该函数的第一个参数应该为:需要编译的代码.或者一个函数 //例1:setTimeout(& ...
- AIX 第7章 指令记录
要点: AIX文件系统的访问路径 AIX文件系统目录树 创建AIX文件系统 文件系统的卸载和删除 文件系统的自动挂载 文件系统的容量管理 文件系统的一致性管理 文件系统的卸载失败 文件系统的快照管理 ...
- Oracle 11gR2用gpnp profile存放ASM的spfile路径
从Oracle 11gR2开始,GI集成了ASM,OCR/VOTEDISK也存放在ASM磁盘组了(11gR2以前需要存放于裸设备中),同时ASM的功能较10g也有很大增强. 我们先引入一个问题:11g ...
- jboss集成eclipse
eclipse Kepler + Jboss7.1 参考引用文档: http://www.tekdigest.com/how-to-install-jboss-tools-in-eclipse.htm ...
- Android MVPR 架构模式
最近我在尝试让 Google 的 IO App 变得可单元测试,我这样做的其中一个原因是验证 Freeman 和 Pryce 在引用中对单元测试的总结.即使现在我还是没有把 IOSched 中的任何一 ...
- android EditText控件可输入正负数及小数位
设置android:inputType="numberSigned|numberDecimal" <EditText android:id="@+id/editTe ...
- pthread_attr_t 线程属性(二)
一.函数: 1.线程属性的初始化与销毁:#include <pthread.h>int pthread_attr_init(pthread_attr_t *attr);int pthrea ...