gdal中调用OGRErr Intersection/Clip/Union等函数后无任何要素生成

迷途不返丶 2020-01-13 09:04:20
小白初学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
...全文
1011 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Monica Bing 2020-12-26
  • 打赏
  • 举报
回复
可能是拓扑错误或者两个shp投影不一致
迷途不返丶 2020-01-13
  • 打赏
  • 举报
回复
希望各位大佬不吝赐教!

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