DianLi/LandslidePredict/im2shp.cpp

154 lines
3.9 KiB
C++
Raw Normal View History

2023-03-13 16:17:55 +08:00
#include "im2shp.h"
#include <ogrsf_frmts.h>
#include "CProcessBase.h"
/**
* \brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>GDAL<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
*
* <EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD>GDAL<EFBFBD><EFBFBD>еĽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CProcessBase<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ
*
* @param dfComplete <EFBFBD><EFBFBD><EFBFBD>ɽ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡֵΪ 0.0 <EFBFBD><EFBFBD> 1.0 ֮<EFBFBD><EFBFBD>
* @param pszMessage <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
* @param pProgressArg CProcessBase<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
*
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>TRUE<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊȡ<EFBFBD><EFBFBD>
*/
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><EFBFBD>");
return false;
}
if (GDALGetDriverCount() == 0)
GDALAllRegister();
OGRRegisterAll();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
CPLSetConfigOption("SHAPE_ENCODING", "gb2312");
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD>񣬲<EFBFBD><F1A3ACB2>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
GDALDataset* poSrcDS = (GDALDataset*)GDALOpen(pszSrcFile, GA_ReadOnly);
if (NULL == poSrcDS)
{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܴ򿪣<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>");
return false;
}
//<2F><><EFBFBD><EFBFBD>ʸ<EFBFBD><CAB8><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʸ<EFBFBD><CAB8>ͼ<EFBFBD><CDBC>
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
if (NULL == poDriver)
{
printf("<EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ʸ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
GDALClose((GDALDatasetH)poSrcDS);
return false;
}
/*
*Create<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>դ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>nXSize<EFBFBD><EFBFBD>nYSize<EFBFBD><EFBFBD>nBands<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD>դ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨<EFBFBD><EFBFBD>Ĭ<EFBFBD>ϴ<EFBFBD><EFBFBD><EFBFBD>ʸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD>**
*/
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʸ<EFBFBD><CAB8><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD>
GDALDataset* poDstDs = poDriver->Create(pszDstFile, 0, 0, 0, GDT_Unknown, NULL);
if (NULL == poDstDs)
{
printf("<EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD><EFBFBD><EFBFBD>ʸ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>");
GDALClose((GDALDatasetH)poSrcDS);
return false;
}
OGRSpatialReference* poSpatialRef = new OGRSpatialReference(poSrcDS->GetProjectionRef());
OGRLayer* poLayer = poDstDs->CreateLayer("Target", poSpatialRef, wkbPolygon, NULL);
if (NULL == poLayer)
{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʸ<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>");
GDALClose((GDALDatasetH)poSrcDS);
GDALClose(poDstDs);
poSpatialRef = NULL;
return false;
}
OGRFieldDefn ofieldDef("value", OFTInteger);
if (OGRERR_NONE != poLayer->CreateField(&ofieldDef))
{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʸ<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>");
GDALClose((GDALDatasetH)poSrcDS);
GDALClose(poDstDs);
poSpatialRef = NULL;
return false;
}
//<2F><>ȡͼ<C8A1><CDBC><EFBFBD>Ķ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>
GDALRasterBandH hSrcBand = (GDALRasterBandH)poSrcDS->GetRasterBand(1);
GDALRasterBand* rasterband = poSrcDS->GetRasterBand(1);
GDALProgressFunc pfnProgress = ALGTermProgress;
if (!probar->SetStartEndValue(0, 100))
{
cout << "<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << 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;
}