php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 258|回复: 0

栅格转矢量

[复制链接]

2620

主题

2627

帖子

9308

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6566
贡献
0
注册时间
2021-4-14
最后登录
2024-4-25
在线时间
666 小时
QQ
发表于 2022-10-24 21:24:52 | 显示全部楼层 |阅读模式
[mw_shl_code=cpp,true]#include "Raster2VectorThread.h"
#include "Raster2VectorApp.h"
#include <windows.h>
#include "qsettings.h"

void progressValueCallback(int dfComplete, void * pProgressArg)
{
        Raster2VectorThread *theThread = (Raster2VectorThread *)pProgressArg;
        if (theThread != NULL)
        {
                theThread->emitProgChanged(dfComplete);
        }
};

void progressMessageCallback(char * pszMessage,
        void * pProgressArg)
{
        Raster2VectorThread *theThread = (Raster2VectorThread *)pProgressArg;
        if (theThread != NULL)
        {
                theThread->emitProgMessage(pszMessage);
        }
};

Raster2VectorThread::Raster2VectorThread(QObject *parent)
{
        gInput = "";
        gOutput = "";
        gRatio = 0.0;
        // 中英文切换
        QString IniFile = QString(QCoreApplication::applicationDirPath() + "\\Geovis\\Geovis.ini");
        IniFile.replace("\/", "\\");

        QSettings settings(IniFile, QSettings::IniFormat);
        isEnglish = settings.value("/Language/isEnglish").toBool();// true;// true 英文;false  中文
}

Raster2VectorThread::~Raster2VectorThread()
{
       
}


