#include "ogrsf_frmts.h"
#include "gdal.h"
#include "gdal_priv.h"
#include "cpl_string.h"
#include <string>
#include <iostream>
#include <strstream>
using namespace std;
// 参考
//https://blog.csdn.net/taiyang1987912/article/details/72751102
//引用
//https://blog.csdn.net/ivan_ljf/article/details/9620681
//https://www.cnblogs.com/bigbigtree/archive/2011/12/07/2278721.html
void createPoint()
{
const char *pszDriverName = "ESRI Shapefile";
OGRSFDriver *poDriver;
OGRRegisterAll();
poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
if( poDriver == NULL )
{
printf( "%s driver not available.
", pszDriverName );
exit( 1 );
}
OGRDataSource *poDS;
poDS = poDriver->CreateDataSource( "point_out.shp", NULL );//但文件夹中以前存在point_out.shp文件时会报错;
if( poDS == NULL )
{
printf( "Creation of output file failed.
" );
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.
" );
exit( 1 );
}
OGRFieldDefn oField( "NAME", OFTString );
oField.SetWidth(32);
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
printf( "Creating Name field failed.
" );
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.
" );
exit( 1 );
}
OGRFeature::DestroyFeature( poFeature );
}
OGRDataSource::DestroyDataSource( poDS );
}
//https://blog.csdn.net/q_l_s/article/details/52516704
void readPoint()
{
OGRRegisterAll();
OGRDataSource *poDS;
//poDS = OGRSFDriverRegistrar::Open( "C:\Users\Administrator\Desktop\VC\testshape\testshape\point_out.shp", FALSE );//shape文件存放的路径(point.shp即为自己创建的文件)
//poDS = OGRSFDriverRegistrar::Open( "C:\Users\Administrator\Desktop\图层数据\Node.shp", FALSE );
poDS = OGRSFDriverRegistrar::Open( "point_out.shp", FALSE );//shape文件存放的路径(point.shp即为自己创建的文件)
if( poDS == NULL )
{
printf( "Open failed.
%s" );
exit( 1 );
}
OGRLayer *poLayer;
//poLayer=NULL;
//poLayer = poDS->GetLayerByName("point_out_layer");
poLayer = poDS->GetLayer(0);
if(poLayer == NULL)
{
printf( "指针 poLayer 为空
" );
}
int n = poDS->GetLayerCount();
int m = poLayer->GetFeatureCount();
OGRFeature *poFeature;
poLayer->ResetReading();
while( (poFeature = poLayer->GetNextFeature()) != NULL )//获得要素,本实例指的是五个点,所以会循环5次
{
OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
int iField;
int i=poFDefn->GetFieldCount(); //获得字段的数目,本实例返回5,不包括前两个字段(FID,Shape),这两个字段在arcgis里也不能被修改;
for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
{
OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );
//根据字段值得类型,选择对应的输出
if( poFieldDefn->GetType() == OFTInteger )
printf( "%d,", poFeature->GetFieldAsInteger( iField ) );
else if( poFieldDefn->GetType() == OFTReal )
printf( "%.3f,", poFeature->GetFieldAsDouble(iField) );
else if( poFieldDefn->GetType() == OFTString )
printf( "%s,", poFeature->GetFieldAsString(iField) );
else
printf( "%s,", poFeature->GetFieldAsString(iField) );
}
OGRGeometry *poGeometry;
poGeometry = poFeature->GetGeometryRef();
if( poGeometry != NULL
&& wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
{
OGRPoint *poPoint = (OGRPoint *) poGeometry;
printf( "%.3f,%3.f
", poPoint->getX(), poPoint->getY() );
}
else
{
printf( "no point geometry
" );
}
OGRFeature::DestroyFeature( poFeature );
}
OGRDataSource::DestroyDataSource( poDS );
}
void readPolygon()
{
OGRRegisterAll();
OGRDataSource *poDS;
//poDS = OGRSFDriverRegistrar::Open( "C:\Users\Administrator\Desktop\VC\testshape\testshape\point_out.shp", FALSE );//shape文件存放的路径(point.shp即为自己创建的文件)
//poDS = OGRSFDriverRegistrar::Open( "C:\Users\Administrator\Desktop\图层数据\Node.shp", FALSE );
//poDS = OGRSFDriverRegistrar::Open( "point_out.shp", FALSE );//shape文件存放的路径(point.shp即为自己创建的文件)
poDS = OGRSFDriverRegistrar::Open( "E:\DCP测试数据\data\M50F029027\Country.shp", FALSE );
if( poDS == NULL )
{
printf( "Open failed.
%s" );
exit( 1 );
}
OGRLayer *poLayer;
//poLayer=NULL;
//poLayer = poDS->GetLayerByName("point_out_layer");
poLayer = poDS->GetLayer(0);
if(poLayer == NULL)
{
printf( "指针 poLayer 为空
" );
}
int n = poDS->GetLayerCount();
int m = poLayer->GetFeatureCount();
OGRFeature *poFeature;
poLayer->ResetReading();
while( (poFeature = poLayer->GetNextFeature()) != NULL )//获得要素,本实例指的是五个点,所以会循环5次
{
OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
int iField;
int i=poFDefn->GetFieldCount(); //获得字段的数目,本实例返回5,不包括前两个字段(FID,Shape),这两个字段在arcgis里也不能被修改;
for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
{
OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );
//根据字段值得类型,选择对应的输出
if( poFieldDefn->GetType() == OFTInteger )
printf( "%d,", poFeature->GetFieldAsInteger( iField ) );
else if( poFieldDefn->GetType() == OFTReal )
printf( "%.3f,", poFeature->GetFieldAsDouble(iField) );
else if( poFieldDefn->GetType() == OFTString )
printf( "%s,", poFeature->GetFieldAsString(iField) );
else
printf( "%s,", poFeature->GetFieldAsString(iField) );
}
OGRGeometry *poGeometry;
poGeometry = poFeature->GetGeometryRef();
if( poGeometry != NULL
&& wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
{
OGRPoint *poPoint = (OGRPoint *) poGeometry;
printf( "%.3f,%3.f
", poPoint->getX(), poPoint->getY() );
}
else if( poGeometry != NULL && wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon )
{
//OGRPoint *poPoint = (OGRPoint *) poGeometry;
OGRPolygon *poPolygon = (OGRPolygon *) poGeometry;
//printf( "%.3f,%3.f
", poPoint->getX(), poPoint->getY() );
}
else
{
printf( "no point geometry
" );
}
OGRFeature::DestroyFeature( poFeature );
}
OGRDataSource::DestroyDataSource( poDS );
}
int main()
{
//createPoint();
//readPoint();
readPolygon();
system("pause");
return 0;
}