本文介绍了GDAL:无法使用vsizip创建和写入zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GDAL库及其虚拟文件系统驱动程序vsizip将GeoTIFF写入zip文件. las,这给了我一个错误:

I'm trying to write a GeoTIFF to zip file with GDAL library and its virtual filesystem driver vsizip. Alas, it gives me an error:

ERROR 1: Random access not supported for writable file in /vsizip
ERROR 4: Attempt to create new tiff file `/vsizip/D:\Projects\oy_vey.zip\my.tif'
 failed: No such file or directory

我该如何应对?

P.S.这是我的代码:

P.S. Here's my code:

#include "stdafx.h"
#include "MainWnd.h"
#include <QtWidgets/QApplication>

#include <gdal.h>
#include <gdal_priv.h>
#include <gdal_frmts.h>
#include <ogr_spatialref.h>

int main(int argc, char *argv[])
{
    //QApplication a(argc, argv);
    //MainWnd w;
    //w.show();
    //return a.exec();

    GDALRegister_GTiff();

    int numLatPx = 480;
    int numLonPx = 640;

    double northDeg = 3;
    double southDeg = 0;
    double westDeg = 0;
    double eastDeg = 4;


    double latStep = (northDeg - southDeg) / numLatPx;
    double lonStep = (eastDeg - westDeg) / numLonPx;


    GDALDriver* poDriver;
    char** papszMetadata;

    GDALDriverManager* driverManager = GetGDALDriverManager();
    poDriver = driverManager->GetDriverByName("GTiff");
    if (poDriver == NULL)
    {
        qDebug() << "GEOTIFF DRIVER NULL!!!";
        exit(1);
    }
    //it's okay so far...

    GDALDataset* poDstDS;

    double adfGeoTransform[6];
    adfGeoTransform[0] = westDeg; /* top left x */
    adfGeoTransform[1] = lonStep; /* w-e pixel resolution */
    adfGeoTransform[2] = 0; /* 0 */
    adfGeoTransform[3] = northDeg; /* top left y */
    adfGeoTransform[4] = 0; /* 0 */
    adfGeoTransform[5] = -latStep;/* n-s pixel resolution (negative value) */

    char** papszOptions = NULL;

    poDstDS = poDriver->Create("/vsizip/D:\\Projects\\oy_vey.zip\\my.tif", numLonPx, numLatPx, 1, GDT_Int16, papszOptions);
    // poDstDS will give NULL

    OGRSpatialReference oSRS;
    char *pszSRS_WKT = NULL;
    GDALRasterBand* poBand;

    poDstDS->SetGeoTransform(adfGeoTransform);
    //oSRS.importFromEPSG(4284); // PULKOVO 1942
    oSRS.exportToWkt(&pszSRS_WKT);

    poDstDS->SetProjection(pszSRS_WKT);
    CPLFree(pszSRS_WKT);
    poBand = poDstDS->GetRasterBand(1);

    GInt16* abyRaster = new GInt16[numLatPx * numLonPx];

    abyRaster[10000] = 0;
    abyRaster[10001] = 1;

    poBand->RasterIO(GF_Write, 0, 0, numLonPx, numLatPx, abyRaster, numLonPx, numLatPx, GDT_Int16, 0, 0);

    double minVal, maxVal, meanVal, stdDevVal;
    poBand->ComputeStatistics(false, &minVal, &maxVal, &meanVal, &stdDevVal, NULL, NULL);
    poBand->SetStatistics(minVal, maxVal, meanVal, stdDevVal);
    poBand->SetColorInterpretation(GCI_GrayIndex);


    /* Once we're done, close properly the dataset */
    GDALClose((GDALDatasetH)poDstDS);



    qDebug() << "^_^" << endl;
    return 0;
}

推荐答案

GTiff驱动程序Create()将尝试以读写模式打开数据集,而vsizip虚拟文件系统不支持该数据集.

GTiff driver Create() will attempt to open the dataset in read / write mode, which is not supported by the vsizip virtual filesystem.

作为解决方法,您可以执行以下操作:

As a workaround, you can do the following:

  1. 创建一个临时TIFF文件
  2. 使用具有vsizip语法的CopyFiles方法
  3. 删除临时文件

这是我更改代码的方式(我在Linux上,因此您应该调整文件路径以在Windows上使用它.)

Here is the how I changed your code (I'm on Linux so you should adjust the file path to use it on Windows).

int main(int argc, char *argv[])
{
    //QApplication a(argc, argv);
    //MainWnd w;
    //w.show();
    //return a.exec();

    GDALAllRegister();

    int numLatPx = 480;
    int numLonPx = 640;

    double northDeg = 3;
    double southDeg = 0;
    double westDeg = 0;
    double eastDeg = 4;


    double latStep = (northDeg - southDeg) / numLatPx;
    double lonStep = (eastDeg - westDeg) / numLonPx;


    GDALDriver* poDriver;
    char** papszMetadata;

    GDALDriverManager* driverManager = GetGDALDriverManager();
    poDriver = driverManager->GetDriverByName("GTiff");
    if (poDriver == NULL)
    {
        qDebug() << "GEOTIFF DRIVER NULL!!!";
        exit(1);
    }
    //it's okay so far...

    GDALDataset* poDstDS;

    double adfGeoTransform[6];
    adfGeoTransform[0] = westDeg; /* top left x */
    adfGeoTransform[1] = lonStep; /* w-e pixel resolution */
    adfGeoTransform[2] = 0; /* 0 */
    adfGeoTransform[3] = northDeg; /* top left y */
    adfGeoTransform[4] = 0; /* 0 */
    adfGeoTransform[5] = -latStep;/* n-s pixel resolution (negative value) */

    char** papszOptions = NULL;

    poDstDS = poDriver->Create("/home/me/my.tif", numLonPx, numLatPx, 1, GDT_Int16, papszOptions);
    // poDstDS will give NULL

    OGRSpatialReference oSRS;
    char *pszSRS_WKT = NULL;
    GDALRasterBand* poBand;

    poDstDS->SetGeoTransform(adfGeoTransform);
    //oSRS.importFromEPSG(4284); // PULKOVO 1942
    oSRS.exportToWkt(&pszSRS_WKT);

    poDstDS->SetProjection(pszSRS_WKT);
    CPLFree(pszSRS_WKT);
    poBand = poDstDS->GetRasterBand(1);

    GInt16* abyRaster = new GInt16[numLatPx * numLonPx];

    abyRaster[10000] = 0;
    abyRaster[10001] = 1;

    poBand->RasterIO(GF_Write, 0, 0, numLonPx, numLatPx, abyRaster, numLonPx, numLatPx, GDT_Int16, 0, 0);

    double minVal, maxVal, meanVal, stdDevVal;
    poBand->ComputeStatistics(false, &minVal, &maxVal, &meanVal, &stdDevVal, NULL, NULL);
    poBand->SetStatistics(minVal, maxVal, meanVal, stdDevVal);
    poBand->SetColorInterpretation(GCI_GrayIndex);


    /* Once we're done, close properly the dataset */
   GDALClose((GDALDatasetH)poDstDS);


   poDriver->CopyFiles("/vsizip//home/me/test.zip/my.tif", "/home/me/my.tif");


    qDebug() << "^_^" << endl;
    return 0;
}

这篇关于GDAL:无法使用vsizip创建和写入zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:29