//int Raster2VectorThread::ImagePolygonize(const char * pszSrcFile, const char* pszDstFile, const char* pszFormat, int progessValue, int part_Value, QString * showmessages)
//{
//       
//        emitProgChanged(progessValue);
//       
//        QString file_shp = QString::fromLocal8Bit(pszDstFile);
//        QString file_raster = QString::fromLocal8Bit(pszSrcFile);
//
//        showmessages->clear();
//        *showmessages = file_raster;
//        showmessages->append(tr(" to transform ..."));
//        //showmessages->append(QStringLiteral("文件转换开始..."));
//       
//       
//        emitProgMessage(*showmessages);
//
//        int part_splt = 6;
//        GDALAllRegister();
//        OGRRegisterAll();//记得添加驱动注册
//        CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
//       
//
//        GDALDataset* poSrcDS1 = (GDALDataset*)GDALOpen(pszSrcFile, GA_Update);
//        if (poSrcDS1 == NULL)
//        {
//                showmessages->clear();
//                //*showmessages = file_raster;
//                showmessages->append(tr("Falied to open the intput image in update mode!"));
//                //showmessages->append(QStringLiteral("文件可更新模式打开失败!"));
//               
//                emitProgMessage(*showmessages);
//                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//                return 0;
//        }
//        emitProgChanged(progessValue + 1 * part_Value / part_splt);
//        GDALClose(poSrcDS1);
//       
//
//        GDALDataset* poSrcDS = (GDALDataset*)GDALOpen(pszSrcFile, GA_ReadOnly);
//        if (poSrcDS == NULL)
//        {
//                showmessages->clear();
//                //*showmessages = file_raster;
//                showmessages->append(tr("Falied to open the intput image in read-only mode!"));
//                //showmessages->append(QStringLiteral("文件只读模式打开失败!"));
//               
//               
//                emitProgMessage(*showmessages);
//                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//                return 0;
//        }
//       
//        double dGeo[6] = { 0, 1, 0, 0, 0,1 };
//        poSrcDS->SetGeoTransform(dGeo);
//
//        // 创建输出矢量文件
//        //OGRSFDriver *poDriver;
//        //poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszFormat);// GDALDriver
//        //if (poDriver == NULL)
//        //{
//        //        GDALClose((GDALDatasetH)poSrcDS);
//        //        return 0;
//        //}
//
//
//        GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
//        if (poDriver == NULL)
//        {
//                GDALClose((GDALDatasetH)poSrcDS);
//                showmessages->clear();
//                showmessages->append(tr("Falied to obtain the driver."));
//                //showmessages->append(QStringLiteral("获取驱动失败!"));
//               
//               
//                emitProgMessage(*showmessages);
//                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//                return 0;
//        }
//        emitProgChanged(progessValue + 2 * part_Value / part_splt);
//        /////////////////////////////////////////////////////////////////
//
//        //根据文件名创建输出矢量文件
//        /*OGRDataSource* poDstDS = poDriver->CreateDataSource(pszDstFile);
//        if (poDstDS == NULL)
//        {
//        GDALClose((GDALDatasetH)poSrcDS);
//        return 0;
//        }*/
//
//        GDALDataset* poDstDS = poDriver->Create(pszDstFile, 0, 0, 0, GDT_Unknown, NULL); //创建输出矢量文件
//        if (poDstDS == NULL)
//        {
//                GDALClose((GDALDatasetH)poSrcDS);
//                showmessages->clear();
//                //*showmessages = file_shp;
//                showmessages->append(tr("Falied to create the vector file."));
//                //showmessages->append(QStringLiteral("矢量文件创建失败!"));
//               
//               
//                emitProgMessage(*showmessages);
//                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//                return 0;
//        }
//        emitProgChanged(progessValue +3 * part_Value / part_splt);
//
//        showmessages->clear();
//        //*showmessages = file_shp;
//        showmessages->append(tr("File creation succeeded!"));
//        //showmessages->append(QStringLiteral("文件创建成功!"));
//        emitProgMessage(*showmessages);
//
//        //////////////////////////////////////////////////////////////
//
//
//        // 定义空间参考,与输入图像相同;
//        OGRSpatialReference *poSpatialRef = new OGRSpatialReference(poSrcDS->GetProjectionRef());
//        OGRLayer* poLayer = poDstDS->CreateLayer("Result", poSpatialRef, wkbPolygon, NULL);
//        if ( poSpatialRef == NULL || poLayer ==NULL)
//        {
//                showmessages->clear();
//                //*showmessages = file_raster;
//                showmessages->append(tr("File drop or layer information failed!"));
//                //showmessages->append(QStringLiteral("文件投影或图层信息获取失败!"));
//               
//                emitProgMessage(*showmessages);
//
//                GDALClose((GDALDatasetH)poSrcDS);
//                //OGRDataSource:estroyDataSource(poDstDS);
//                GDALClose(poDstDS);
//                delete poSpatialRef;
//                poSpatialRef = NULL;
//                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//                return 0;
//        }
//
//        showmessages->clear();
//        //*showmessages = file_raster;
//        showmessages->append(tr("The file projection information is obtained successfuly."));
//        //showmessages->append(QStringLiteral("文件投影信息获取成功!"));
//       
//        emitProgMessage(*showmessages);
//
//        emitProgChanged(progessValue + 4 * part_Value / part_splt);
//        //OGRFieldDefn ofieldDef("Segment", OFTInteger); //创建属性表,只有一个字段即“Segment”,里面保存对应的栅格的像元值
//        //OGRFieldDefn ofieldDef("Segment", OFTIntegerList); //创建属性表,只有一个字段即“Segment”,里面保存对应的栅格的像元值
//
//        OGRFieldDefn ofieldDef("Segment", OFTInteger); //创建属性表,只有一个字段即“Segment”(分段),里面保存对应的栅格的像元值
//       
//        poLayer->CreateField(&ofieldDef);
//        GDALRasterBandH hSrcBand = (GDALRasterBandH)poSrcDS->GetRasterBand(1); //获取图像的第一个波段
//        if (!hSrcBand)
//        {
//                showmessages->clear();
//                //*showmessages = file_raster;
//                showmessages->append(tr("Falied to obtain the first band data of the file."));
//                //showmessages->append(QStringLiteral("文件第一波段数据获取失败!"));
//               
//               
//                emitProgMessage(*showmessages);
//
//                GDALClose((GDALDatasetH)poSrcDS);
//                //OGRDataSource:estroyDataSource(poDstDS);
//                GDALClose(poDstDS);
//                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//                return 0;
//        }
//        showmessages->clear();
//        //*showmessages = file_raster;
//        showmessages->append(tr("dealing..."));
//        //showmessages->append(QStringLiteral("处理中..."));
//       
//       
//        emitProgMessage(*showmessages);
//
//        GDALDataType poSrcDS_dateType = poSrcDS->GetRasterBand(1)->GetRasterDataType();// 输入影像数据类型
//        double Minimun = poSrcDS->GetRasterBand(1)->GetMinimum();
//        double Maximun = poSrcDS->GetRasterBand(1)->GetMaximum();
//        double No_date_vaule = poSrcDS->GetRasterBand(1)->GetNoDataValue();
//       
//        if (Minimun != 0 || Maximun>255 /*|| No_date_vaule!=0*/)// 暂定分类二值tiff数据格式用于区分 GF等遥感影像数据
//        {
//                showmessages->clear();
//                //*showmessages = file_raster;
//                showmessages->append(tr("File is not image data,processing failed!"));
//                //showmessages->append(QStringLiteral("文件为非分类影像数据,处理失败!"));
//               
//                emitProgMessage(*showmessages);
//
//                GDALClose((GDALDatasetH)poSrcDS);
//                //OGRDataSource:estroyDataSource(poDstDS);
//                GDALClose(poDstDS);
//                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//                return 0;
//
//        }
//
//       
//        //if (Minimun != 0 || Maximun>255 || No_date_vaule!=0)// 暂定分类二值tiff数据格式用于区分 GF等遥感影像数据
//        //{
//        //        // 进行遥感影像数据转换
//        //        std::string outputImageFile = pszDstFile;
//        //        outputImageFile.append(".tif");
//
//        //        // 创建输出文件并设置空间参考和坐标信息
//        //        char* pszFormat = "GTIFF";
//        //        GDALDriver* poDriver = (GDALDriver *)GDALGetDriverByName(pszFormat);
//        //        int iDstWidth, iDstHeight;
//        //        iDstWidth = poSrcDS->GetRasterXSize();
//        //        iDstHeight = poSrcDS->GetRasterYSize();
//        //       
//
//        //        GDALDataset *pDstDS_output = poDriver->Create(outputImageFile.c_str(), iDstWidth, iDstHeight, 1, GDALDataType::GDT_Int32, NULL);
//        //        GDALDataset *pDstDS_output = poDriver->Create(outputImageFile.c_str(), iDstWidth, iDstHeight, 1, , NULL);
//        //        pDstDS_output->SetGeoTransform(dGeo);
//        //        pDstDS_output->SetProjection(poSrcDS->GetProjectionRef());
//        //        double novalidvalue = 0.0;
//        //        pDstDS_output->GetRasterBand(1)->SetNoDataValue(novalidvalue);
//
//        //        //读取原始数据
//        //
//        //        int nPixSize = GDALGetDataTypeSize(poSrcDS_dateType) / 8;
//        //        unsigned char *pInputDataBuff = NULL;
//        //        pInputDataBuff = new unsigned char[iDstWidth*iDstHeight*nPixSize]();
//        //        int *pBandMap = new int[1];
//        //        poSrcDS->RasterIO(GF_Read, 0, 0, iDstWidth, iDstHeight, pInputDataBuff, iDstWidth, iDstHeight, poSrcDS_dateType, 1, pBandMap, 0, 0, 0);
//
//
//        //        //输出数据
//        //       
//        //       
//        //        int *pInputDataBuff_out = NULL;
//        //        //int nPixSize_out = sizeof(int)/8;/* GDALGetDataTypeSize(GDALDataType::GDT_Int32) / 8*/;
//        //        pInputDataBuff_out = new int[iDstWidth*iDstHeight];
//        //       
//        //        double output_NO_date_value = 0.0;
//        //        for (int index_src = 0; index_src < iDstWidth*iDstHeight*nPixSize;index_src = index_src + nPixSize )
//        //        {
//        //                int index_out = index_src/ nPixSize;
//
//        //                unsigned char *tempValue = NULL;
//        //                tempValue = new unsigned char[nPixSize]();
//        //                for (int i = 0;i < nPixSize;i++)
//        //                {
//        //                        *(tempValue + i) = *(pInputDataBuff + index_src +i );
//        //                }
//        //                //tempValue = pInputDataBuff+index_src;
//
//        //                double date_vaule = static_cast<double>(*tempValue);
//
//        //                if (date_vaule == No_date_vaule)
//        //                {
//        //                        *(pInputDataBuff_out+index_out) = static_cast<int>(output_NO_date_value);
//
//        //                }
//        //                else
//        //                {
//        //                        if (date_vaule == 255.0 || date_vaule == 1.0)
//        //                        {
//        //                                int iiii = 0;
//        //                                iiii++;
//        //                        }
//        //                        double output_date_value;
//        //                        output_date_value = (date_vaule - Minimun) / (Maximun - Minimun)*(255.0 - 1.0) + 1.0;
//
//        //                        *(pInputDataBuff_out+index_out) = static_cast<int>(output_date_value);
//        //                }
//        //               
//        //               
//        //        }
//
//        //        //数据写入
//        //        pDstDS_output->RasterIO(GF_Write, 0, 0, iDstWidth, iDstHeight, pInputDataBuff_out, iDstWidth, iDstHeight, GDALDataType::GDT_Int32, 1, pBandMap, 0, 0, 0);
//
//
//        //        int iPixValField = 0;
//        //        int recode = 0;
//        //        int RasterdataType = pDstDS_output->GetRasterBand(1)->GetRasterDataType();
//        //       
//        //        hSrcBand = NULL;
//        //        hSrcBand = (GDALRasterBandH)pDstDS_output->GetRasterBand(1); //获取图像的第一个波段
//        //        if (!hSrcBand)
//        //        {
//        //                showmessages->clear();
//        //                //*showmessages = file_raster;
//        //                showmessages->append(QStringLiteral("文件第一波段数据获取失败!"));
//        //                emitProgMessage(*showmessages);
//
//        //                GDALClose((GDALDatasetH)poSrcDS);
//        //                //OGRDataSource:estroyDataSource(poDstDS);
//        //                GDALClose(poDstDS);
//        //                GDALClose(pDstDS_output);
//        //                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//
//        //                delete(pInputDataBuff);
//        //                delete(pInputDataBuff_out);
//        //                delete(pBandMap);
//
//        //                return 0;
//        //        }
//        //        if (RasterdataType == GDALDataType::GDT_Byte
//        //                || RasterdataType == GDALDataType::GDT_Int32
//        //                )
//        //        {
//        //                recode = GDALPolygonize(hSrcBand, NULL, (OGRLayerH)poLayer, iPixValField, NULL, NULL, NULL); //调用栅格矢量化
//        //        }
//        //        else if (RasterdataType == GDALDataType::GDT_Float32) {
//        //                recode = GDALFPolygonize(hSrcBand, NULL, (OGRLayerH)poLayer, iPixValField, NULL, NULL, NULL); //调用栅格矢量化
//        //        }
//        //        if (recode != 0)
//        //        {
//        //                showmessages->clear();
//        //                //*showmessages = file_raster;
//        //                showmessages->append(QStringLiteral("数据转换失败!"));
//        //                emitProgMessage(*showmessages);
//        //                GDALClose((GDALDatasetH)poSrcDS);
//        //                //OGRDataSource:estroyDataSource(poDstDS);
//        //               
//        //                GDALClose(poSrcDS); //关闭文件
//        //                GDALClose(poDstDS);
//        //                GDALClose(pDstDS_output);
//        //                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//        //               
//        //                delete(pInputDataBuff);
//        //                delete(pInputDataBuff_out);
//        //                delete(pBandMap);
//
//        //                return 0;
//        //        }
//        //        emitProgChanged(progessValue + 5 * part_Value / part_splt);
//
//        //        /*//GDALPolygonize(GDALRasterBandH hSrcBand,                 //输入栅格图像波段
//        //        //        3:                 GDALRasterBandH hMaskBand,                //掩码图像波段,可以为NULL
//        //        //        4 : OGRLayerH hOutLayer,                      //矢量化后的矢量图层
//        //        //        5 : int iPixValField,                         //需要将像元DN值写入矢量属性字段的字段索引
//        //        //        6 : char **papszOptions,                      //算法选项,目前算法中没有用到,设置为NULL即可
//        //        //        7 : GDALProgressFunc pfnProgress,             //进度条回调函数
//        //        //        8 : void * pProgressArg);                    //进度条参数
//        //        */
//        //        showmessages->clear();
//        //        //*showmessages = file_raster;
//        //        showmessages->append(QStringLiteral("转换成功!"));
//        //        emitProgMessage(*showmessages);
//
//        //        GDALClose(poSrcDS); //关闭文件
//        //                                                //OGRDataSource:estroyDataSource(poDstDS);
//        //        GDALClose(poDstDS);
//        //        GDALClose(pDstDS_output);
//
//        //        delete(pInputDataBuff);
//        //        delete(pInputDataBuff_out);
//        //        delete(pBandMap);
//        //        CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//
//        //        emitProgChanged(progessValue + 6 * part_Value / part_splt);
//
//        //        return 1;
//
//        //}
//
//        //typedef enum {
//        //        /*! Unknown or unspecified type */          GDT_Unknown = 0,
//        //        /*! Eight bit unsigned integer */           GDT_Byte = 1,
//        //        /*! Sixteen bit unsigned integer */         GDT_UInt16 = 2,
//        //        /*! Sixteen bit signed integer */           GDT_Int16 = 3,
//        //        /*! Thirty two bit unsigned integer */      GDT_UInt32 = 4,
//        //        /*! Thirty two bit signed integer */        GDT_Int32 = 5,
//        //        /*! Thirty two bit floating point */        GDT_Float32 = 6,
//        //        /*! Sixty four bit floating point */        GDT_Float64 = 7,
//        //        /*! Complex Int16 */                        GDT_CInt16 = 8,
//        //        /*! Complex Int32 */                        GDT_CInt32 = 9,
//        //        /*! Complex Float32 */                      GDT_CFloat32 = 10,
//        //        /*! Complex Float64 */                      GDT_CFloat64 = 11,
//        //        GDT_TypeCount = 12          /* maximum type # + 1 */
//        //} GDALDataType;
//
//        int iPixValField = 0;
//        int recode = 0;
//        int RasterdataType  = poSrcDS->GetRasterBand(1)->GetRasterDataType();
//        if (RasterdataType == GDALDataType::GDT_Byte
//                || RasterdataType == GDALDataType::GDT_Int32
//                )
//        {
//                recode = GDALPolygonize(hSrcBand, NULL, (OGRLayerH)poLayer, iPixValField, NULL, NULL, NULL); //调用栅格矢量化
//        }
//        else if (RasterdataType == GDALDataType::GDT_Float32){
//                recode = GDALFPolygonize(hSrcBand, NULL, (OGRLayerH)poLayer, iPixValField, NULL, NULL, NULL); //调用栅格矢量化
//        }
//        if (recode !=0 )
//        {
//                showmessages->clear();
//                //*showmessages = file_raster;
//                showmessages->append(tr("Data conversion failed!"));
//                //showmessages->append(QStringLiteral("数据转换失败!"));
//       
//               
//                emitProgMessage(*showmessages);
//                GDALClose((GDALDatasetH)poSrcDS);
//                //OGRDataSource:estroyDataSource(poDstDS);
//                GDALClose(poDstDS);
//                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//                return 0;
//        }
//        emitProgChanged(progessValue + 5 * part_Value / part_splt);
//
//        /*//GDALPolygonize(GDALRasterBandH hSrcBand,                 //输入栅格图像波段
//        //        3:                 GDALRasterBandH hMaskBand,                //掩码图像波段,可以为NULL
//        //        4 : OGRLayerH hOutLayer,                      //矢量化后的矢量图层
//        //        5 : int iPixValField,                         //需要将像元DN值写入矢量属性字段的字段索引
//        //        6 : char **papszOptions,                      //算法选项,目前算法中没有用到,设置为NULL即可
//        //        7 : GDALProgressFunc pfnProgress,             //进度条回调函数
//        //        8 : void * pProgressArg);                    //进度条参数
//        */
//        showmessages->clear();
//        //*showmessages = file_raster;
//        showmessages->append(tr("Conversion success!"));
//        //showmessages->append(QStringLiteral("转换成功!"));
//       
//        emitProgMessage(*showmessages);
//
//        GDALClose(poSrcDS); //关闭文件
//        GDALClose(poDstDS);
//       
//        CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//
//        emitProgChanged(progessValue +6 * part_Value / part_splt);
//
//        return 1;
//}

