小白初學gdal,想用c++實作矢量點和面的clip操作,找到官網的說明如下:

我的代碼塊:
#include "pch.h"
#include "gdal_priv.h"
#include "ogrsf_frmts.h"
#include <iostream>
bool VectorIntersection(const char *pszSrcShp, const char *pszMethodShp, const char *pszDstShp, const char* pszFormat);
int main()
{
const char *pszSrcFile = "E:\\MyThesis\\HeNan\\henan.shp"; //原始檔案
const char *pszMethodFile = "E:\\MyThesis\\SiChuan\\Sichuan.shp"; //用來clip的檔案
const char *pszOutFile = "E:\\MyThesis\\union.shp"; //結果檔案
VectorIntersection(pszSrcFile, pszMethodFile, pszOutFile, "ESRI Shapefile");
return 0;
}
bool VectorIntersection(const char *pszSrcShp, const char *pszMethodShp, const char *pszDstShp, const char* pszFormat)
{
GDALAllRegister();
CPLSetConfigOption("SHAPE_ENCODING", "");
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
//讀取資料
GDALDataset *poSrcDS;
poSrcDS = (GDALDataset*)GDALOpenEx(pszSrcShp, GDAL_OF_VECTOR, NULL, NULL, NULL);
if (poSrcDS == NULL)
{
printf("Open failed.\n");
exit(1);
}
GDALDataset *poMethodDS;
poMethodDS = (GDALDataset*)GDALOpenEx(pszMethodShp, GDAL_OF_VECTOR, NULL, NULL, NULL);
if (poMethodDS == NULL)
{
printf("Open failed.\n");
exit(1);
}
//使用Shapefile驅動
GDALDriver *poDriver;
poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
if (poDriver == NULL)
{
printf("%s driver not available.\n", pszFormat);
exit(1);
}
//根據檔案名創建shp檔案
GDALDataset* poDstDS = poDriver->Create(pszDstShp, 0, 0, 0, GDT_Unknown, NULL);
if (poDstDS == NULL)
{
printf("Creation of output file failed.\n");
exit(1);
}
//獲取圖層
OGRLayer *poSrcLayer = poSrcDS->GetLayer(0);
if (poSrcLayer == NULL)
{
printf("Creation of layer failed.\n");
GDALClose(poSrcDS); //關閉檔案
GDALClose(poMethodDS);
GDALClose(poDstDS);
exit(1);
}
OGRLayer *poMethodLayer = poMethodDS->GetLayer(0);
if (poMethodLayer == NULL)
{
printf("Creation of layer failed.\n");
GDALClose(poSrcDS); //關閉檔案
GDALClose(poMethodDS);
GDALClose(poDstDS);
exit(1);
}
//定義空間參考與原始矢量資料相同并創建圖層
OGRLayer *poDstLayer;
OGRSpatialReference *pSRS = poSrcLayer->GetSpatialRef();
poDstLayer = poDstDS->CreateLayer("NewLayer", pSRS, wkbPolygon, NULL);
if (poDstLayer == NULL)
{
printf("Creation of layer failed.\n");
GDALClose(poSrcDS); //關閉檔案
GDALClose(poMethodDS);
GDALClose(poDstDS);
exit(1);
}
poSrcLayer->Union(poMethodLayer, poDstLayer, NULL, NULL, NULL);
GDALClose(poSrcDS); //關閉檔案
GDALClose(poMethodDS);
GDALClose(poDstDS);
return true;
}
參考博客地址:https://blog.csdn.net/secyb/article/details/80246105
uj5u.com熱心網友回復:
希望各位大佬不吝賜教!uj5u.com熱心網友回復:
可能是拓撲錯誤或者兩個shp投影不一致轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/240670.html
標籤:C++ 語言
上一篇:c語言歌星大獎賽
下一篇:想請教一個邏輯性的問題
