结合C++和GDAL实现shapefile(shp)文件的读取

工具:vs2012+GDAL 2.0

数据:中国省界SHP文件bou2_4p.shp   可点击下载

包含头文件:

#include "ogrsf_frmts.h"

代码:

int main()
{
GDALAllRegister(); GDALDataset
*poDS; CPLSetConfigOption("SHAPE_ENCODING",""); //解决中文乱码问题 //读取shp文件 poDS = (GDALDataset*) GDALOpenEx("d:/shapefile/bou2_4p.shp", GDAL_OF_VECTOR, NULL, NULL, NULL ); if( poDS == NULL ) { printf( "Open failed.\n%s" ); return 0; } OGRLayer *poLayer; poLayer = poDS->GetLayer(0); //读取层 OGRFeature *poFeature; poLayer->ResetReading(); int i=0; while( (poFeature = poLayer->GetNextFeature()) != NULL ) { if(poFeature->GetFieldAsDouble("AREA")<1) continue; //去掉面积过小的polygon i=i++; cout<<i<<" "; OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn(); int iField; int n=poFDefn->GetFieldCount(); //获得字段的数目,不包括前两个字段(FID,Shape); for( iField = 0; iField <n; iField++ ) { //输出每个字段的值 cout<<poFeature->GetFieldAsString(iField)<<" "; } cout<<endl; OGRFeature::DestroyFeature( poFeature ); } GDALClose( poDS ); system("pause"); return 1;
}

结果显示:

posted @ 2015-11-12 18:24 denny402 阅读(...) 评论(...) 编辑 收藏