int Raster2VectorThread::ImagePolygonize(const char * pszSrcFile, const char* pszDstFile, const char* pszFormat, int progessValue, int part_Value, QString * showmessages)
{

        emitProgChanged(progessValue);

        QString file_shp = QString::fromLocal8Bit(pszDstFile);
        QString file_raster = QString::fromLocal8Bit(pszSrcFile);

        showmessages->clear();
        *showmessages = file_raster;
        showmessages->append(tr(" to transform ..."));
        //showmessages->append(QStringLiteral("文件转换开始..."));


        emitProgMessage(*showmessages);

        int part_splt = 6;
        GDALAllRegister();
        OGRRegisterAll();//记得添加驱动注册
        CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");


        GDALDataset* poSrcDS1 = (GDALDataset*)GDALOpen(pszSrcFile, GA_Update);
        if (poSrcDS1 == NULL)
        {
                showmessages->clear();
                //*showmessages = file_raster;
                showmessages->append(tr("Falied to open the intput image in update mode!"));
                //showmessages->append(QStringLiteral("文件可更新模式打开失败!"));

                emitProgMessage(*showmessages);
                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
                emitProgChanged(progessValue + 6 * part_Value / part_splt);
                return 0;
        }
        emitProgChanged(progessValue + 1 * part_Value / part_splt);
        GDALClose(poSrcDS1);


        GDALDataset* poSrcDS = (GDALDataset*)GDALOpen(pszSrcFile, GA_ReadOnly);
        if (poSrcDS == NULL)
        {
                showmessages->clear();
                //*showmessages = file_raster;
                showmessages->append(tr("Falied to open the intput image in read-only mode!"));
                //showmessages->append(QStringLiteral("文件只读模式打开失败!"));


                emitProgMessage(*showmessages);
                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
                emitProgChanged(progessValue + 6 * part_Value / part_splt);
                return 0;
        }

        //double dGeo[6] = { 0, 1, 0, 0, 0,1 };
        //poSrcDS->SetGeoTransform(dGeo);

        // 创建输出矢量文件
        //OGRSFDriver *poDriver;
        //poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszFormat);// GDALDriver
        //if (poDriver == NULL)
        //{
        //        GDALClose((GDALDatasetH)poSrcDS);
        //        return 0;
        //}


        GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
        if (poDriver == NULL)
        {
                GDALClose((GDALDatasetH)poSrcDS);
                showmessages->clear();
                showmessages->append(tr("Falied to obtain the driver."));
                //showmessages->append(QStringLiteral("获取驱动失败!"));


                emitProgMessage(*showmessages);
                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
                emitProgChanged(progessValue + 6 * part_Value / part_splt);
                return 0;
        }
        emitProgChanged(progessValue + 2 * part_Value / part_splt);
        /////////////////////////////////////////////////////////////////

        //根据文件名创建输出矢量文件
        /*OGRDataSource* poDstDS = poDriver->CreateDataSource(pszDstFile);
        if (poDstDS == NULL)
        {
        GDALClose((GDALDatasetH)poSrcDS);
        return 0;
        }*/

        GDALDataset* poDstDS = poDriver->Create(pszDstFile, 0, 0, 0, GDT_Unknown, NULL); //创建输出矢量文件
        if (poDstDS == NULL)
        {
                GDALClose((GDALDatasetH)poSrcDS);
                showmessages->clear();
                //*showmessages = file_shp;
                showmessages->append(tr("Falied to create the vector file."));
                //showmessages->append(QStringLiteral("矢量文件创建失败!"));


                emitProgMessage(*showmessages);
                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
                emitProgChanged(progessValue + 6 * part_Value / part_splt);
                return 0;
        }
        emitProgChanged(progessValue + 3 * part_Value / part_splt);

        showmessages->clear();
        //*showmessages = file_shp;
        showmessages->append(tr("File creation succeeded!"));
        //showmessages->append(QStringLiteral("文件创建成功!"));
        emitProgMessage(*showmessages);

        //////////////////////////////////////////////////////////////


        // 定义空间参考,与输入图像相同;
        char *pszFBasename = CPLStrdup(CPLGetBasename(pszDstFile));
        std::string strLayerName = pszFBasename;
        CPLFree(pszFBasename);

        OGRSpatialReference *poSpatialRef = new OGRSpatialReference(poSrcDS->GetProjectionRef());
        OGRLayer* poLayer = poDstDS->CreateLayer(strLayerName.c_str(), poSpatialRef, wkbPolygon, NULL);
        if (poSpatialRef == NULL || poLayer == NULL)
        {
                showmessages->clear();
                //*showmessages = file_raster;
                showmessages->append(tr("File drop or layer information failed!"));
                //showmessages->append(QStringLiteral("文件投影或图层信息获取失败!"));

                emitProgMessage(*showmessages);

                GDALClose((GDALDatasetH)poSrcDS);
                //OGRDataSource:estroyDataSource(poDstDS);
                GDALClose(poDstDS);
                delete poSpatialRef;
                poSpatialRef = NULL;
                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
                emitProgChanged(progessValue + 6 * part_Value / part_splt);
                return 0;
        }

        showmessages->clear();
        //*showmessages = file_raster;
        showmessages->append(tr("The file projection information is obtained successfuly."));
        //showmessages->append(QStringLiteral("文件投影信息获取成功!"));

        emitProgMessage(*showmessages);

        emitProgChanged(progessValue + 4 * part_Value / part_splt);
        //OGRFieldDefn ofieldDef("Segment", OFTInteger); //创建属性表,只有一个字段即“Segment”,里面保存对应的栅格的像元值
        //OGRFieldDefn ofieldDef("Segment", OFTIntegerList); //创建属性表,只有一个字段即“Segment”,里面保存对应的栅格的像元值

        OGRFieldDefn ofieldDef("Segment", OFTInteger); //创建属性表,只有一个字段即“Segment”(分段),里面保存对应的栅格的像元值

        poLayer->CreateField(&ofieldDef);
        GDALRasterBandH hSrcBand = (GDALRasterBandH)poSrcDS->GetRasterBand(1); //获取图像的第一个波段
        if (!hSrcBand)
        {
                showmessages->clear();
                //*showmessages = file_raster;
                showmessages->append(tr("Falied to obtain the first band data of the file."));
                //showmessages->append(QStringLiteral("文件第一波段数据获取失败!"));


                emitProgMessage(*showmessages);

                GDALClose((GDALDatasetH)poSrcDS);
                //OGRDataSource:estroyDataSource(poDstDS);
                GDALClose(poDstDS);
                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
                emitProgChanged(progessValue + 6 * part_Value / part_splt);
                return 0;
        }
        showmessages->clear();
        //*showmessages = file_raster;
        showmessages->append(tr("dealing..."));
        //showmessages->append(QStringLiteral("处理中..."));


        emitProgMessage(*showmessages);

        GDALDataType poSrcDS_dateType = poSrcDS->GetRasterBand(1)->GetRasterDataType();// 输入影像数据类型
        double Minimun = poSrcDS->GetRasterBand(1)->GetMinimum();
        double Maximun = poSrcDS->GetRasterBand(1)->GetMaximum();
        double No_date_vaule = poSrcDS->GetRasterBand(1)->GetNoDataValue();

        //if (Minimun < 0 || Maximun>255 /*|| No_date_vaule!=0*/)// 暂定分类二值tiff数据格式用于区分 GF等遥感影像数据
        //{
        //        showmessages->clear();
        //        //*showmessages = file_raster;
        //        showmessages->append(tr("File is not image data,processing failed!"));
        //        //showmessages->append(QStringLiteral("文件为非分类影像数据,处理失败!"));

        //        emitProgMessage(*showmessages);

        //        GDALClose((GDALDatasetH)poSrcDS);
        //        //OGRDataSource:estroyDataSource(poDstDS);
        //        GDALClose(poDstDS);
        //        CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
        //       
        //        emitProgChanged(progessValue + 6 * part_Value / part_splt);
        //        return 0;

        //}



        //typedef enum {
        //        /*! Unknown or unspecified type */          GDT_Unknown = 0,
        //        /*! Eight bit unsigned integer */           GDT_Byte = 1,
        //        /*! Sixteen bit unsigned integer */         GDT_UInt16 = 2,
        //        /*! Sixteen bit signed integer */           GDT_Int16 = 3,
        //        /*! Thirty two bit unsigned integer */      GDT_UInt32 = 4,
        //        /*! Thirty two bit signed integer */        GDT_Int32 = 5,
        //        /*! Thirty two bit floating point */        GDT_Float32 = 6,
        //        /*! Sixty four bit floating point */        GDT_Float64 = 7,
        //        /*! Complex Int16 */                        GDT_CInt16 = 8,
        //        /*! Complex Int32 */                        GDT_CInt32 = 9,
        //        /*! Complex Float32 */                      GDT_CFloat32 = 10,
        //        /*! Complex Float64 */                      GDT_CFloat64 = 11,
        //        GDT_TypeCount = 12          /* maximum type # + 1 */
        //} GDALDataType;
        int recode = 0;
        int iPixValField = 0;
        /*
       
       
        int RasterdataType = poSrcDS->GetRasterBand(1)->GetRasterDataType();
        if (RasterdataType == GDALDataType::GDT_Byte
                || RasterdataType == GDALDataType::GDT_Int32
                )
        {
                recode = GDALPolygonize(hSrcBand, NULL, (OGRLayerH)poLayer, iPixValField, NULL, NULL, NULL); //调用栅格矢量化
        }
        else if (RasterdataType == GDALDataType::GDT_Float32) {
                recode = GDALFPolygonize(hSrcBand, NULL, (OGRLayerH)poLayer, iPixValField, NULL, NULL, NULL); //调用栅格矢量化
        }
        if (recode != 0)
        {
                showmessages->clear();
                //*showmessages = file_raster;
                showmessages->append(tr("Data conversion failed!"));
                //showmessages->append(QStringLiteral("数据转换失败!"));


                emitProgMessage(*showmessages);
                GDALClose((GDALDatasetH)poSrcDS);
                GDALClose(poDstDS);
                CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
                return 0;
        }
        */

        // 设置联通方式
        char** papszOptions = NULL;
        bool bis8Con = false;
        if (bis8Con)
        {
                papszOptions = CSLSetNameValue(papszOptions ,"8CONNECTED","8");
        }
        else
        {
                papszOptions = CSLSetNameValue(papszOptions, "8CONNECTED", "4");
        }
        // 进行矢量处理,若果是浮点类型请使用 GDALFPolygonize进行处理
        int RasterdataType = poSrcDS->GetRasterBand(1)->GetRasterDataType();
        if (RasterdataType == GDALDataType::GDT_Byte
                || RasterdataType == GDALDataType::GDT_Int32
                || RasterdataType == GDALDataType::GDT_UInt16
                || RasterdataType == GDALDataType::GDT_Int16
                || RasterdataType == GDALDataType::GDT_UInt32
                || RasterdataType == GDALDataType::GDT_Unknown
                )
        {
                recode = GDALPolygonize(hSrcBand, hSrcBand, (OGRLayerH)poLayer, iPixValField, papszOptions, NULL, NULL); //调用栅格矢量化
        }
        else if (RasterdataType == GDALDataType::GDT_Float32
                || RasterdataType == GDALDataType::GDT_Float64
                ) {
                recode = GDALFPolygonize(hSrcBand, hSrcBand, (OGRLayerH)poLayer, iPixValField, papszOptions, NULL, NULL); //调用栅格矢量化
        }


        emitProgChanged(progessValue + 5 * part_Value / part_splt);

        /*//GDALPolygonize(GDALRasterBandH hSrcBand,                 //输入栅格图像波段
        //        3:                 GDALRasterBandH hMaskBand,                //掩码图像波段,可以为NULL
        //        4 : OGRLayerH hOutLayer,                      //矢量化后的矢量图层
        //        5 : int iPixValField,                         //需要将像元DN值写入矢量属性字段的字段索引
        //        6 : char **papszOptions,                      //算法选项,目前算法中没有用到,设置为NULL即可
        //        7 : GDALProgressFunc pfnProgress,             //进度条回调函数
        //        8 : void * pProgressArg);                    //进度条参数
        */
        showmessages->clear();
        //*showmessages = file_raster;
        showmessages->append(tr("Conversion success!"));
        //showmessages->append(QStringLiteral("转换成功!"));

        emitProgMessage(*showmessages);

        GDALClose(poSrcDS); //关闭文件
        GDALClose(poDstDS);

        CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");

        emitProgChanged(progessValue + 6 * part_Value / part_splt);

        return 1;
}

