tif转shp文件

    std::string imgPath = CT2A(strTifPath);//tif文件路径
    strShpTemp += CFileToolKit::GetFileLogicName(strShpPath);
    std::string tempShpPath = CT2A(strShpTemp);
    GDALDataset * pInDataset = (GDALDataset *)GDALOpen(imgPath.c_str(), GA_ReadOnly);
    if (pInDataset == NULL)
    {
        printf("读取图像失败!");
        return false;
    }

    int nDemWidth = pInDataset->GetRasterXSize(); //获取图像宽 
    int nDemHeight = pInDataset->GetRasterYSize(); //获取图像高 
    int Count = pInDataset->GetRasterCount(); //波段数 

    //读取图像数据波段
    GDALRasterBand *pInRasterBand = pInDataset->GetRasterBand(1);
    float *pData = new float[nDemWidth*nDemHeight]();
    CPLErr err = pInRasterBand->RasterIO(GF_Read, 0, 0, nDemWidth, nDemHeight, pData, nDemWidth, nDemHeight, GDT_Float32, 0, 0);
    if (err == CE_Failure)
    {
        cout << "读取DEM图像数据时出错!" << endl;
        GDALDestroyDriverManager();
        return false;
    }

    //判断图像中是否有异常值,并获取异常值实际值
    float fNoData = 0;
    int nIdx;
    for (int i = 0; i < nDemHeight; i++)
    {
        for (int j = 0; j < nDemWidth; j++)
        {
            nIdx = i*nDemWidth + j;
            if (pData[nIdx] <= -9999)
            {
                fNoData = pData[nIdx];
            }
        }
    }

    //创建矢量图
    const char *pszDriverName = "ESRI Shapefile";
    GDALDriver *poDriver;
    GDALAllRegister();
    poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName);
    if (poDriver == NULL)
    {
        printf("%s driver not available.\n", pszDriverName);
        exit(1);
    }

    GDALDataset *poDS = poDriver->Create(tempShpPath.c_str(), 0, 0, 0, GDT_Unknown, NULL); //创建数据源
    if (poDS == NULL)
    {
        printf("Creation of output file failed.\n");
        exit(1);
    }

//    const char *szWkt = pInDataset->GetProjectionRef();
     CString strWkt = CSpatialRef::WebMercatorWkt;
     std::string str = CW2A(strWkt);
    OGRSpatialReference *poSpatialRef = new OGRSpatialReference(pInDataset->GetProjectionRef());
    OGRLayer *poLayer = poDS->CreateLayer("Elevation", poSpatialRef, wkbLineString, NULL); //创建图层
    if (poLayer == NULL)
    {
        printf("Layer creation failed.\n");
        exit(1);
    }

    OGRFieldDefn ofieldDef1("Elevation", OFTInteger);    //在矢量图中创建高程值字段
    if (poLayer->CreateField(&ofieldDef1) != OGRERR_NONE)
    {
        cout << "创建矢量图层属性表失败!" << endl;
        GDALClose((GDALDatasetH)poDS);
        GDALClose(pInDataset);
        return false;
    }

    //根据图像波段生成矢量图等高线
    if (FLOAT_EQUAL2(0.0, fNoData))
        GDALContourGenerate(pInRasterBand, dfContourInterval, 0, 0, NULL, FALSE, 0, (OGRLayerH)poLayer, -1, 0, NULL, NULL);
    else //有异常值时,不对异常值进行处理
        GDALContourGenerate(pInRasterBand, dfContourInterval, 0, 0, NULL, TRUE, fNoData, (OGRLayerH)poLayer, -1, 0, NULL, NULL);

    // 对生成shp文件重投影坐标转换成墨卡托
    GDALDataset *poLDS = poDriver->Create(shpPath.c_str(), 0, 0, 0, GDT_Unknown, NULL); //创建数据源
    OGRSpatialReference *poLSpatialRef = new OGRSpatialReference(str.c_str());
    OGRCoordinateTransformation* ctx = OGRCreateCoordinateTransformation(poSpatialRef, poLSpatialRef);
    OGRLayer *poLLayer = poLDS->CreateLayer("Elevation", poLSpatialRef, wkbLineString, NULL); //创建图层
    OGRFeatureDefn* layer_def = poLayer->GetLayerDefn();
    for (int i = 0; i < layer_def->GetFieldCount(); ++i)
    {
        OGRFieldDefn* field_def = layer_def->GetFieldDefn(i);
        poLLayer->CreateField(field_def);
    }
    
    OGRFeature* src_feature = poLayer->GetNextFeature();
    while (src_feature)
    {
        OGRGeometry* geometry = src_feature->GetGeometryRef();
        geometry->transform(ctx);
        poLLayer->CreateFeature(src_feature);
        src_feature = poLayer->GetNextFeature();
    }

    GDALClose(poLDS);
    GDALClose(poDS);
    GDALClose(pInDataset);

    delete[] pData;
    pData = NULL;

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值