154 lines
3.9 KiB
C++
154 lines
3.9 KiB
C++
#include "im2shp.h"
|
||
#include <ogrsf_frmts.h>
|
||
#include "CProcessBase.h"
|
||
/**
|
||
* \brief 调用GDAL进度条接口
|
||
*
|
||
* 该函数用于将GDAL算法中的进度信息导出到CProcessBase基类中,供给界面显示
|
||
*
|
||
* @param dfComplete 完成进度值,其取值为 0.0 到 1.0 之间
|
||
* @param pszMessage 进度信息
|
||
* @param pProgressArg CProcessBase的指针
|
||
*
|
||
* @return 返回TRUE表示继续计算,否则为取消
|
||
*/
|
||
int STD_API ALGTermProgress(double dfComplete, const char* pszMessage, void* pProgressArg)
|
||
{
|
||
if (pProgressArg != NULL)
|
||
{
|
||
QtGDALProcessBar* pProcess = (QtGDALProcessBar*)pProgressArg;
|
||
pProcess->m_bIsContinue = pProcess->SetPosition(dfComplete);
|
||
|
||
if (pProcess->m_bIsContinue)
|
||
return TRUE;
|
||
else
|
||
return FALSE;
|
||
}
|
||
else
|
||
return TRUE;
|
||
}
|
||
|
||
|
||
int img2shp::ImagePolygonize(const char* pszSrcFile, const char* pszDstFile, const char* pszFormat, int BandNum,
|
||
QtGDALProcessBar* probar, QProgressBar* progressBar)
|
||
{
|
||
if (NULL == pszSrcFile || NULL == pszDstFile)
|
||
{
|
||
printf("待处理的栅格数据及输出矢量数据数据无效!");
|
||
return false;
|
||
}
|
||
|
||
if (GDALGetDriverCount() == 0)
|
||
GDALAllRegister();
|
||
OGRRegisterAll();
|
||
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
|
||
CPLSetConfigOption("SHAPE_ENCODING", "gb2312");
|
||
|
||
//打开输入图像,并判断是否正常
|
||
GDALDataset* poSrcDS = (GDALDataset*)GDALOpen(pszSrcFile, GA_ReadOnly);
|
||
if (NULL == poSrcDS)
|
||
{
|
||
printf("输入文件不能打开,请检查文件是否存在!");
|
||
return false;
|
||
}
|
||
|
||
//构造矢量文件驱动并创建矢量图层
|
||
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
|
||
if (NULL == poDriver)
|
||
{
|
||
printf("不能创建指定类型的矢量文件驱动!");
|
||
GDALClose((GDALDatasetH)poSrcDS);
|
||
return false;
|
||
}
|
||
|
||
|
||
/*
|
||
*Create方法主要用于创建栅格文件的新数据集,其中nXSize,nYSize,nBands,都是对栅格文件的描述,**都置零后台会默认创建矢量数据集**
|
||
*/
|
||
//根据文件名创建输出矢量数据集
|
||
|
||
GDALDataset* poDstDs = poDriver->Create(pszDstFile, 0, 0, 0, GDT_Unknown, NULL);
|
||
if (NULL == poDstDs)
|
||
{
|
||
printf("不能创建矢量文件!");
|
||
GDALClose((GDALDatasetH)poSrcDS);
|
||
|
||
return false;
|
||
}
|
||
|
||
OGRSpatialReference* poSpatialRef = new OGRSpatialReference(poSrcDS->GetProjectionRef());
|
||
|
||
OGRLayer* poLayer = poDstDs->CreateLayer("Target", poSpatialRef, wkbPolygon, NULL);
|
||
if (NULL == poLayer)
|
||
{
|
||
printf("创建矢量图层失败!");
|
||
GDALClose((GDALDatasetH)poSrcDS);
|
||
GDALClose(poDstDs);
|
||
poSpatialRef = NULL;
|
||
|
||
return false;
|
||
}
|
||
OGRFieldDefn ofieldDef("value", OFTInteger);
|
||
if (OGRERR_NONE != poLayer->CreateField(&ofieldDef))
|
||
{
|
||
printf("创建矢量图层属性表失败!");
|
||
GDALClose((GDALDatasetH)poSrcDS);
|
||
GDALClose(poDstDs);
|
||
poSpatialRef = NULL;
|
||
|
||
return false;
|
||
}
|
||
|
||
//获取图像的对应波段
|
||
GDALRasterBandH hSrcBand = (GDALRasterBandH)poSrcDS->GetRasterBand(1);
|
||
|
||
GDALRasterBand* rasterband = poSrcDS->GetRasterBand(1);
|
||
|
||
GDALProgressFunc pfnProgress = ALGTermProgress;
|
||
|
||
if (!probar->SetStartEndValue(0, 100))
|
||
{
|
||
cout << "初始进度值设置失败!!!" << endl;
|
||
}
|
||
|
||
|
||
//if (GDALPolygonize(hSrcBand, NULL, (OGRLayerH)poLayer, 0, NULL, pfnProgress, probar) != CE_None)
|
||
//if (GDALPolygonize(hSrcBand, NULL, (OGRLayerH)poLayer, 0, NULL, GDALTermProgress, NULL) != CE_None)
|
||
//qDebug() << rasterband->GetNoDataValue() << endl;
|
||
//if (0.0 == poSrcDS->GetRasterBand(1)->GetNoDataValue())
|
||
//{
|
||
// if (CE_None != GDALPolygonize(hSrcBand, hSrcBand, (OGRLayerH)poLayer, 0, NULL, pfnProgress, probar))
|
||
// {
|
||
// GDALClose((GDALDatasetH)poSrcDS);
|
||
// GDALClose(poDstDs);
|
||
// delete poSpatialRef;
|
||
// poSpatialRef = NULL;
|
||
// return 0;
|
||
// }
|
||
//}
|
||
//else
|
||
//{
|
||
// if (CE_None != GDALPolygonize(hSrcBand, NULL, (OGRLayerH)poLayer, 0, NULL, pfnProgress, probar))
|
||
// {
|
||
// GDALClose((GDALDatasetH)poSrcDS);
|
||
// GDALClose(poDstDs);
|
||
// delete poSpatialRef;
|
||
// poSpatialRef = NULL;
|
||
// return 0;
|
||
// }
|
||
//}
|
||
if (CE_None != GDALPolygonize(hSrcBand, hSrcBand, (OGRLayerH)poLayer, 0, NULL, pfnProgress, probar))
|
||
{
|
||
GDALClose((GDALDatasetH)poSrcDS);
|
||
GDALClose(poDstDs);
|
||
delete poSpatialRef;
|
||
poSpatialRef = NULL;
|
||
return 0;
|
||
}
|
||
|
||
GDALClose((GDALDatasetH)poSrcDS);
|
||
GDALClose(poDstDs);
|
||
poSpatialRef = NULL;
|
||
|
||
return true;
|
||
} |