#include "ogrsf_frmts.h"
bool LayerMethod(const char *pszSrcShp,const char *pszMethodShp,const char *pszDstShp,int iType)
{
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");
//注:此处注册不是GDALAllRegister
OGRRegisterAll();
//打开数据
OGRDataSource *poSrcDS=OGRSFDriverRegistrar::Open(pszSrcShp,FALSE );
if( poSrcDS == NULL )
{
printf("打开文件 %s 失败.\n",pszSrcShp);
return false;
}
OGRDataSource *poMethodDS=OGRSFDriverRegistrar::Open(pszMethodShp,FALSE );
if( poMethodDS == NULL )
{
printf("打开文件 %s 失败.\n",poMethodDS);
OGRDataSource::DestroyDataSource(poSrcDS);
return false;
}
//创建数据
OGRSFDriver *poDriver=OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("ESRI Shapefile");
if( poDriver == NULL )
{
printf("格式%s不支持.\n","ESRI Shapefile");
OGRDataSource::DestroyDataSource(poSrcDS);
OGRDataSource::DestroyDataSource(poMethodDS);
return false;
}
OGRDataSource *poDstDS=poDriver->CreateDataSource(pszDstShp,NULL);
if( poDstDS == NULL )
{
printf("输出数据集失败.\n");
OGRDataSource::DestroyDataSource(poSrcDS);
OGRDataSource::DestroyDataSource(poMethodDS);
return false;
}
OGRLayer *poSrcLayer=poSrcDS->GetLayer(0);
if(poSrcLayer==NULL)
{
printf("创建图层失败.\n");
OGRDataSource::DestroyDataSource(poSrcDS);
OGRDataSource::DestroyDataSource(poMethodDS);
OGRDataSource::DestroyDataSource(poDstDS);
return false;
}
OGRLayer *poMethodLayer=poMethodDS->GetLayer(0);
if(poMethodLayer==NULL)
{
printf("创建图层失败.\n");
OGRDataSource::DestroyDataSource(poSrcDS);
OGRDataSource::DestroyDataSource(poMethodDS);
OGRDataSource::DestroyDataSource(poDstDS);
return false;
}
OGRSpatialReference *pSRS=poSrcLayer->GetSpatialRef();
OGRLayer *poDstLayer=poDstDS->CreateLayer("NewLayer",pSRS,wkbPolygon,NULL);
if(poDstLayer==NULL)
{
printf("创建图层失败.\n");
OGRDataSource::DestroyDataSource(poSrcDS);
OGRDataSource::DestroyDataSource(poMethodDS);
OGRDataSource::DestroyDataSource(poDstDS);
return false;
}
switch(iType)
{
case 0:
poSrcLayer->Intersection(poMethodLayer,poDstLayer,NULL,GDALTermProgress,NULL);
break;
case 1:
poSrcLayer->Union(poMethodLayer,poDstLayer,NULL,GDALTermProgress,NULL);
break;
case 2:
poSrcLayer->SymDifference(poMethodLayer,poDstLayer,NULL,GDALTermProgress,NULL);
break;
case 3:
poSrcLayer->Identity(poMethodLayer,poDstLayer,NULL,GDALTermProgress,NULL);
break;
case 4:
poSrcLayer->Update(poMethodLayer,poDstLayer,NULL,GDALTermProgress,NULL);
break;
case 5:
poSrcLayer->Clip(poMethodLayer,poDstLayer,NULL,GDALTermProgress,NULL);
break;
case 6:
poSrcLayer->Erase(poMethodLayer,poDstLayer,NULL,GDALTermProgress,NULL);
break;
default:break;
}
OGRDataSource::DestroyDataSource(poSrcDS);
OGRDataSource::DestroyDataSource(poMethodDS);
OGRDataSource::DestroyDataSource(poDstDS);
return true;
}
int main()
{
const char *pszSrcFile="D:\\Documents\\VS\\CreateIm\\shpManage\\SrcLayer.shp";
const char *pszMethodFile="D:\\Documents\\VS\\CreateIm\\shpManage\\MethodLayer.shp";
const char *pszOutFile0="D:\\Documents\\VS\\CreateIm\\shpManage\\Intersection.shp";
const char *pszOutFile1="D:\\Documents\\VS\\CreateIm\\shpManage\\Union.shp";
const char *pszOutFile2="D:\\Documents\\VS\\CreateIm\\shpManage\\SymDifference.shp";
const char *pszOutFile3="D:\\Documents\\VS\\CreateIm\\shpManage\\Identity.shp";
const char *pszOutFile4="D:\\Documents\\VS\\CreateIm\\shpManage\\Update.shp";
const char *pszOutFile5="D:\\Documents\\VS\\CreateIm\\shpManage\\Clip.shp";
const char *pszOutFile6="D:\\Documents\\VS\\CreateIm\\shpManage\\Erase.shp";
LayerMethod(pszSrcFile,pszMethodFile,pszOutFile0,0);
LayerMethod(pszSrcFile,pszMethodFile,pszOutFile1,1);
LayerMethod(pszSrcFile,pszMethodFile,pszOutFile2,2);
LayerMethod(pszSrcFile,pszMethodFile,pszOutFile3,3);
LayerMethod(pszSrcFile,pszMethodFile,pszOutFile4,4);
LayerMethod(pszSrcFile,pszMethodFile,pszOutFile5,5);
LayerMethod(pszSrcFile,pszMethodFile,pszOutFile6,6);
// CreateRasterFile();
// UpdateRasterFile();
return 0;
}
- 1
- 2
前往页