void Raster2VectorThread::run()
{
        int start_ProgessValue = 5;
        emitProgChanged(start_ProgessValue);

        QStringList inputlist = gInput.split(",");
        QString outstr;
        QStringList forward;
        QString forwardstr;
        int part_Value = 95/ inputlist.size();
        QString showmessages;
        QStringList errMessagelist;
        errMessagelist.clear();

        for (int i = 0; i < inputlist.size(); i++)
        {
                int new_progessValus =start_ProgessValue + i*part_Value;
                emitProgChanged(new_progessValus);
                forward = inputlist.split("/");
                forwardstr = forward[forward.size() - 1];
                int index;
                index = forwardstr.lastIndexOf(".");  //一般来说,扩展名为最后一个“.”之后的字符;
                forwardstr.truncate(index);
                outstr = gOutput + "/" + forwardstr + ".shp";

                int recode = ImagePolygonize(TO8(inputlist), TO8(outstr), "ESRI Shapefile", new_progessValus, part_Value, &showmessages);
                if (recode !=1 )
                {
                        errMessagelist << showmessages;
                }
                //int re = CBrowseThumbDllAPI(TO8(inputlist), TO8(outstr), width, thumb_width, progressValueCallback, progressMessageCallback, this);

                /*QString program = QCoreApplication::applicationDirPath() + "/H4TOH5/h4toh5convert.exe";
       
                std::wstring wprogram = program.toStdWString();
                LPCWSTR lpwprogram = wprogram.c_str();


                std::wstring wparam = (inputlist+" "+outstr).toStdWString();
                LPCWSTR lpwparam = wparam.c_str();

                SHELLEXECUTEINFO ShExecInfo = { 0 };
                ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
                ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
                ShExecInfo.hwnd = NULL;
                ShExecInfo.lpVerb = NULL;
                ShExecInfo.lpFile = lpwprogram;
                ShExecInfo.lpParameters = lpwparam;
                ShExecInfo.lpDirectory = L"";
                ShExecInfo.nShow = SW_HIDE;
                ShExecInfo.hInstApp = NULL;
                ShellExecuteEx(&ShExecInfo);
                WaitForSingleObject(ShExecInfo.hProcess, INFINITE);*/

        }
       
        emitProgChanged(100);
        if (errMessagelist.size() >0)
        {
                showmessages.clear();
                for (int i = 0; i < errMessagelist.size();i++ )
                {
                        if (i!=0)
                        {
                                showmessages.append("\n");
                        }
                        showmessages.append(errMessagelist);
                }
       
        }
        else{
                showmessages.clear();
                showmessages.append(tr("Finished!"));
                //showmessages.append(QStringLiteral("处理完成!"));
               
               
        }
                emitProgMessage(showmessages);

}

void Raster2VectorThread::emitProgChanged(int progValue)
{
        emit updateProgValue(progValue);
}

void Raster2VectorThread::emitProgMessage(QString progMessage)
{
        emit updateProgMsg(progMessage);
}
[/mw_shl_code]





上一篇:Python实现文字提取器源码
下一篇:Matlab:sps并联机构空间求解源码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-4-25 08:15 , Processed in 0.172560 second(s), 37 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表