GDAL官网 OGR API Tutorial学习笔记二:Writing To OGR

#include "ogrsf_frmts.h"

int main()

{

const char *pszDriverName = "ESRI Shapefile";

GDALDriver *poDriver;

GDALAllRegister(); //注册所有驱动

poDriver = GetGDALDriverManager->GetDriverByName( pszDriverName ); //获取创建输出文件所需要的Shapefile driver

if( poDriver == NULL )

{

printf( "%s driver not availabel.\n", pszDriverName );

exit( 1 );

}

//创建数据源

GDALDataset *poDS;

//The ESRI Shapefile driver allows us to create a directory full of shapefiles, or a single shapefile as a datasource.

poDS = poDriver->Create( "point_out.shp", 0, 0, 0, GDT_Unknown, NULL );

if( poDS == NULL )

{

printf( "Creation of output file failed.\n" );

exit( 1 );

}

//创建输出图层

//In this case since the datasource is a single file, we can only have one layer.

OGRLayer *poLayer;

poLayer = poDS->CreateLayer( "point_out", NULL, wkbPoint, NULL ); //wkbPoint指定几何图形类型,也可设置参数指定坐标系等

if( poLayer == NULL )

{

printf( "Layer creation failed.\n" );

exit( 1 );

}

//创建属性字段

OGRFieldDefn oField( "Name", OFTString ); //Name字段

oField.SetWidth(32); //字段宽度

if( poLayer->CreateField( &oField ) != OGRERR_NONE )

{

printf( "Creating Name field failed.\n" );

exit( 1 );

}

double x, y;

char szName[33];

//In this case since the datasource is a single file, we can only have one layer.

while( ! feof(stdin) && fscanf( stdin, "%lf, %lf, %32s", &x, &y, szName ) == 3 )

{

//创建本地OGRFeature,设置属性

OGRFeature *poFeature;

poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );

poFeature->SetField( "Name",  szName );

//创建本地几何图形对象,将它的副本赋给feature

OGRPoint pt;

pt.setX( x );

pt.setY( y );

poFeature->SetGeometry( &pt );

//Now we create a feature in the file. The OGRLayer::CreateFeature() does not take ownership of our feature so we clean it up when done with it.

if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )

{

printf( "Failed to create feature in shapfile.\n" );

exit( 1 ); 

}

OGRFeature::DestroyFeature( poFeature );

}

GDALClose( poDS );

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值